blob: d041d007cfe542b8cfd26af6851a4303641e4344 [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
Daniel Vetter3dec0092010-08-20 21:40:52 +020044static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010045static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080046
Jesse Barnesf1f644d2013-06-27 00:39:25 +030047static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030049static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030051
Damien Lespiaue7457a92013-08-08 22:28:59 +010052static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
54
55
Jesse Barnes79e53942008-11-07 14:24:08 -080056typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040057 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_range_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int dot_limit;
62 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080063} intel_p2_t;
64
Ma Lingd4906092009-03-18 20:13:27 +080065typedef struct intel_limit intel_limit_t;
66struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 intel_range_t dot, vco, n, m, m1, m2, p, p1;
68 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080069};
Jesse Barnes79e53942008-11-07 14:24:08 -080070
Daniel Vetterd2acd212012-10-20 20:57:43 +020071int
72intel_pch_rawclk(struct drm_device *dev)
73{
74 struct drm_i915_private *dev_priv = dev->dev_private;
75
76 WARN_ON(!HAS_PCH_SPLIT(dev));
77
78 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
79}
80
Chris Wilson021357a2010-09-07 20:54:59 +010081static inline u32 /* units of 100MHz */
82intel_fdi_link_freq(struct drm_device *dev)
83{
Chris Wilson8b99e682010-10-13 09:59:17 +010084 if (IS_GEN5(dev)) {
85 struct drm_i915_private *dev_priv = dev->dev_private;
86 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 } else
88 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010089}
90
Daniel Vetter5d536e22013-07-06 12:52:06 +020091static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040092 .dot = { .min = 25000, .max = 350000 },
93 .vco = { .min = 930000, .max = 1400000 },
94 .n = { .min = 3, .max = 16 },
95 .m = { .min = 96, .max = 140 },
96 .m1 = { .min = 18, .max = 26 },
97 .m2 = { .min = 6, .max = 16 },
98 .p = { .min = 4, .max = 128 },
99 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700100 .p2 = { .dot_limit = 165000,
101 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700102};
103
Daniel Vetter5d536e22013-07-06 12:52:06 +0200104static const intel_limit_t intel_limits_i8xx_dvo = {
105 .dot = { .min = 25000, .max = 350000 },
106 .vco = { .min = 930000, .max = 1400000 },
107 .n = { .min = 3, .max = 16 },
108 .m = { .min = 96, .max = 140 },
109 .m1 = { .min = 18, .max = 26 },
110 .m2 = { .min = 6, .max = 16 },
111 .p = { .min = 4, .max = 128 },
112 .p1 = { .min = 2, .max = 33 },
113 .p2 = { .dot_limit = 165000,
114 .p2_slow = 4, .p2_fast = 4 },
115};
116
Keith Packarde4b36692009-06-05 19:22:17 -0700117static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 .dot = { .min = 25000, .max = 350000 },
119 .vco = { .min = 930000, .max = 1400000 },
120 .n = { .min = 3, .max = 16 },
121 .m = { .min = 96, .max = 140 },
122 .m1 = { .min = 18, .max = 26 },
123 .m2 = { .min = 6, .max = 16 },
124 .p = { .min = 4, .max = 128 },
125 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700126 .p2 = { .dot_limit = 165000,
127 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700128};
Eric Anholt273e27c2011-03-30 13:01:10 -0700129
Keith Packarde4b36692009-06-05 19:22:17 -0700130static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 .dot = { .min = 20000, .max = 400000 },
132 .vco = { .min = 1400000, .max = 2800000 },
133 .n = { .min = 1, .max = 6 },
134 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100135 .m1 = { .min = 8, .max = 18 },
136 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .p = { .min = 5, .max = 80 },
138 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700139 .p2 = { .dot_limit = 200000,
140 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700141};
142
143static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400144 .dot = { .min = 20000, .max = 400000 },
145 .vco = { .min = 1400000, .max = 2800000 },
146 .n = { .min = 1, .max = 6 },
147 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100148 .m1 = { .min = 8, .max = 18 },
149 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .p = { .min = 7, .max = 98 },
151 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700152 .p2 = { .dot_limit = 112000,
153 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700154};
155
Eric Anholt273e27c2011-03-30 13:01:10 -0700156
Keith Packarde4b36692009-06-05 19:22:17 -0700157static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .dot = { .min = 25000, .max = 270000 },
159 .vco = { .min = 1750000, .max = 3500000},
160 .n = { .min = 1, .max = 4 },
161 .m = { .min = 104, .max = 138 },
162 .m1 = { .min = 17, .max = 23 },
163 .m2 = { .min = 5, .max = 11 },
164 .p = { .min = 10, .max = 30 },
165 .p1 = { .min = 1, .max = 3},
166 .p2 = { .dot_limit = 270000,
167 .p2_slow = 10,
168 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800169 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
172static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .dot = { .min = 22000, .max = 400000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 16, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8},
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 20000, .max = 115000 },
187 .vco = { .min = 1750000, .max = 3500000 },
188 .n = { .min = 1, .max = 3 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 28, .max = 112 },
193 .p1 = { .min = 2, .max = 8 },
194 .p2 = { .dot_limit = 0,
195 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800196 },
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
198
199static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 80000, .max = 224000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 14, .max = 42 },
207 .p1 = { .min = 2, .max = 6 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800210 },
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500213static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400214 .dot = { .min = 20000, .max = 400000},
215 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .n = { .min = 3, .max = 6 },
218 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .m1 = { .min = 0, .max = 0 },
221 .m2 = { .min = 0, .max = 254 },
222 .p = { .min = 5, .max = 80 },
223 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700224 .p2 = { .dot_limit = 200000,
225 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500228static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 20000, .max = 400000 },
230 .vco = { .min = 1700000, .max = 3500000 },
231 .n = { .min = 3, .max = 6 },
232 .m = { .min = 2, .max = 256 },
233 .m1 = { .min = 0, .max = 0 },
234 .m2 = { .min = 0, .max = 254 },
235 .p = { .min = 7, .max = 112 },
236 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700237 .p2 = { .dot_limit = 112000,
238 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
Eric Anholt273e27c2011-03-30 13:01:10 -0700241/* Ironlake / Sandybridge
242 *
243 * We calculate clock using (register_value + 2) for N/M1/M2, so here
244 * the range value for them is (actual_value - 2).
245 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800246static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 25000, .max = 350000 },
248 .vco = { .min = 1760000, .max = 3510000 },
249 .n = { .min = 1, .max = 5 },
250 .m = { .min = 79, .max = 127 },
251 .m1 = { .min = 12, .max = 22 },
252 .m2 = { .min = 5, .max = 9 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
255 .p2 = { .dot_limit = 225000,
256 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800259static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .dot = { .min = 25000, .max = 350000 },
261 .vco = { .min = 1760000, .max = 3510000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 79, .max = 118 },
264 .m1 = { .min = 12, .max = 22 },
265 .m2 = { .min = 5, .max = 9 },
266 .p = { .min = 28, .max = 112 },
267 .p1 = { .min = 2, .max = 8 },
268 .p2 = { .dot_limit = 225000,
269 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800270};
271
272static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .dot = { .min = 25000, .max = 350000 },
274 .vco = { .min = 1760000, .max = 3510000 },
275 .n = { .min = 1, .max = 3 },
276 .m = { .min = 79, .max = 127 },
277 .m1 = { .min = 12, .max = 22 },
278 .m2 = { .min = 5, .max = 9 },
279 .p = { .min = 14, .max = 56 },
280 .p1 = { .min = 2, .max = 8 },
281 .p2 = { .dot_limit = 225000,
282 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800286static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .dot = { .min = 25000, .max = 350000 },
288 .vco = { .min = 1760000, .max = 3510000 },
289 .n = { .min = 1, .max = 2 },
290 .m = { .min = 79, .max = 126 },
291 .m1 = { .min = 12, .max = 22 },
292 .m2 = { .min = 5, .max = 9 },
293 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .p2 = { .dot_limit = 225000,
296 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 126 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800310};
311
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700312static const intel_limit_t intel_limits_vlv_dac = {
313 .dot = { .min = 25000, .max = 270000 },
314 .vco = { .min = 4000000, .max = 6000000 },
315 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700316 .m1 = { .min = 2, .max = 3 },
317 .m2 = { .min = 11, .max = 156 },
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300318 .p1 = { .min = 2, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700319 .p2 = { .dot_limit = 270000,
Ville Syrjälä889059d2013-09-24 21:26:27 +0300320 .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700321};
322
323static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200324 .dot = { .min = 25000, .max = 270000 },
325 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700326 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700327 .m1 = { .min = 2, .max = 3 },
328 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300329 .p1 = { .min = 2, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700330 .p2 = { .dot_limit = 270000,
Ville Syrjälä889059d2013-09-24 21:26:27 +0300331 .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700332};
333
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300334static void vlv_clock(int refclk, intel_clock_t *clock)
335{
336 clock->m = clock->m1 * clock->m2;
337 clock->p = clock->p1 * clock->p2;
338 clock->vco = refclk * clock->m / clock->n;
339 clock->dot = clock->vco / clock->p;
340}
341
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300342/**
343 * Returns whether any output on the specified pipe is of the specified type
344 */
345static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
346{
347 struct drm_device *dev = crtc->dev;
348 struct intel_encoder *encoder;
349
350 for_each_encoder_on_crtc(dev, crtc, encoder)
351 if (encoder->type == type)
352 return true;
353
354 return false;
355}
356
Chris Wilson1b894b52010-12-14 20:04:54 +0000357static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
358 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800359{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800361 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362
363 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100364 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000365 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800366 limit = &intel_limits_ironlake_dual_lvds_100m;
367 else
368 limit = &intel_limits_ironlake_dual_lvds;
369 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000370 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800371 limit = &intel_limits_ironlake_single_lvds_100m;
372 else
373 limit = &intel_limits_ironlake_single_lvds;
374 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200375 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800376 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800377
378 return limit;
379}
380
Ma Ling044c7c42009-03-18 20:13:23 +0800381static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
382{
383 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800384 const intel_limit_t *limit;
385
386 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100387 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700388 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800389 else
Keith Packarde4b36692009-06-05 19:22:17 -0700390 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800391 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
392 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700393 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800394 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700395 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800396 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700397 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800398
399 return limit;
400}
401
Chris Wilson1b894b52010-12-14 20:04:54 +0000402static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800403{
404 struct drm_device *dev = crtc->dev;
405 const intel_limit_t *limit;
406
Eric Anholtbad720f2009-10-22 16:11:14 -0700407 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000408 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800409 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800410 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500411 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800412 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500413 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800414 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500415 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700416 } else if (IS_VALLEYVIEW(dev)) {
417 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
418 limit = &intel_limits_vlv_dac;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700419 else
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +0800420 limit = &intel_limits_vlv_hdmi;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100421 } else if (!IS_GEN2(dev)) {
422 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
423 limit = &intel_limits_i9xx_lvds;
424 else
425 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800426 } else {
427 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700428 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200429 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700430 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200431 else
432 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800433 }
434 return limit;
435}
436
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500437/* m1 is reserved as 0 in Pineview, n is a ring counter */
438static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800439{
Shaohua Li21778322009-02-23 15:19:16 +0800440 clock->m = clock->m2 + 2;
441 clock->p = clock->p1 * clock->p2;
442 clock->vco = refclk * clock->m / clock->n;
443 clock->dot = clock->vco / clock->p;
444}
445
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200446static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
447{
448 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
449}
450
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200451static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800452{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200453 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800454 clock->p = clock->p1 * clock->p2;
455 clock->vco = refclk * clock->m / (clock->n + 2);
456 clock->dot = clock->vco / clock->p;
457}
458
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800459#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800460/**
461 * Returns whether the given set of divisors are valid for a given refclk with
462 * the given connectors.
463 */
464
Chris Wilson1b894b52010-12-14 20:04:54 +0000465static bool intel_PLL_is_valid(struct drm_device *dev,
466 const intel_limit_t *limit,
467 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800468{
Jesse Barnes79e53942008-11-07 14:24:08 -0800469 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400470 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400472 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800473 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800475 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400476 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500477 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800479 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400480 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800481 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400482 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800483 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400484 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
486 * connector, etc., rather than just a single range.
487 */
488 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400489 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800490
491 return true;
492}
493
Ma Lingd4906092009-03-18 20:13:27 +0800494static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200495i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800496 int target, int refclk, intel_clock_t *match_clock,
497 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800498{
499 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800500 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800501 int err = target;
502
Daniel Vettera210b022012-11-26 17:22:08 +0100503 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800504 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100505 * For LVDS just rely on its current settings for dual-channel.
506 * We haven't figured out how to reliably set up different
507 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800508 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100509 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800510 clock.p2 = limit->p2.p2_fast;
511 else
512 clock.p2 = limit->p2.p2_slow;
513 } else {
514 if (target < limit->p2.dot_limit)
515 clock.p2 = limit->p2.p2_slow;
516 else
517 clock.p2 = limit->p2.p2_fast;
518 }
519
Akshay Joshi0206e352011-08-16 15:34:10 -0400520 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800521
Zhao Yakui42158662009-11-20 11:24:18 +0800522 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
523 clock.m1++) {
524 for (clock.m2 = limit->m2.min;
525 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200526 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800527 break;
528 for (clock.n = limit->n.min;
529 clock.n <= limit->n.max; clock.n++) {
530 for (clock.p1 = limit->p1.min;
531 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800532 int this_err;
533
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200534 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000535 if (!intel_PLL_is_valid(dev, limit,
536 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800537 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800538 if (match_clock &&
539 clock.p != match_clock->p)
540 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800541
542 this_err = abs(clock.dot - target);
543 if (this_err < err) {
544 *best_clock = clock;
545 err = this_err;
546 }
547 }
548 }
549 }
550 }
551
552 return (err != target);
553}
554
Ma Lingd4906092009-03-18 20:13:27 +0800555static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200556pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
557 int target, int refclk, intel_clock_t *match_clock,
558 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200559{
560 struct drm_device *dev = crtc->dev;
561 intel_clock_t clock;
562 int err = target;
563
564 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
565 /*
566 * For LVDS just rely on its current settings for dual-channel.
567 * We haven't figured out how to reliably set up different
568 * single/dual channel state, if we even can.
569 */
570 if (intel_is_dual_link_lvds(dev))
571 clock.p2 = limit->p2.p2_fast;
572 else
573 clock.p2 = limit->p2.p2_slow;
574 } else {
575 if (target < limit->p2.dot_limit)
576 clock.p2 = limit->p2.p2_slow;
577 else
578 clock.p2 = limit->p2.p2_fast;
579 }
580
581 memset(best_clock, 0, sizeof(*best_clock));
582
583 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
584 clock.m1++) {
585 for (clock.m2 = limit->m2.min;
586 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200587 for (clock.n = limit->n.min;
588 clock.n <= limit->n.max; clock.n++) {
589 for (clock.p1 = limit->p1.min;
590 clock.p1 <= limit->p1.max; clock.p1++) {
591 int this_err;
592
593 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 if (!intel_PLL_is_valid(dev, limit,
595 &clock))
596 continue;
597 if (match_clock &&
598 clock.p != match_clock->p)
599 continue;
600
601 this_err = abs(clock.dot - target);
602 if (this_err < err) {
603 *best_clock = clock;
604 err = this_err;
605 }
606 }
607 }
608 }
609 }
610
611 return (err != target);
612}
613
Ma Lingd4906092009-03-18 20:13:27 +0800614static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200615g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
616 int target, int refclk, intel_clock_t *match_clock,
617 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800618{
619 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800620 intel_clock_t clock;
621 int max_n;
622 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400623 /* approximately equals target * 0.00585 */
624 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800625 found = false;
626
627 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100628 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800629 clock.p2 = limit->p2.p2_fast;
630 else
631 clock.p2 = limit->p2.p2_slow;
632 } else {
633 if (target < limit->p2.dot_limit)
634 clock.p2 = limit->p2.p2_slow;
635 else
636 clock.p2 = limit->p2.p2_fast;
637 }
638
639 memset(best_clock, 0, sizeof(*best_clock));
640 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200641 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800642 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200643 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800644 for (clock.m1 = limit->m1.max;
645 clock.m1 >= limit->m1.min; clock.m1--) {
646 for (clock.m2 = limit->m2.max;
647 clock.m2 >= limit->m2.min; clock.m2--) {
648 for (clock.p1 = limit->p1.max;
649 clock.p1 >= limit->p1.min; clock.p1--) {
650 int this_err;
651
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200652 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000653 if (!intel_PLL_is_valid(dev, limit,
654 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800655 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000656
657 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800658 if (this_err < err_most) {
659 *best_clock = clock;
660 err_most = this_err;
661 max_n = clock.n;
662 found = true;
663 }
664 }
665 }
666 }
667 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800668 return found;
669}
Ma Lingd4906092009-03-18 20:13:27 +0800670
Zhenyu Wang2c072452009-06-05 15:38:42 +0800671static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200672vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
673 int target, int refclk, intel_clock_t *match_clock,
674 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700675{
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300676 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300677 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300678 /* min update 19.2 MHz */
679 int max_n = min(limit->n.max, refclk / 19200);
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700680
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300681 target *= 5; /* fast clock */
682
683 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700684
685 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300686 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300687 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300688 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300689 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300690 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700691 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300692 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300693 unsigned int ppm, diff;
694
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300695 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
696 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300697
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300698 vlv_clock(refclk, &clock);
699
700 if (clock.vco < limit->vco.min ||
701 clock.vco >= limit->vco.max)
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300702 continue;
703
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300704 diff = abs(clock.dot - target);
705 ppm = div_u64(1000000ULL * diff, target);
706
707 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300708 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300709 *best_clock = clock;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300710 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300711
Ville Syrjäläc6861222013-09-24 21:26:21 +0300712 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300713 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300714 *best_clock = clock;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700715 }
716 }
717 }
718 }
719 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700720
721 return true;
722}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700723
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300724bool intel_crtc_active(struct drm_crtc *crtc)
725{
726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
727
728 /* Be paranoid as we can arrive here with only partial
729 * state retrieved from the hardware during setup.
730 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100731 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300732 * as Haswell has gained clock readout/fastboot support.
733 *
734 * We can ditch the crtc->fb check as soon as we can
735 * properly reconstruct framebuffers.
736 */
737 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100738 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300739}
740
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200741enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
742 enum pipe pipe)
743{
744 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
746
Daniel Vetter3b117c82013-04-17 20:15:07 +0200747 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200748}
749
Paulo Zanonia928d532012-05-04 17:18:15 -0300750static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
751{
752 struct drm_i915_private *dev_priv = dev->dev_private;
753 u32 frame, frame_reg = PIPEFRAME(pipe);
754
755 frame = I915_READ(frame_reg);
756
757 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
758 DRM_DEBUG_KMS("vblank wait timed out\n");
759}
760
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700761/**
762 * intel_wait_for_vblank - wait for vblank on a given pipe
763 * @dev: drm device
764 * @pipe: pipe to wait for
765 *
766 * Wait for vblank to occur on a given pipe. Needed for various bits of
767 * mode setting code.
768 */
769void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800770{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700771 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800772 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700773
Paulo Zanonia928d532012-05-04 17:18:15 -0300774 if (INTEL_INFO(dev)->gen >= 5) {
775 ironlake_wait_for_vblank(dev, pipe);
776 return;
777 }
778
Chris Wilson300387c2010-09-05 20:25:43 +0100779 /* Clear existing vblank status. Note this will clear any other
780 * sticky status fields as well.
781 *
782 * This races with i915_driver_irq_handler() with the result
783 * that either function could miss a vblank event. Here it is not
784 * fatal, as we will either wait upon the next vblank interrupt or
785 * timeout. Generally speaking intel_wait_for_vblank() is only
786 * called during modeset at which time the GPU should be idle and
787 * should *not* be performing page flips and thus not waiting on
788 * vblanks...
789 * Currently, the result of us stealing a vblank from the irq
790 * handler is that a single frame will be skipped during swapbuffers.
791 */
792 I915_WRITE(pipestat_reg,
793 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
794
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700795 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100796 if (wait_for(I915_READ(pipestat_reg) &
797 PIPE_VBLANK_INTERRUPT_STATUS,
798 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700799 DRM_DEBUG_KMS("vblank wait timed out\n");
800}
801
Keith Packardab7ad7f2010-10-03 00:33:06 -0700802/*
803 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700804 * @dev: drm device
805 * @pipe: pipe to wait for
806 *
807 * After disabling a pipe, we can't wait for vblank in the usual way,
808 * spinning on the vblank interrupt status bit, since we won't actually
809 * see an interrupt when the pipe is disabled.
810 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700811 * On Gen4 and above:
812 * wait for the pipe register state bit to turn off
813 *
814 * Otherwise:
815 * wait for the display line value to settle (it usually
816 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100817 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700818 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100819void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700820{
821 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200822 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
823 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700824
Keith Packardab7ad7f2010-10-03 00:33:06 -0700825 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200826 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700827
Keith Packardab7ad7f2010-10-03 00:33:06 -0700828 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100829 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
830 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200831 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700832 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300833 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100834 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700835 unsigned long timeout = jiffies + msecs_to_jiffies(100);
836
Paulo Zanoni837ba002012-05-04 17:18:14 -0300837 if (IS_GEN2(dev))
838 line_mask = DSL_LINEMASK_GEN2;
839 else
840 line_mask = DSL_LINEMASK_GEN3;
841
Keith Packardab7ad7f2010-10-03 00:33:06 -0700842 /* Wait for the display line to settle */
843 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300844 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700845 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300846 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700847 time_after(timeout, jiffies));
848 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200849 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700850 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800851}
852
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000853/*
854 * ibx_digital_port_connected - is the specified port connected?
855 * @dev_priv: i915 private structure
856 * @port: the port to test
857 *
858 * Returns true if @port is connected, false otherwise.
859 */
860bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
861 struct intel_digital_port *port)
862{
863 u32 bit;
864
Damien Lespiauc36346e2012-12-13 16:09:03 +0000865 if (HAS_PCH_IBX(dev_priv->dev)) {
866 switch(port->port) {
867 case PORT_B:
868 bit = SDE_PORTB_HOTPLUG;
869 break;
870 case PORT_C:
871 bit = SDE_PORTC_HOTPLUG;
872 break;
873 case PORT_D:
874 bit = SDE_PORTD_HOTPLUG;
875 break;
876 default:
877 return true;
878 }
879 } else {
880 switch(port->port) {
881 case PORT_B:
882 bit = SDE_PORTB_HOTPLUG_CPT;
883 break;
884 case PORT_C:
885 bit = SDE_PORTC_HOTPLUG_CPT;
886 break;
887 case PORT_D:
888 bit = SDE_PORTD_HOTPLUG_CPT;
889 break;
890 default:
891 return true;
892 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000893 }
894
895 return I915_READ(SDEISR) & bit;
896}
897
Jesse Barnesb24e7172011-01-04 15:09:30 -0800898static const char *state_string(bool enabled)
899{
900 return enabled ? "on" : "off";
901}
902
903/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200904void assert_pll(struct drm_i915_private *dev_priv,
905 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800906{
907 int reg;
908 u32 val;
909 bool cur_state;
910
911 reg = DPLL(pipe);
912 val = I915_READ(reg);
913 cur_state = !!(val & DPLL_VCO_ENABLE);
914 WARN(cur_state != state,
915 "PLL state assertion failure (expected %s, current %s)\n",
916 state_string(state), state_string(cur_state));
917}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800918
Jani Nikula23538ef2013-08-27 15:12:22 +0300919/* XXX: the dsi pll is shared between MIPI DSI ports */
920static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
921{
922 u32 val;
923 bool cur_state;
924
925 mutex_lock(&dev_priv->dpio_lock);
926 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
927 mutex_unlock(&dev_priv->dpio_lock);
928
929 cur_state = val & DSI_PLL_VCO_EN;
930 WARN(cur_state != state,
931 "DSI PLL state assertion failure (expected %s, current %s)\n",
932 state_string(state), state_string(cur_state));
933}
934#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
935#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
936
Daniel Vetter55607e82013-06-16 21:42:39 +0200937struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200938intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800939{
Daniel Vettere2b78262013-06-07 23:10:03 +0200940 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
941
Daniel Vettera43f6e02013-06-07 23:10:32 +0200942 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200943 return NULL;
944
Daniel Vettera43f6e02013-06-07 23:10:32 +0200945 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200946}
947
Jesse Barnesb24e7172011-01-04 15:09:30 -0800948/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200949void assert_shared_dpll(struct drm_i915_private *dev_priv,
950 struct intel_shared_dpll *pll,
951 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800952{
Jesse Barnes040484a2011-01-03 12:14:26 -0800953 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200954 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800955
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300956 if (HAS_PCH_LPT(dev_priv->dev)) {
957 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
958 return;
959 }
960
Chris Wilson92b27b02012-05-20 18:10:50 +0100961 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200962 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100963 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100964
Daniel Vetter53589012013-06-05 13:34:16 +0200965 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100966 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200967 "%s assertion failure (expected %s, current %s)\n",
968 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800969}
Jesse Barnes040484a2011-01-03 12:14:26 -0800970
971static void assert_fdi_tx(struct drm_i915_private *dev_priv,
972 enum pipe pipe, bool state)
973{
974 int reg;
975 u32 val;
976 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200977 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
978 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800979
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200980 if (HAS_DDI(dev_priv->dev)) {
981 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200982 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300983 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200984 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300985 } else {
986 reg = FDI_TX_CTL(pipe);
987 val = I915_READ(reg);
988 cur_state = !!(val & FDI_TX_ENABLE);
989 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800990 WARN(cur_state != state,
991 "FDI TX state assertion failure (expected %s, current %s)\n",
992 state_string(state), state_string(cur_state));
993}
994#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
995#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
996
997static void assert_fdi_rx(struct drm_i915_private *dev_priv,
998 enum pipe pipe, bool state)
999{
1000 int reg;
1001 u32 val;
1002 bool cur_state;
1003
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001004 reg = FDI_RX_CTL(pipe);
1005 val = I915_READ(reg);
1006 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001007 WARN(cur_state != state,
1008 "FDI RX state assertion failure (expected %s, current %s)\n",
1009 state_string(state), state_string(cur_state));
1010}
1011#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1012#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1013
1014static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1015 enum pipe pipe)
1016{
1017 int reg;
1018 u32 val;
1019
1020 /* ILK FDI PLL is always enabled */
1021 if (dev_priv->info->gen == 5)
1022 return;
1023
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001024 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001025 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001026 return;
1027
Jesse Barnes040484a2011-01-03 12:14:26 -08001028 reg = FDI_TX_CTL(pipe);
1029 val = I915_READ(reg);
1030 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1031}
1032
Daniel Vetter55607e82013-06-16 21:42:39 +02001033void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1034 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001035{
1036 int reg;
1037 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001038 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001039
1040 reg = FDI_RX_CTL(pipe);
1041 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001042 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1043 WARN(cur_state != state,
1044 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1045 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001046}
1047
Jesse Barnesea0760c2011-01-04 15:09:32 -08001048static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1049 enum pipe pipe)
1050{
1051 int pp_reg, lvds_reg;
1052 u32 val;
1053 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001054 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001055
1056 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1057 pp_reg = PCH_PP_CONTROL;
1058 lvds_reg = PCH_LVDS;
1059 } else {
1060 pp_reg = PP_CONTROL;
1061 lvds_reg = LVDS;
1062 }
1063
1064 val = I915_READ(pp_reg);
1065 if (!(val & PANEL_POWER_ON) ||
1066 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1067 locked = false;
1068
1069 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1070 panel_pipe = PIPE_B;
1071
1072 WARN(panel_pipe == pipe && locked,
1073 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001074 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001075}
1076
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001077static void assert_cursor(struct drm_i915_private *dev_priv,
1078 enum pipe pipe, bool state)
1079{
1080 struct drm_device *dev = dev_priv->dev;
1081 bool cur_state;
1082
1083 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1084 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1085 else if (IS_845G(dev) || IS_I865G(dev))
1086 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1087 else
1088 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1089
1090 WARN(cur_state != state,
1091 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1092 pipe_name(pipe), state_string(state), state_string(cur_state));
1093}
1094#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1095#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1096
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001097void assert_pipe(struct drm_i915_private *dev_priv,
1098 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001099{
1100 int reg;
1101 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001102 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001103 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1104 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105
Daniel Vetter8e636782012-01-22 01:36:48 +01001106 /* if we need the pipe A quirk it must be always on */
1107 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1108 state = true;
1109
Paulo Zanonib97186f2013-05-03 12:15:36 -03001110 if (!intel_display_power_enabled(dev_priv->dev,
1111 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001112 cur_state = false;
1113 } else {
1114 reg = PIPECONF(cpu_transcoder);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & PIPECONF_ENABLE);
1117 }
1118
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001119 WARN(cur_state != state,
1120 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001121 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001122}
1123
Chris Wilson931872f2012-01-16 23:01:13 +00001124static void assert_plane(struct drm_i915_private *dev_priv,
1125 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001126{
1127 int reg;
1128 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001129 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001130
1131 reg = DSPCNTR(plane);
1132 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001133 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1134 WARN(cur_state != state,
1135 "plane %c assertion failure (expected %s, current %s)\n",
1136 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137}
1138
Chris Wilson931872f2012-01-16 23:01:13 +00001139#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1140#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1141
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1143 enum pipe pipe)
1144{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001145 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001146 int reg, i;
1147 u32 val;
1148 int cur_pipe;
1149
Ville Syrjälä653e1022013-06-04 13:49:05 +03001150 /* Primary planes are fixed to pipes on gen4+ */
1151 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001152 reg = DSPCNTR(pipe);
1153 val = I915_READ(reg);
1154 WARN((val & DISPLAY_PLANE_ENABLE),
1155 "plane %c assertion failure, should be disabled but not\n",
1156 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001157 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001158 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001159
Jesse Barnesb24e7172011-01-04 15:09:30 -08001160 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001161 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001162 reg = DSPCNTR(i);
1163 val = I915_READ(reg);
1164 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1165 DISPPLANE_SEL_PIPE_SHIFT;
1166 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001167 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1168 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001169 }
1170}
1171
Jesse Barnes19332d72013-03-28 09:55:38 -07001172static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1173 enum pipe pipe)
1174{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001175 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001176 int reg, i;
1177 u32 val;
1178
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001179 if (IS_VALLEYVIEW(dev)) {
1180 for (i = 0; i < dev_priv->num_plane; i++) {
1181 reg = SPCNTR(pipe, i);
1182 val = I915_READ(reg);
1183 WARN((val & SP_ENABLE),
1184 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1185 sprite_name(pipe, i), pipe_name(pipe));
1186 }
1187 } else if (INTEL_INFO(dev)->gen >= 7) {
1188 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001189 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001190 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001191 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001192 plane_name(pipe), pipe_name(pipe));
1193 } else if (INTEL_INFO(dev)->gen >= 5) {
1194 reg = DVSCNTR(pipe);
1195 val = I915_READ(reg);
1196 WARN((val & DVS_ENABLE),
1197 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1198 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001199 }
1200}
1201
Jesse Barnes92f25842011-01-04 15:09:34 -08001202static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1203{
1204 u32 val;
1205 bool enabled;
1206
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001207 if (HAS_PCH_LPT(dev_priv->dev)) {
1208 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1209 return;
1210 }
1211
Jesse Barnes92f25842011-01-04 15:09:34 -08001212 val = I915_READ(PCH_DREF_CONTROL);
1213 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1214 DREF_SUPERSPREAD_SOURCE_MASK));
1215 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1216}
1217
Daniel Vetterab9412b2013-05-03 11:49:46 +02001218static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1219 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001220{
1221 int reg;
1222 u32 val;
1223 bool enabled;
1224
Daniel Vetterab9412b2013-05-03 11:49:46 +02001225 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001226 val = I915_READ(reg);
1227 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 WARN(enabled,
1229 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1230 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001231}
1232
Keith Packard4e634382011-08-06 10:39:45 -07001233static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1234 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001235{
1236 if ((val & DP_PORT_EN) == 0)
1237 return false;
1238
1239 if (HAS_PCH_CPT(dev_priv->dev)) {
1240 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1241 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1242 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1243 return false;
1244 } else {
1245 if ((val & DP_PIPE_MASK) != (pipe << 30))
1246 return false;
1247 }
1248 return true;
1249}
1250
Keith Packard1519b992011-08-06 10:35:34 -07001251static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1252 enum pipe pipe, u32 val)
1253{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001254 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001255 return false;
1256
1257 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001258 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001259 return false;
1260 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001261 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001262 return false;
1263 }
1264 return true;
1265}
1266
1267static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1268 enum pipe pipe, u32 val)
1269{
1270 if ((val & LVDS_PORT_EN) == 0)
1271 return false;
1272
1273 if (HAS_PCH_CPT(dev_priv->dev)) {
1274 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1275 return false;
1276 } else {
1277 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1278 return false;
1279 }
1280 return true;
1281}
1282
1283static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1284 enum pipe pipe, u32 val)
1285{
1286 if ((val & ADPA_DAC_ENABLE) == 0)
1287 return false;
1288 if (HAS_PCH_CPT(dev_priv->dev)) {
1289 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1290 return false;
1291 } else {
1292 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1293 return false;
1294 }
1295 return true;
1296}
1297
Jesse Barnes291906f2011-02-02 12:28:03 -08001298static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001299 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001300{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001301 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001302 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001303 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001304 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001305
Daniel Vetter75c5da22012-09-10 21:58:29 +02001306 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1307 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001308 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001309}
1310
1311static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1312 enum pipe pipe, int reg)
1313{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001314 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001315 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001316 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001317 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001318
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001319 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001320 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001321 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001322}
1323
1324static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1325 enum pipe pipe)
1326{
1327 int reg;
1328 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001329
Keith Packardf0575e92011-07-25 22:12:43 -07001330 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1331 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1332 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001333
1334 reg = PCH_ADPA;
1335 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001336 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001337 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001338 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001339
1340 reg = PCH_LVDS;
1341 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001342 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001343 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001344 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001345
Paulo Zanonie2debe92013-02-18 19:00:27 -03001346 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1347 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1348 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001349}
1350
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001351static void intel_init_dpio(struct drm_device *dev)
1352{
1353 struct drm_i915_private *dev_priv = dev->dev_private;
1354
1355 if (!IS_VALLEYVIEW(dev))
1356 return;
1357
1358 /*
1359 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1360 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1361 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1362 * b. The other bits such as sfr settings / modesel may all be set
1363 * to 0.
1364 *
1365 * This should only be done on init and resume from S3 with both
1366 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1367 */
1368 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1369}
1370
Daniel Vetter426115c2013-07-11 22:13:42 +02001371static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001372{
Daniel Vetter426115c2013-07-11 22:13:42 +02001373 struct drm_device *dev = crtc->base.dev;
1374 struct drm_i915_private *dev_priv = dev->dev_private;
1375 int reg = DPLL(crtc->pipe);
1376 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001377
Daniel Vetter426115c2013-07-11 22:13:42 +02001378 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001379
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001380 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001381 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1382
1383 /* PLL is protected by panel, make sure we can write it */
1384 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001385 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001386
Daniel Vetter426115c2013-07-11 22:13:42 +02001387 I915_WRITE(reg, dpll);
1388 POSTING_READ(reg);
1389 udelay(150);
1390
1391 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1392 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1393
1394 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1395 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001396
1397 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001398 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001399 POSTING_READ(reg);
1400 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001401 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001402 POSTING_READ(reg);
1403 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001404 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001405 POSTING_READ(reg);
1406 udelay(150); /* wait for warmup */
1407}
1408
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001409static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001410{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001411 struct drm_device *dev = crtc->base.dev;
1412 struct drm_i915_private *dev_priv = dev->dev_private;
1413 int reg = DPLL(crtc->pipe);
1414 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001415
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001416 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001417
1418 /* No really, not for ILK+ */
1419 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001420
1421 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001422 if (IS_MOBILE(dev) && !IS_I830(dev))
1423 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001424
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001425 I915_WRITE(reg, dpll);
1426
1427 /* Wait for the clocks to stabilize. */
1428 POSTING_READ(reg);
1429 udelay(150);
1430
1431 if (INTEL_INFO(dev)->gen >= 4) {
1432 I915_WRITE(DPLL_MD(crtc->pipe),
1433 crtc->config.dpll_hw_state.dpll_md);
1434 } else {
1435 /* The pixel multiplier can only be updated once the
1436 * DPLL is enabled and the clocks are stable.
1437 *
1438 * So write it again.
1439 */
1440 I915_WRITE(reg, dpll);
1441 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001442
1443 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001444 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001445 POSTING_READ(reg);
1446 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001447 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001448 POSTING_READ(reg);
1449 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001450 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001451 POSTING_READ(reg);
1452 udelay(150); /* wait for warmup */
1453}
1454
1455/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001456 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001457 * @dev_priv: i915 private structure
1458 * @pipe: pipe PLL to disable
1459 *
1460 * Disable the PLL for @pipe, making sure the pipe is off first.
1461 *
1462 * Note! This is for pre-ILK only.
1463 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001464static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001465{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001466 /* Don't disable pipe A or pipe A PLLs if needed */
1467 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1468 return;
1469
1470 /* Make sure the pipe isn't still relying on us */
1471 assert_pipe_disabled(dev_priv, pipe);
1472
Daniel Vetter50b44a42013-06-05 13:34:33 +02001473 I915_WRITE(DPLL(pipe), 0);
1474 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001475}
1476
Jesse Barnesf6071162013-10-01 10:41:38 -07001477static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1478{
1479 u32 val = 0;
1480
1481 /* Make sure the pipe isn't still relying on us */
1482 assert_pipe_disabled(dev_priv, pipe);
1483
1484 /* Leave integrated clock source enabled */
1485 if (pipe == PIPE_B)
1486 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1487 I915_WRITE(DPLL(pipe), val);
1488 POSTING_READ(DPLL(pipe));
1489}
1490
Jesse Barnes89b667f2013-04-18 14:51:36 -07001491void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1492{
1493 u32 port_mask;
1494
1495 if (!port)
1496 port_mask = DPLL_PORTB_READY_MASK;
1497 else
1498 port_mask = DPLL_PORTC_READY_MASK;
1499
1500 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1501 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1502 'B' + port, I915_READ(DPLL(0)));
1503}
1504
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001505/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001506 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001507 * @dev_priv: i915 private structure
1508 * @pipe: pipe PLL to enable
1509 *
1510 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1511 * drives the transcoder clock.
1512 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001513static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001514{
Daniel Vettere2b78262013-06-07 23:10:03 +02001515 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1516 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001517
Chris Wilson48da64a2012-05-13 20:16:12 +01001518 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001519 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001520 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001521 return;
1522
1523 if (WARN_ON(pll->refcount == 0))
1524 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001525
Daniel Vetter46edb022013-06-05 13:34:12 +02001526 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1527 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001528 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001529
Daniel Vettercdbd2312013-06-05 13:34:03 +02001530 if (pll->active++) {
1531 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001532 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001533 return;
1534 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001535 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001536
Daniel Vetter46edb022013-06-05 13:34:12 +02001537 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001538 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001539 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001540}
1541
Daniel Vettere2b78262013-06-07 23:10:03 +02001542static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001543{
Daniel Vettere2b78262013-06-07 23:10:03 +02001544 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1545 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001546
Jesse Barnes92f25842011-01-04 15:09:34 -08001547 /* PCH only available on ILK+ */
1548 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001549 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001550 return;
1551
Chris Wilson48da64a2012-05-13 20:16:12 +01001552 if (WARN_ON(pll->refcount == 0))
1553 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001554
Daniel Vetter46edb022013-06-05 13:34:12 +02001555 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1556 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001557 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001558
Chris Wilson48da64a2012-05-13 20:16:12 +01001559 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001560 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001561 return;
1562 }
1563
Daniel Vettere9d69442013-06-05 13:34:15 +02001564 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001565 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001566 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001567 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001568
Daniel Vetter46edb022013-06-05 13:34:12 +02001569 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001570 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001571 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001572}
1573
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001574static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1575 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001576{
Daniel Vetter23670b322012-11-01 09:15:30 +01001577 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001578 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001580 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001581
1582 /* PCH only available on ILK+ */
1583 BUG_ON(dev_priv->info->gen < 5);
1584
1585 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001586 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001587 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001588
1589 /* FDI must be feeding us bits for PCH ports */
1590 assert_fdi_tx_enabled(dev_priv, pipe);
1591 assert_fdi_rx_enabled(dev_priv, pipe);
1592
Daniel Vetter23670b322012-11-01 09:15:30 +01001593 if (HAS_PCH_CPT(dev)) {
1594 /* Workaround: Set the timing override bit before enabling the
1595 * pch transcoder. */
1596 reg = TRANS_CHICKEN2(pipe);
1597 val = I915_READ(reg);
1598 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1599 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001600 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001601
Daniel Vetterab9412b2013-05-03 11:49:46 +02001602 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001603 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001604 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001605
1606 if (HAS_PCH_IBX(dev_priv->dev)) {
1607 /*
1608 * make the BPC in transcoder be consistent with
1609 * that in pipeconf reg.
1610 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001611 val &= ~PIPECONF_BPC_MASK;
1612 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001613 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001614
1615 val &= ~TRANS_INTERLACE_MASK;
1616 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001617 if (HAS_PCH_IBX(dev_priv->dev) &&
1618 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1619 val |= TRANS_LEGACY_INTERLACED_ILK;
1620 else
1621 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001622 else
1623 val |= TRANS_PROGRESSIVE;
1624
Jesse Barnes040484a2011-01-03 12:14:26 -08001625 I915_WRITE(reg, val | TRANS_ENABLE);
1626 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001627 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001628}
1629
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001630static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001631 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001632{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001633 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001634
1635 /* PCH only available on ILK+ */
1636 BUG_ON(dev_priv->info->gen < 5);
1637
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001638 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001639 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001640 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001641
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001642 /* Workaround: set timing override bit. */
1643 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001644 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001645 I915_WRITE(_TRANSA_CHICKEN2, val);
1646
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001647 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001648 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001649
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001650 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1651 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001652 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001653 else
1654 val |= TRANS_PROGRESSIVE;
1655
Daniel Vetterab9412b2013-05-03 11:49:46 +02001656 I915_WRITE(LPT_TRANSCONF, val);
1657 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001658 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001659}
1660
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001661static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1662 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001663{
Daniel Vetter23670b322012-11-01 09:15:30 +01001664 struct drm_device *dev = dev_priv->dev;
1665 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001666
1667 /* FDI relies on the transcoder */
1668 assert_fdi_tx_disabled(dev_priv, pipe);
1669 assert_fdi_rx_disabled(dev_priv, pipe);
1670
Jesse Barnes291906f2011-02-02 12:28:03 -08001671 /* Ports must be off as well */
1672 assert_pch_ports_disabled(dev_priv, pipe);
1673
Daniel Vetterab9412b2013-05-03 11:49:46 +02001674 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001675 val = I915_READ(reg);
1676 val &= ~TRANS_ENABLE;
1677 I915_WRITE(reg, val);
1678 /* wait for PCH transcoder off, transcoder state */
1679 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001680 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001681
1682 if (!HAS_PCH_IBX(dev)) {
1683 /* Workaround: Clear the timing override chicken bit again. */
1684 reg = TRANS_CHICKEN2(pipe);
1685 val = I915_READ(reg);
1686 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1687 I915_WRITE(reg, val);
1688 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001689}
1690
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001691static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001692{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001693 u32 val;
1694
Daniel Vetterab9412b2013-05-03 11:49:46 +02001695 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001696 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001697 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001698 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001699 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001700 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001701
1702 /* Workaround: clear timing override bit. */
1703 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001704 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001705 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001706}
1707
1708/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001709 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001710 * @dev_priv: i915 private structure
1711 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001712 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001713 *
1714 * Enable @pipe, making sure that various hardware specific requirements
1715 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1716 *
1717 * @pipe should be %PIPE_A or %PIPE_B.
1718 *
1719 * Will wait until the pipe is actually running (i.e. first vblank) before
1720 * returning.
1721 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001722static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001723 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001724{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001725 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1726 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001727 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001728 int reg;
1729 u32 val;
1730
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001731 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001732 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001733 assert_sprites_disabled(dev_priv, pipe);
1734
Paulo Zanoni681e5812012-12-06 11:12:38 -02001735 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001736 pch_transcoder = TRANSCODER_A;
1737 else
1738 pch_transcoder = pipe;
1739
Jesse Barnesb24e7172011-01-04 15:09:30 -08001740 /*
1741 * A pipe without a PLL won't actually be able to drive bits from
1742 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1743 * need the check.
1744 */
1745 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001746 if (dsi)
1747 assert_dsi_pll_enabled(dev_priv);
1748 else
1749 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001750 else {
1751 if (pch_port) {
1752 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001753 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001754 assert_fdi_tx_pll_enabled(dev_priv,
1755 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001756 }
1757 /* FIXME: assert CPU port conditions for SNB+ */
1758 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001759
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001760 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001761 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001762 if (val & PIPECONF_ENABLE)
1763 return;
1764
1765 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001766 intel_wait_for_vblank(dev_priv->dev, pipe);
1767}
1768
1769/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001770 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001771 * @dev_priv: i915 private structure
1772 * @pipe: pipe to disable
1773 *
1774 * Disable @pipe, making sure that various hardware specific requirements
1775 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1776 *
1777 * @pipe should be %PIPE_A or %PIPE_B.
1778 *
1779 * Will wait until the pipe has shut down before returning.
1780 */
1781static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1782 enum pipe pipe)
1783{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001784 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1785 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001786 int reg;
1787 u32 val;
1788
1789 /*
1790 * Make sure planes won't keep trying to pump pixels to us,
1791 * or we might hang the display.
1792 */
1793 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001794 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001795 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001796
1797 /* Don't disable pipe A or pipe A PLLs if needed */
1798 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1799 return;
1800
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001801 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001802 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001803 if ((val & PIPECONF_ENABLE) == 0)
1804 return;
1805
1806 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001807 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1808}
1809
Keith Packardd74362c2011-07-28 14:47:14 -07001810/*
1811 * Plane regs are double buffered, going from enabled->disabled needs a
1812 * trigger in order to latch. The display address reg provides this.
1813 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001814void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001815 enum plane plane)
1816{
Damien Lespiau14f86142012-10-29 15:24:49 +00001817 if (dev_priv->info->gen >= 4)
1818 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1819 else
1820 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001821}
1822
Jesse Barnesb24e7172011-01-04 15:09:30 -08001823/**
1824 * intel_enable_plane - enable a display plane on a given pipe
1825 * @dev_priv: i915 private structure
1826 * @plane: plane to enable
1827 * @pipe: pipe being fed
1828 *
1829 * Enable @plane on @pipe, making sure that @pipe is running first.
1830 */
1831static void intel_enable_plane(struct drm_i915_private *dev_priv,
1832 enum plane plane, enum pipe pipe)
1833{
1834 int reg;
1835 u32 val;
1836
1837 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1838 assert_pipe_enabled(dev_priv, pipe);
1839
1840 reg = DSPCNTR(plane);
1841 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001842 if (val & DISPLAY_PLANE_ENABLE)
1843 return;
1844
1845 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001846 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001847 intel_wait_for_vblank(dev_priv->dev, pipe);
1848}
1849
Jesse Barnesb24e7172011-01-04 15:09:30 -08001850/**
1851 * intel_disable_plane - disable a display plane
1852 * @dev_priv: i915 private structure
1853 * @plane: plane to disable
1854 * @pipe: pipe consuming the data
1855 *
1856 * Disable @plane; should be an independent operation.
1857 */
1858static void intel_disable_plane(struct drm_i915_private *dev_priv,
1859 enum plane plane, enum pipe pipe)
1860{
1861 int reg;
1862 u32 val;
1863
1864 reg = DSPCNTR(plane);
1865 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001866 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1867 return;
1868
1869 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001870 intel_flush_display_plane(dev_priv, plane);
1871 intel_wait_for_vblank(dev_priv->dev, pipe);
1872}
1873
Chris Wilson693db182013-03-05 14:52:39 +00001874static bool need_vtd_wa(struct drm_device *dev)
1875{
1876#ifdef CONFIG_INTEL_IOMMU
1877 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1878 return true;
1879#endif
1880 return false;
1881}
1882
Chris Wilson127bd2a2010-07-23 23:32:05 +01001883int
Chris Wilson48b956c2010-09-14 12:50:34 +01001884intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001885 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001886 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001887{
Chris Wilsonce453d82011-02-21 14:43:56 +00001888 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001889 u32 alignment;
1890 int ret;
1891
Chris Wilson05394f32010-11-08 19:18:58 +00001892 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001893 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001894 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1895 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001896 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001897 alignment = 4 * 1024;
1898 else
1899 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001900 break;
1901 case I915_TILING_X:
1902 /* pin() will align the object as required by fence */
1903 alignment = 0;
1904 break;
1905 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001906 /* Despite that we check this in framebuffer_init userspace can
1907 * screw us over and change the tiling after the fact. Only
1908 * pinned buffers can't change their tiling. */
1909 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001910 return -EINVAL;
1911 default:
1912 BUG();
1913 }
1914
Chris Wilson693db182013-03-05 14:52:39 +00001915 /* Note that the w/a also requires 64 PTE of padding following the
1916 * bo. We currently fill all unused PTE with the shadow page and so
1917 * we should always have valid PTE following the scanout preventing
1918 * the VT-d warning.
1919 */
1920 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1921 alignment = 256 * 1024;
1922
Chris Wilsonce453d82011-02-21 14:43:56 +00001923 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001924 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001925 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001926 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001927
1928 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1929 * fence, whereas 965+ only requires a fence if using
1930 * framebuffer compression. For simplicity, we always install
1931 * a fence as the cost is not that onerous.
1932 */
Chris Wilson06d98132012-04-17 15:31:24 +01001933 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001934 if (ret)
1935 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001936
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001937 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001938
Chris Wilsonce453d82011-02-21 14:43:56 +00001939 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001940 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001941
1942err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001943 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001944err_interruptible:
1945 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001946 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001947}
1948
Chris Wilson1690e1e2011-12-14 13:57:08 +01001949void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1950{
1951 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001952 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001953}
1954
Daniel Vetterc2c75132012-07-05 12:17:30 +02001955/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1956 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001957unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1958 unsigned int tiling_mode,
1959 unsigned int cpp,
1960 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001961{
Chris Wilsonbc752862013-02-21 20:04:31 +00001962 if (tiling_mode != I915_TILING_NONE) {
1963 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001964
Chris Wilsonbc752862013-02-21 20:04:31 +00001965 tile_rows = *y / 8;
1966 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001967
Chris Wilsonbc752862013-02-21 20:04:31 +00001968 tiles = *x / (512/cpp);
1969 *x %= 512/cpp;
1970
1971 return tile_rows * pitch * 8 + tiles * 4096;
1972 } else {
1973 unsigned int offset;
1974
1975 offset = *y * pitch + *x * cpp;
1976 *y = 0;
1977 *x = (offset & 4095) / cpp;
1978 return offset & -4096;
1979 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001980}
1981
Jesse Barnes17638cd2011-06-24 12:19:23 -07001982static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1983 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001984{
1985 struct drm_device *dev = crtc->dev;
1986 struct drm_i915_private *dev_priv = dev->dev_private;
1987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1988 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001989 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001990 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001991 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001992 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001993 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001994
1995 switch (plane) {
1996 case 0:
1997 case 1:
1998 break;
1999 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002000 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002001 return -EINVAL;
2002 }
2003
2004 intel_fb = to_intel_framebuffer(fb);
2005 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002006
Chris Wilson5eddb702010-09-11 13:48:45 +01002007 reg = DSPCNTR(plane);
2008 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002009 /* Mask out pixel format bits in case we change it */
2010 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002011 switch (fb->pixel_format) {
2012 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002013 dspcntr |= DISPPLANE_8BPP;
2014 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002015 case DRM_FORMAT_XRGB1555:
2016 case DRM_FORMAT_ARGB1555:
2017 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002018 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002019 case DRM_FORMAT_RGB565:
2020 dspcntr |= DISPPLANE_BGRX565;
2021 break;
2022 case DRM_FORMAT_XRGB8888:
2023 case DRM_FORMAT_ARGB8888:
2024 dspcntr |= DISPPLANE_BGRX888;
2025 break;
2026 case DRM_FORMAT_XBGR8888:
2027 case DRM_FORMAT_ABGR8888:
2028 dspcntr |= DISPPLANE_RGBX888;
2029 break;
2030 case DRM_FORMAT_XRGB2101010:
2031 case DRM_FORMAT_ARGB2101010:
2032 dspcntr |= DISPPLANE_BGRX101010;
2033 break;
2034 case DRM_FORMAT_XBGR2101010:
2035 case DRM_FORMAT_ABGR2101010:
2036 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002037 break;
2038 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002039 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002040 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002041
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002042 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002043 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002044 dspcntr |= DISPPLANE_TILED;
2045 else
2046 dspcntr &= ~DISPPLANE_TILED;
2047 }
2048
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002049 if (IS_G4X(dev))
2050 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2051
Chris Wilson5eddb702010-09-11 13:48:45 +01002052 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002053
Daniel Vettere506a0c2012-07-05 12:17:29 +02002054 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002055
Daniel Vetterc2c75132012-07-05 12:17:30 +02002056 if (INTEL_INFO(dev)->gen >= 4) {
2057 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002058 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2059 fb->bits_per_pixel / 8,
2060 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002061 linear_offset -= intel_crtc->dspaddr_offset;
2062 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002063 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002064 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002065
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002066 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2067 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2068 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002069 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002070 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002071 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002072 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002073 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002074 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002075 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002076 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002077 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002078
Jesse Barnes17638cd2011-06-24 12:19:23 -07002079 return 0;
2080}
2081
2082static int ironlake_update_plane(struct drm_crtc *crtc,
2083 struct drm_framebuffer *fb, int x, int y)
2084{
2085 struct drm_device *dev = crtc->dev;
2086 struct drm_i915_private *dev_priv = dev->dev_private;
2087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2088 struct intel_framebuffer *intel_fb;
2089 struct drm_i915_gem_object *obj;
2090 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002091 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002092 u32 dspcntr;
2093 u32 reg;
2094
2095 switch (plane) {
2096 case 0:
2097 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002098 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002099 break;
2100 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002101 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 return -EINVAL;
2103 }
2104
2105 intel_fb = to_intel_framebuffer(fb);
2106 obj = intel_fb->obj;
2107
2108 reg = DSPCNTR(plane);
2109 dspcntr = I915_READ(reg);
2110 /* Mask out pixel format bits in case we change it */
2111 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002112 switch (fb->pixel_format) {
2113 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002114 dspcntr |= DISPPLANE_8BPP;
2115 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002116 case DRM_FORMAT_RGB565:
2117 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002118 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002119 case DRM_FORMAT_XRGB8888:
2120 case DRM_FORMAT_ARGB8888:
2121 dspcntr |= DISPPLANE_BGRX888;
2122 break;
2123 case DRM_FORMAT_XBGR8888:
2124 case DRM_FORMAT_ABGR8888:
2125 dspcntr |= DISPPLANE_RGBX888;
2126 break;
2127 case DRM_FORMAT_XRGB2101010:
2128 case DRM_FORMAT_ARGB2101010:
2129 dspcntr |= DISPPLANE_BGRX101010;
2130 break;
2131 case DRM_FORMAT_XBGR2101010:
2132 case DRM_FORMAT_ABGR2101010:
2133 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002134 break;
2135 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002136 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002137 }
2138
2139 if (obj->tiling_mode != I915_TILING_NONE)
2140 dspcntr |= DISPPLANE_TILED;
2141 else
2142 dspcntr &= ~DISPPLANE_TILED;
2143
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002144 if (IS_HASWELL(dev))
2145 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2146 else
2147 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002148
2149 I915_WRITE(reg, dspcntr);
2150
Daniel Vettere506a0c2012-07-05 12:17:29 +02002151 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002152 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002153 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2154 fb->bits_per_pixel / 8,
2155 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002156 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002157
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002158 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2159 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2160 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002161 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002162 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002163 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002164 if (IS_HASWELL(dev)) {
2165 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2166 } else {
2167 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2168 I915_WRITE(DSPLINOFF(plane), linear_offset);
2169 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002170 POSTING_READ(reg);
2171
2172 return 0;
2173}
2174
2175/* Assume fb object is pinned & idle & fenced and just update base pointers */
2176static int
2177intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2178 int x, int y, enum mode_set_atomic state)
2179{
2180 struct drm_device *dev = crtc->dev;
2181 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002182
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002183 if (dev_priv->display.disable_fbc)
2184 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002185 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002186
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002187 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002188}
2189
Ville Syrjälä96a02912013-02-18 19:08:49 +02002190void intel_display_handle_reset(struct drm_device *dev)
2191{
2192 struct drm_i915_private *dev_priv = dev->dev_private;
2193 struct drm_crtc *crtc;
2194
2195 /*
2196 * Flips in the rings have been nuked by the reset,
2197 * so complete all pending flips so that user space
2198 * will get its events and not get stuck.
2199 *
2200 * Also update the base address of all primary
2201 * planes to the the last fb to make sure we're
2202 * showing the correct fb after a reset.
2203 *
2204 * Need to make two loops over the crtcs so that we
2205 * don't try to grab a crtc mutex before the
2206 * pending_flip_queue really got woken up.
2207 */
2208
2209 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2211 enum plane plane = intel_crtc->plane;
2212
2213 intel_prepare_page_flip(dev, plane);
2214 intel_finish_page_flip_plane(dev, plane);
2215 }
2216
2217 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2219
2220 mutex_lock(&crtc->mutex);
2221 if (intel_crtc->active)
2222 dev_priv->display.update_plane(crtc, crtc->fb,
2223 crtc->x, crtc->y);
2224 mutex_unlock(&crtc->mutex);
2225 }
2226}
2227
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002228static int
Chris Wilson14667a42012-04-03 17:58:35 +01002229intel_finish_fb(struct drm_framebuffer *old_fb)
2230{
2231 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2232 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2233 bool was_interruptible = dev_priv->mm.interruptible;
2234 int ret;
2235
Chris Wilson14667a42012-04-03 17:58:35 +01002236 /* Big Hammer, we also need to ensure that any pending
2237 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2238 * current scanout is retired before unpinning the old
2239 * framebuffer.
2240 *
2241 * This should only fail upon a hung GPU, in which case we
2242 * can safely continue.
2243 */
2244 dev_priv->mm.interruptible = false;
2245 ret = i915_gem_object_finish_gpu(obj);
2246 dev_priv->mm.interruptible = was_interruptible;
2247
2248 return ret;
2249}
2250
Ville Syrjälä198598d2012-10-31 17:50:24 +02002251static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2252{
2253 struct drm_device *dev = crtc->dev;
2254 struct drm_i915_master_private *master_priv;
2255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2256
2257 if (!dev->primary->master)
2258 return;
2259
2260 master_priv = dev->primary->master->driver_priv;
2261 if (!master_priv->sarea_priv)
2262 return;
2263
2264 switch (intel_crtc->pipe) {
2265 case 0:
2266 master_priv->sarea_priv->pipeA_x = x;
2267 master_priv->sarea_priv->pipeA_y = y;
2268 break;
2269 case 1:
2270 master_priv->sarea_priv->pipeB_x = x;
2271 master_priv->sarea_priv->pipeB_y = y;
2272 break;
2273 default:
2274 break;
2275 }
2276}
2277
Chris Wilson14667a42012-04-03 17:58:35 +01002278static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002279intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002280 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002281{
2282 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002283 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002285 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002286 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002287
2288 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002289 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002290 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002291 return 0;
2292 }
2293
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002294 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002295 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2296 plane_name(intel_crtc->plane),
2297 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002298 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002299 }
2300
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002301 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002302 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002303 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002304 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002305 if (ret != 0) {
2306 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002307 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002308 return ret;
2309 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002310
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002311 /*
2312 * Update pipe size and adjust fitter if needed: the reason for this is
2313 * that in compute_mode_changes we check the native mode (not the pfit
2314 * mode) to see if we can flip rather than do a full mode set. In the
2315 * fastboot case, we'll flip, but if we don't update the pipesrc and
2316 * pfit state, we'll end up with a big fb scanned out into the wrong
2317 * sized surface.
2318 *
2319 * To fix this properly, we need to hoist the checks up into
2320 * compute_mode_changes (or above), check the actual pfit state and
2321 * whether the platform allows pfit disable with pipe active, and only
2322 * then update the pipesrc and pfit state, even on the flip path.
2323 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002324 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002325 const struct drm_display_mode *adjusted_mode =
2326 &intel_crtc->config.adjusted_mode;
2327
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002328 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002329 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2330 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002331 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002332 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2333 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2334 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2335 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2336 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2337 }
2338 }
2339
Daniel Vetter94352cf2012-07-05 22:51:56 +02002340 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002341 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002342 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002343 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002344 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002345 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002346 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002347
Daniel Vetter94352cf2012-07-05 22:51:56 +02002348 old_fb = crtc->fb;
2349 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002350 crtc->x = x;
2351 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002352
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002353 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002354 if (intel_crtc->active && old_fb != fb)
2355 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002356 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002357 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002358
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002359 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002360 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002361 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002362
Ville Syrjälä198598d2012-10-31 17:50:24 +02002363 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002364
2365 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002366}
2367
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002368static void intel_fdi_normal_train(struct drm_crtc *crtc)
2369{
2370 struct drm_device *dev = crtc->dev;
2371 struct drm_i915_private *dev_priv = dev->dev_private;
2372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2373 int pipe = intel_crtc->pipe;
2374 u32 reg, temp;
2375
2376 /* enable normal train */
2377 reg = FDI_TX_CTL(pipe);
2378 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002379 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002380 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2381 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002382 } else {
2383 temp &= ~FDI_LINK_TRAIN_NONE;
2384 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002385 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002386 I915_WRITE(reg, temp);
2387
2388 reg = FDI_RX_CTL(pipe);
2389 temp = I915_READ(reg);
2390 if (HAS_PCH_CPT(dev)) {
2391 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2392 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2393 } else {
2394 temp &= ~FDI_LINK_TRAIN_NONE;
2395 temp |= FDI_LINK_TRAIN_NONE;
2396 }
2397 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2398
2399 /* wait one idle pattern time */
2400 POSTING_READ(reg);
2401 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002402
2403 /* IVB wants error correction enabled */
2404 if (IS_IVYBRIDGE(dev))
2405 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2406 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002407}
2408
Daniel Vetter1e833f42013-02-19 22:31:57 +01002409static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2410{
2411 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2412}
2413
Daniel Vetter01a415f2012-10-27 15:58:40 +02002414static void ivb_modeset_global_resources(struct drm_device *dev)
2415{
2416 struct drm_i915_private *dev_priv = dev->dev_private;
2417 struct intel_crtc *pipe_B_crtc =
2418 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2419 struct intel_crtc *pipe_C_crtc =
2420 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2421 uint32_t temp;
2422
Daniel Vetter1e833f42013-02-19 22:31:57 +01002423 /*
2424 * When everything is off disable fdi C so that we could enable fdi B
2425 * with all lanes. Note that we don't care about enabled pipes without
2426 * an enabled pch encoder.
2427 */
2428 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2429 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002430 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2431 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2432
2433 temp = I915_READ(SOUTH_CHICKEN1);
2434 temp &= ~FDI_BC_BIFURCATION_SELECT;
2435 DRM_DEBUG_KMS("disabling fdi C rx\n");
2436 I915_WRITE(SOUTH_CHICKEN1, temp);
2437 }
2438}
2439
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002440/* The FDI link training functions for ILK/Ibexpeak. */
2441static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2442{
2443 struct drm_device *dev = crtc->dev;
2444 struct drm_i915_private *dev_priv = dev->dev_private;
2445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2446 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002447 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002448 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002449
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002450 /* FDI needs bits from pipe & plane first */
2451 assert_pipe_enabled(dev_priv, pipe);
2452 assert_plane_enabled(dev_priv, plane);
2453
Adam Jacksone1a44742010-06-25 15:32:14 -04002454 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2455 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002456 reg = FDI_RX_IMR(pipe);
2457 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002458 temp &= ~FDI_RX_SYMBOL_LOCK;
2459 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002460 I915_WRITE(reg, temp);
2461 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002462 udelay(150);
2463
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002464 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002465 reg = FDI_TX_CTL(pipe);
2466 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002467 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2468 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002469 temp &= ~FDI_LINK_TRAIN_NONE;
2470 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472
Chris Wilson5eddb702010-09-11 13:48:45 +01002473 reg = FDI_RX_CTL(pipe);
2474 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002475 temp &= ~FDI_LINK_TRAIN_NONE;
2476 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002477 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2478
2479 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002480 udelay(150);
2481
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002482 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002483 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2484 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2485 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002486
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002488 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002489 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002490 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2491
2492 if ((temp & FDI_RX_BIT_LOCK)) {
2493 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495 break;
2496 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002497 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002498 if (tries == 5)
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;
Chris Wilson5eddb702010-09-11 13:48:45 +01002506 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002507
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 reg = FDI_RX_CTL(pipe);
2509 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002510 temp &= ~FDI_LINK_TRAIN_NONE;
2511 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 I915_WRITE(reg, temp);
2513
2514 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002515 udelay(150);
2516
Chris Wilson5eddb702010-09-11 13:48:45 +01002517 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002518 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002519 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002520 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2521
2522 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002523 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002524 DRM_DEBUG_KMS("FDI train 2 done.\n");
2525 break;
2526 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002527 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002528 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002529 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530
2531 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002532
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533}
2534
Akshay Joshi0206e352011-08-16 15:34:10 -04002535static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2537 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2538 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2539 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2540};
2541
2542/* The FDI link training functions for SNB/Cougarpoint. */
2543static void gen6_fdi_link_train(struct drm_crtc *crtc)
2544{
2545 struct drm_device *dev = crtc->dev;
2546 struct drm_i915_private *dev_priv = dev->dev_private;
2547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2548 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002549 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002550
Adam Jacksone1a44742010-06-25 15:32:14 -04002551 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2552 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002553 reg = FDI_RX_IMR(pipe);
2554 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002555 temp &= ~FDI_RX_SYMBOL_LOCK;
2556 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002557 I915_WRITE(reg, temp);
2558
2559 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002560 udelay(150);
2561
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002563 reg = FDI_TX_CTL(pipe);
2564 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002565 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2566 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002567 temp &= ~FDI_LINK_TRAIN_NONE;
2568 temp |= FDI_LINK_TRAIN_PATTERN_1;
2569 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2570 /* SNB-B */
2571 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002572 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002573
Daniel Vetterd74cf322012-10-26 10:58:13 +02002574 I915_WRITE(FDI_RX_MISC(pipe),
2575 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2576
Chris Wilson5eddb702010-09-11 13:48:45 +01002577 reg = FDI_RX_CTL(pipe);
2578 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002579 if (HAS_PCH_CPT(dev)) {
2580 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2581 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2582 } else {
2583 temp &= ~FDI_LINK_TRAIN_NONE;
2584 temp |= FDI_LINK_TRAIN_PATTERN_1;
2585 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2587
2588 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002589 udelay(150);
2590
Akshay Joshi0206e352011-08-16 15:34:10 -04002591 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 reg = FDI_TX_CTL(pipe);
2593 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002594 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2595 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 I915_WRITE(reg, temp);
2597
2598 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002599 udelay(500);
2600
Sean Paulfa37d392012-03-02 12:53:39 -05002601 for (retry = 0; retry < 5; retry++) {
2602 reg = FDI_RX_IIR(pipe);
2603 temp = I915_READ(reg);
2604 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2605 if (temp & FDI_RX_BIT_LOCK) {
2606 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2607 DRM_DEBUG_KMS("FDI train 1 done.\n");
2608 break;
2609 }
2610 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002611 }
Sean Paulfa37d392012-03-02 12:53:39 -05002612 if (retry < 5)
2613 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002614 }
2615 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617
2618 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002619 reg = FDI_TX_CTL(pipe);
2620 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002621 temp &= ~FDI_LINK_TRAIN_NONE;
2622 temp |= FDI_LINK_TRAIN_PATTERN_2;
2623 if (IS_GEN6(dev)) {
2624 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2625 /* SNB-B */
2626 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2627 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002628 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002629
Chris Wilson5eddb702010-09-11 13:48:45 +01002630 reg = FDI_RX_CTL(pipe);
2631 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002632 if (HAS_PCH_CPT(dev)) {
2633 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2634 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2635 } else {
2636 temp &= ~FDI_LINK_TRAIN_NONE;
2637 temp |= FDI_LINK_TRAIN_PATTERN_2;
2638 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002639 I915_WRITE(reg, temp);
2640
2641 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002642 udelay(150);
2643
Akshay Joshi0206e352011-08-16 15:34:10 -04002644 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002645 reg = FDI_TX_CTL(pipe);
2646 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002647 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2648 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002649 I915_WRITE(reg, temp);
2650
2651 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002652 udelay(500);
2653
Sean Paulfa37d392012-03-02 12:53:39 -05002654 for (retry = 0; retry < 5; retry++) {
2655 reg = FDI_RX_IIR(pipe);
2656 temp = I915_READ(reg);
2657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2658 if (temp & FDI_RX_SYMBOL_LOCK) {
2659 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2660 DRM_DEBUG_KMS("FDI train 2 done.\n");
2661 break;
2662 }
2663 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002664 }
Sean Paulfa37d392012-03-02 12:53:39 -05002665 if (retry < 5)
2666 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002667 }
2668 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002669 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002670
2671 DRM_DEBUG_KMS("FDI train done.\n");
2672}
2673
Jesse Barnes357555c2011-04-28 15:09:55 -07002674/* Manual link training for Ivy Bridge A0 parts */
2675static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2676{
2677 struct drm_device *dev = crtc->dev;
2678 struct drm_i915_private *dev_priv = dev->dev_private;
2679 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2680 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002681 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002682
2683 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2684 for train result */
2685 reg = FDI_RX_IMR(pipe);
2686 temp = I915_READ(reg);
2687 temp &= ~FDI_RX_SYMBOL_LOCK;
2688 temp &= ~FDI_RX_BIT_LOCK;
2689 I915_WRITE(reg, temp);
2690
2691 POSTING_READ(reg);
2692 udelay(150);
2693
Daniel Vetter01a415f2012-10-27 15:58:40 +02002694 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2695 I915_READ(FDI_RX_IIR(pipe)));
2696
Jesse Barnes139ccd32013-08-19 11:04:55 -07002697 /* Try each vswing and preemphasis setting twice before moving on */
2698 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2699 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002700 reg = FDI_TX_CTL(pipe);
2701 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002702 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2703 temp &= ~FDI_TX_ENABLE;
2704 I915_WRITE(reg, temp);
2705
2706 reg = FDI_RX_CTL(pipe);
2707 temp = I915_READ(reg);
2708 temp &= ~FDI_LINK_TRAIN_AUTO;
2709 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2710 temp &= ~FDI_RX_ENABLE;
2711 I915_WRITE(reg, temp);
2712
2713 /* enable CPU FDI TX and PCH FDI RX */
2714 reg = FDI_TX_CTL(pipe);
2715 temp = I915_READ(reg);
2716 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2717 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2718 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002719 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002720 temp |= snb_b_fdi_train_param[j/2];
2721 temp |= FDI_COMPOSITE_SYNC;
2722 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2723
2724 I915_WRITE(FDI_RX_MISC(pipe),
2725 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2726
2727 reg = FDI_RX_CTL(pipe);
2728 temp = I915_READ(reg);
2729 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2730 temp |= FDI_COMPOSITE_SYNC;
2731 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2732
2733 POSTING_READ(reg);
2734 udelay(1); /* should be 0.5us */
2735
2736 for (i = 0; i < 4; i++) {
2737 reg = FDI_RX_IIR(pipe);
2738 temp = I915_READ(reg);
2739 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2740
2741 if (temp & FDI_RX_BIT_LOCK ||
2742 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2743 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2744 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2745 i);
2746 break;
2747 }
2748 udelay(1); /* should be 0.5us */
2749 }
2750 if (i == 4) {
2751 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2752 continue;
2753 }
2754
2755 /* Train 2 */
2756 reg = FDI_TX_CTL(pipe);
2757 temp = I915_READ(reg);
2758 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2759 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2760 I915_WRITE(reg, temp);
2761
2762 reg = FDI_RX_CTL(pipe);
2763 temp = I915_READ(reg);
2764 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2765 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002766 I915_WRITE(reg, temp);
2767
2768 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002769 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002770
Jesse Barnes139ccd32013-08-19 11:04:55 -07002771 for (i = 0; i < 4; i++) {
2772 reg = FDI_RX_IIR(pipe);
2773 temp = I915_READ(reg);
2774 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002775
Jesse Barnes139ccd32013-08-19 11:04:55 -07002776 if (temp & FDI_RX_SYMBOL_LOCK ||
2777 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2778 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2779 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2780 i);
2781 goto train_done;
2782 }
2783 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002784 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002785 if (i == 4)
2786 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002787 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002788
Jesse Barnes139ccd32013-08-19 11:04:55 -07002789train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002790 DRM_DEBUG_KMS("FDI train done.\n");
2791}
2792
Daniel Vetter88cefb62012-08-12 19:27:14 +02002793static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002794{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002795 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002796 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002797 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002798 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002799
Jesse Barnesc64e3112010-09-10 11:27:03 -07002800
Jesse Barnes0e23b992010-09-10 11:10:00 -07002801 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002802 reg = FDI_RX_CTL(pipe);
2803 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002804 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2805 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002806 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002807 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2808
2809 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002810 udelay(200);
2811
2812 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002813 temp = I915_READ(reg);
2814 I915_WRITE(reg, temp | FDI_PCDCLK);
2815
2816 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002817 udelay(200);
2818
Paulo Zanoni20749732012-11-23 15:30:38 -02002819 /* Enable CPU FDI TX PLL, always on for Ironlake */
2820 reg = FDI_TX_CTL(pipe);
2821 temp = I915_READ(reg);
2822 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2823 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002824
Paulo Zanoni20749732012-11-23 15:30:38 -02002825 POSTING_READ(reg);
2826 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002827 }
2828}
2829
Daniel Vetter88cefb62012-08-12 19:27:14 +02002830static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2831{
2832 struct drm_device *dev = intel_crtc->base.dev;
2833 struct drm_i915_private *dev_priv = dev->dev_private;
2834 int pipe = intel_crtc->pipe;
2835 u32 reg, temp;
2836
2837 /* Switch from PCDclk to Rawclk */
2838 reg = FDI_RX_CTL(pipe);
2839 temp = I915_READ(reg);
2840 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2841
2842 /* Disable CPU FDI TX PLL */
2843 reg = FDI_TX_CTL(pipe);
2844 temp = I915_READ(reg);
2845 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2846
2847 POSTING_READ(reg);
2848 udelay(100);
2849
2850 reg = FDI_RX_CTL(pipe);
2851 temp = I915_READ(reg);
2852 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2853
2854 /* Wait for the clocks to turn off. */
2855 POSTING_READ(reg);
2856 udelay(100);
2857}
2858
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002859static void ironlake_fdi_disable(struct drm_crtc *crtc)
2860{
2861 struct drm_device *dev = crtc->dev;
2862 struct drm_i915_private *dev_priv = dev->dev_private;
2863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2864 int pipe = intel_crtc->pipe;
2865 u32 reg, temp;
2866
2867 /* disable CPU FDI tx and PCH FDI rx */
2868 reg = FDI_TX_CTL(pipe);
2869 temp = I915_READ(reg);
2870 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2871 POSTING_READ(reg);
2872
2873 reg = FDI_RX_CTL(pipe);
2874 temp = I915_READ(reg);
2875 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002876 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002877 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2878
2879 POSTING_READ(reg);
2880 udelay(100);
2881
2882 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002883 if (HAS_PCH_IBX(dev)) {
2884 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002885 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002886
2887 /* still set train pattern 1 */
2888 reg = FDI_TX_CTL(pipe);
2889 temp = I915_READ(reg);
2890 temp &= ~FDI_LINK_TRAIN_NONE;
2891 temp |= FDI_LINK_TRAIN_PATTERN_1;
2892 I915_WRITE(reg, temp);
2893
2894 reg = FDI_RX_CTL(pipe);
2895 temp = I915_READ(reg);
2896 if (HAS_PCH_CPT(dev)) {
2897 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2898 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2899 } else {
2900 temp &= ~FDI_LINK_TRAIN_NONE;
2901 temp |= FDI_LINK_TRAIN_PATTERN_1;
2902 }
2903 /* BPC in FDI rx is consistent with that in PIPECONF */
2904 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002905 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002906 I915_WRITE(reg, temp);
2907
2908 POSTING_READ(reg);
2909 udelay(100);
2910}
2911
Chris Wilson5bb61642012-09-27 21:25:58 +01002912static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2913{
2914 struct drm_device *dev = crtc->dev;
2915 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002917 unsigned long flags;
2918 bool pending;
2919
Ville Syrjälä10d83732013-01-29 18:13:34 +02002920 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2921 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002922 return false;
2923
2924 spin_lock_irqsave(&dev->event_lock, flags);
2925 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2926 spin_unlock_irqrestore(&dev->event_lock, flags);
2927
2928 return pending;
2929}
2930
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002931static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2932{
Chris Wilson0f911282012-04-17 10:05:38 +01002933 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002934 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002935
2936 if (crtc->fb == NULL)
2937 return;
2938
Daniel Vetter2c10d572012-12-20 21:24:07 +01002939 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2940
Chris Wilson5bb61642012-09-27 21:25:58 +01002941 wait_event(dev_priv->pending_flip_queue,
2942 !intel_crtc_has_pending_flip(crtc));
2943
Chris Wilson0f911282012-04-17 10:05:38 +01002944 mutex_lock(&dev->struct_mutex);
2945 intel_finish_fb(crtc->fb);
2946 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002947}
2948
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002949/* Program iCLKIP clock to the desired frequency */
2950static void lpt_program_iclkip(struct drm_crtc *crtc)
2951{
2952 struct drm_device *dev = crtc->dev;
2953 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002954 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002955 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2956 u32 temp;
2957
Daniel Vetter09153002012-12-12 14:06:44 +01002958 mutex_lock(&dev_priv->dpio_lock);
2959
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002960 /* It is necessary to ungate the pixclk gate prior to programming
2961 * the divisors, and gate it back when it is done.
2962 */
2963 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2964
2965 /* Disable SSCCTL */
2966 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002967 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2968 SBI_SSCCTL_DISABLE,
2969 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002970
2971 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002972 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002973 auxdiv = 1;
2974 divsel = 0x41;
2975 phaseinc = 0x20;
2976 } else {
2977 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01002978 * but the adjusted_mode->crtc_clock in in KHz. To get the
2979 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002980 * convert the virtual clock precision to KHz here for higher
2981 * precision.
2982 */
2983 u32 iclk_virtual_root_freq = 172800 * 1000;
2984 u32 iclk_pi_range = 64;
2985 u32 desired_divisor, msb_divisor_value, pi_value;
2986
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002987 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002988 msb_divisor_value = desired_divisor / iclk_pi_range;
2989 pi_value = desired_divisor % iclk_pi_range;
2990
2991 auxdiv = 0;
2992 divsel = msb_divisor_value - 2;
2993 phaseinc = pi_value;
2994 }
2995
2996 /* This should not happen with any sane values */
2997 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2998 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2999 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3000 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3001
3002 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003003 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003004 auxdiv,
3005 divsel,
3006 phasedir,
3007 phaseinc);
3008
3009 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003010 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003011 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3012 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3013 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3014 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3015 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3016 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003017 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003018
3019 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003020 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003021 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3022 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003023 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003024
3025 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003026 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003027 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003028 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003029
3030 /* Wait for initialization time */
3031 udelay(24);
3032
3033 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003034
3035 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003036}
3037
Daniel Vetter275f01b22013-05-03 11:49:47 +02003038static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3039 enum pipe pch_transcoder)
3040{
3041 struct drm_device *dev = crtc->base.dev;
3042 struct drm_i915_private *dev_priv = dev->dev_private;
3043 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3044
3045 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3046 I915_READ(HTOTAL(cpu_transcoder)));
3047 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3048 I915_READ(HBLANK(cpu_transcoder)));
3049 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3050 I915_READ(HSYNC(cpu_transcoder)));
3051
3052 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3053 I915_READ(VTOTAL(cpu_transcoder)));
3054 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3055 I915_READ(VBLANK(cpu_transcoder)));
3056 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3057 I915_READ(VSYNC(cpu_transcoder)));
3058 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3059 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3060}
3061
Jesse Barnesf67a5592011-01-05 10:31:48 -08003062/*
3063 * Enable PCH resources required for PCH ports:
3064 * - PCH PLLs
3065 * - FDI training & RX/TX
3066 * - update transcoder timings
3067 * - DP transcoding bits
3068 * - transcoder
3069 */
3070static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003071{
3072 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003073 struct drm_i915_private *dev_priv = dev->dev_private;
3074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3075 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003076 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003077
Daniel Vetterab9412b2013-05-03 11:49:46 +02003078 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003079
Daniel Vettercd986ab2012-10-26 10:58:12 +02003080 /* Write the TU size bits before fdi link training, so that error
3081 * detection works. */
3082 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3083 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3084
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003085 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003086 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003087
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003088 /* We need to program the right clock selection before writing the pixel
3089 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003090 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003091 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003092
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003093 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003094 temp |= TRANS_DPLL_ENABLE(pipe);
3095 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003096 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003097 temp |= sel;
3098 else
3099 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003100 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003101 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003102
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003103 /* XXX: pch pll's can be enabled any time before we enable the PCH
3104 * transcoder, and we actually should do this to not upset any PCH
3105 * transcoder that already use the clock when we share it.
3106 *
3107 * Note that enable_shared_dpll tries to do the right thing, but
3108 * get_shared_dpll unconditionally resets the pll - we need that to have
3109 * the right LVDS enable sequence. */
3110 ironlake_enable_shared_dpll(intel_crtc);
3111
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003112 /* set transcoder timing, panel must allow it */
3113 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003114 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003115
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003116 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003117
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003118 /* For PCH DP, enable TRANS_DP_CTL */
3119 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003120 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3121 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003122 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003123 reg = TRANS_DP_CTL(pipe);
3124 temp = I915_READ(reg);
3125 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003126 TRANS_DP_SYNC_MASK |
3127 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003128 temp |= (TRANS_DP_OUTPUT_ENABLE |
3129 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003130 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003131
3132 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003133 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003134 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003135 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003136
3137 switch (intel_trans_dp_port_sel(crtc)) {
3138 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003139 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003140 break;
3141 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003142 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003143 break;
3144 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003145 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003146 break;
3147 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003148 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003149 }
3150
Chris Wilson5eddb702010-09-11 13:48:45 +01003151 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003152 }
3153
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003154 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003155}
3156
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003157static void lpt_pch_enable(struct drm_crtc *crtc)
3158{
3159 struct drm_device *dev = crtc->dev;
3160 struct drm_i915_private *dev_priv = dev->dev_private;
3161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003162 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003163
Daniel Vetterab9412b2013-05-03 11:49:46 +02003164 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003165
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003166 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003167
Paulo Zanoni0540e482012-10-31 18:12:40 -02003168 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003169 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003170
Paulo Zanoni937bb612012-10-31 18:12:47 -02003171 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003172}
3173
Daniel Vettere2b78262013-06-07 23:10:03 +02003174static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003175{
Daniel Vettere2b78262013-06-07 23:10:03 +02003176 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003177
3178 if (pll == NULL)
3179 return;
3180
3181 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003182 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003183 return;
3184 }
3185
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003186 if (--pll->refcount == 0) {
3187 WARN_ON(pll->on);
3188 WARN_ON(pll->active);
3189 }
3190
Daniel Vettera43f6e02013-06-07 23:10:32 +02003191 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003192}
3193
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003194static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003195{
Daniel Vettere2b78262013-06-07 23:10:03 +02003196 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3197 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3198 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003199
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003200 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003201 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3202 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003203 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003204 }
3205
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003206 if (HAS_PCH_IBX(dev_priv->dev)) {
3207 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003208 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003209 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003210
Daniel Vetter46edb022013-06-05 13:34:12 +02003211 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3212 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003213
3214 goto found;
3215 }
3216
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003217 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3218 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003219
3220 /* Only want to check enabled timings first */
3221 if (pll->refcount == 0)
3222 continue;
3223
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003224 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3225 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003226 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003227 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003228 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003229
3230 goto found;
3231 }
3232 }
3233
3234 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003235 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3236 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003237 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003238 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3239 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003240 goto found;
3241 }
3242 }
3243
3244 return NULL;
3245
3246found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003247 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003248 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3249 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003250
Daniel Vettercdbd2312013-06-05 13:34:03 +02003251 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003252 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3253 sizeof(pll->hw_state));
3254
Daniel Vetter46edb022013-06-05 13:34:12 +02003255 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003256 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003257 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003258
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003259 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003260 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003261 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003262
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003263 return pll;
3264}
3265
Daniel Vettera1520312013-05-03 11:49:50 +02003266static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003267{
3268 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003269 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003270 u32 temp;
3271
3272 temp = I915_READ(dslreg);
3273 udelay(500);
3274 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003275 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003276 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003277 }
3278}
3279
Jesse Barnesb074cec2013-04-25 12:55:02 -07003280static void ironlake_pfit_enable(struct intel_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->base.dev;
3283 struct drm_i915_private *dev_priv = dev->dev_private;
3284 int pipe = crtc->pipe;
3285
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003286 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003287 /* Force use of hard-coded filter coefficients
3288 * as some pre-programmed values are broken,
3289 * e.g. x201.
3290 */
3291 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3292 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3293 PF_PIPE_SEL_IVB(pipe));
3294 else
3295 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3296 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3297 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003298 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003299}
3300
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003301static void intel_enable_planes(struct drm_crtc *crtc)
3302{
3303 struct drm_device *dev = crtc->dev;
3304 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3305 struct intel_plane *intel_plane;
3306
3307 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3308 if (intel_plane->pipe == pipe)
3309 intel_plane_restore(&intel_plane->base);
3310}
3311
3312static void intel_disable_planes(struct drm_crtc *crtc)
3313{
3314 struct drm_device *dev = crtc->dev;
3315 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3316 struct intel_plane *intel_plane;
3317
3318 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3319 if (intel_plane->pipe == pipe)
3320 intel_plane_disable(&intel_plane->base);
3321}
3322
Paulo Zanonid77e4532013-09-24 13:52:55 -03003323static void hsw_enable_ips(struct intel_crtc *crtc)
3324{
3325 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3326
3327 if (!crtc->config.ips_enabled)
3328 return;
3329
3330 /* We can only enable IPS after we enable a plane and wait for a vblank.
3331 * We guarantee that the plane is enabled by calling intel_enable_ips
3332 * only after intel_enable_plane. And intel_enable_plane already waits
3333 * for a vblank, so all we need to do here is to enable the IPS bit. */
3334 assert_plane_enabled(dev_priv, crtc->plane);
3335 I915_WRITE(IPS_CTL, IPS_ENABLE);
3336}
3337
3338static void hsw_disable_ips(struct intel_crtc *crtc)
3339{
3340 struct drm_device *dev = crtc->base.dev;
3341 struct drm_i915_private *dev_priv = dev->dev_private;
3342
3343 if (!crtc->config.ips_enabled)
3344 return;
3345
3346 assert_plane_enabled(dev_priv, crtc->plane);
3347 I915_WRITE(IPS_CTL, 0);
3348 POSTING_READ(IPS_CTL);
3349
3350 /* We need to wait for a vblank before we can disable the plane. */
3351 intel_wait_for_vblank(dev, crtc->pipe);
3352}
3353
3354/** Loads the palette/gamma unit for the CRTC with the prepared values */
3355static void intel_crtc_load_lut(struct drm_crtc *crtc)
3356{
3357 struct drm_device *dev = crtc->dev;
3358 struct drm_i915_private *dev_priv = dev->dev_private;
3359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3360 enum pipe pipe = intel_crtc->pipe;
3361 int palreg = PALETTE(pipe);
3362 int i;
3363 bool reenable_ips = false;
3364
3365 /* The clocks have to be on to load the palette. */
3366 if (!crtc->enabled || !intel_crtc->active)
3367 return;
3368
3369 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3370 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3371 assert_dsi_pll_enabled(dev_priv);
3372 else
3373 assert_pll_enabled(dev_priv, pipe);
3374 }
3375
3376 /* use legacy palette for Ironlake */
3377 if (HAS_PCH_SPLIT(dev))
3378 palreg = LGC_PALETTE(pipe);
3379
3380 /* Workaround : Do not read or write the pipe palette/gamma data while
3381 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3382 */
3383 if (intel_crtc->config.ips_enabled &&
3384 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3385 GAMMA_MODE_MODE_SPLIT)) {
3386 hsw_disable_ips(intel_crtc);
3387 reenable_ips = true;
3388 }
3389
3390 for (i = 0; i < 256; i++) {
3391 I915_WRITE(palreg + 4 * i,
3392 (intel_crtc->lut_r[i] << 16) |
3393 (intel_crtc->lut_g[i] << 8) |
3394 intel_crtc->lut_b[i]);
3395 }
3396
3397 if (reenable_ips)
3398 hsw_enable_ips(intel_crtc);
3399}
3400
Jesse Barnesf67a5592011-01-05 10:31:48 -08003401static void ironlake_crtc_enable(struct drm_crtc *crtc)
3402{
3403 struct drm_device *dev = crtc->dev;
3404 struct drm_i915_private *dev_priv = dev->dev_private;
3405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003406 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003407 int pipe = intel_crtc->pipe;
3408 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003409
Daniel Vetter08a48462012-07-02 11:43:47 +02003410 WARN_ON(!crtc->enabled);
3411
Jesse Barnesf67a5592011-01-05 10:31:48 -08003412 if (intel_crtc->active)
3413 return;
3414
3415 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003416
3417 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3418 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3419
Daniel Vetterf6736a12013-06-05 13:34:30 +02003420 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003421 if (encoder->pre_enable)
3422 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003423
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003424 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003425 /* Note: FDI PLL enabling _must_ be done before we enable the
3426 * cpu pipes, hence this is separate from all the other fdi/pch
3427 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003428 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003429 } else {
3430 assert_fdi_tx_disabled(dev_priv, pipe);
3431 assert_fdi_rx_disabled(dev_priv, pipe);
3432 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003433
Jesse Barnesb074cec2013-04-25 12:55:02 -07003434 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003435
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003436 /*
3437 * On ILK+ LUT must be loaded before the pipe is running but with
3438 * clocks enabled
3439 */
3440 intel_crtc_load_lut(crtc);
3441
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003442 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003443 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003444 intel_crtc->config.has_pch_encoder, false);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003445 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003446 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003447 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003448
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003449 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003450 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003451
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003452 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003453 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003454 mutex_unlock(&dev->struct_mutex);
3455
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003456 for_each_encoder_on_crtc(dev, crtc, encoder)
3457 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003458
3459 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003460 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003461
3462 /*
3463 * There seems to be a race in PCH platform hw (at least on some
3464 * outputs) where an enabled pipe still completes any pageflip right
3465 * away (as if the pipe is off) instead of waiting for vblank. As soon
3466 * as the first vblank happend, everything works as expected. Hence just
3467 * wait for one vblank before returning to avoid strange things
3468 * happening.
3469 */
3470 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003471}
3472
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003473/* IPS only exists on ULT machines and is tied to pipe A. */
3474static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3475{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003476 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003477}
3478
Ville Syrjälädda9a662013-09-19 17:00:37 -03003479static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3480{
3481 struct drm_device *dev = crtc->dev;
3482 struct drm_i915_private *dev_priv = dev->dev_private;
3483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3484 int pipe = intel_crtc->pipe;
3485 int plane = intel_crtc->plane;
3486
3487 intel_enable_plane(dev_priv, plane, pipe);
3488 intel_enable_planes(crtc);
3489 intel_crtc_update_cursor(crtc, true);
3490
3491 hsw_enable_ips(intel_crtc);
3492
3493 mutex_lock(&dev->struct_mutex);
3494 intel_update_fbc(dev);
3495 mutex_unlock(&dev->struct_mutex);
3496}
3497
3498static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3499{
3500 struct drm_device *dev = crtc->dev;
3501 struct drm_i915_private *dev_priv = dev->dev_private;
3502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3503 int pipe = intel_crtc->pipe;
3504 int plane = intel_crtc->plane;
3505
3506 intel_crtc_wait_for_pending_flips(crtc);
3507 drm_vblank_off(dev, pipe);
3508
3509 /* FBC must be disabled before disabling the plane on HSW. */
3510 if (dev_priv->fbc.plane == plane)
3511 intel_disable_fbc(dev);
3512
3513 hsw_disable_ips(intel_crtc);
3514
3515 intel_crtc_update_cursor(crtc, false);
3516 intel_disable_planes(crtc);
3517 intel_disable_plane(dev_priv, plane, pipe);
3518}
3519
Paulo Zanonie4916942013-09-20 16:21:19 -03003520/*
3521 * This implements the workaround described in the "notes" section of the mode
3522 * set sequence documentation. When going from no pipes or single pipe to
3523 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3524 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3525 */
3526static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3527{
3528 struct drm_device *dev = crtc->base.dev;
3529 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3530
3531 /* We want to get the other_active_crtc only if there's only 1 other
3532 * active crtc. */
3533 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3534 if (!crtc_it->active || crtc_it == crtc)
3535 continue;
3536
3537 if (other_active_crtc)
3538 return;
3539
3540 other_active_crtc = crtc_it;
3541 }
3542 if (!other_active_crtc)
3543 return;
3544
3545 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3546 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3547}
3548
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003549static void haswell_crtc_enable(struct drm_crtc *crtc)
3550{
3551 struct drm_device *dev = crtc->dev;
3552 struct drm_i915_private *dev_priv = dev->dev_private;
3553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3554 struct intel_encoder *encoder;
3555 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003556
3557 WARN_ON(!crtc->enabled);
3558
3559 if (intel_crtc->active)
3560 return;
3561
3562 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003563
3564 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3565 if (intel_crtc->config.has_pch_encoder)
3566 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3567
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003568 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003569 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003570
3571 for_each_encoder_on_crtc(dev, crtc, encoder)
3572 if (encoder->pre_enable)
3573 encoder->pre_enable(encoder);
3574
Paulo Zanoni1f544382012-10-24 11:32:00 -02003575 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003576
Jesse Barnesb074cec2013-04-25 12:55:02 -07003577 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003578
3579 /*
3580 * On ILK+ LUT must be loaded before the pipe is running but with
3581 * clocks enabled
3582 */
3583 intel_crtc_load_lut(crtc);
3584
Paulo Zanoni1f544382012-10-24 11:32:00 -02003585 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003586 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003587
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003588 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003589 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003590 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003591
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003592 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003593 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003594
Jani Nikula8807e552013-08-30 19:40:32 +03003595 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003596 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003597 intel_opregion_notify_encoder(encoder, true);
3598 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003599
Paulo Zanonie4916942013-09-20 16:21:19 -03003600 /* If we change the relative order between pipe/planes enabling, we need
3601 * to change the workaround. */
3602 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003603 haswell_crtc_enable_planes(crtc);
3604
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003605 /*
3606 * There seems to be a race in PCH platform hw (at least on some
3607 * outputs) where an enabled pipe still completes any pageflip right
3608 * away (as if the pipe is off) instead of waiting for vblank. As soon
3609 * as the first vblank happend, everything works as expected. Hence just
3610 * wait for one vblank before returning to avoid strange things
3611 * happening.
3612 */
3613 intel_wait_for_vblank(dev, intel_crtc->pipe);
3614}
3615
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003616static void ironlake_pfit_disable(struct intel_crtc *crtc)
3617{
3618 struct drm_device *dev = crtc->base.dev;
3619 struct drm_i915_private *dev_priv = dev->dev_private;
3620 int pipe = crtc->pipe;
3621
3622 /* To avoid upsetting the power well on haswell only disable the pfit if
3623 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003624 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003625 I915_WRITE(PF_CTL(pipe), 0);
3626 I915_WRITE(PF_WIN_POS(pipe), 0);
3627 I915_WRITE(PF_WIN_SZ(pipe), 0);
3628 }
3629}
3630
Jesse Barnes6be4a602010-09-10 10:26:01 -07003631static void ironlake_crtc_disable(struct drm_crtc *crtc)
3632{
3633 struct drm_device *dev = crtc->dev;
3634 struct drm_i915_private *dev_priv = dev->dev_private;
3635 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003636 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003637 int pipe = intel_crtc->pipe;
3638 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003639 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003640
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003641
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003642 if (!intel_crtc->active)
3643 return;
3644
Daniel Vetterea9d7582012-07-10 10:42:52 +02003645 for_each_encoder_on_crtc(dev, crtc, encoder)
3646 encoder->disable(encoder);
3647
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003648 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003649 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003650
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003651 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003652 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003653
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003654 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003655 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003656 intel_disable_plane(dev_priv, plane, pipe);
3657
Daniel Vetterd925c592013-06-05 13:34:04 +02003658 if (intel_crtc->config.has_pch_encoder)
3659 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3660
Jesse Barnesb24e7172011-01-04 15:09:30 -08003661 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003662
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003663 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003664
Daniel Vetterbf49ec8c2012-09-06 22:15:40 +02003665 for_each_encoder_on_crtc(dev, crtc, encoder)
3666 if (encoder->post_disable)
3667 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003668
Daniel Vetterd925c592013-06-05 13:34:04 +02003669 if (intel_crtc->config.has_pch_encoder) {
3670 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003671
Daniel Vetterd925c592013-06-05 13:34:04 +02003672 ironlake_disable_pch_transcoder(dev_priv, pipe);
3673 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003674
Daniel Vetterd925c592013-06-05 13:34:04 +02003675 if (HAS_PCH_CPT(dev)) {
3676 /* disable TRANS_DP_CTL */
3677 reg = TRANS_DP_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3680 TRANS_DP_PORT_SEL_MASK);
3681 temp |= TRANS_DP_PORT_SEL_NONE;
3682 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003683
Daniel Vetterd925c592013-06-05 13:34:04 +02003684 /* disable DPLL_SEL */
3685 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003686 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003687 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003688 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003689
3690 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003691 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003692
3693 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003694 }
3695
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003696 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003697 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003698
3699 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003700 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003701 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003702}
3703
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003704static void haswell_crtc_disable(struct drm_crtc *crtc)
3705{
3706 struct drm_device *dev = crtc->dev;
3707 struct drm_i915_private *dev_priv = dev->dev_private;
3708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3709 struct intel_encoder *encoder;
3710 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003711 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003712
3713 if (!intel_crtc->active)
3714 return;
3715
Ville Syrjälädda9a662013-09-19 17:00:37 -03003716 haswell_crtc_disable_planes(crtc);
3717
Jani Nikula8807e552013-08-30 19:40:32 +03003718 for_each_encoder_on_crtc(dev, crtc, encoder) {
3719 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003720 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003721 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003722
Paulo Zanoni86642812013-04-12 17:57:57 -03003723 if (intel_crtc->config.has_pch_encoder)
3724 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003725 intel_disable_pipe(dev_priv, pipe);
3726
Paulo Zanoniad80a812012-10-24 16:06:19 -02003727 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003728
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003729 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003730
Paulo Zanoni1f544382012-10-24 11:32:00 -02003731 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003732
3733 for_each_encoder_on_crtc(dev, crtc, encoder)
3734 if (encoder->post_disable)
3735 encoder->post_disable(encoder);
3736
Daniel Vetter88adfff2013-03-28 10:42:01 +01003737 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003738 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003739 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003740 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003741 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003742
3743 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003744 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003745
3746 mutex_lock(&dev->struct_mutex);
3747 intel_update_fbc(dev);
3748 mutex_unlock(&dev->struct_mutex);
3749}
3750
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003751static void ironlake_crtc_off(struct drm_crtc *crtc)
3752{
3753 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003754 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003755}
3756
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003757static void haswell_crtc_off(struct drm_crtc *crtc)
3758{
3759 intel_ddi_put_crtc_pll(crtc);
3760}
3761
Daniel Vetter02e792f2009-09-15 22:57:34 +02003762static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3763{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003764 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003765 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003766 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003767
Chris Wilson23f09ce2010-08-12 13:53:37 +01003768 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003769 dev_priv->mm.interruptible = false;
3770 (void) intel_overlay_switch_off(intel_crtc->overlay);
3771 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003772 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003773 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003774
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003775 /* Let userspace switch the overlay on again. In most cases userspace
3776 * has to recompute where to put it anyway.
3777 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003778}
3779
Egbert Eich61bc95c2013-03-04 09:24:38 -05003780/**
3781 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3782 * cursor plane briefly if not already running after enabling the display
3783 * plane.
3784 * This workaround avoids occasional blank screens when self refresh is
3785 * enabled.
3786 */
3787static void
3788g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3789{
3790 u32 cntl = I915_READ(CURCNTR(pipe));
3791
3792 if ((cntl & CURSOR_MODE) == 0) {
3793 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3794
3795 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3796 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3797 intel_wait_for_vblank(dev_priv->dev, pipe);
3798 I915_WRITE(CURCNTR(pipe), cntl);
3799 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3800 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3801 }
3802}
3803
Jesse Barnes2dd24552013-04-25 12:55:01 -07003804static void i9xx_pfit_enable(struct intel_crtc *crtc)
3805{
3806 struct drm_device *dev = crtc->base.dev;
3807 struct drm_i915_private *dev_priv = dev->dev_private;
3808 struct intel_crtc_config *pipe_config = &crtc->config;
3809
Daniel Vetter328d8e82013-05-08 10:36:31 +02003810 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003811 return;
3812
Daniel Vetterc0b03412013-05-28 12:05:54 +02003813 /*
3814 * The panel fitter should only be adjusted whilst the pipe is disabled,
3815 * according to register description and PRM.
3816 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003817 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3818 assert_pipe_disabled(dev_priv, crtc->pipe);
3819
Jesse Barnesb074cec2013-04-25 12:55:02 -07003820 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3821 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003822
3823 /* Border color in case we don't scale up to the full screen. Black by
3824 * default, change to something else for debugging. */
3825 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003826}
3827
Jesse Barnes89b667f2013-04-18 14:51:36 -07003828static void valleyview_crtc_enable(struct drm_crtc *crtc)
3829{
3830 struct drm_device *dev = crtc->dev;
3831 struct drm_i915_private *dev_priv = dev->dev_private;
3832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3833 struct intel_encoder *encoder;
3834 int pipe = intel_crtc->pipe;
3835 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03003836 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003837
3838 WARN_ON(!crtc->enabled);
3839
3840 if (intel_crtc->active)
3841 return;
3842
3843 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003844
Jesse Barnes89b667f2013-04-18 14:51:36 -07003845 for_each_encoder_on_crtc(dev, crtc, encoder)
3846 if (encoder->pre_pll_enable)
3847 encoder->pre_pll_enable(encoder);
3848
Jani Nikula23538ef2013-08-27 15:12:22 +03003849 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
3850
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003851 if (!is_dsi)
3852 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003853
3854 for_each_encoder_on_crtc(dev, crtc, encoder)
3855 if (encoder->pre_enable)
3856 encoder->pre_enable(encoder);
3857
Jesse Barnes2dd24552013-04-25 12:55:01 -07003858 i9xx_pfit_enable(intel_crtc);
3859
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003860 intel_crtc_load_lut(crtc);
3861
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003862 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003863 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003864 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003865 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003866 intel_crtc_update_cursor(crtc, true);
3867
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003868 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03003869
3870 for_each_encoder_on_crtc(dev, crtc, encoder)
3871 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003872}
3873
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003874static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003875{
3876 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003877 struct drm_i915_private *dev_priv = dev->dev_private;
3878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003879 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003880 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003881 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003882
Daniel Vetter08a48462012-07-02 11:43:47 +02003883 WARN_ON(!crtc->enabled);
3884
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003885 if (intel_crtc->active)
3886 return;
3887
3888 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003889
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003890 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003891 if (encoder->pre_enable)
3892 encoder->pre_enable(encoder);
3893
Daniel Vetterf6736a12013-06-05 13:34:30 +02003894 i9xx_enable_pll(intel_crtc);
3895
Jesse Barnes2dd24552013-04-25 12:55:01 -07003896 i9xx_pfit_enable(intel_crtc);
3897
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003898 intel_crtc_load_lut(crtc);
3899
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003900 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003901 intel_enable_pipe(dev_priv, pipe, false, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003902 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003903 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003904 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003905 if (IS_G4X(dev))
3906 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003907 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003908
3909 /* Give the overlay scaler a chance to enable if it's on this pipe */
3910 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003911
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003912 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003913
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003914 for_each_encoder_on_crtc(dev, crtc, encoder)
3915 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003916}
3917
Daniel Vetter87476d62013-04-11 16:29:06 +02003918static void i9xx_pfit_disable(struct intel_crtc *crtc)
3919{
3920 struct drm_device *dev = crtc->base.dev;
3921 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003922
3923 if (!crtc->config.gmch_pfit.control)
3924 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003925
3926 assert_pipe_disabled(dev_priv, crtc->pipe);
3927
Daniel Vetter328d8e82013-05-08 10:36:31 +02003928 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3929 I915_READ(PFIT_CONTROL));
3930 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003931}
3932
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003933static void i9xx_crtc_disable(struct drm_crtc *crtc)
3934{
3935 struct drm_device *dev = crtc->dev;
3936 struct drm_i915_private *dev_priv = dev->dev_private;
3937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003938 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003939 int pipe = intel_crtc->pipe;
3940 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003941
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003942 if (!intel_crtc->active)
3943 return;
3944
Daniel Vetterea9d7582012-07-10 10:42:52 +02003945 for_each_encoder_on_crtc(dev, crtc, encoder)
3946 encoder->disable(encoder);
3947
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003948 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003949 intel_crtc_wait_for_pending_flips(crtc);
3950 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003951
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003952 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003953 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003954
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003955 intel_crtc_dpms_overlay(intel_crtc, false);
3956 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003957 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003958 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003959
Jesse Barnesb24e7172011-01-04 15:09:30 -08003960 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003961
Daniel Vetter87476d62013-04-11 16:29:06 +02003962 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003963
Jesse Barnes89b667f2013-04-18 14:51:36 -07003964 for_each_encoder_on_crtc(dev, crtc, encoder)
3965 if (encoder->post_disable)
3966 encoder->post_disable(encoder);
3967
Jesse Barnesf6071162013-10-01 10:41:38 -07003968 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3969 vlv_disable_pll(dev_priv, pipe);
3970 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003971 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003972
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003973 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003974 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003975
Chris Wilson6b383a72010-09-13 13:54:26 +01003976 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003977}
3978
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003979static void i9xx_crtc_off(struct drm_crtc *crtc)
3980{
3981}
3982
Daniel Vetter976f8a22012-07-08 22:34:21 +02003983static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3984 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003985{
3986 struct drm_device *dev = crtc->dev;
3987 struct drm_i915_master_private *master_priv;
3988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3989 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003990
3991 if (!dev->primary->master)
3992 return;
3993
3994 master_priv = dev->primary->master->driver_priv;
3995 if (!master_priv->sarea_priv)
3996 return;
3997
Jesse Barnes79e53942008-11-07 14:24:08 -08003998 switch (pipe) {
3999 case 0:
4000 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4001 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4002 break;
4003 case 1:
4004 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4005 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4006 break;
4007 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004008 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004009 break;
4010 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004011}
4012
Daniel Vetter976f8a22012-07-08 22:34:21 +02004013/**
4014 * Sets the power management mode of the pipe and plane.
4015 */
4016void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004017{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004018 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004019 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004020 struct intel_encoder *intel_encoder;
4021 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004022
Daniel Vetter976f8a22012-07-08 22:34:21 +02004023 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4024 enable |= intel_encoder->connectors_active;
4025
4026 if (enable)
4027 dev_priv->display.crtc_enable(crtc);
4028 else
4029 dev_priv->display.crtc_disable(crtc);
4030
4031 intel_crtc_update_sarea(crtc, enable);
4032}
4033
Daniel Vetter976f8a22012-07-08 22:34:21 +02004034static void intel_crtc_disable(struct drm_crtc *crtc)
4035{
4036 struct drm_device *dev = crtc->dev;
4037 struct drm_connector *connector;
4038 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004040
4041 /* crtc should still be enabled when we disable it. */
4042 WARN_ON(!crtc->enabled);
4043
4044 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004045 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004046 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004047 dev_priv->display.off(crtc);
4048
Chris Wilson931872f2012-01-16 23:01:13 +00004049 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004050 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004051 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004052
4053 if (crtc->fb) {
4054 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004055 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004056 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004057 crtc->fb = NULL;
4058 }
4059
4060 /* Update computed state. */
4061 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4062 if (!connector->encoder || !connector->encoder->crtc)
4063 continue;
4064
4065 if (connector->encoder->crtc != crtc)
4066 continue;
4067
4068 connector->dpms = DRM_MODE_DPMS_OFF;
4069 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004070 }
4071}
4072
Chris Wilsonea5b2132010-08-04 13:50:23 +01004073void intel_encoder_destroy(struct drm_encoder *encoder)
4074{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004075 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004076
Chris Wilsonea5b2132010-08-04 13:50:23 +01004077 drm_encoder_cleanup(encoder);
4078 kfree(intel_encoder);
4079}
4080
Damien Lespiau92373292013-08-08 22:28:57 +01004081/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004082 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4083 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004084static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004085{
4086 if (mode == DRM_MODE_DPMS_ON) {
4087 encoder->connectors_active = true;
4088
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004089 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004090 } else {
4091 encoder->connectors_active = false;
4092
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004093 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004094 }
4095}
4096
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004097/* Cross check the actual hw state with our own modeset state tracking (and it's
4098 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004099static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004100{
4101 if (connector->get_hw_state(connector)) {
4102 struct intel_encoder *encoder = connector->encoder;
4103 struct drm_crtc *crtc;
4104 bool encoder_enabled;
4105 enum pipe pipe;
4106
4107 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4108 connector->base.base.id,
4109 drm_get_connector_name(&connector->base));
4110
4111 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4112 "wrong connector dpms state\n");
4113 WARN(connector->base.encoder != &encoder->base,
4114 "active connector not linked to encoder\n");
4115 WARN(!encoder->connectors_active,
4116 "encoder->connectors_active not set\n");
4117
4118 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4119 WARN(!encoder_enabled, "encoder not enabled\n");
4120 if (WARN_ON(!encoder->base.crtc))
4121 return;
4122
4123 crtc = encoder->base.crtc;
4124
4125 WARN(!crtc->enabled, "crtc not enabled\n");
4126 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4127 WARN(pipe != to_intel_crtc(crtc)->pipe,
4128 "encoder active on the wrong pipe\n");
4129 }
4130}
4131
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004132/* Even simpler default implementation, if there's really no special case to
4133 * consider. */
4134void intel_connector_dpms(struct drm_connector *connector, int mode)
4135{
4136 struct intel_encoder *encoder = intel_attached_encoder(connector);
4137
4138 /* All the simple cases only support two dpms states. */
4139 if (mode != DRM_MODE_DPMS_ON)
4140 mode = DRM_MODE_DPMS_OFF;
4141
4142 if (mode == connector->dpms)
4143 return;
4144
4145 connector->dpms = mode;
4146
4147 /* Only need to change hw state when actually enabled */
4148 if (encoder->base.crtc)
4149 intel_encoder_dpms(encoder, mode);
4150 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02004151 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004152
Daniel Vetterb9805142012-08-31 17:37:33 +02004153 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004154}
4155
Daniel Vetterf0947c32012-07-02 13:10:34 +02004156/* Simple connector->get_hw_state implementation for encoders that support only
4157 * one connector and no cloning and hence the encoder state determines the state
4158 * of the connector. */
4159bool intel_connector_get_hw_state(struct intel_connector *connector)
4160{
Daniel Vetter24929352012-07-02 20:28:59 +02004161 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004162 struct intel_encoder *encoder = connector->encoder;
4163
4164 return encoder->get_hw_state(encoder, &pipe);
4165}
4166
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004167static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4168 struct intel_crtc_config *pipe_config)
4169{
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *pipe_B_crtc =
4172 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4173
4174 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4175 pipe_name(pipe), pipe_config->fdi_lanes);
4176 if (pipe_config->fdi_lanes > 4) {
4177 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4178 pipe_name(pipe), pipe_config->fdi_lanes);
4179 return false;
4180 }
4181
4182 if (IS_HASWELL(dev)) {
4183 if (pipe_config->fdi_lanes > 2) {
4184 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4185 pipe_config->fdi_lanes);
4186 return false;
4187 } else {
4188 return true;
4189 }
4190 }
4191
4192 if (INTEL_INFO(dev)->num_pipes == 2)
4193 return true;
4194
4195 /* Ivybridge 3 pipe is really complicated */
4196 switch (pipe) {
4197 case PIPE_A:
4198 return true;
4199 case PIPE_B:
4200 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4201 pipe_config->fdi_lanes > 2) {
4202 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4203 pipe_name(pipe), pipe_config->fdi_lanes);
4204 return false;
4205 }
4206 return true;
4207 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004208 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004209 pipe_B_crtc->config.fdi_lanes <= 2) {
4210 if (pipe_config->fdi_lanes > 2) {
4211 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4212 pipe_name(pipe), pipe_config->fdi_lanes);
4213 return false;
4214 }
4215 } else {
4216 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4217 return false;
4218 }
4219 return true;
4220 default:
4221 BUG();
4222 }
4223}
4224
Daniel Vettere29c22c2013-02-21 00:00:16 +01004225#define RETRY 1
4226static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4227 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004228{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004229 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004230 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004231 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004232 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004233
Daniel Vettere29c22c2013-02-21 00:00:16 +01004234retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004235 /* FDI is a binary signal running at ~2.7GHz, encoding
4236 * each output octet as 10 bits. The actual frequency
4237 * is stored as a divider into a 100MHz clock, and the
4238 * mode pixel clock is stored in units of 1KHz.
4239 * Hence the bw of each lane in terms of the mode signal
4240 * is:
4241 */
4242 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4243
Damien Lespiau241bfc32013-09-25 16:45:37 +01004244 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004245
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004246 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004247 pipe_config->pipe_bpp);
4248
4249 pipe_config->fdi_lanes = lane;
4250
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004251 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004252 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004253
Daniel Vettere29c22c2013-02-21 00:00:16 +01004254 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4255 intel_crtc->pipe, pipe_config);
4256 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4257 pipe_config->pipe_bpp -= 2*3;
4258 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4259 pipe_config->pipe_bpp);
4260 needs_recompute = true;
4261 pipe_config->bw_constrained = true;
4262
4263 goto retry;
4264 }
4265
4266 if (needs_recompute)
4267 return RETRY;
4268
4269 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004270}
4271
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004272static void hsw_compute_ips_config(struct intel_crtc *crtc,
4273 struct intel_crtc_config *pipe_config)
4274{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004275 pipe_config->ips_enabled = i915_enable_ips &&
4276 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004277 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004278}
4279
Daniel Vettera43f6e02013-06-07 23:10:32 +02004280static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004281 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004282{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004283 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004284 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004285
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004286 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004287 if (INTEL_INFO(dev)->gen < 4) {
4288 struct drm_i915_private *dev_priv = dev->dev_private;
4289 int clock_limit =
4290 dev_priv->display.get_display_clock_speed(dev);
4291
4292 /*
4293 * Enable pixel doubling when the dot clock
4294 * is > 90% of the (display) core speed.
4295 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004296 * GDG double wide on either pipe,
4297 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004298 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004299 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004300 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004301 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004302 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004303 }
4304
Damien Lespiau241bfc32013-09-25 16:45:37 +01004305 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004306 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004307 }
Chris Wilson89749352010-09-12 18:25:19 +01004308
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004309 /*
4310 * Pipe horizontal size must be even in:
4311 * - DVO ganged mode
4312 * - LVDS dual channel mode
4313 * - Double wide pipe
4314 */
4315 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4316 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4317 pipe_config->pipe_src_w &= ~1;
4318
Damien Lespiau8693a822013-05-03 18:48:11 +01004319 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4320 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004321 */
4322 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4323 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004324 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004325
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004326 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004327 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004328 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004329 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4330 * for lvds. */
4331 pipe_config->pipe_bpp = 8*3;
4332 }
4333
Damien Lespiauf5adf942013-06-24 18:29:34 +01004334 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004335 hsw_compute_ips_config(crtc, pipe_config);
4336
4337 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4338 * clock survives for now. */
4339 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4340 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004341
Daniel Vetter877d48d2013-04-19 11:24:43 +02004342 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004343 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004344
Daniel Vettere29c22c2013-02-21 00:00:16 +01004345 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004346}
4347
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004348static int valleyview_get_display_clock_speed(struct drm_device *dev)
4349{
4350 return 400000; /* FIXME */
4351}
4352
Jesse Barnese70236a2009-09-21 10:42:27 -07004353static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004354{
Jesse Barnese70236a2009-09-21 10:42:27 -07004355 return 400000;
4356}
Jesse Barnes79e53942008-11-07 14:24:08 -08004357
Jesse Barnese70236a2009-09-21 10:42:27 -07004358static int i915_get_display_clock_speed(struct drm_device *dev)
4359{
4360 return 333000;
4361}
Jesse Barnes79e53942008-11-07 14:24:08 -08004362
Jesse Barnese70236a2009-09-21 10:42:27 -07004363static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4364{
4365 return 200000;
4366}
Jesse Barnes79e53942008-11-07 14:24:08 -08004367
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004368static int pnv_get_display_clock_speed(struct drm_device *dev)
4369{
4370 u16 gcfgc = 0;
4371
4372 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4373
4374 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4375 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4376 return 267000;
4377 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4378 return 333000;
4379 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4380 return 444000;
4381 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4382 return 200000;
4383 default:
4384 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4385 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4386 return 133000;
4387 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4388 return 167000;
4389 }
4390}
4391
Jesse Barnese70236a2009-09-21 10:42:27 -07004392static int i915gm_get_display_clock_speed(struct drm_device *dev)
4393{
4394 u16 gcfgc = 0;
4395
4396 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4397
4398 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004399 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004400 else {
4401 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4402 case GC_DISPLAY_CLOCK_333_MHZ:
4403 return 333000;
4404 default:
4405 case GC_DISPLAY_CLOCK_190_200_MHZ:
4406 return 190000;
4407 }
4408 }
4409}
Jesse Barnes79e53942008-11-07 14:24:08 -08004410
Jesse Barnese70236a2009-09-21 10:42:27 -07004411static int i865_get_display_clock_speed(struct drm_device *dev)
4412{
4413 return 266000;
4414}
4415
4416static int i855_get_display_clock_speed(struct drm_device *dev)
4417{
4418 u16 hpllcc = 0;
4419 /* Assume that the hardware is in the high speed state. This
4420 * should be the default.
4421 */
4422 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4423 case GC_CLOCK_133_200:
4424 case GC_CLOCK_100_200:
4425 return 200000;
4426 case GC_CLOCK_166_250:
4427 return 250000;
4428 case GC_CLOCK_100_133:
4429 return 133000;
4430 }
4431
4432 /* Shouldn't happen */
4433 return 0;
4434}
4435
4436static int i830_get_display_clock_speed(struct drm_device *dev)
4437{
4438 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004439}
4440
Zhenyu Wang2c072452009-06-05 15:38:42 +08004441static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004442intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004443{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004444 while (*num > DATA_LINK_M_N_MASK ||
4445 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004446 *num >>= 1;
4447 *den >>= 1;
4448 }
4449}
4450
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004451static void compute_m_n(unsigned int m, unsigned int n,
4452 uint32_t *ret_m, uint32_t *ret_n)
4453{
4454 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4455 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4456 intel_reduce_m_n_ratio(ret_m, ret_n);
4457}
4458
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004459void
4460intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4461 int pixel_clock, int link_clock,
4462 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004463{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004464 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004465
4466 compute_m_n(bits_per_pixel * pixel_clock,
4467 link_clock * nlanes * 8,
4468 &m_n->gmch_m, &m_n->gmch_n);
4469
4470 compute_m_n(pixel_clock, link_clock,
4471 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004472}
4473
Chris Wilsona7615032011-01-12 17:04:08 +00004474static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4475{
Keith Packard72bbe582011-09-26 16:09:45 -07004476 if (i915_panel_use_ssc >= 0)
4477 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004478 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004479 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004480}
4481
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004482static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4483{
4484 struct drm_device *dev = crtc->dev;
4485 struct drm_i915_private *dev_priv = dev->dev_private;
4486 int refclk;
4487
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004488 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004489 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004490 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004491 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004492 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004493 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4494 refclk / 1000);
4495 } else if (!IS_GEN2(dev)) {
4496 refclk = 96000;
4497 } else {
4498 refclk = 48000;
4499 }
4500
4501 return refclk;
4502}
4503
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004504static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004505{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004506 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004507}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004508
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004509static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4510{
4511 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004512}
4513
Daniel Vetterf47709a2013-03-28 10:42:02 +01004514static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004515 intel_clock_t *reduced_clock)
4516{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004517 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004518 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004519 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004520 u32 fp, fp2 = 0;
4521
4522 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004523 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004524 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004525 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004526 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004527 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004528 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004529 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004530 }
4531
4532 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004533 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004534
Daniel Vetterf47709a2013-03-28 10:42:02 +01004535 crtc->lowfreq_avail = false;
4536 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004537 reduced_clock && i915_powersave) {
4538 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004539 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004540 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004541 } else {
4542 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004543 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004544 }
4545}
4546
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004547static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4548 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004549{
4550 u32 reg_val;
4551
4552 /*
4553 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4554 * and set it to a reasonable value instead.
4555 */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004556 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004557 reg_val &= 0xffffff00;
4558 reg_val |= 0x00000030;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004559 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004560
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004561 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004562 reg_val &= 0x8cffffff;
4563 reg_val = 0x8c000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004564 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004565
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004566 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004567 reg_val &= 0xffffff00;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004568 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004569
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004570 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004571 reg_val &= 0x00ffffff;
4572 reg_val |= 0xb0000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004573 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004574}
4575
Daniel Vetterb5518422013-05-03 11:49:48 +02004576static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4577 struct intel_link_m_n *m_n)
4578{
4579 struct drm_device *dev = crtc->base.dev;
4580 struct drm_i915_private *dev_priv = dev->dev_private;
4581 int pipe = crtc->pipe;
4582
Daniel Vettere3b95f12013-05-03 11:49:49 +02004583 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4584 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4585 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4586 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004587}
4588
4589static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4590 struct intel_link_m_n *m_n)
4591{
4592 struct drm_device *dev = crtc->base.dev;
4593 struct drm_i915_private *dev_priv = dev->dev_private;
4594 int pipe = crtc->pipe;
4595 enum transcoder transcoder = crtc->config.cpu_transcoder;
4596
4597 if (INTEL_INFO(dev)->gen >= 5) {
4598 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4599 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4600 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4601 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4602 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004603 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4604 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4605 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4606 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004607 }
4608}
4609
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004610static void intel_dp_set_m_n(struct intel_crtc *crtc)
4611{
4612 if (crtc->config.has_pch_encoder)
4613 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4614 else
4615 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4616}
4617
Daniel Vetterf47709a2013-03-28 10:42:02 +01004618static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004619{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004620 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004621 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004622 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004623 u32 dpll, mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004624 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004625 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004626
Daniel Vetter09153002012-12-12 14:06:44 +01004627 mutex_lock(&dev_priv->dpio_lock);
4628
Daniel Vetterf47709a2013-03-28 10:42:02 +01004629 bestn = crtc->config.dpll.n;
4630 bestm1 = crtc->config.dpll.m1;
4631 bestm2 = crtc->config.dpll.m2;
4632 bestp1 = crtc->config.dpll.p1;
4633 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004634
Jesse Barnes89b667f2013-04-18 14:51:36 -07004635 /* See eDP HDMI DPIO driver vbios notes doc */
4636
4637 /* PLL B needs special handling */
4638 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004639 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004640
4641 /* Set up Tx target for periodic Rcomp update */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004642 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004643
4644 /* Disable target IRef on PLL */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004645 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004646 reg_val &= 0x00ffffff;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004647 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004648
4649 /* Disable fast lock */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004650 vlv_dpio_write(dev_priv, pipe, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004651
4652 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004653 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4654 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4655 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004656 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004657
4658 /*
4659 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4660 * but we don't support that).
4661 * Note: don't use the DAC post divider as it seems unstable.
4662 */
4663 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004664 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004665
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004666 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004667 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004668
Jesse Barnes89b667f2013-04-18 14:51:36 -07004669 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004670 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004671 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004672 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004673 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004674 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004675 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004676 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004677 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004678
Jesse Barnes89b667f2013-04-18 14:51:36 -07004679 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4680 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4681 /* Use SSC source */
4682 if (!pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004683 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004684 0x0df40000);
4685 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004686 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004687 0x0df70000);
4688 } else { /* HDMI or VGA */
4689 /* Use bend source */
4690 if (!pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004691 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004692 0x0df70000);
4693 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004694 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004695 0x0df40000);
4696 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004697
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004698 coreclk = vlv_dpio_read(dev_priv, pipe, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004699 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4700 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4701 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4702 coreclk |= 0x01000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004703 vlv_dpio_write(dev_priv, pipe, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004704
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004705 vlv_dpio_write(dev_priv, pipe, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004706
Jesse Barnes89b667f2013-04-18 14:51:36 -07004707 /* Enable DPIO clock input */
4708 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4709 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004710 /* We should never disable this, set it here for state tracking */
4711 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004712 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004713 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004714 crtc->config.dpll_hw_state.dpll = dpll;
4715
Daniel Vetteref1b4602013-06-01 17:17:04 +02004716 dpll_md = (crtc->config.pixel_multiplier - 1)
4717 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004718 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4719
Daniel Vetterf47709a2013-03-28 10:42:02 +01004720 if (crtc->config.has_dp_encoder)
4721 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304722
Daniel Vetter09153002012-12-12 14:06:44 +01004723 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004724}
4725
Daniel Vetterf47709a2013-03-28 10:42:02 +01004726static void i9xx_update_pll(struct intel_crtc *crtc,
4727 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004728 int num_connectors)
4729{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004730 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004731 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004732 u32 dpll;
4733 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004734 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004735
Daniel Vetterf47709a2013-03-28 10:42:02 +01004736 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304737
Daniel Vetterf47709a2013-03-28 10:42:02 +01004738 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4739 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004740
4741 dpll = DPLL_VGA_MODE_DIS;
4742
Daniel Vetterf47709a2013-03-28 10:42:02 +01004743 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004744 dpll |= DPLLB_MODE_LVDS;
4745 else
4746 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004747
Daniel Vetteref1b4602013-06-01 17:17:04 +02004748 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004749 dpll |= (crtc->config.pixel_multiplier - 1)
4750 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004751 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004752
4753 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004754 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004755
Daniel Vetterf47709a2013-03-28 10:42:02 +01004756 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004757 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004758
4759 /* compute bitmask from p1 value */
4760 if (IS_PINEVIEW(dev))
4761 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4762 else {
4763 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4764 if (IS_G4X(dev) && reduced_clock)
4765 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4766 }
4767 switch (clock->p2) {
4768 case 5:
4769 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4770 break;
4771 case 7:
4772 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4773 break;
4774 case 10:
4775 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4776 break;
4777 case 14:
4778 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4779 break;
4780 }
4781 if (INTEL_INFO(dev)->gen >= 4)
4782 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4783
Daniel Vetter09ede542013-04-30 14:01:45 +02004784 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004785 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004786 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004787 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4788 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4789 else
4790 dpll |= PLL_REF_INPUT_DREFCLK;
4791
4792 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004793 crtc->config.dpll_hw_state.dpll = dpll;
4794
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004795 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004796 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4797 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004798 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004799 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004800
4801 if (crtc->config.has_dp_encoder)
4802 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004803}
4804
Daniel Vetterf47709a2013-03-28 10:42:02 +01004805static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004806 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004807 int num_connectors)
4808{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004809 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004810 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004811 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004812 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004813
Daniel Vetterf47709a2013-03-28 10:42:02 +01004814 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304815
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004816 dpll = DPLL_VGA_MODE_DIS;
4817
Daniel Vetterf47709a2013-03-28 10:42:02 +01004818 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004819 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4820 } else {
4821 if (clock->p1 == 2)
4822 dpll |= PLL_P1_DIVIDE_BY_TWO;
4823 else
4824 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4825 if (clock->p2 == 4)
4826 dpll |= PLL_P2_DIVIDE_BY_4;
4827 }
4828
Daniel Vetter4a33e482013-07-06 12:52:05 +02004829 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4830 dpll |= DPLL_DVO_2X_MODE;
4831
Daniel Vetterf47709a2013-03-28 10:42:02 +01004832 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004833 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4834 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4835 else
4836 dpll |= PLL_REF_INPUT_DREFCLK;
4837
4838 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004839 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004840}
4841
Daniel Vetter8a654f32013-06-01 17:16:22 +02004842static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004843{
4844 struct drm_device *dev = intel_crtc->base.dev;
4845 struct drm_i915_private *dev_priv = dev->dev_private;
4846 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004847 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004848 struct drm_display_mode *adjusted_mode =
4849 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004850 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4851
4852 /* We need to be careful not to changed the adjusted mode, for otherwise
4853 * the hw state checker will get angry at the mismatch. */
4854 crtc_vtotal = adjusted_mode->crtc_vtotal;
4855 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004856
4857 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4858 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004859 crtc_vtotal -= 1;
4860 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004861 vsyncshift = adjusted_mode->crtc_hsync_start
4862 - adjusted_mode->crtc_htotal / 2;
4863 } else {
4864 vsyncshift = 0;
4865 }
4866
4867 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004868 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004869
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004870 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004871 (adjusted_mode->crtc_hdisplay - 1) |
4872 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004873 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004874 (adjusted_mode->crtc_hblank_start - 1) |
4875 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004876 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004877 (adjusted_mode->crtc_hsync_start - 1) |
4878 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4879
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004880 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004881 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004882 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004883 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004884 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004885 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004886 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004887 (adjusted_mode->crtc_vsync_start - 1) |
4888 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4889
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004890 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4891 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4892 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4893 * bits. */
4894 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4895 (pipe == PIPE_B || pipe == PIPE_C))
4896 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4897
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004898 /* pipesrc controls the size that is scaled from, which should
4899 * always be the user's requested size.
4900 */
4901 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004902 ((intel_crtc->config.pipe_src_w - 1) << 16) |
4903 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004904}
4905
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004906static void intel_get_pipe_timings(struct intel_crtc *crtc,
4907 struct intel_crtc_config *pipe_config)
4908{
4909 struct drm_device *dev = crtc->base.dev;
4910 struct drm_i915_private *dev_priv = dev->dev_private;
4911 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4912 uint32_t tmp;
4913
4914 tmp = I915_READ(HTOTAL(cpu_transcoder));
4915 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4916 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4917 tmp = I915_READ(HBLANK(cpu_transcoder));
4918 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4919 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4920 tmp = I915_READ(HSYNC(cpu_transcoder));
4921 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4922 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4923
4924 tmp = I915_READ(VTOTAL(cpu_transcoder));
4925 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4926 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4927 tmp = I915_READ(VBLANK(cpu_transcoder));
4928 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4929 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4930 tmp = I915_READ(VSYNC(cpu_transcoder));
4931 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4932 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4933
4934 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4935 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4936 pipe_config->adjusted_mode.crtc_vtotal += 1;
4937 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4938 }
4939
4940 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004941 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
4942 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
4943
4944 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
4945 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004946}
4947
Jesse Barnesbabea612013-06-26 18:57:38 +03004948static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4949 struct intel_crtc_config *pipe_config)
4950{
4951 struct drm_crtc *crtc = &intel_crtc->base;
4952
4953 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4954 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4955 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4956 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4957
4958 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4959 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4960 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4961 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4962
4963 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4964
Damien Lespiau241bfc32013-09-25 16:45:37 +01004965 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03004966 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4967}
4968
Daniel Vetter84b046f2013-02-19 18:48:54 +01004969static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4970{
4971 struct drm_device *dev = intel_crtc->base.dev;
4972 struct drm_i915_private *dev_priv = dev->dev_private;
4973 uint32_t pipeconf;
4974
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004975 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004976
Daniel Vetter67c72a12013-09-24 11:46:14 +02004977 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
4978 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
4979 pipeconf |= PIPECONF_ENABLE;
4980
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004981 if (intel_crtc->config.double_wide)
4982 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004983
Daniel Vetterff9ce462013-04-24 14:57:17 +02004984 /* only g4x and later have fancy bpc/dither controls */
4985 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004986 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4987 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4988 pipeconf |= PIPECONF_DITHER_EN |
4989 PIPECONF_DITHER_TYPE_SP;
4990
4991 switch (intel_crtc->config.pipe_bpp) {
4992 case 18:
4993 pipeconf |= PIPECONF_6BPC;
4994 break;
4995 case 24:
4996 pipeconf |= PIPECONF_8BPC;
4997 break;
4998 case 30:
4999 pipeconf |= PIPECONF_10BPC;
5000 break;
5001 default:
5002 /* Case prevented by intel_choose_pipe_bpp_dither. */
5003 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005004 }
5005 }
5006
5007 if (HAS_PIPE_CXSR(dev)) {
5008 if (intel_crtc->lowfreq_avail) {
5009 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5010 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5011 } else {
5012 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005013 }
5014 }
5015
Daniel Vetter84b046f2013-02-19 18:48:54 +01005016 if (!IS_GEN2(dev) &&
5017 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5018 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5019 else
5020 pipeconf |= PIPECONF_PROGRESSIVE;
5021
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005022 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5023 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005024
Daniel Vetter84b046f2013-02-19 18:48:54 +01005025 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5026 POSTING_READ(PIPECONF(intel_crtc->pipe));
5027}
5028
Eric Anholtf564048e2011-03-30 13:01:02 -07005029static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005030 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005031 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005032{
5033 struct drm_device *dev = crtc->dev;
5034 struct drm_i915_private *dev_priv = dev->dev_private;
5035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5036 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005037 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005038 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005039 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005040 u32 dspcntr;
Daniel Vettera16af722013-04-30 14:01:44 +02005041 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005042 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005043 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005044 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005045 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005046
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005047 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005048 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005049 case INTEL_OUTPUT_LVDS:
5050 is_lvds = true;
5051 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005052 case INTEL_OUTPUT_DSI:
5053 is_dsi = true;
5054 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005055 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005056
Eric Anholtc751ce42010-03-25 11:48:48 -07005057 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005058 }
5059
Jani Nikulaf2335332013-09-13 11:03:09 +03005060 if (is_dsi)
5061 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005062
Jani Nikulaf2335332013-09-13 11:03:09 +03005063 if (!intel_crtc->config.clock_set) {
5064 refclk = i9xx_get_refclk(crtc, num_connectors);
5065
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005066 /*
5067 * Returns a set of divisors for the desired target clock with
5068 * the given refclk, or FALSE. The returned values represent
5069 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5070 * 2) / p1 / p2.
5071 */
5072 limit = intel_limit(crtc, refclk);
5073 ok = dev_priv->display.find_dpll(limit, crtc,
5074 intel_crtc->config.port_clock,
5075 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005076 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005077 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5078 return -EINVAL;
5079 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005080
Jani Nikulaf2335332013-09-13 11:03:09 +03005081 if (is_lvds && dev_priv->lvds_downclock_avail) {
5082 /*
5083 * Ensure we match the reduced clock's P to the target
5084 * clock. If the clocks don't match, we can't switch
5085 * the display clock by using the FP0/FP1. In such case
5086 * we will disable the LVDS downclock feature.
5087 */
5088 has_reduced_clock =
5089 dev_priv->display.find_dpll(limit, crtc,
5090 dev_priv->lvds_downclock,
5091 refclk, &clock,
5092 &reduced_clock);
5093 }
5094 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005095 intel_crtc->config.dpll.n = clock.n;
5096 intel_crtc->config.dpll.m1 = clock.m1;
5097 intel_crtc->config.dpll.m2 = clock.m2;
5098 intel_crtc->config.dpll.p1 = clock.p1;
5099 intel_crtc->config.dpll.p2 = clock.p2;
5100 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005101
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005102 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005103 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305104 has_reduced_clock ? &reduced_clock : NULL,
5105 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005106 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005107 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005108 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005109 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005110 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005111 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005112 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005113
Jani Nikulaf2335332013-09-13 11:03:09 +03005114skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005115 /* Set up the display plane register */
5116 dspcntr = DISPPLANE_GAMMA_ENABLE;
5117
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005118 if (!IS_VALLEYVIEW(dev)) {
5119 if (pipe == 0)
5120 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5121 else
5122 dspcntr |= DISPPLANE_SEL_PIPE_B;
5123 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005124
Daniel Vetter8a654f32013-06-01 17:16:22 +02005125 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005126
5127 /* pipesrc and dspsize control the size that is scaled from,
5128 * which should always be the user's requested size.
5129 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005130 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005131 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5132 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005133 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005134
Daniel Vetter84b046f2013-02-19 18:48:54 +01005135 i9xx_set_pipeconf(intel_crtc);
5136
Eric Anholtf564048e2011-03-30 13:01:02 -07005137 I915_WRITE(DSPCNTR(plane), dspcntr);
5138 POSTING_READ(DSPCNTR(plane));
5139
Daniel Vetter94352cf2012-07-05 22:51:56 +02005140 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005141
Eric Anholtf564048e2011-03-30 13:01:02 -07005142 return ret;
5143}
5144
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005145static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5146 struct intel_crtc_config *pipe_config)
5147{
5148 struct drm_device *dev = crtc->base.dev;
5149 struct drm_i915_private *dev_priv = dev->dev_private;
5150 uint32_t tmp;
5151
5152 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005153 if (!(tmp & PFIT_ENABLE))
5154 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005155
Daniel Vetter06922822013-07-11 13:35:40 +02005156 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005157 if (INTEL_INFO(dev)->gen < 4) {
5158 if (crtc->pipe != PIPE_B)
5159 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005160 } else {
5161 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5162 return;
5163 }
5164
Daniel Vetter06922822013-07-11 13:35:40 +02005165 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005166 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5167 if (INTEL_INFO(dev)->gen < 5)
5168 pipe_config->gmch_pfit.lvds_border_bits =
5169 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5170}
5171
Jesse Barnesacbec812013-09-20 11:29:32 -07005172static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5173 struct intel_crtc_config *pipe_config)
5174{
5175 struct drm_device *dev = crtc->base.dev;
5176 struct drm_i915_private *dev_priv = dev->dev_private;
5177 int pipe = pipe_config->cpu_transcoder;
5178 intel_clock_t clock;
5179 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005180 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005181
5182 mutex_lock(&dev_priv->dpio_lock);
5183 mdiv = vlv_dpio_read(dev_priv, pipe, DPIO_DIV(pipe));
5184 mutex_unlock(&dev_priv->dpio_lock);
5185
5186 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5187 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5188 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5189 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5190 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5191
Chris Wilson662c6ec2013-09-25 14:24:01 -07005192 clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
5193 clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
Jesse Barnesacbec812013-09-20 11:29:32 -07005194
5195 pipe_config->port_clock = clock.dot / 10;
5196}
5197
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005198static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5199 struct intel_crtc_config *pipe_config)
5200{
5201 struct drm_device *dev = crtc->base.dev;
5202 struct drm_i915_private *dev_priv = dev->dev_private;
5203 uint32_t tmp;
5204
Daniel Vettere143a212013-07-04 12:01:15 +02005205 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005206 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005207
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005208 tmp = I915_READ(PIPECONF(crtc->pipe));
5209 if (!(tmp & PIPECONF_ENABLE))
5210 return false;
5211
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005212 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5213 switch (tmp & PIPECONF_BPC_MASK) {
5214 case PIPECONF_6BPC:
5215 pipe_config->pipe_bpp = 18;
5216 break;
5217 case PIPECONF_8BPC:
5218 pipe_config->pipe_bpp = 24;
5219 break;
5220 case PIPECONF_10BPC:
5221 pipe_config->pipe_bpp = 30;
5222 break;
5223 default:
5224 break;
5225 }
5226 }
5227
Ville Syrjälä282740f2013-09-04 18:30:03 +03005228 if (INTEL_INFO(dev)->gen < 4)
5229 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5230
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005231 intel_get_pipe_timings(crtc, pipe_config);
5232
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005233 i9xx_get_pfit_config(crtc, pipe_config);
5234
Daniel Vetter6c49f242013-06-06 12:45:25 +02005235 if (INTEL_INFO(dev)->gen >= 4) {
5236 tmp = I915_READ(DPLL_MD(crtc->pipe));
5237 pipe_config->pixel_multiplier =
5238 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5239 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005240 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005241 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5242 tmp = I915_READ(DPLL(crtc->pipe));
5243 pipe_config->pixel_multiplier =
5244 ((tmp & SDVO_MULTIPLIER_MASK)
5245 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5246 } else {
5247 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5248 * port and will be fixed up in the encoder->get_config
5249 * function. */
5250 pipe_config->pixel_multiplier = 1;
5251 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005252 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5253 if (!IS_VALLEYVIEW(dev)) {
5254 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5255 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005256 } else {
5257 /* Mask out read-only status bits. */
5258 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5259 DPLL_PORTC_READY_MASK |
5260 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005261 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005262
Jesse Barnesacbec812013-09-20 11:29:32 -07005263 if (IS_VALLEYVIEW(dev))
5264 vlv_crtc_clock_get(crtc, pipe_config);
5265 else
5266 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005267
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005268 return true;
5269}
5270
Paulo Zanonidde86e22012-12-01 12:04:25 -02005271static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005272{
5273 struct drm_i915_private *dev_priv = dev->dev_private;
5274 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005275 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005276 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005277 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005278 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005279 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005280 bool has_ck505 = false;
5281 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005282
5283 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005284 list_for_each_entry(encoder, &mode_config->encoder_list,
5285 base.head) {
5286 switch (encoder->type) {
5287 case INTEL_OUTPUT_LVDS:
5288 has_panel = true;
5289 has_lvds = true;
5290 break;
5291 case INTEL_OUTPUT_EDP:
5292 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005293 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005294 has_cpu_edp = true;
5295 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005296 }
5297 }
5298
Keith Packard99eb6a02011-09-26 14:29:12 -07005299 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005300 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005301 can_ssc = has_ck505;
5302 } else {
5303 has_ck505 = false;
5304 can_ssc = true;
5305 }
5306
Imre Deak2de69052013-05-08 13:14:04 +03005307 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5308 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005309
5310 /* Ironlake: try to setup display ref clock before DPLL
5311 * enabling. This is only under driver's control after
5312 * PCH B stepping, previous chipset stepping should be
5313 * ignoring this setting.
5314 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005315 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005316
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005317 /* As we must carefully and slowly disable/enable each source in turn,
5318 * compute the final state we want first and check if we need to
5319 * make any changes at all.
5320 */
5321 final = val;
5322 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005323 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005324 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005325 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005326 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5327
5328 final &= ~DREF_SSC_SOURCE_MASK;
5329 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5330 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005331
Keith Packard199e5d72011-09-22 12:01:57 -07005332 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005333 final |= DREF_SSC_SOURCE_ENABLE;
5334
5335 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5336 final |= DREF_SSC1_ENABLE;
5337
5338 if (has_cpu_edp) {
5339 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5340 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5341 else
5342 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5343 } else
5344 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5345 } else {
5346 final |= DREF_SSC_SOURCE_DISABLE;
5347 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5348 }
5349
5350 if (final == val)
5351 return;
5352
5353 /* Always enable nonspread source */
5354 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5355
5356 if (has_ck505)
5357 val |= DREF_NONSPREAD_CK505_ENABLE;
5358 else
5359 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5360
5361 if (has_panel) {
5362 val &= ~DREF_SSC_SOURCE_MASK;
5363 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005364
Keith Packard199e5d72011-09-22 12:01:57 -07005365 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005366 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005367 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005368 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005369 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005370 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005371
5372 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005373 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005374 POSTING_READ(PCH_DREF_CONTROL);
5375 udelay(200);
5376
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005377 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005378
5379 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005380 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005381 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005382 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005383 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005384 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005385 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005386 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005387 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005388 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005389
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005390 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005391 POSTING_READ(PCH_DREF_CONTROL);
5392 udelay(200);
5393 } else {
5394 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5395
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005396 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005397
5398 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005399 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005400
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005401 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005402 POSTING_READ(PCH_DREF_CONTROL);
5403 udelay(200);
5404
5405 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005406 val &= ~DREF_SSC_SOURCE_MASK;
5407 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005408
5409 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005410 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005411
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005412 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005413 POSTING_READ(PCH_DREF_CONTROL);
5414 udelay(200);
5415 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005416
5417 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005418}
5419
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005420static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005421{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005422 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005423
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005424 tmp = I915_READ(SOUTH_CHICKEN2);
5425 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5426 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005427
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005428 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5429 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5430 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005431
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005432 tmp = I915_READ(SOUTH_CHICKEN2);
5433 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5434 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005435
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005436 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5437 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5438 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005439}
5440
5441/* WaMPhyProgramming:hsw */
5442static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5443{
5444 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005445
5446 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5447 tmp &= ~(0xFF << 24);
5448 tmp |= (0x12 << 24);
5449 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5450
Paulo Zanonidde86e22012-12-01 12:04:25 -02005451 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5452 tmp |= (1 << 11);
5453 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5454
5455 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5456 tmp |= (1 << 11);
5457 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5458
Paulo Zanonidde86e22012-12-01 12:04:25 -02005459 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5460 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5461 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5462
5463 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5464 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5465 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5466
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005467 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5468 tmp &= ~(7 << 13);
5469 tmp |= (5 << 13);
5470 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005472 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5473 tmp &= ~(7 << 13);
5474 tmp |= (5 << 13);
5475 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005476
5477 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5478 tmp &= ~0xFF;
5479 tmp |= 0x1C;
5480 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5481
5482 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5483 tmp &= ~0xFF;
5484 tmp |= 0x1C;
5485 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5486
5487 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5488 tmp &= ~(0xFF << 16);
5489 tmp |= (0x1C << 16);
5490 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5491
5492 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5493 tmp &= ~(0xFF << 16);
5494 tmp |= (0x1C << 16);
5495 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5496
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005497 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5498 tmp |= (1 << 27);
5499 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005500
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005501 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5502 tmp |= (1 << 27);
5503 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005504
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005505 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5506 tmp &= ~(0xF << 28);
5507 tmp |= (4 << 28);
5508 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005509
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005510 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5511 tmp &= ~(0xF << 28);
5512 tmp |= (4 << 28);
5513 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005514}
5515
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005516/* Implements 3 different sequences from BSpec chapter "Display iCLK
5517 * Programming" based on the parameters passed:
5518 * - Sequence to enable CLKOUT_DP
5519 * - Sequence to enable CLKOUT_DP without spread
5520 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5521 */
5522static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5523 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005524{
5525 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005526 uint32_t reg, tmp;
5527
5528 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5529 with_spread = true;
5530 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5531 with_fdi, "LP PCH doesn't have FDI\n"))
5532 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005533
5534 mutex_lock(&dev_priv->dpio_lock);
5535
5536 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5537 tmp &= ~SBI_SSCCTL_DISABLE;
5538 tmp |= SBI_SSCCTL_PATHALT;
5539 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5540
5541 udelay(24);
5542
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005543 if (with_spread) {
5544 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5545 tmp &= ~SBI_SSCCTL_PATHALT;
5546 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005547
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005548 if (with_fdi) {
5549 lpt_reset_fdi_mphy(dev_priv);
5550 lpt_program_fdi_mphy(dev_priv);
5551 }
5552 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005553
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005554 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5555 SBI_GEN0 : SBI_DBUFF0;
5556 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5557 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5558 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005559
5560 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005561}
5562
Paulo Zanoni47701c32013-07-23 11:19:25 -03005563/* Sequence to disable CLKOUT_DP */
5564static void lpt_disable_clkout_dp(struct drm_device *dev)
5565{
5566 struct drm_i915_private *dev_priv = dev->dev_private;
5567 uint32_t reg, tmp;
5568
5569 mutex_lock(&dev_priv->dpio_lock);
5570
5571 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5572 SBI_GEN0 : SBI_DBUFF0;
5573 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5574 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5575 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5576
5577 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5578 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5579 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5580 tmp |= SBI_SSCCTL_PATHALT;
5581 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5582 udelay(32);
5583 }
5584 tmp |= SBI_SSCCTL_DISABLE;
5585 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5586 }
5587
5588 mutex_unlock(&dev_priv->dpio_lock);
5589}
5590
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005591static void lpt_init_pch_refclk(struct drm_device *dev)
5592{
5593 struct drm_mode_config *mode_config = &dev->mode_config;
5594 struct intel_encoder *encoder;
5595 bool has_vga = false;
5596
5597 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5598 switch (encoder->type) {
5599 case INTEL_OUTPUT_ANALOG:
5600 has_vga = true;
5601 break;
5602 }
5603 }
5604
Paulo Zanoni47701c32013-07-23 11:19:25 -03005605 if (has_vga)
5606 lpt_enable_clkout_dp(dev, true, true);
5607 else
5608 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005609}
5610
Paulo Zanonidde86e22012-12-01 12:04:25 -02005611/*
5612 * Initialize reference clocks when the driver loads
5613 */
5614void intel_init_pch_refclk(struct drm_device *dev)
5615{
5616 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5617 ironlake_init_pch_refclk(dev);
5618 else if (HAS_PCH_LPT(dev))
5619 lpt_init_pch_refclk(dev);
5620}
5621
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005622static int ironlake_get_refclk(struct drm_crtc *crtc)
5623{
5624 struct drm_device *dev = crtc->dev;
5625 struct drm_i915_private *dev_priv = dev->dev_private;
5626 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005627 int num_connectors = 0;
5628 bool is_lvds = false;
5629
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005630 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005631 switch (encoder->type) {
5632 case INTEL_OUTPUT_LVDS:
5633 is_lvds = true;
5634 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005635 }
5636 num_connectors++;
5637 }
5638
5639 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5640 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005641 dev_priv->vbt.lvds_ssc_freq);
5642 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005643 }
5644
5645 return 120000;
5646}
5647
Daniel Vetter6ff93602013-04-19 11:24:36 +02005648static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005649{
5650 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5652 int pipe = intel_crtc->pipe;
5653 uint32_t val;
5654
Daniel Vetter78114072013-06-13 00:54:57 +02005655 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005656
Daniel Vetter965e0c42013-03-27 00:44:57 +01005657 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005658 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005659 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005660 break;
5661 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005662 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005663 break;
5664 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005665 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005666 break;
5667 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005668 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005669 break;
5670 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005671 /* Case prevented by intel_choose_pipe_bpp_dither. */
5672 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005673 }
5674
Daniel Vetterd8b32242013-04-25 17:54:44 +02005675 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005676 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5677
Daniel Vetter6ff93602013-04-19 11:24:36 +02005678 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005679 val |= PIPECONF_INTERLACED_ILK;
5680 else
5681 val |= PIPECONF_PROGRESSIVE;
5682
Daniel Vetter50f3b012013-03-27 00:44:56 +01005683 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005684 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005685
Paulo Zanonic8203562012-09-12 10:06:29 -03005686 I915_WRITE(PIPECONF(pipe), val);
5687 POSTING_READ(PIPECONF(pipe));
5688}
5689
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005690/*
5691 * Set up the pipe CSC unit.
5692 *
5693 * Currently only full range RGB to limited range RGB conversion
5694 * is supported, but eventually this should handle various
5695 * RGB<->YCbCr scenarios as well.
5696 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005697static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005698{
5699 struct drm_device *dev = crtc->dev;
5700 struct drm_i915_private *dev_priv = dev->dev_private;
5701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5702 int pipe = intel_crtc->pipe;
5703 uint16_t coeff = 0x7800; /* 1.0 */
5704
5705 /*
5706 * TODO: Check what kind of values actually come out of the pipe
5707 * with these coeff/postoff values and adjust to get the best
5708 * accuracy. Perhaps we even need to take the bpc value into
5709 * consideration.
5710 */
5711
Daniel Vetter50f3b012013-03-27 00:44:56 +01005712 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005713 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5714
5715 /*
5716 * GY/GU and RY/RU should be the other way around according
5717 * to BSpec, but reality doesn't agree. Just set them up in
5718 * a way that results in the correct picture.
5719 */
5720 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5721 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5722
5723 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5724 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5725
5726 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5727 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5728
5729 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5730 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5731 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5732
5733 if (INTEL_INFO(dev)->gen > 6) {
5734 uint16_t postoff = 0;
5735
Daniel Vetter50f3b012013-03-27 00:44:56 +01005736 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005737 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5738
5739 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5740 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5741 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5742
5743 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5744 } else {
5745 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5746
Daniel Vetter50f3b012013-03-27 00:44:56 +01005747 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005748 mode |= CSC_BLACK_SCREEN_OFFSET;
5749
5750 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5751 }
5752}
5753
Daniel Vetter6ff93602013-04-19 11:24:36 +02005754static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005755{
5756 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5757 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005758 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005759 uint32_t val;
5760
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005761 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005762
Daniel Vetterd8b32242013-04-25 17:54:44 +02005763 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005764 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5765
Daniel Vetter6ff93602013-04-19 11:24:36 +02005766 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005767 val |= PIPECONF_INTERLACED_ILK;
5768 else
5769 val |= PIPECONF_PROGRESSIVE;
5770
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005771 I915_WRITE(PIPECONF(cpu_transcoder), val);
5772 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005773
5774 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5775 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005776}
5777
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005778static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005779 intel_clock_t *clock,
5780 bool *has_reduced_clock,
5781 intel_clock_t *reduced_clock)
5782{
5783 struct drm_device *dev = crtc->dev;
5784 struct drm_i915_private *dev_priv = dev->dev_private;
5785 struct intel_encoder *intel_encoder;
5786 int refclk;
5787 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02005788 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005789
5790 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5791 switch (intel_encoder->type) {
5792 case INTEL_OUTPUT_LVDS:
5793 is_lvds = true;
5794 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005795 }
5796 }
5797
5798 refclk = ironlake_get_refclk(crtc);
5799
5800 /*
5801 * Returns a set of divisors for the desired target clock with the given
5802 * refclk, or FALSE. The returned values represent the clock equation:
5803 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5804 */
5805 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005806 ret = dev_priv->display.find_dpll(limit, crtc,
5807 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005808 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005809 if (!ret)
5810 return false;
5811
5812 if (is_lvds && dev_priv->lvds_downclock_avail) {
5813 /*
5814 * Ensure we match the reduced clock's P to the target clock.
5815 * If the clocks don't match, we can't switch the display clock
5816 * by using the FP0/FP1. In such case we will disable the LVDS
5817 * downclock feature.
5818 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005819 *has_reduced_clock =
5820 dev_priv->display.find_dpll(limit, crtc,
5821 dev_priv->lvds_downclock,
5822 refclk, clock,
5823 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005824 }
5825
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005826 return true;
5827}
5828
Daniel Vetter01a415f2012-10-27 15:58:40 +02005829static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5830{
5831 struct drm_i915_private *dev_priv = dev->dev_private;
5832 uint32_t temp;
5833
5834 temp = I915_READ(SOUTH_CHICKEN1);
5835 if (temp & FDI_BC_BIFURCATION_SELECT)
5836 return;
5837
5838 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5839 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5840
5841 temp |= FDI_BC_BIFURCATION_SELECT;
5842 DRM_DEBUG_KMS("enabling fdi C rx\n");
5843 I915_WRITE(SOUTH_CHICKEN1, temp);
5844 POSTING_READ(SOUTH_CHICKEN1);
5845}
5846
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005847static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005848{
5849 struct drm_device *dev = intel_crtc->base.dev;
5850 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005851
5852 switch (intel_crtc->pipe) {
5853 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005854 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005855 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005856 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005857 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5858 else
5859 cpt_enable_fdi_bc_bifurcation(dev);
5860
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005861 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005862 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005863 cpt_enable_fdi_bc_bifurcation(dev);
5864
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005865 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005866 default:
5867 BUG();
5868 }
5869}
5870
Paulo Zanonid4b19312012-11-29 11:29:32 -02005871int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5872{
5873 /*
5874 * Account for spread spectrum to avoid
5875 * oversubscribing the link. Max center spread
5876 * is 2.5%; use 5% for safety's sake.
5877 */
5878 u32 bps = target_clock * bpp * 21 / 20;
5879 return bps / (link_bw * 8) + 1;
5880}
5881
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005882static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005883{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005884 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005885}
5886
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005887static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005888 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005889 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005890{
5891 struct drm_crtc *crtc = &intel_crtc->base;
5892 struct drm_device *dev = crtc->dev;
5893 struct drm_i915_private *dev_priv = dev->dev_private;
5894 struct intel_encoder *intel_encoder;
5895 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005896 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005897 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005898
5899 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5900 switch (intel_encoder->type) {
5901 case INTEL_OUTPUT_LVDS:
5902 is_lvds = true;
5903 break;
5904 case INTEL_OUTPUT_SDVO:
5905 case INTEL_OUTPUT_HDMI:
5906 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005907 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005908 }
5909
5910 num_connectors++;
5911 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005912
Chris Wilsonc1858122010-12-03 21:35:48 +00005913 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005914 factor = 21;
5915 if (is_lvds) {
5916 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005917 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005918 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005919 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005920 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005921 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005922
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005923 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005924 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005925
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005926 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5927 *fp2 |= FP_CB_TUNE;
5928
Chris Wilson5eddb702010-09-11 13:48:45 +01005929 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005930
Eric Anholta07d6782011-03-30 13:01:08 -07005931 if (is_lvds)
5932 dpll |= DPLLB_MODE_LVDS;
5933 else
5934 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005935
Daniel Vetteref1b4602013-06-01 17:17:04 +02005936 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5937 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005938
5939 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005940 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005941 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005942 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005943
Eric Anholta07d6782011-03-30 13:01:08 -07005944 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005945 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005946 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005947 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005948
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005949 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005950 case 5:
5951 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5952 break;
5953 case 7:
5954 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5955 break;
5956 case 10:
5957 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5958 break;
5959 case 14:
5960 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5961 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005962 }
5963
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005964 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005965 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005966 else
5967 dpll |= PLL_REF_INPUT_DREFCLK;
5968
Daniel Vetter959e16d2013-06-05 13:34:21 +02005969 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005970}
5971
Jesse Barnes79e53942008-11-07 14:24:08 -08005972static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005973 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005974 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005975{
5976 struct drm_device *dev = crtc->dev;
5977 struct drm_i915_private *dev_priv = dev->dev_private;
5978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5979 int pipe = intel_crtc->pipe;
5980 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005981 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005982 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005983 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005984 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005985 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005986 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005987 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005988 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005989
5990 for_each_encoder_on_crtc(dev, crtc, encoder) {
5991 switch (encoder->type) {
5992 case INTEL_OUTPUT_LVDS:
5993 is_lvds = true;
5994 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005995 }
5996
5997 num_connectors++;
5998 }
5999
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006000 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6001 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6002
Daniel Vetterff9a6752013-06-01 17:16:21 +02006003 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006004 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006005 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006006 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6007 return -EINVAL;
6008 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006009 /* Compat-code for transition, will disappear. */
6010 if (!intel_crtc->config.clock_set) {
6011 intel_crtc->config.dpll.n = clock.n;
6012 intel_crtc->config.dpll.m1 = clock.m1;
6013 intel_crtc->config.dpll.m2 = clock.m2;
6014 intel_crtc->config.dpll.p1 = clock.p1;
6015 intel_crtc->config.dpll.p2 = clock.p2;
6016 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006017
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006018 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006019 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006020 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006021 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006022 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006023
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006024 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006025 &fp, &reduced_clock,
6026 has_reduced_clock ? &fp2 : NULL);
6027
Daniel Vetter959e16d2013-06-05 13:34:21 +02006028 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006029 intel_crtc->config.dpll_hw_state.fp0 = fp;
6030 if (has_reduced_clock)
6031 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6032 else
6033 intel_crtc->config.dpll_hw_state.fp1 = fp;
6034
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006035 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006036 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006037 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6038 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006039 return -EINVAL;
6040 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006041 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006042 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006043
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006044 if (intel_crtc->config.has_dp_encoder)
6045 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006046
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006047 if (is_lvds && has_reduced_clock && i915_powersave)
6048 intel_crtc->lowfreq_avail = true;
6049 else
6050 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006051
6052 if (intel_crtc->config.has_pch_encoder) {
6053 pll = intel_crtc_to_shared_dpll(intel_crtc);
6054
Jesse Barnes79e53942008-11-07 14:24:08 -08006055 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006056
Daniel Vetter8a654f32013-06-01 17:16:22 +02006057 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006058
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006059 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006060 intel_cpu_transcoder_set_m_n(intel_crtc,
6061 &intel_crtc->config.fdi_m_n);
6062 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006063
Daniel Vetterebfd86f2013-04-19 11:24:44 +02006064 if (IS_IVYBRIDGE(dev))
6065 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006066
Daniel Vetter6ff93602013-04-19 11:24:36 +02006067 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006068
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006069 /* Set up the display plane register */
6070 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006071 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006072
Daniel Vetter94352cf2012-07-05 22:51:56 +02006073 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006074
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006075 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006076}
6077
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006078static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6079 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006080{
6081 struct drm_device *dev = crtc->base.dev;
6082 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006083 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006084
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006085 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6086 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6087 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6088 & ~TU_SIZE_MASK;
6089 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6090 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6091 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6092}
6093
6094static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6095 enum transcoder transcoder,
6096 struct intel_link_m_n *m_n)
6097{
6098 struct drm_device *dev = crtc->base.dev;
6099 struct drm_i915_private *dev_priv = dev->dev_private;
6100 enum pipe pipe = crtc->pipe;
6101
6102 if (INTEL_INFO(dev)->gen >= 5) {
6103 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6104 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6105 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6106 & ~TU_SIZE_MASK;
6107 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6108 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6109 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6110 } else {
6111 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6112 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6113 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6114 & ~TU_SIZE_MASK;
6115 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6116 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6117 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6118 }
6119}
6120
6121void intel_dp_get_m_n(struct intel_crtc *crtc,
6122 struct intel_crtc_config *pipe_config)
6123{
6124 if (crtc->config.has_pch_encoder)
6125 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6126 else
6127 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6128 &pipe_config->dp_m_n);
6129}
6130
Daniel Vetter72419202013-04-04 13:28:53 +02006131static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6132 struct intel_crtc_config *pipe_config)
6133{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006134 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6135 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006136}
6137
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006138static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6139 struct intel_crtc_config *pipe_config)
6140{
6141 struct drm_device *dev = crtc->base.dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
6143 uint32_t tmp;
6144
6145 tmp = I915_READ(PF_CTL(crtc->pipe));
6146
6147 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006148 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006149 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6150 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006151
6152 /* We currently do not free assignements of panel fitters on
6153 * ivb/hsw (since we don't use the higher upscaling modes which
6154 * differentiates them) so just WARN about this case for now. */
6155 if (IS_GEN7(dev)) {
6156 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6157 PF_PIPE_SEL_IVB(crtc->pipe));
6158 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006159 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006160}
6161
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006162static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6163 struct intel_crtc_config *pipe_config)
6164{
6165 struct drm_device *dev = crtc->base.dev;
6166 struct drm_i915_private *dev_priv = dev->dev_private;
6167 uint32_t tmp;
6168
Daniel Vettere143a212013-07-04 12:01:15 +02006169 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006170 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006171
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006172 tmp = I915_READ(PIPECONF(crtc->pipe));
6173 if (!(tmp & PIPECONF_ENABLE))
6174 return false;
6175
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006176 switch (tmp & PIPECONF_BPC_MASK) {
6177 case PIPECONF_6BPC:
6178 pipe_config->pipe_bpp = 18;
6179 break;
6180 case PIPECONF_8BPC:
6181 pipe_config->pipe_bpp = 24;
6182 break;
6183 case PIPECONF_10BPC:
6184 pipe_config->pipe_bpp = 30;
6185 break;
6186 case PIPECONF_12BPC:
6187 pipe_config->pipe_bpp = 36;
6188 break;
6189 default:
6190 break;
6191 }
6192
Daniel Vetterab9412b2013-05-03 11:49:46 +02006193 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006194 struct intel_shared_dpll *pll;
6195
Daniel Vetter88adfff2013-03-28 10:42:01 +01006196 pipe_config->has_pch_encoder = true;
6197
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006198 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6199 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6200 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006201
6202 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006203
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006204 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006205 pipe_config->shared_dpll =
6206 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006207 } else {
6208 tmp = I915_READ(PCH_DPLL_SEL);
6209 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6210 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6211 else
6212 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6213 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006214
6215 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6216
6217 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6218 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006219
6220 tmp = pipe_config->dpll_hw_state.dpll;
6221 pipe_config->pixel_multiplier =
6222 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6223 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006224
6225 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006226 } else {
6227 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006228 }
6229
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006230 intel_get_pipe_timings(crtc, pipe_config);
6231
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006232 ironlake_get_pfit_config(crtc, pipe_config);
6233
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006234 return true;
6235}
6236
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006237static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6238{
6239 struct drm_device *dev = dev_priv->dev;
6240 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6241 struct intel_crtc *crtc;
6242 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006243 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006244
6245 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6246 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6247 pipe_name(crtc->pipe));
6248
6249 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6250 WARN(plls->spll_refcount, "SPLL enabled\n");
6251 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6252 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6253 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6254 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6255 "CPU PWM1 enabled\n");
6256 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6257 "CPU PWM2 enabled\n");
6258 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6259 "PCH PWM1 enabled\n");
6260 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6261 "Utility pin enabled\n");
6262 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6263
6264 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6265 val = I915_READ(DEIMR);
6266 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6267 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6268 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006269 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006270 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6271 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6272}
6273
6274/*
6275 * This function implements pieces of two sequences from BSpec:
6276 * - Sequence for display software to disable LCPLL
6277 * - Sequence for display software to allow package C8+
6278 * The steps implemented here are just the steps that actually touch the LCPLL
6279 * register. Callers should take care of disabling all the display engine
6280 * functions, doing the mode unset, fixing interrupts, etc.
6281 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006282static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6283 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006284{
6285 uint32_t val;
6286
6287 assert_can_disable_lcpll(dev_priv);
6288
6289 val = I915_READ(LCPLL_CTL);
6290
6291 if (switch_to_fclk) {
6292 val |= LCPLL_CD_SOURCE_FCLK;
6293 I915_WRITE(LCPLL_CTL, val);
6294
6295 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6296 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6297 DRM_ERROR("Switching to FCLK failed\n");
6298
6299 val = I915_READ(LCPLL_CTL);
6300 }
6301
6302 val |= LCPLL_PLL_DISABLE;
6303 I915_WRITE(LCPLL_CTL, val);
6304 POSTING_READ(LCPLL_CTL);
6305
6306 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6307 DRM_ERROR("LCPLL still locked\n");
6308
6309 val = I915_READ(D_COMP);
6310 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006311 mutex_lock(&dev_priv->rps.hw_lock);
6312 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6313 DRM_ERROR("Failed to disable D_COMP\n");
6314 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006315 POSTING_READ(D_COMP);
6316 ndelay(100);
6317
6318 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6319 DRM_ERROR("D_COMP RCOMP still in progress\n");
6320
6321 if (allow_power_down) {
6322 val = I915_READ(LCPLL_CTL);
6323 val |= LCPLL_POWER_DOWN_ALLOW;
6324 I915_WRITE(LCPLL_CTL, val);
6325 POSTING_READ(LCPLL_CTL);
6326 }
6327}
6328
6329/*
6330 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6331 * source.
6332 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006333static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006334{
6335 uint32_t val;
6336
6337 val = I915_READ(LCPLL_CTL);
6338
6339 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6340 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6341 return;
6342
Paulo Zanoni215733f2013-08-19 13:18:07 -03006343 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6344 * we'll hang the machine! */
6345 dev_priv->uncore.funcs.force_wake_get(dev_priv);
6346
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006347 if (val & LCPLL_POWER_DOWN_ALLOW) {
6348 val &= ~LCPLL_POWER_DOWN_ALLOW;
6349 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006350 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006351 }
6352
6353 val = I915_READ(D_COMP);
6354 val |= D_COMP_COMP_FORCE;
6355 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006356 mutex_lock(&dev_priv->rps.hw_lock);
6357 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6358 DRM_ERROR("Failed to enable D_COMP\n");
6359 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006360 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006361
6362 val = I915_READ(LCPLL_CTL);
6363 val &= ~LCPLL_PLL_DISABLE;
6364 I915_WRITE(LCPLL_CTL, val);
6365
6366 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6367 DRM_ERROR("LCPLL not locked yet\n");
6368
6369 if (val & LCPLL_CD_SOURCE_FCLK) {
6370 val = I915_READ(LCPLL_CTL);
6371 val &= ~LCPLL_CD_SOURCE_FCLK;
6372 I915_WRITE(LCPLL_CTL, val);
6373
6374 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6375 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6376 DRM_ERROR("Switching back to LCPLL failed\n");
6377 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006378
6379 dev_priv->uncore.funcs.force_wake_put(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006380}
6381
Paulo Zanonic67a4702013-08-19 13:18:09 -03006382void hsw_enable_pc8_work(struct work_struct *__work)
6383{
6384 struct drm_i915_private *dev_priv =
6385 container_of(to_delayed_work(__work), struct drm_i915_private,
6386 pc8.enable_work);
6387 struct drm_device *dev = dev_priv->dev;
6388 uint32_t val;
6389
6390 if (dev_priv->pc8.enabled)
6391 return;
6392
6393 DRM_DEBUG_KMS("Enabling package C8+\n");
6394
6395 dev_priv->pc8.enabled = true;
6396
6397 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6398 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6399 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6400 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6401 }
6402
6403 lpt_disable_clkout_dp(dev);
6404 hsw_pc8_disable_interrupts(dev);
6405 hsw_disable_lcpll(dev_priv, true, true);
6406}
6407
6408static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6409{
6410 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6411 WARN(dev_priv->pc8.disable_count < 1,
6412 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6413
6414 dev_priv->pc8.disable_count--;
6415 if (dev_priv->pc8.disable_count != 0)
6416 return;
6417
6418 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006419 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006420}
6421
6422static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6423{
6424 struct drm_device *dev = dev_priv->dev;
6425 uint32_t val;
6426
6427 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6428 WARN(dev_priv->pc8.disable_count < 0,
6429 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6430
6431 dev_priv->pc8.disable_count++;
6432 if (dev_priv->pc8.disable_count != 1)
6433 return;
6434
6435 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6436 if (!dev_priv->pc8.enabled)
6437 return;
6438
6439 DRM_DEBUG_KMS("Disabling package C8+\n");
6440
6441 hsw_restore_lcpll(dev_priv);
6442 hsw_pc8_restore_interrupts(dev);
6443 lpt_init_pch_refclk(dev);
6444
6445 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6446 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6447 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6448 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6449 }
6450
6451 intel_prepare_ddi(dev);
6452 i915_gem_init_swizzling(dev);
6453 mutex_lock(&dev_priv->rps.hw_lock);
6454 gen6_update_ring_freq(dev);
6455 mutex_unlock(&dev_priv->rps.hw_lock);
6456 dev_priv->pc8.enabled = false;
6457}
6458
6459void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6460{
6461 mutex_lock(&dev_priv->pc8.lock);
6462 __hsw_enable_package_c8(dev_priv);
6463 mutex_unlock(&dev_priv->pc8.lock);
6464}
6465
6466void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6467{
6468 mutex_lock(&dev_priv->pc8.lock);
6469 __hsw_disable_package_c8(dev_priv);
6470 mutex_unlock(&dev_priv->pc8.lock);
6471}
6472
6473static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6474{
6475 struct drm_device *dev = dev_priv->dev;
6476 struct intel_crtc *crtc;
6477 uint32_t val;
6478
6479 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6480 if (crtc->base.enabled)
6481 return false;
6482
6483 /* This case is still possible since we have the i915.disable_power_well
6484 * parameter and also the KVMr or something else might be requesting the
6485 * power well. */
6486 val = I915_READ(HSW_PWR_WELL_DRIVER);
6487 if (val != 0) {
6488 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6489 return false;
6490 }
6491
6492 return true;
6493}
6494
6495/* Since we're called from modeset_global_resources there's no way to
6496 * symmetrically increase and decrease the refcount, so we use
6497 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6498 * or not.
6499 */
6500static void hsw_update_package_c8(struct drm_device *dev)
6501{
6502 struct drm_i915_private *dev_priv = dev->dev_private;
6503 bool allow;
6504
6505 if (!i915_enable_pc8)
6506 return;
6507
6508 mutex_lock(&dev_priv->pc8.lock);
6509
6510 allow = hsw_can_enable_package_c8(dev_priv);
6511
6512 if (allow == dev_priv->pc8.requirements_met)
6513 goto done;
6514
6515 dev_priv->pc8.requirements_met = allow;
6516
6517 if (allow)
6518 __hsw_enable_package_c8(dev_priv);
6519 else
6520 __hsw_disable_package_c8(dev_priv);
6521
6522done:
6523 mutex_unlock(&dev_priv->pc8.lock);
6524}
6525
6526static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6527{
6528 if (!dev_priv->pc8.gpu_idle) {
6529 dev_priv->pc8.gpu_idle = true;
6530 hsw_enable_package_c8(dev_priv);
6531 }
6532}
6533
6534static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6535{
6536 if (dev_priv->pc8.gpu_idle) {
6537 dev_priv->pc8.gpu_idle = false;
6538 hsw_disable_package_c8(dev_priv);
6539 }
Daniel Vetter94352cf2012-07-05 22:51:56 +02006540}
Eric Anholtf564048e2011-03-30 13:01:02 -07006541
6542static void haswell_modeset_global_resources(struct drm_device *dev)
6543{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006544 bool enable = false;
6545 struct intel_crtc *crtc;
Eric Anholt0b701d22011-03-30 13:01:03 -07006546
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006547 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6548 if (!crtc->base.enabled)
6549 continue;
Eric Anholt0b701d22011-03-30 13:01:03 -07006550
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006551 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
Jesse Barnes79e53942008-11-07 14:24:08 -08006552 crtc->config.cpu_transcoder != TRANSCODER_EDP)
6553 enable = true;
6554 }
6555
6556 intel_set_power_well(dev, enable);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006557
6558 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006559}
6560
6561static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6562 int x, int y,
6563 struct drm_framebuffer *fb)
6564{
6565 struct drm_device *dev = crtc->dev;
6566 struct drm_i915_private *dev_priv = dev->dev_private;
6567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6568 int plane = intel_crtc->plane;
6569 int ret;
6570
6571 if (!intel_ddi_pll_mode_set(crtc))
6572 return -EINVAL;
6573
Chris Wilson560b85b2010-08-07 11:01:38 +01006574 if (intel_crtc->config.has_dp_encoder)
6575 intel_dp_set_m_n(intel_crtc);
6576
6577 intel_crtc->lowfreq_avail = false;
6578
6579 intel_set_pipe_timings(intel_crtc);
6580
6581 if (intel_crtc->config.has_pch_encoder) {
6582 intel_cpu_transcoder_set_m_n(intel_crtc,
6583 &intel_crtc->config.fdi_m_n);
6584 }
6585
6586 haswell_set_pipeconf(crtc);
6587
6588 intel_set_pipe_csc(crtc);
6589
6590 /* Set up the display plane register */
6591 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6592 POSTING_READ(DSPCNTR(plane));
6593
6594 ret = intel_pipe_set_base(crtc, x, y, fb);
6595
Chris Wilson560b85b2010-08-07 11:01:38 +01006596 return ret;
6597}
6598
6599static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6600 struct intel_crtc_config *pipe_config)
6601{
6602 struct drm_device *dev = crtc->base.dev;
6603 struct drm_i915_private *dev_priv = dev->dev_private;
6604 enum intel_display_power_domain pfit_domain;
6605 uint32_t tmp;
6606
6607 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6608 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6609
6610 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6611 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6612 enum pipe trans_edp_pipe;
6613 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6614 default:
6615 WARN(1, "unknown pipe linked to edp transcoder\n");
6616 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6617 case TRANS_DDI_EDP_INPUT_A_ON:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006618 trans_edp_pipe = PIPE_A;
Chris Wilson6b383a72010-09-13 13:54:26 +01006619 break;
6620 case TRANS_DDI_EDP_INPUT_B_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006621 trans_edp_pipe = PIPE_B;
6622 break;
6623 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6624 trans_edp_pipe = PIPE_C;
6625 break;
6626 }
6627
Chris Wilson560b85b2010-08-07 11:01:38 +01006628 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006629 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6630 }
6631
6632 if (!intel_display_power_enabled(dev,
Chris Wilson6b383a72010-09-13 13:54:26 +01006633 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006634 return false;
6635
6636 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6637 if (!(tmp & PIPECONF_ENABLE))
6638 return false;
6639
6640 /*
6641 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6642 * DDI E. So just check whether this pipe is wired to DDI E and whether
6643 * the PCH transcoder is on.
6644 */
6645 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6646 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6647 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6648 pipe_config->has_pch_encoder = true;
6649
6650 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6651 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6652 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6653
6654 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6655 }
6656
6657 intel_get_pipe_timings(crtc, pipe_config);
6658
6659 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6660 if (intel_display_power_enabled(dev, pfit_domain))
6661 ironlake_get_pfit_config(crtc, pipe_config);
Chris Wilson560b85b2010-08-07 11:01:38 +01006662
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006663 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6664 (I915_READ(IPS_CTL) & IPS_ENABLE);
6665
Chris Wilson560b85b2010-08-07 11:01:38 +01006666 pipe_config->pixel_multiplier = 1;
6667
6668 return true;
6669}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006670
6671static int intel_crtc_mode_set(struct drm_crtc *crtc,
6672 int x, int y,
6673 struct drm_framebuffer *fb)
6674{
Jesse Barnes79e53942008-11-07 14:24:08 -08006675 struct drm_device *dev = crtc->dev;
Chris Wilson05394f32010-11-08 19:18:58 +00006676 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006677 struct intel_encoder *encoder;
6678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07006679 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6680 int pipe = intel_crtc->pipe;
6681 int ret;
6682
Eric Anholt0b701d22011-03-30 13:01:03 -07006683 drm_vblank_pre_modeset(dev, pipe);
6684
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006685 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6686
Jesse Barnes79e53942008-11-07 14:24:08 -08006687 drm_vblank_post_modeset(dev, pipe);
6688
Daniel Vetter9256aa12012-10-31 19:26:13 +01006689 if (ret != 0)
6690 return ret;
6691
6692 for_each_encoder_on_crtc(dev, crtc, encoder) {
6693 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6694 encoder->base.base.id,
6695 drm_get_encoder_name(&encoder->base),
6696 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02006697 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01006698 }
6699
6700 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006701}
6702
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006703static bool intel_eld_uptodate(struct drm_connector *connector,
6704 int reg_eldv, uint32_t bits_eldv,
6705 int reg_elda, uint32_t bits_elda,
6706 int reg_edid)
6707{
6708 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6709 uint8_t *eld = connector->eld;
6710 uint32_t i;
6711
6712 i = I915_READ(reg_eldv);
6713 i &= bits_eldv;
6714
6715 if (!eld[0])
6716 return !i;
6717
6718 if (!i)
6719 return false;
6720
6721 i = I915_READ(reg_elda);
6722 i &= ~bits_elda;
6723 I915_WRITE(reg_elda, i);
6724
6725 for (i = 0; i < eld[2]; i++)
6726 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6727 return false;
6728
6729 return true;
6730}
6731
Wu Fengguange0dac652011-09-05 14:25:34 +08006732static void g4x_write_eld(struct drm_connector *connector,
6733 struct drm_crtc *crtc)
6734{
6735 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6736 uint8_t *eld = connector->eld;
6737 uint32_t eldv;
6738 uint32_t len;
6739 uint32_t i;
6740
6741 i = I915_READ(G4X_AUD_VID_DID);
6742
6743 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6744 eldv = G4X_ELDV_DEVCL_DEVBLC;
6745 else
6746 eldv = G4X_ELDV_DEVCTG;
6747
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006748 if (intel_eld_uptodate(connector,
6749 G4X_AUD_CNTL_ST, eldv,
6750 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6751 G4X_HDMIW_HDMIEDID))
6752 return;
6753
Wu Fengguange0dac652011-09-05 14:25:34 +08006754 i = I915_READ(G4X_AUD_CNTL_ST);
6755 i &= ~(eldv | G4X_ELD_ADDR);
6756 len = (i >> 9) & 0x1f; /* ELD buffer size */
6757 I915_WRITE(G4X_AUD_CNTL_ST, i);
6758
6759 if (!eld[0])
6760 return;
6761
6762 len = min_t(uint8_t, eld[2], len);
6763 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6764 for (i = 0; i < len; i++)
6765 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6766
6767 i = I915_READ(G4X_AUD_CNTL_ST);
6768 i |= eldv;
6769 I915_WRITE(G4X_AUD_CNTL_ST, i);
6770}
6771
Wang Xingchao83358c852012-08-16 22:43:37 +08006772static void haswell_write_eld(struct drm_connector *connector,
6773 struct drm_crtc *crtc)
6774{
6775 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6776 uint8_t *eld = connector->eld;
6777 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006779 uint32_t eldv;
6780 uint32_t i;
6781 int len;
6782 int pipe = to_intel_crtc(crtc)->pipe;
6783 int tmp;
6784
6785 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6786 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6787 int aud_config = HSW_AUD_CFG(pipe);
6788 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6789
6790
6791 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6792
6793 /* Audio output enable */
6794 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6795 tmp = I915_READ(aud_cntrl_st2);
6796 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6797 I915_WRITE(aud_cntrl_st2, tmp);
6798
6799 /* Wait for 1 vertical blank */
6800 intel_wait_for_vblank(dev, pipe);
6801
6802 /* Set ELD valid state */
6803 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006804 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006805 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6806 I915_WRITE(aud_cntrl_st2, tmp);
6807 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006808 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006809
6810 /* Enable HDMI mode */
6811 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006812 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006813 /* clear N_programing_enable and N_value_index */
6814 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6815 I915_WRITE(aud_config, tmp);
6816
6817 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6818
6819 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006820 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006821
6822 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6823 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6824 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6825 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6826 } else
6827 I915_WRITE(aud_config, 0);
6828
6829 if (intel_eld_uptodate(connector,
6830 aud_cntrl_st2, eldv,
6831 aud_cntl_st, IBX_ELD_ADDRESS,
6832 hdmiw_hdmiedid))
6833 return;
6834
6835 i = I915_READ(aud_cntrl_st2);
6836 i &= ~eldv;
6837 I915_WRITE(aud_cntrl_st2, i);
6838
6839 if (!eld[0])
6840 return;
6841
6842 i = I915_READ(aud_cntl_st);
6843 i &= ~IBX_ELD_ADDRESS;
6844 I915_WRITE(aud_cntl_st, i);
6845 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6846 DRM_DEBUG_DRIVER("port num:%d\n", i);
6847
6848 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6849 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6850 for (i = 0; i < len; i++)
6851 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6852
6853 i = I915_READ(aud_cntrl_st2);
6854 i |= eldv;
6855 I915_WRITE(aud_cntrl_st2, i);
6856
6857}
6858
Wu Fengguange0dac652011-09-05 14:25:34 +08006859static void ironlake_write_eld(struct drm_connector *connector,
6860 struct drm_crtc *crtc)
6861{
6862 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6863 uint8_t *eld = connector->eld;
6864 uint32_t eldv;
6865 uint32_t i;
6866 int len;
6867 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006868 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006869 int aud_cntl_st;
6870 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006871 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006872
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006873 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006874 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6875 aud_config = IBX_AUD_CFG(pipe);
6876 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006877 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006878 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006879 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6880 aud_config = CPT_AUD_CFG(pipe);
6881 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006882 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006883 }
6884
Wang Xingchao9b138a82012-08-09 16:52:18 +08006885 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006886
6887 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006888 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006889 if (!i) {
6890 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6891 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006892 eldv = IBX_ELD_VALIDB;
6893 eldv |= IBX_ELD_VALIDB << 4;
6894 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006895 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006896 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006897 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006898 }
6899
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006900 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6901 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6902 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006903 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6904 } else
6905 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006906
6907 if (intel_eld_uptodate(connector,
6908 aud_cntrl_st2, eldv,
6909 aud_cntl_st, IBX_ELD_ADDRESS,
6910 hdmiw_hdmiedid))
6911 return;
6912
Wu Fengguange0dac652011-09-05 14:25:34 +08006913 i = I915_READ(aud_cntrl_st2);
6914 i &= ~eldv;
6915 I915_WRITE(aud_cntrl_st2, i);
6916
6917 if (!eld[0])
6918 return;
6919
Wu Fengguange0dac652011-09-05 14:25:34 +08006920 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006921 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006922 I915_WRITE(aud_cntl_st, i);
6923
6924 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6925 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6926 for (i = 0; i < len; i++)
6927 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6928
6929 i = I915_READ(aud_cntrl_st2);
6930 i |= eldv;
6931 I915_WRITE(aud_cntrl_st2, i);
6932}
6933
6934void intel_write_eld(struct drm_encoder *encoder,
6935 struct drm_display_mode *mode)
6936{
6937 struct drm_crtc *crtc = encoder->crtc;
6938 struct drm_connector *connector;
6939 struct drm_device *dev = encoder->dev;
6940 struct drm_i915_private *dev_priv = dev->dev_private;
6941
6942 connector = drm_select_eld(encoder, mode);
6943 if (!connector)
6944 return;
6945
6946 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6947 connector->base.id,
6948 drm_get_connector_name(connector),
6949 connector->encoder->base.id,
6950 drm_get_encoder_name(connector->encoder));
6951
6952 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6953
6954 if (dev_priv->display.write_eld)
6955 dev_priv->display.write_eld(connector, crtc);
6956}
6957
Jesse Barnes79e53942008-11-07 14:24:08 -08006958static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6959{
6960 struct drm_device *dev = crtc->dev;
6961 struct drm_i915_private *dev_priv = dev->dev_private;
6962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6963 bool visible = base != 0;
6964 u32 cntl;
6965
6966 if (intel_crtc->cursor_visible == visible)
6967 return;
6968
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006969 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08006970 if (visible) {
6971 /* On these chipsets we can only modify the base whilst
6972 * the cursor is disabled.
6973 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006974 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006975
6976 cntl &= ~(CURSOR_FORMAT_MASK);
6977 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6978 cntl |= CURSOR_ENABLE |
6979 CURSOR_GAMMA_ENABLE |
6980 CURSOR_FORMAT_ARGB;
6981 } else
6982 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006983 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08006984
6985 intel_crtc->cursor_visible = visible;
6986}
6987
6988static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6989{
6990 struct drm_device *dev = crtc->dev;
6991 struct drm_i915_private *dev_priv = dev->dev_private;
6992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6993 int pipe = intel_crtc->pipe;
6994 bool visible = base != 0;
6995
6996 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006997 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006998 if (base) {
6999 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7000 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7001 cntl |= pipe << 28; /* Connect to correct pipe */
7002 } else {
7003 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7004 cntl |= CURSOR_MODE_DISABLE;
7005 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007006 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007007
7008 intel_crtc->cursor_visible = visible;
7009 }
7010 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007011 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007012}
7013
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007014static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7015{
7016 struct drm_device *dev = crtc->dev;
7017 struct drm_i915_private *dev_priv = dev->dev_private;
7018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7019 int pipe = intel_crtc->pipe;
7020 bool visible = base != 0;
7021
7022 if (intel_crtc->cursor_visible != visible) {
7023 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7024 if (base) {
7025 cntl &= ~CURSOR_MODE;
7026 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7027 } else {
7028 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7029 cntl |= CURSOR_MODE_DISABLE;
7030 }
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007031 if (IS_HASWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007032 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007033 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7034 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007035 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7036
7037 intel_crtc->cursor_visible = visible;
7038 }
7039 /* and commit changes on next vblank */
7040 I915_WRITE(CURBASE_IVB(pipe), base);
7041}
7042
Jesse Barnes79e53942008-11-07 14:24:08 -08007043/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7044static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7045 bool on)
7046{
7047 struct drm_device *dev = crtc->dev;
7048 struct drm_i915_private *dev_priv = dev->dev_private;
7049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7050 int pipe = intel_crtc->pipe;
7051 int x = intel_crtc->cursor_x;
7052 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007053 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007054 bool visible;
7055
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007056 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007057 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007058
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007059 if (x >= intel_crtc->config.pipe_src_w)
7060 base = 0;
7061
7062 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007063 base = 0;
7064
7065 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007066 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007067 base = 0;
7068
7069 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7070 x = -x;
7071 }
7072 pos |= x << CURSOR_X_SHIFT;
7073
7074 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007075 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007076 base = 0;
7077
7078 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7079 y = -y;
7080 }
7081 pos |= y << CURSOR_Y_SHIFT;
7082
7083 visible = base != 0;
7084 if (!visible && !intel_crtc->cursor_visible)
7085 return;
7086
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03007087 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007088 I915_WRITE(CURPOS_IVB(pipe), pos);
7089 ivb_update_cursor(crtc, base);
7090 } else {
7091 I915_WRITE(CURPOS(pipe), pos);
7092 if (IS_845G(dev) || IS_I865G(dev))
7093 i845_update_cursor(crtc, base);
7094 else
7095 i9xx_update_cursor(crtc, base);
7096 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007097}
7098
7099static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7100 struct drm_file *file,
7101 uint32_t handle,
7102 uint32_t width, uint32_t height)
7103{
7104 struct drm_device *dev = crtc->dev;
7105 struct drm_i915_private *dev_priv = dev->dev_private;
7106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007107 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007108 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007109 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007110
Jesse Barnes79e53942008-11-07 14:24:08 -08007111 /* if we want to turn off the cursor ignore width and height */
7112 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007113 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007114 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007115 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007116 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007117 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007118 }
7119
7120 /* Currently we only support 64x64 cursors */
7121 if (width != 64 || height != 64) {
7122 DRM_ERROR("we currently only support 64x64 cursors\n");
7123 return -EINVAL;
7124 }
7125
Chris Wilson05394f32010-11-08 19:18:58 +00007126 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007127 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007128 return -ENOENT;
7129
Chris Wilson05394f32010-11-08 19:18:58 +00007130 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007131 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007132 ret = -ENOMEM;
7133 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007134 }
7135
Dave Airlie71acb5e2008-12-30 20:31:46 +10007136 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007137 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007138 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007139 unsigned alignment;
7140
Chris Wilsond9e86c02010-11-10 16:40:20 +00007141 if (obj->tiling_mode) {
7142 DRM_ERROR("cursor cannot be tiled\n");
7143 ret = -EINVAL;
7144 goto fail_locked;
7145 }
7146
Chris Wilson693db182013-03-05 14:52:39 +00007147 /* Note that the w/a also requires 2 PTE of padding following
7148 * the bo. We currently fill all unused PTE with the shadow
7149 * page and so we should always have valid PTE following the
7150 * cursor preventing the VT-d warning.
7151 */
7152 alignment = 0;
7153 if (need_vtd_wa(dev))
7154 alignment = 64*1024;
7155
7156 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007157 if (ret) {
7158 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007159 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007160 }
7161
Chris Wilsond9e86c02010-11-10 16:40:20 +00007162 ret = i915_gem_object_put_fence(obj);
7163 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007164 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007165 goto fail_unpin;
7166 }
7167
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007168 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007169 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007170 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007171 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007172 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7173 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007174 if (ret) {
7175 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007176 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007177 }
Chris Wilson05394f32010-11-08 19:18:58 +00007178 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007179 }
7180
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007181 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007182 I915_WRITE(CURSIZE, (height << 12) | width);
7183
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007184 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007185 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007186 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007187 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007188 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7189 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007190 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007191 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007192 }
Jesse Barnes80824002009-09-10 15:28:06 -07007193
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007194 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007195
7196 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007197 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007198 intel_crtc->cursor_width = width;
7199 intel_crtc->cursor_height = height;
7200
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007201 if (intel_crtc->active)
7202 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007203
Jesse Barnes79e53942008-11-07 14:24:08 -08007204 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007205fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007206 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007207fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007208 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007209fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007210 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007211 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007212}
7213
7214static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7215{
Jesse Barnes79e53942008-11-07 14:24:08 -08007216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007217
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007218 intel_crtc->cursor_x = x;
7219 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07007220
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007221 if (intel_crtc->active)
7222 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007223
7224 return 0;
7225}
7226
Jesse Barnes79e53942008-11-07 14:24:08 -08007227static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007228 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007229{
James Simmons72034252010-08-03 01:33:19 +01007230 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007232
James Simmons72034252010-08-03 01:33:19 +01007233 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007234 intel_crtc->lut_r[i] = red[i] >> 8;
7235 intel_crtc->lut_g[i] = green[i] >> 8;
7236 intel_crtc->lut_b[i] = blue[i] >> 8;
7237 }
7238
7239 intel_crtc_load_lut(crtc);
7240}
7241
Jesse Barnes79e53942008-11-07 14:24:08 -08007242/* VESA 640x480x72Hz mode to set on the pipe */
7243static struct drm_display_mode load_detect_mode = {
7244 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7245 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7246};
7247
Chris Wilsond2dff872011-04-19 08:36:26 +01007248static struct drm_framebuffer *
7249intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007250 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007251 struct drm_i915_gem_object *obj)
7252{
7253 struct intel_framebuffer *intel_fb;
7254 int ret;
7255
7256 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7257 if (!intel_fb) {
7258 drm_gem_object_unreference_unlocked(&obj->base);
7259 return ERR_PTR(-ENOMEM);
7260 }
7261
7262 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
7263 if (ret) {
7264 drm_gem_object_unreference_unlocked(&obj->base);
7265 kfree(intel_fb);
7266 return ERR_PTR(ret);
7267 }
7268
7269 return &intel_fb->base;
7270}
7271
7272static u32
7273intel_framebuffer_pitch_for_width(int width, int bpp)
7274{
7275 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7276 return ALIGN(pitch, 64);
7277}
7278
7279static u32
7280intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7281{
7282 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7283 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7284}
7285
7286static struct drm_framebuffer *
7287intel_framebuffer_create_for_mode(struct drm_device *dev,
7288 struct drm_display_mode *mode,
7289 int depth, int bpp)
7290{
7291 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007292 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007293
7294 obj = i915_gem_alloc_object(dev,
7295 intel_framebuffer_size_for_mode(mode, bpp));
7296 if (obj == NULL)
7297 return ERR_PTR(-ENOMEM);
7298
7299 mode_cmd.width = mode->hdisplay;
7300 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007301 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7302 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007303 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007304
7305 return intel_framebuffer_create(dev, &mode_cmd, obj);
7306}
7307
7308static struct drm_framebuffer *
7309mode_fits_in_fbdev(struct drm_device *dev,
7310 struct drm_display_mode *mode)
7311{
7312 struct drm_i915_private *dev_priv = dev->dev_private;
7313 struct drm_i915_gem_object *obj;
7314 struct drm_framebuffer *fb;
7315
7316 if (dev_priv->fbdev == NULL)
7317 return NULL;
7318
7319 obj = dev_priv->fbdev->ifb.obj;
7320 if (obj == NULL)
7321 return NULL;
7322
7323 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007324 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7325 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007326 return NULL;
7327
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007328 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007329 return NULL;
7330
7331 return fb;
7332}
7333
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007334bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007335 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007336 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007337{
7338 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007339 struct intel_encoder *intel_encoder =
7340 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007341 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007342 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007343 struct drm_crtc *crtc = NULL;
7344 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007345 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007346 int i = -1;
7347
Chris Wilsond2dff872011-04-19 08:36:26 +01007348 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7349 connector->base.id, drm_get_connector_name(connector),
7350 encoder->base.id, drm_get_encoder_name(encoder));
7351
Jesse Barnes79e53942008-11-07 14:24:08 -08007352 /*
7353 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007354 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007355 * - if the connector already has an assigned crtc, use it (but make
7356 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007357 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007358 * - try to find the first unused crtc that can drive this connector,
7359 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007360 */
7361
7362 /* See if we already have a CRTC for this connector */
7363 if (encoder->crtc) {
7364 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007365
Daniel Vetter7b240562012-12-12 00:35:33 +01007366 mutex_lock(&crtc->mutex);
7367
Daniel Vetter24218aa2012-08-12 19:27:11 +02007368 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007369 old->load_detect_temp = false;
7370
7371 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007372 if (connector->dpms != DRM_MODE_DPMS_ON)
7373 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007374
Chris Wilson71731882011-04-19 23:10:58 +01007375 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007376 }
7377
7378 /* Find an unused one (if possible) */
7379 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7380 i++;
7381 if (!(encoder->possible_crtcs & (1 << i)))
7382 continue;
7383 if (!possible_crtc->enabled) {
7384 crtc = possible_crtc;
7385 break;
7386 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007387 }
7388
7389 /*
7390 * If we didn't find an unused CRTC, don't use any.
7391 */
7392 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007393 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7394 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007395 }
7396
Daniel Vetter7b240562012-12-12 00:35:33 +01007397 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007398 intel_encoder->new_crtc = to_intel_crtc(crtc);
7399 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007400
7401 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007402 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007403 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007404 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007405
Chris Wilson64927112011-04-20 07:25:26 +01007406 if (!mode)
7407 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007408
Chris Wilsond2dff872011-04-19 08:36:26 +01007409 /* We need a framebuffer large enough to accommodate all accesses
7410 * that the plane may generate whilst we perform load detection.
7411 * We can not rely on the fbcon either being present (we get called
7412 * during its initialisation to detect all boot displays, or it may
7413 * not even exist) or that it is large enough to satisfy the
7414 * requested mode.
7415 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007416 fb = mode_fits_in_fbdev(dev, mode);
7417 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007418 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007419 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7420 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007421 } else
7422 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007423 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007424 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007425 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007426 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007427 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007428
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007429 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007430 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007431 if (old->release_fb)
7432 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007433 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007434 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007435 }
Chris Wilson71731882011-04-19 23:10:58 +01007436
Jesse Barnes79e53942008-11-07 14:24:08 -08007437 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007438 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007439 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007440}
7441
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007442void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007443 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007444{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007445 struct intel_encoder *intel_encoder =
7446 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007447 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007448 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007449
Chris Wilsond2dff872011-04-19 08:36:26 +01007450 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7451 connector->base.id, drm_get_connector_name(connector),
7452 encoder->base.id, drm_get_encoder_name(encoder));
7453
Chris Wilson8261b192011-04-19 23:18:09 +01007454 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007455 to_intel_connector(connector)->new_encoder = NULL;
7456 intel_encoder->new_crtc = NULL;
7457 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007458
Daniel Vetter36206362012-12-10 20:42:17 +01007459 if (old->release_fb) {
7460 drm_framebuffer_unregister_private(old->release_fb);
7461 drm_framebuffer_unreference(old->release_fb);
7462 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007463
Daniel Vetter67c96402013-01-23 16:25:09 +00007464 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007465 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007466 }
7467
Eric Anholtc751ce42010-03-25 11:48:48 -07007468 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007469 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7470 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007471
7472 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007473}
7474
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007475static int i9xx_pll_refclk(struct drm_device *dev,
7476 const struct intel_crtc_config *pipe_config)
7477{
7478 struct drm_i915_private *dev_priv = dev->dev_private;
7479 u32 dpll = pipe_config->dpll_hw_state.dpll;
7480
7481 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7482 return dev_priv->vbt.lvds_ssc_freq * 1000;
7483 else if (HAS_PCH_SPLIT(dev))
7484 return 120000;
7485 else if (!IS_GEN2(dev))
7486 return 96000;
7487 else
7488 return 48000;
7489}
7490
Jesse Barnes79e53942008-11-07 14:24:08 -08007491/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007492static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7493 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007494{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007495 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007496 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007497 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007498 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007499 u32 fp;
7500 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007501 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007502
7503 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007504 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007505 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007506 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007507
7508 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007509 if (IS_PINEVIEW(dev)) {
7510 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7511 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007512 } else {
7513 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7514 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7515 }
7516
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007517 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007518 if (IS_PINEVIEW(dev))
7519 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7520 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007521 else
7522 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007523 DPLL_FPA01_P1_POST_DIV_SHIFT);
7524
7525 switch (dpll & DPLL_MODE_MASK) {
7526 case DPLLB_MODE_DAC_SERIAL:
7527 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7528 5 : 10;
7529 break;
7530 case DPLLB_MODE_LVDS:
7531 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7532 7 : 14;
7533 break;
7534 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007535 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007536 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007537 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007538 }
7539
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007540 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007541 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007542 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007543 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007544 } else {
7545 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7546
7547 if (is_lvds) {
7548 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7549 DPLL_FPA01_P1_POST_DIV_SHIFT);
7550 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007551 } else {
7552 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7553 clock.p1 = 2;
7554 else {
7555 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7556 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7557 }
7558 if (dpll & PLL_P2_DIVIDE_BY_4)
7559 clock.p2 = 4;
7560 else
7561 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007562 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007563
7564 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007565 }
7566
Ville Syrjälä18442d02013-09-13 16:00:08 +03007567 /*
7568 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007569 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007570 * encoder's get_config() function.
7571 */
7572 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007573}
7574
Ville Syrjälä6878da02013-09-13 15:59:11 +03007575int intel_dotclock_calculate(int link_freq,
7576 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007577{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007578 /*
7579 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007580 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007581 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007582 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007583 *
7584 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007585 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007586 */
7587
Ville Syrjälä6878da02013-09-13 15:59:11 +03007588 if (!m_n->link_n)
7589 return 0;
7590
7591 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7592}
7593
Ville Syrjälä18442d02013-09-13 16:00:08 +03007594static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7595 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007596{
7597 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007598
7599 /* read out port_clock from the DPLL */
7600 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007601
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007602 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007603 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007604 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007605 * agree once we know their relationship in the encoder's
7606 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007607 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007608 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007609 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7610 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007611}
7612
7613/** Returns the currently programmed mode of the given pipe. */
7614struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7615 struct drm_crtc *crtc)
7616{
Jesse Barnes548f2452011-02-17 10:40:53 -08007617 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007618 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007619 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007620 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007621 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007622 int htot = I915_READ(HTOTAL(cpu_transcoder));
7623 int hsync = I915_READ(HSYNC(cpu_transcoder));
7624 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7625 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03007626 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08007627
7628 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7629 if (!mode)
7630 return NULL;
7631
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007632 /*
7633 * Construct a pipe_config sufficient for getting the clock info
7634 * back out of crtc_clock_get.
7635 *
7636 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7637 * to use a real value here instead.
7638 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03007639 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007640 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007641 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
7642 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
7643 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007644 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7645
Ville Syrjälä773ae032013-09-23 17:48:20 +03007646 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08007647 mode->hdisplay = (htot & 0xffff) + 1;
7648 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7649 mode->hsync_start = (hsync & 0xffff) + 1;
7650 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7651 mode->vdisplay = (vtot & 0xffff) + 1;
7652 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7653 mode->vsync_start = (vsync & 0xffff) + 1;
7654 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7655
7656 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007657
7658 return mode;
7659}
7660
Daniel Vetter3dec0092010-08-20 21:40:52 +02007661static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007662{
7663 struct drm_device *dev = crtc->dev;
7664 drm_i915_private_t *dev_priv = dev->dev_private;
7665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7666 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007667 int dpll_reg = DPLL(pipe);
7668 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007669
Eric Anholtbad720f2009-10-22 16:11:14 -07007670 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007671 return;
7672
7673 if (!dev_priv->lvds_downclock_avail)
7674 return;
7675
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007676 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007677 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007678 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007679
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007680 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007681
7682 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7683 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007684 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007685
Jesse Barnes652c3932009-08-17 13:31:43 -07007686 dpll = I915_READ(dpll_reg);
7687 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007688 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007689 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007690}
7691
7692static void intel_decrease_pllclock(struct drm_crtc *crtc)
7693{
7694 struct drm_device *dev = crtc->dev;
7695 drm_i915_private_t *dev_priv = dev->dev_private;
7696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007697
Eric Anholtbad720f2009-10-22 16:11:14 -07007698 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007699 return;
7700
7701 if (!dev_priv->lvds_downclock_avail)
7702 return;
7703
7704 /*
7705 * Since this is called by a timer, we should never get here in
7706 * the manual case.
7707 */
7708 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007709 int pipe = intel_crtc->pipe;
7710 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007711 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007712
Zhao Yakui44d98a62009-10-09 11:39:40 +08007713 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007714
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007715 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007716
Chris Wilson074b5e12012-05-02 12:07:06 +01007717 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007718 dpll |= DISPLAY_RATE_SELECT_FPA1;
7719 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007720 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007721 dpll = I915_READ(dpll_reg);
7722 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007723 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007724 }
7725
7726}
7727
Chris Wilsonf047e392012-07-21 12:31:41 +01007728void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007729{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007730 struct drm_i915_private *dev_priv = dev->dev_private;
7731
7732 hsw_package_c8_gpu_busy(dev_priv);
7733 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01007734}
7735
7736void intel_mark_idle(struct drm_device *dev)
7737{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007738 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00007739 struct drm_crtc *crtc;
7740
Paulo Zanonic67a4702013-08-19 13:18:09 -03007741 hsw_package_c8_gpu_idle(dev_priv);
7742
Chris Wilson725a5b52013-01-08 11:02:57 +00007743 if (!i915_powersave)
7744 return;
7745
7746 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7747 if (!crtc->fb)
7748 continue;
7749
7750 intel_decrease_pllclock(crtc);
7751 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01007752
7753 if (dev_priv->info->gen >= 6)
7754 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01007755}
7756
Chris Wilsonc65355b2013-06-06 16:53:41 -03007757void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7758 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007759{
7760 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007761 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007762
7763 if (!i915_powersave)
7764 return;
7765
Jesse Barnes652c3932009-08-17 13:31:43 -07007766 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007767 if (!crtc->fb)
7768 continue;
7769
Chris Wilsonc65355b2013-06-06 16:53:41 -03007770 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7771 continue;
7772
7773 intel_increase_pllclock(crtc);
7774 if (ring && intel_fbc_enabled(dev))
7775 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007776 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007777}
7778
Jesse Barnes79e53942008-11-07 14:24:08 -08007779static void intel_crtc_destroy(struct drm_crtc *crtc)
7780{
7781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007782 struct drm_device *dev = crtc->dev;
7783 struct intel_unpin_work *work;
7784 unsigned long flags;
7785
7786 spin_lock_irqsave(&dev->event_lock, flags);
7787 work = intel_crtc->unpin_work;
7788 intel_crtc->unpin_work = NULL;
7789 spin_unlock_irqrestore(&dev->event_lock, flags);
7790
7791 if (work) {
7792 cancel_work_sync(&work->work);
7793 kfree(work);
7794 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007795
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007796 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7797
Jesse Barnes79e53942008-11-07 14:24:08 -08007798 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007799
Jesse Barnes79e53942008-11-07 14:24:08 -08007800 kfree(intel_crtc);
7801}
7802
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007803static void intel_unpin_work_fn(struct work_struct *__work)
7804{
7805 struct intel_unpin_work *work =
7806 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007807 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007808
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007809 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007810 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007811 drm_gem_object_unreference(&work->pending_flip_obj->base);
7812 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007813
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007814 intel_update_fbc(dev);
7815 mutex_unlock(&dev->struct_mutex);
7816
7817 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7818 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7819
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007820 kfree(work);
7821}
7822
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007823static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007824 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007825{
7826 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7828 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007829 unsigned long flags;
7830
7831 /* Ignore early vblank irqs */
7832 if (intel_crtc == NULL)
7833 return;
7834
7835 spin_lock_irqsave(&dev->event_lock, flags);
7836 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007837
7838 /* Ensure we don't miss a work->pending update ... */
7839 smp_rmb();
7840
7841 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007842 spin_unlock_irqrestore(&dev->event_lock, flags);
7843 return;
7844 }
7845
Chris Wilsone7d841c2012-12-03 11:36:30 +00007846 /* and that the unpin work is consistent wrt ->pending. */
7847 smp_rmb();
7848
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007849 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007850
Rob Clark45a066e2012-10-08 14:50:40 -05007851 if (work->event)
7852 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007853
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007854 drm_vblank_put(dev, intel_crtc->pipe);
7855
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007856 spin_unlock_irqrestore(&dev->event_lock, flags);
7857
Daniel Vetter2c10d572012-12-20 21:24:07 +01007858 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007859
7860 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007861
7862 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007863}
7864
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007865void intel_finish_page_flip(struct drm_device *dev, int pipe)
7866{
7867 drm_i915_private_t *dev_priv = dev->dev_private;
7868 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7869
Mario Kleiner49b14a52010-12-09 07:00:07 +01007870 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007871}
7872
7873void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7874{
7875 drm_i915_private_t *dev_priv = dev->dev_private;
7876 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7877
Mario Kleiner49b14a52010-12-09 07:00:07 +01007878 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007879}
7880
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007881void intel_prepare_page_flip(struct drm_device *dev, int plane)
7882{
7883 drm_i915_private_t *dev_priv = dev->dev_private;
7884 struct intel_crtc *intel_crtc =
7885 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7886 unsigned long flags;
7887
Chris Wilsone7d841c2012-12-03 11:36:30 +00007888 /* NB: An MMIO update of the plane base pointer will also
7889 * generate a page-flip completion irq, i.e. every modeset
7890 * is also accompanied by a spurious intel_prepare_page_flip().
7891 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007892 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007893 if (intel_crtc->unpin_work)
7894 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007895 spin_unlock_irqrestore(&dev->event_lock, flags);
7896}
7897
Chris Wilsone7d841c2012-12-03 11:36:30 +00007898inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7899{
7900 /* Ensure that the work item is consistent when activating it ... */
7901 smp_wmb();
7902 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7903 /* and that it is marked active as soon as the irq could fire. */
7904 smp_wmb();
7905}
7906
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007907static int intel_gen2_queue_flip(struct drm_device *dev,
7908 struct drm_crtc *crtc,
7909 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007910 struct drm_i915_gem_object *obj,
7911 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007912{
7913 struct drm_i915_private *dev_priv = dev->dev_private;
7914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007915 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007916 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007917 int ret;
7918
Daniel Vetter6d90c952012-04-26 23:28:05 +02007919 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007920 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007921 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007922
Daniel Vetter6d90c952012-04-26 23:28:05 +02007923 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007924 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007925 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007926
7927 /* Can't queue multiple flips, so wait for the previous
7928 * one to finish before executing the next.
7929 */
7930 if (intel_crtc->plane)
7931 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7932 else
7933 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007934 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7935 intel_ring_emit(ring, MI_NOOP);
7936 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7937 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7938 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007939 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007940 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007941
7942 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007943 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007944 return 0;
7945
7946err_unpin:
7947 intel_unpin_fb_obj(obj);
7948err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007949 return ret;
7950}
7951
7952static int intel_gen3_queue_flip(struct drm_device *dev,
7953 struct drm_crtc *crtc,
7954 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007955 struct drm_i915_gem_object *obj,
7956 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007957{
7958 struct drm_i915_private *dev_priv = dev->dev_private;
7959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007960 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007961 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007962 int ret;
7963
Daniel Vetter6d90c952012-04-26 23:28:05 +02007964 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007965 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007966 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007967
Daniel Vetter6d90c952012-04-26 23:28:05 +02007968 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007969 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007970 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007971
7972 if (intel_crtc->plane)
7973 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7974 else
7975 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007976 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7977 intel_ring_emit(ring, MI_NOOP);
7978 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7979 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7980 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007981 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007982 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007983
Chris Wilsone7d841c2012-12-03 11:36:30 +00007984 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007985 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007986 return 0;
7987
7988err_unpin:
7989 intel_unpin_fb_obj(obj);
7990err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007991 return ret;
7992}
7993
7994static int intel_gen4_queue_flip(struct drm_device *dev,
7995 struct drm_crtc *crtc,
7996 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007997 struct drm_i915_gem_object *obj,
7998 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007999{
8000 struct drm_i915_private *dev_priv = dev->dev_private;
8001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8002 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008003 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008004 int ret;
8005
Daniel Vetter6d90c952012-04-26 23:28:05 +02008006 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008007 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008008 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008009
Daniel Vetter6d90c952012-04-26 23:28:05 +02008010 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008011 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008012 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008013
8014 /* i965+ uses the linear or tiled offsets from the
8015 * Display Registers (which do not change across a page-flip)
8016 * so we need only reprogram the base address.
8017 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008018 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8019 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8020 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008021 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008022 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008023 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008024
8025 /* XXX Enabling the panel-fitter across page-flip is so far
8026 * untested on non-native modes, so ignore it for now.
8027 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8028 */
8029 pf = 0;
8030 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008031 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008032
8033 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008034 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008035 return 0;
8036
8037err_unpin:
8038 intel_unpin_fb_obj(obj);
8039err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008040 return ret;
8041}
8042
8043static int intel_gen6_queue_flip(struct drm_device *dev,
8044 struct drm_crtc *crtc,
8045 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008046 struct drm_i915_gem_object *obj,
8047 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008048{
8049 struct drm_i915_private *dev_priv = dev->dev_private;
8050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008051 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008052 uint32_t pf, pipesrc;
8053 int ret;
8054
Daniel Vetter6d90c952012-04-26 23:28:05 +02008055 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008056 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008057 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008058
Daniel Vetter6d90c952012-04-26 23:28:05 +02008059 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008060 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008061 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008062
Daniel Vetter6d90c952012-04-26 23:28:05 +02008063 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8064 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8065 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008066 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008067
Chris Wilson99d9acd2012-04-17 20:37:00 +01008068 /* Contrary to the suggestions in the documentation,
8069 * "Enable Panel Fitter" does not seem to be required when page
8070 * flipping with a non-native mode, and worse causes a normal
8071 * modeset to fail.
8072 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8073 */
8074 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008075 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008076 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008077
8078 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008079 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008080 return 0;
8081
8082err_unpin:
8083 intel_unpin_fb_obj(obj);
8084err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008085 return ret;
8086}
8087
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008088static int intel_gen7_queue_flip(struct drm_device *dev,
8089 struct drm_crtc *crtc,
8090 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008091 struct drm_i915_gem_object *obj,
8092 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008093{
8094 struct drm_i915_private *dev_priv = dev->dev_private;
8095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008096 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008097 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008098 int len, ret;
8099
8100 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008101 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008102 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008103
8104 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8105 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008106 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008107
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008108 switch(intel_crtc->plane) {
8109 case PLANE_A:
8110 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8111 break;
8112 case PLANE_B:
8113 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8114 break;
8115 case PLANE_C:
8116 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8117 break;
8118 default:
8119 WARN_ONCE(1, "unknown plane in flip command\n");
8120 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008121 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008122 }
8123
Chris Wilsonffe74d72013-08-26 20:58:12 +01008124 len = 4;
8125 if (ring->id == RCS)
8126 len += 6;
8127
8128 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008129 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008130 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008131
Chris Wilsonffe74d72013-08-26 20:58:12 +01008132 /* Unmask the flip-done completion message. Note that the bspec says that
8133 * we should do this for both the BCS and RCS, and that we must not unmask
8134 * more than one flip event at any time (or ensure that one flip message
8135 * can be sent by waiting for flip-done prior to queueing new flips).
8136 * Experimentation says that BCS works despite DERRMR masking all
8137 * flip-done completion events and that unmasking all planes at once
8138 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8139 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8140 */
8141 if (ring->id == RCS) {
8142 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8143 intel_ring_emit(ring, DERRMR);
8144 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8145 DERRMR_PIPEB_PRI_FLIP_DONE |
8146 DERRMR_PIPEC_PRI_FLIP_DONE));
8147 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8148 intel_ring_emit(ring, DERRMR);
8149 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8150 }
8151
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008152 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008153 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008154 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008155 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008156
8157 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008158 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008159 return 0;
8160
8161err_unpin:
8162 intel_unpin_fb_obj(obj);
8163err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008164 return ret;
8165}
8166
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008167static int intel_default_queue_flip(struct drm_device *dev,
8168 struct drm_crtc *crtc,
8169 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008170 struct drm_i915_gem_object *obj,
8171 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008172{
8173 return -ENODEV;
8174}
8175
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008176static int intel_crtc_page_flip(struct drm_crtc *crtc,
8177 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008178 struct drm_pending_vblank_event *event,
8179 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008180{
8181 struct drm_device *dev = crtc->dev;
8182 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008183 struct drm_framebuffer *old_fb = crtc->fb;
8184 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8186 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008187 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008188 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008189
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008190 /* Can't change pixel format via MI display flips. */
8191 if (fb->pixel_format != crtc->fb->pixel_format)
8192 return -EINVAL;
8193
8194 /*
8195 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8196 * Note that pitch changes could also affect these register.
8197 */
8198 if (INTEL_INFO(dev)->gen > 3 &&
8199 (fb->offsets[0] != crtc->fb->offsets[0] ||
8200 fb->pitches[0] != crtc->fb->pitches[0]))
8201 return -EINVAL;
8202
Daniel Vetterb14c5672013-09-19 12:18:32 +02008203 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008204 if (work == NULL)
8205 return -ENOMEM;
8206
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008207 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008208 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008209 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008210 INIT_WORK(&work->work, intel_unpin_work_fn);
8211
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008212 ret = drm_vblank_get(dev, intel_crtc->pipe);
8213 if (ret)
8214 goto free_work;
8215
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008216 /* We borrow the event spin lock for protecting unpin_work */
8217 spin_lock_irqsave(&dev->event_lock, flags);
8218 if (intel_crtc->unpin_work) {
8219 spin_unlock_irqrestore(&dev->event_lock, flags);
8220 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008221 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008222
8223 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008224 return -EBUSY;
8225 }
8226 intel_crtc->unpin_work = work;
8227 spin_unlock_irqrestore(&dev->event_lock, flags);
8228
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008229 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8230 flush_workqueue(dev_priv->wq);
8231
Chris Wilson79158102012-05-23 11:13:58 +01008232 ret = i915_mutex_lock_interruptible(dev);
8233 if (ret)
8234 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008235
Jesse Barnes75dfca82010-02-10 15:09:44 -08008236 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008237 drm_gem_object_reference(&work->old_fb_obj->base);
8238 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008239
8240 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008241
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008242 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008243
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008244 work->enable_stall_check = true;
8245
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008246 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008247 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008248
Keith Packarded8d1972013-07-22 18:49:58 -07008249 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008250 if (ret)
8251 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008252
Chris Wilson7782de32011-07-08 12:22:41 +01008253 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008254 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008255 mutex_unlock(&dev->struct_mutex);
8256
Jesse Barnese5510fa2010-07-01 16:48:37 -07008257 trace_i915_flip_request(intel_crtc->plane, obj);
8258
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008259 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008260
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008261cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008262 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008263 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008264 drm_gem_object_unreference(&work->old_fb_obj->base);
8265 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008266 mutex_unlock(&dev->struct_mutex);
8267
Chris Wilson79158102012-05-23 11:13:58 +01008268cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008269 spin_lock_irqsave(&dev->event_lock, flags);
8270 intel_crtc->unpin_work = NULL;
8271 spin_unlock_irqrestore(&dev->event_lock, flags);
8272
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008273 drm_vblank_put(dev, intel_crtc->pipe);
8274free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008275 kfree(work);
8276
8277 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008278}
8279
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008280static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008281 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8282 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008283};
8284
Daniel Vetter50f56112012-07-02 09:35:43 +02008285static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8286 struct drm_crtc *crtc)
8287{
8288 struct drm_device *dev;
8289 struct drm_crtc *tmp;
8290 int crtc_mask = 1;
8291
8292 WARN(!crtc, "checking null crtc?\n");
8293
8294 dev = crtc->dev;
8295
8296 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8297 if (tmp == crtc)
8298 break;
8299 crtc_mask <<= 1;
8300 }
8301
8302 if (encoder->possible_crtcs & crtc_mask)
8303 return true;
8304 return false;
8305}
8306
Daniel Vetter9a935852012-07-05 22:34:27 +02008307/**
8308 * intel_modeset_update_staged_output_state
8309 *
8310 * Updates the staged output configuration state, e.g. after we've read out the
8311 * current hw state.
8312 */
8313static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8314{
8315 struct intel_encoder *encoder;
8316 struct intel_connector *connector;
8317
8318 list_for_each_entry(connector, &dev->mode_config.connector_list,
8319 base.head) {
8320 connector->new_encoder =
8321 to_intel_encoder(connector->base.encoder);
8322 }
8323
8324 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8325 base.head) {
8326 encoder->new_crtc =
8327 to_intel_crtc(encoder->base.crtc);
8328 }
8329}
8330
8331/**
8332 * intel_modeset_commit_output_state
8333 *
8334 * This function copies the stage display pipe configuration to the real one.
8335 */
8336static void intel_modeset_commit_output_state(struct drm_device *dev)
8337{
8338 struct intel_encoder *encoder;
8339 struct intel_connector *connector;
8340
8341 list_for_each_entry(connector, &dev->mode_config.connector_list,
8342 base.head) {
8343 connector->base.encoder = &connector->new_encoder->base;
8344 }
8345
8346 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8347 base.head) {
8348 encoder->base.crtc = &encoder->new_crtc->base;
8349 }
8350}
8351
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008352static void
8353connected_sink_compute_bpp(struct intel_connector * connector,
8354 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008355{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008356 int bpp = pipe_config->pipe_bpp;
8357
8358 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8359 connector->base.base.id,
8360 drm_get_connector_name(&connector->base));
8361
8362 /* Don't use an invalid EDID bpc value */
8363 if (connector->base.display_info.bpc &&
8364 connector->base.display_info.bpc * 3 < bpp) {
8365 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8366 bpp, connector->base.display_info.bpc*3);
8367 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8368 }
8369
8370 /* Clamp bpp to 8 on screens without EDID 1.4 */
8371 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8372 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8373 bpp);
8374 pipe_config->pipe_bpp = 24;
8375 }
8376}
8377
8378static int
8379compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8380 struct drm_framebuffer *fb,
8381 struct intel_crtc_config *pipe_config)
8382{
8383 struct drm_device *dev = crtc->base.dev;
8384 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008385 int bpp;
8386
Daniel Vetterd42264b2013-03-28 16:38:08 +01008387 switch (fb->pixel_format) {
8388 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008389 bpp = 8*3; /* since we go through a colormap */
8390 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008391 case DRM_FORMAT_XRGB1555:
8392 case DRM_FORMAT_ARGB1555:
8393 /* checked in intel_framebuffer_init already */
8394 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8395 return -EINVAL;
8396 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008397 bpp = 6*3; /* min is 18bpp */
8398 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008399 case DRM_FORMAT_XBGR8888:
8400 case DRM_FORMAT_ABGR8888:
8401 /* checked in intel_framebuffer_init already */
8402 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8403 return -EINVAL;
8404 case DRM_FORMAT_XRGB8888:
8405 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008406 bpp = 8*3;
8407 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008408 case DRM_FORMAT_XRGB2101010:
8409 case DRM_FORMAT_ARGB2101010:
8410 case DRM_FORMAT_XBGR2101010:
8411 case DRM_FORMAT_ABGR2101010:
8412 /* checked in intel_framebuffer_init already */
8413 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008414 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008415 bpp = 10*3;
8416 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008417 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008418 default:
8419 DRM_DEBUG_KMS("unsupported depth\n");
8420 return -EINVAL;
8421 }
8422
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008423 pipe_config->pipe_bpp = bpp;
8424
8425 /* Clamp display bpp to EDID value */
8426 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008427 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008428 if (!connector->new_encoder ||
8429 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008430 continue;
8431
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008432 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008433 }
8434
8435 return bpp;
8436}
8437
Daniel Vetter644db712013-09-19 14:53:58 +02008438static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8439{
8440 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8441 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008442 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008443 mode->crtc_hdisplay, mode->crtc_hsync_start,
8444 mode->crtc_hsync_end, mode->crtc_htotal,
8445 mode->crtc_vdisplay, mode->crtc_vsync_start,
8446 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8447}
8448
Daniel Vetterc0b03412013-05-28 12:05:54 +02008449static void intel_dump_pipe_config(struct intel_crtc *crtc,
8450 struct intel_crtc_config *pipe_config,
8451 const char *context)
8452{
8453 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8454 context, pipe_name(crtc->pipe));
8455
8456 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8457 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8458 pipe_config->pipe_bpp, pipe_config->dither);
8459 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8460 pipe_config->has_pch_encoder,
8461 pipe_config->fdi_lanes,
8462 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8463 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8464 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008465 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8466 pipe_config->has_dp_encoder,
8467 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8468 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8469 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008470 DRM_DEBUG_KMS("requested mode:\n");
8471 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8472 DRM_DEBUG_KMS("adjusted mode:\n");
8473 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008474 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008475 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008476 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8477 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008478 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8479 pipe_config->gmch_pfit.control,
8480 pipe_config->gmch_pfit.pgm_ratios,
8481 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008482 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008483 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008484 pipe_config->pch_pfit.size,
8485 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008486 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008487 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008488}
8489
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008490static bool check_encoder_cloning(struct drm_crtc *crtc)
8491{
8492 int num_encoders = 0;
8493 bool uncloneable_encoders = false;
8494 struct intel_encoder *encoder;
8495
8496 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8497 base.head) {
8498 if (&encoder->new_crtc->base != crtc)
8499 continue;
8500
8501 num_encoders++;
8502 if (!encoder->cloneable)
8503 uncloneable_encoders = true;
8504 }
8505
8506 return !(num_encoders > 1 && uncloneable_encoders);
8507}
8508
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008509static struct intel_crtc_config *
8510intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008511 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008512 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008513{
8514 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008515 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008516 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008517 int plane_bpp, ret = -EINVAL;
8518 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008519
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008520 if (!check_encoder_cloning(crtc)) {
8521 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8522 return ERR_PTR(-EINVAL);
8523 }
8524
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008525 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8526 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008527 return ERR_PTR(-ENOMEM);
8528
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008529 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8530 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008531
Daniel Vettere143a212013-07-04 12:01:15 +02008532 pipe_config->cpu_transcoder =
8533 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008534 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008535
Imre Deak2960bc92013-07-30 13:36:32 +03008536 /*
8537 * Sanitize sync polarity flags based on requested ones. If neither
8538 * positive or negative polarity is requested, treat this as meaning
8539 * negative polarity.
8540 */
8541 if (!(pipe_config->adjusted_mode.flags &
8542 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8543 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8544
8545 if (!(pipe_config->adjusted_mode.flags &
8546 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8547 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8548
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008549 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8550 * plane pixel format and any sink constraints into account. Returns the
8551 * source plane bpp so that dithering can be selected on mismatches
8552 * after encoders and crtc also have had their say. */
8553 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8554 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008555 if (plane_bpp < 0)
8556 goto fail;
8557
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008558 /*
8559 * Determine the real pipe dimensions. Note that stereo modes can
8560 * increase the actual pipe size due to the frame doubling and
8561 * insertion of additional space for blanks between the frame. This
8562 * is stored in the crtc timings. We use the requested mode to do this
8563 * computation to clearly distinguish it from the adjusted mode, which
8564 * can be changed by the connectors in the below retry loop.
8565 */
8566 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8567 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8568 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8569
Daniel Vettere29c22c2013-02-21 00:00:16 +01008570encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008571 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008572 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008573 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008574
Daniel Vetter135c81b2013-07-21 21:37:09 +02008575 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008576 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008577
Daniel Vetter7758a112012-07-08 19:40:39 +02008578 /* Pass our mode to the connectors and the CRTC to give them a chance to
8579 * adjust it according to limitations or connector properties, and also
8580 * a chance to reject the mode entirely.
8581 */
8582 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8583 base.head) {
8584
8585 if (&encoder->new_crtc->base != crtc)
8586 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008587
Daniel Vetterefea6e82013-07-21 21:36:59 +02008588 if (!(encoder->compute_config(encoder, pipe_config))) {
8589 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008590 goto fail;
8591 }
8592 }
8593
Daniel Vetterff9a6752013-06-01 17:16:21 +02008594 /* Set default port clock if not overwritten by the encoder. Needs to be
8595 * done afterwards in case the encoder adjusts the mode. */
8596 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008597 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8598 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008599
Daniel Vettera43f6e02013-06-07 23:10:32 +02008600 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008601 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008602 DRM_DEBUG_KMS("CRTC fixup failed\n");
8603 goto fail;
8604 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008605
8606 if (ret == RETRY) {
8607 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8608 ret = -EINVAL;
8609 goto fail;
8610 }
8611
8612 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8613 retry = false;
8614 goto encoder_retry;
8615 }
8616
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008617 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8618 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8619 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8620
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008621 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008622fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008623 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008624 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008625}
8626
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008627/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8628 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8629static void
8630intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8631 unsigned *prepare_pipes, unsigned *disable_pipes)
8632{
8633 struct intel_crtc *intel_crtc;
8634 struct drm_device *dev = crtc->dev;
8635 struct intel_encoder *encoder;
8636 struct intel_connector *connector;
8637 struct drm_crtc *tmp_crtc;
8638
8639 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8640
8641 /* Check which crtcs have changed outputs connected to them, these need
8642 * to be part of the prepare_pipes mask. We don't (yet) support global
8643 * modeset across multiple crtcs, so modeset_pipes will only have one
8644 * bit set at most. */
8645 list_for_each_entry(connector, &dev->mode_config.connector_list,
8646 base.head) {
8647 if (connector->base.encoder == &connector->new_encoder->base)
8648 continue;
8649
8650 if (connector->base.encoder) {
8651 tmp_crtc = connector->base.encoder->crtc;
8652
8653 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8654 }
8655
8656 if (connector->new_encoder)
8657 *prepare_pipes |=
8658 1 << connector->new_encoder->new_crtc->pipe;
8659 }
8660
8661 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8662 base.head) {
8663 if (encoder->base.crtc == &encoder->new_crtc->base)
8664 continue;
8665
8666 if (encoder->base.crtc) {
8667 tmp_crtc = encoder->base.crtc;
8668
8669 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8670 }
8671
8672 if (encoder->new_crtc)
8673 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8674 }
8675
8676 /* Check for any pipes that will be fully disabled ... */
8677 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8678 base.head) {
8679 bool used = false;
8680
8681 /* Don't try to disable disabled crtcs. */
8682 if (!intel_crtc->base.enabled)
8683 continue;
8684
8685 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8686 base.head) {
8687 if (encoder->new_crtc == intel_crtc)
8688 used = true;
8689 }
8690
8691 if (!used)
8692 *disable_pipes |= 1 << intel_crtc->pipe;
8693 }
8694
8695
8696 /* set_mode is also used to update properties on life display pipes. */
8697 intel_crtc = to_intel_crtc(crtc);
8698 if (crtc->enabled)
8699 *prepare_pipes |= 1 << intel_crtc->pipe;
8700
Daniel Vetterb6c51642013-04-12 18:48:43 +02008701 /*
8702 * For simplicity do a full modeset on any pipe where the output routing
8703 * changed. We could be more clever, but that would require us to be
8704 * more careful with calling the relevant encoder->mode_set functions.
8705 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008706 if (*prepare_pipes)
8707 *modeset_pipes = *prepare_pipes;
8708
8709 /* ... and mask these out. */
8710 *modeset_pipes &= ~(*disable_pipes);
8711 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008712
8713 /*
8714 * HACK: We don't (yet) fully support global modesets. intel_set_config
8715 * obies this rule, but the modeset restore mode of
8716 * intel_modeset_setup_hw_state does not.
8717 */
8718 *modeset_pipes &= 1 << intel_crtc->pipe;
8719 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008720
8721 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8722 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008723}
8724
Daniel Vetterea9d7582012-07-10 10:42:52 +02008725static bool intel_crtc_in_use(struct drm_crtc *crtc)
8726{
8727 struct drm_encoder *encoder;
8728 struct drm_device *dev = crtc->dev;
8729
8730 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8731 if (encoder->crtc == crtc)
8732 return true;
8733
8734 return false;
8735}
8736
8737static void
8738intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8739{
8740 struct intel_encoder *intel_encoder;
8741 struct intel_crtc *intel_crtc;
8742 struct drm_connector *connector;
8743
8744 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8745 base.head) {
8746 if (!intel_encoder->base.crtc)
8747 continue;
8748
8749 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8750
8751 if (prepare_pipes & (1 << intel_crtc->pipe))
8752 intel_encoder->connectors_active = false;
8753 }
8754
8755 intel_modeset_commit_output_state(dev);
8756
8757 /* Update computed state. */
8758 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8759 base.head) {
8760 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8761 }
8762
8763 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8764 if (!connector->encoder || !connector->encoder->crtc)
8765 continue;
8766
8767 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8768
8769 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008770 struct drm_property *dpms_property =
8771 dev->mode_config.dpms_property;
8772
Daniel Vetterea9d7582012-07-10 10:42:52 +02008773 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008774 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008775 dpms_property,
8776 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008777
8778 intel_encoder = to_intel_encoder(connector->encoder);
8779 intel_encoder->connectors_active = true;
8780 }
8781 }
8782
8783}
8784
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008785static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008786{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008787 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008788
8789 if (clock1 == clock2)
8790 return true;
8791
8792 if (!clock1 || !clock2)
8793 return false;
8794
8795 diff = abs(clock1 - clock2);
8796
8797 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8798 return true;
8799
8800 return false;
8801}
8802
Daniel Vetter25c5b262012-07-08 22:08:04 +02008803#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8804 list_for_each_entry((intel_crtc), \
8805 &(dev)->mode_config.crtc_list, \
8806 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008807 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008808
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008809static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008810intel_pipe_config_compare(struct drm_device *dev,
8811 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008812 struct intel_crtc_config *pipe_config)
8813{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008814#define PIPE_CONF_CHECK_X(name) \
8815 if (current_config->name != pipe_config->name) { \
8816 DRM_ERROR("mismatch in " #name " " \
8817 "(expected 0x%08x, found 0x%08x)\n", \
8818 current_config->name, \
8819 pipe_config->name); \
8820 return false; \
8821 }
8822
Daniel Vetter08a24032013-04-19 11:25:34 +02008823#define PIPE_CONF_CHECK_I(name) \
8824 if (current_config->name != pipe_config->name) { \
8825 DRM_ERROR("mismatch in " #name " " \
8826 "(expected %i, found %i)\n", \
8827 current_config->name, \
8828 pipe_config->name); \
8829 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008830 }
8831
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008832#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8833 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008834 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008835 "(expected %i, found %i)\n", \
8836 current_config->name & (mask), \
8837 pipe_config->name & (mask)); \
8838 return false; \
8839 }
8840
Ville Syrjälä5e550652013-09-06 23:29:07 +03008841#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
8842 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8843 DRM_ERROR("mismatch in " #name " " \
8844 "(expected %i, found %i)\n", \
8845 current_config->name, \
8846 pipe_config->name); \
8847 return false; \
8848 }
8849
Daniel Vetterbb760062013-06-06 14:55:52 +02008850#define PIPE_CONF_QUIRK(quirk) \
8851 ((current_config->quirks | pipe_config->quirks) & (quirk))
8852
Daniel Vettereccb1402013-05-22 00:50:22 +02008853 PIPE_CONF_CHECK_I(cpu_transcoder);
8854
Daniel Vetter08a24032013-04-19 11:25:34 +02008855 PIPE_CONF_CHECK_I(has_pch_encoder);
8856 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008857 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8858 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8859 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8860 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8861 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008862
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008863 PIPE_CONF_CHECK_I(has_dp_encoder);
8864 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
8865 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
8866 PIPE_CONF_CHECK_I(dp_m_n.link_m);
8867 PIPE_CONF_CHECK_I(dp_m_n.link_n);
8868 PIPE_CONF_CHECK_I(dp_m_n.tu);
8869
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008870 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8871 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8872 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8873 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8874 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8875 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8876
8877 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8878 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8879 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8880 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8881 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8882 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8883
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008884 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008885
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008886 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8887 DRM_MODE_FLAG_INTERLACE);
8888
Daniel Vetterbb760062013-06-06 14:55:52 +02008889 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8890 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8891 DRM_MODE_FLAG_PHSYNC);
8892 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8893 DRM_MODE_FLAG_NHSYNC);
8894 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8895 DRM_MODE_FLAG_PVSYNC);
8896 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8897 DRM_MODE_FLAG_NVSYNC);
8898 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008899
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008900 PIPE_CONF_CHECK_I(pipe_src_w);
8901 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008902
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008903 PIPE_CONF_CHECK_I(gmch_pfit.control);
8904 /* pfit ratios are autocomputed by the hw on gen4+ */
8905 if (INTEL_INFO(dev)->gen < 4)
8906 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8907 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008908 PIPE_CONF_CHECK_I(pch_pfit.enabled);
8909 if (current_config->pch_pfit.enabled) {
8910 PIPE_CONF_CHECK_I(pch_pfit.pos);
8911 PIPE_CONF_CHECK_I(pch_pfit.size);
8912 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008913
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008914 PIPE_CONF_CHECK_I(ips_enabled);
8915
Ville Syrjälä282740f2013-09-04 18:30:03 +03008916 PIPE_CONF_CHECK_I(double_wide);
8917
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008918 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008919 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008920 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008921 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8922 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008923
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008924 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
8925 PIPE_CONF_CHECK_I(pipe_bpp);
8926
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008927 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01008928 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008929 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
8930 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03008931
Daniel Vetter66e985c2013-06-05 13:34:20 +02008932#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008933#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008934#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03008935#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02008936#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008937
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008938 return true;
8939}
8940
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008941static void
8942check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008943{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008944 struct intel_connector *connector;
8945
8946 list_for_each_entry(connector, &dev->mode_config.connector_list,
8947 base.head) {
8948 /* This also checks the encoder/connector hw state with the
8949 * ->get_hw_state callbacks. */
8950 intel_connector_check_state(connector);
8951
8952 WARN(&connector->new_encoder->base != connector->base.encoder,
8953 "connector's staged encoder doesn't match current encoder\n");
8954 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008955}
8956
8957static void
8958check_encoder_state(struct drm_device *dev)
8959{
8960 struct intel_encoder *encoder;
8961 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008962
8963 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8964 base.head) {
8965 bool enabled = false;
8966 bool active = false;
8967 enum pipe pipe, tracked_pipe;
8968
8969 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8970 encoder->base.base.id,
8971 drm_get_encoder_name(&encoder->base));
8972
8973 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8974 "encoder's stage crtc doesn't match current crtc\n");
8975 WARN(encoder->connectors_active && !encoder->base.crtc,
8976 "encoder's active_connectors set, but no crtc\n");
8977
8978 list_for_each_entry(connector, &dev->mode_config.connector_list,
8979 base.head) {
8980 if (connector->base.encoder != &encoder->base)
8981 continue;
8982 enabled = true;
8983 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8984 active = true;
8985 }
8986 WARN(!!encoder->base.crtc != enabled,
8987 "encoder's enabled state mismatch "
8988 "(expected %i, found %i)\n",
8989 !!encoder->base.crtc, enabled);
8990 WARN(active && !encoder->base.crtc,
8991 "active encoder with no crtc\n");
8992
8993 WARN(encoder->connectors_active != active,
8994 "encoder's computed active state doesn't match tracked active state "
8995 "(expected %i, found %i)\n", active, encoder->connectors_active);
8996
8997 active = encoder->get_hw_state(encoder, &pipe);
8998 WARN(active != encoder->connectors_active,
8999 "encoder's hw state doesn't match sw tracking "
9000 "(expected %i, found %i)\n",
9001 encoder->connectors_active, active);
9002
9003 if (!encoder->base.crtc)
9004 continue;
9005
9006 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9007 WARN(active && pipe != tracked_pipe,
9008 "active encoder's pipe doesn't match"
9009 "(expected %i, found %i)\n",
9010 tracked_pipe, pipe);
9011
9012 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009013}
9014
9015static void
9016check_crtc_state(struct drm_device *dev)
9017{
9018 drm_i915_private_t *dev_priv = dev->dev_private;
9019 struct intel_crtc *crtc;
9020 struct intel_encoder *encoder;
9021 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009022
9023 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9024 base.head) {
9025 bool enabled = false;
9026 bool active = false;
9027
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009028 memset(&pipe_config, 0, sizeof(pipe_config));
9029
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009030 DRM_DEBUG_KMS("[CRTC:%d]\n",
9031 crtc->base.base.id);
9032
9033 WARN(crtc->active && !crtc->base.enabled,
9034 "active crtc, but not enabled in sw tracking\n");
9035
9036 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9037 base.head) {
9038 if (encoder->base.crtc != &crtc->base)
9039 continue;
9040 enabled = true;
9041 if (encoder->connectors_active)
9042 active = true;
9043 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009044
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009045 WARN(active != crtc->active,
9046 "crtc's computed active state doesn't match tracked active state "
9047 "(expected %i, found %i)\n", active, crtc->active);
9048 WARN(enabled != crtc->base.enabled,
9049 "crtc's computed enabled state doesn't match tracked enabled state "
9050 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9051
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009052 active = dev_priv->display.get_pipe_config(crtc,
9053 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009054
9055 /* hw state is inconsistent with the pipe A quirk */
9056 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9057 active = crtc->active;
9058
Daniel Vetter6c49f242013-06-06 12:45:25 +02009059 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9060 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009061 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009062 if (encoder->base.crtc != &crtc->base)
9063 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009064 if (encoder->get_config &&
9065 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009066 encoder->get_config(encoder, &pipe_config);
9067 }
9068
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009069 WARN(crtc->active != active,
9070 "crtc active state doesn't match with hw state "
9071 "(expected %i, found %i)\n", crtc->active, active);
9072
Daniel Vetterc0b03412013-05-28 12:05:54 +02009073 if (active &&
9074 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9075 WARN(1, "pipe state doesn't match!\n");
9076 intel_dump_pipe_config(crtc, &pipe_config,
9077 "[hw state]");
9078 intel_dump_pipe_config(crtc, &crtc->config,
9079 "[sw state]");
9080 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009081 }
9082}
9083
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009084static void
9085check_shared_dpll_state(struct drm_device *dev)
9086{
9087 drm_i915_private_t *dev_priv = dev->dev_private;
9088 struct intel_crtc *crtc;
9089 struct intel_dpll_hw_state dpll_hw_state;
9090 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009091
9092 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9093 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9094 int enabled_crtcs = 0, active_crtcs = 0;
9095 bool active;
9096
9097 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9098
9099 DRM_DEBUG_KMS("%s\n", pll->name);
9100
9101 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9102
9103 WARN(pll->active > pll->refcount,
9104 "more active pll users than references: %i vs %i\n",
9105 pll->active, pll->refcount);
9106 WARN(pll->active && !pll->on,
9107 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009108 WARN(pll->on && !pll->active,
9109 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009110 WARN(pll->on != active,
9111 "pll on state mismatch (expected %i, found %i)\n",
9112 pll->on, active);
9113
9114 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9115 base.head) {
9116 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9117 enabled_crtcs++;
9118 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9119 active_crtcs++;
9120 }
9121 WARN(pll->active != active_crtcs,
9122 "pll active crtcs mismatch (expected %i, found %i)\n",
9123 pll->active, active_crtcs);
9124 WARN(pll->refcount != enabled_crtcs,
9125 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9126 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009127
9128 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9129 sizeof(dpll_hw_state)),
9130 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009131 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009132}
9133
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009134void
9135intel_modeset_check_state(struct drm_device *dev)
9136{
9137 check_connector_state(dev);
9138 check_encoder_state(dev);
9139 check_crtc_state(dev);
9140 check_shared_dpll_state(dev);
9141}
9142
Ville Syrjälä18442d02013-09-13 16:00:08 +03009143void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9144 int dotclock)
9145{
9146 /*
9147 * FDI already provided one idea for the dotclock.
9148 * Yell if the encoder disagrees.
9149 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009150 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009151 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009152 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009153}
9154
Daniel Vetterf30da182013-04-11 20:22:50 +02009155static int __intel_set_mode(struct drm_crtc *crtc,
9156 struct drm_display_mode *mode,
9157 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009158{
9159 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009160 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009161 struct drm_display_mode *saved_mode, *saved_hwmode;
9162 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009163 struct intel_crtc *intel_crtc;
9164 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009165 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009166
Daniel Vettera1e22652013-09-21 00:35:38 +02009167 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009168 if (!saved_mode)
9169 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009170 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009171
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009172 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009173 &prepare_pipes, &disable_pipes);
9174
Tim Gardner3ac18232012-12-07 07:54:26 -07009175 *saved_hwmode = crtc->hwmode;
9176 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009177
Daniel Vetter25c5b262012-07-08 22:08:04 +02009178 /* Hack: Because we don't (yet) support global modeset on multiple
9179 * crtcs, we don't keep track of the new mode for more than one crtc.
9180 * Hence simply check whether any bit is set in modeset_pipes in all the
9181 * pieces of code that are not yet converted to deal with mutliple crtcs
9182 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009183 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009184 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009185 if (IS_ERR(pipe_config)) {
9186 ret = PTR_ERR(pipe_config);
9187 pipe_config = NULL;
9188
Tim Gardner3ac18232012-12-07 07:54:26 -07009189 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009190 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009191 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9192 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009193 }
9194
Daniel Vetter460da9162013-03-27 00:44:51 +01009195 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9196 intel_crtc_disable(&intel_crtc->base);
9197
Daniel Vetterea9d7582012-07-10 10:42:52 +02009198 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9199 if (intel_crtc->base.enabled)
9200 dev_priv->display.crtc_disable(&intel_crtc->base);
9201 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009202
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009203 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9204 * to set it here already despite that we pass it down the callchain.
9205 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009206 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009207 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009208 /* mode_set/enable/disable functions rely on a correct pipe
9209 * config. */
9210 to_intel_crtc(crtc)->config = *pipe_config;
9211 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009212
Daniel Vetterea9d7582012-07-10 10:42:52 +02009213 /* Only after disabling all output pipelines that will be changed can we
9214 * update the the output configuration. */
9215 intel_modeset_update_state(dev, prepare_pipes);
9216
Daniel Vetter47fab732012-10-26 10:58:18 +02009217 if (dev_priv->display.modeset_global_resources)
9218 dev_priv->display.modeset_global_resources(dev);
9219
Daniel Vettera6778b32012-07-02 09:56:42 +02009220 /* Set up the DPLL and any encoders state that needs to adjust or depend
9221 * on the DPLL.
9222 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009223 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009224 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009225 x, y, fb);
9226 if (ret)
9227 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009228 }
9229
9230 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009231 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9232 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009233
Daniel Vetter25c5b262012-07-08 22:08:04 +02009234 if (modeset_pipes) {
9235 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009236 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009237
Daniel Vetter25c5b262012-07-08 22:08:04 +02009238 /* Calculate and store various constants which
9239 * are later needed by vblank and swap-completion
9240 * timestamping. They are derived from true hwmode.
9241 */
9242 drm_calc_timestamping_constants(crtc);
9243 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009244
9245 /* FIXME: add subpixel order */
9246done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009247 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009248 crtc->hwmode = *saved_hwmode;
9249 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009250 }
9251
Tim Gardner3ac18232012-12-07 07:54:26 -07009252out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009253 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009254 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009255 return ret;
9256}
9257
Damien Lespiaue7457a92013-08-08 22:28:59 +01009258static int intel_set_mode(struct drm_crtc *crtc,
9259 struct drm_display_mode *mode,
9260 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009261{
9262 int ret;
9263
9264 ret = __intel_set_mode(crtc, mode, x, y, fb);
9265
9266 if (ret == 0)
9267 intel_modeset_check_state(crtc->dev);
9268
9269 return ret;
9270}
9271
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009272void intel_crtc_restore_mode(struct drm_crtc *crtc)
9273{
9274 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9275}
9276
Daniel Vetter25c5b262012-07-08 22:08:04 +02009277#undef for_each_intel_crtc_masked
9278
Daniel Vetterd9e55602012-07-04 22:16:09 +02009279static void intel_set_config_free(struct intel_set_config *config)
9280{
9281 if (!config)
9282 return;
9283
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009284 kfree(config->save_connector_encoders);
9285 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009286 kfree(config);
9287}
9288
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009289static int intel_set_config_save_state(struct drm_device *dev,
9290 struct intel_set_config *config)
9291{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009292 struct drm_encoder *encoder;
9293 struct drm_connector *connector;
9294 int count;
9295
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009296 config->save_encoder_crtcs =
9297 kcalloc(dev->mode_config.num_encoder,
9298 sizeof(struct drm_crtc *), GFP_KERNEL);
9299 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009300 return -ENOMEM;
9301
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009302 config->save_connector_encoders =
9303 kcalloc(dev->mode_config.num_connector,
9304 sizeof(struct drm_encoder *), GFP_KERNEL);
9305 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009306 return -ENOMEM;
9307
9308 /* Copy data. Note that driver private data is not affected.
9309 * Should anything bad happen only the expected state is
9310 * restored, not the drivers personal bookkeeping.
9311 */
9312 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009313 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009314 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009315 }
9316
9317 count = 0;
9318 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009319 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009320 }
9321
9322 return 0;
9323}
9324
9325static void intel_set_config_restore_state(struct drm_device *dev,
9326 struct intel_set_config *config)
9327{
Daniel Vetter9a935852012-07-05 22:34:27 +02009328 struct intel_encoder *encoder;
9329 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009330 int count;
9331
9332 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009333 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9334 encoder->new_crtc =
9335 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009336 }
9337
9338 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009339 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9340 connector->new_encoder =
9341 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009342 }
9343}
9344
Imre Deake3de42b2013-05-03 19:44:07 +02009345static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009346is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009347{
9348 int i;
9349
Chris Wilson2e57f472013-07-17 12:14:40 +01009350 if (set->num_connectors == 0)
9351 return false;
9352
9353 if (WARN_ON(set->connectors == NULL))
9354 return false;
9355
9356 for (i = 0; i < set->num_connectors; i++)
9357 if (set->connectors[i]->encoder &&
9358 set->connectors[i]->encoder->crtc == set->crtc &&
9359 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009360 return true;
9361
9362 return false;
9363}
9364
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009365static void
9366intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9367 struct intel_set_config *config)
9368{
9369
9370 /* We should be able to check here if the fb has the same properties
9371 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009372 if (is_crtc_connector_off(set)) {
9373 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009374 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009375 /* If we have no fb then treat it as a full mode set */
9376 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009377 struct intel_crtc *intel_crtc =
9378 to_intel_crtc(set->crtc);
9379
9380 if (intel_crtc->active && i915_fastboot) {
9381 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9382 config->fb_changed = true;
9383 } else {
9384 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9385 config->mode_changed = true;
9386 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009387 } else if (set->fb == NULL) {
9388 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009389 } else if (set->fb->pixel_format !=
9390 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009391 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009392 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009393 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009394 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009395 }
9396
Daniel Vetter835c5872012-07-10 18:11:08 +02009397 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009398 config->fb_changed = true;
9399
9400 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9401 DRM_DEBUG_KMS("modes are different, full mode set\n");
9402 drm_mode_debug_printmodeline(&set->crtc->mode);
9403 drm_mode_debug_printmodeline(set->mode);
9404 config->mode_changed = true;
9405 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009406
9407 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9408 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009409}
9410
Daniel Vetter2e431052012-07-04 22:42:15 +02009411static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009412intel_modeset_stage_output_state(struct drm_device *dev,
9413 struct drm_mode_set *set,
9414 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009415{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009416 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009417 struct intel_connector *connector;
9418 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009419 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009420
Damien Lespiau9abdda72013-02-13 13:29:23 +00009421 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009422 * of connectors. For paranoia, double-check this. */
9423 WARN_ON(!set->fb && (set->num_connectors != 0));
9424 WARN_ON(set->fb && (set->num_connectors == 0));
9425
Daniel Vetter9a935852012-07-05 22:34:27 +02009426 list_for_each_entry(connector, &dev->mode_config.connector_list,
9427 base.head) {
9428 /* Otherwise traverse passed in connector list and get encoders
9429 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009430 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009431 if (set->connectors[ro] == &connector->base) {
9432 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009433 break;
9434 }
9435 }
9436
Daniel Vetter9a935852012-07-05 22:34:27 +02009437 /* If we disable the crtc, disable all its connectors. Also, if
9438 * the connector is on the changing crtc but not on the new
9439 * connector list, disable it. */
9440 if ((!set->fb || ro == set->num_connectors) &&
9441 connector->base.encoder &&
9442 connector->base.encoder->crtc == set->crtc) {
9443 connector->new_encoder = NULL;
9444
9445 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9446 connector->base.base.id,
9447 drm_get_connector_name(&connector->base));
9448 }
9449
9450
9451 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009452 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009453 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009454 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009455 }
9456 /* connector->new_encoder is now updated for all connectors. */
9457
9458 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009459 list_for_each_entry(connector, &dev->mode_config.connector_list,
9460 base.head) {
9461 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009462 continue;
9463
Daniel Vetter9a935852012-07-05 22:34:27 +02009464 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009465
9466 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009467 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009468 new_crtc = set->crtc;
9469 }
9470
9471 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009472 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9473 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009474 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009475 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009476 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9477
9478 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9479 connector->base.base.id,
9480 drm_get_connector_name(&connector->base),
9481 new_crtc->base.id);
9482 }
9483
9484 /* Check for any encoders that needs to be disabled. */
9485 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9486 base.head) {
9487 list_for_each_entry(connector,
9488 &dev->mode_config.connector_list,
9489 base.head) {
9490 if (connector->new_encoder == encoder) {
9491 WARN_ON(!connector->new_encoder->new_crtc);
9492
9493 goto next_encoder;
9494 }
9495 }
9496 encoder->new_crtc = NULL;
9497next_encoder:
9498 /* Only now check for crtc changes so we don't miss encoders
9499 * that will be disabled. */
9500 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009501 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009502 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009503 }
9504 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009505 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009506
Daniel Vetter2e431052012-07-04 22:42:15 +02009507 return 0;
9508}
9509
9510static int intel_crtc_set_config(struct drm_mode_set *set)
9511{
9512 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009513 struct drm_mode_set save_set;
9514 struct intel_set_config *config;
9515 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009516
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009517 BUG_ON(!set);
9518 BUG_ON(!set->crtc);
9519 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009520
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009521 /* Enforce sane interface api - has been abused by the fb helper. */
9522 BUG_ON(!set->mode && set->fb);
9523 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009524
Daniel Vetter2e431052012-07-04 22:42:15 +02009525 if (set->fb) {
9526 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9527 set->crtc->base.id, set->fb->base.id,
9528 (int)set->num_connectors, set->x, set->y);
9529 } else {
9530 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009531 }
9532
9533 dev = set->crtc->dev;
9534
9535 ret = -ENOMEM;
9536 config = kzalloc(sizeof(*config), GFP_KERNEL);
9537 if (!config)
9538 goto out_config;
9539
9540 ret = intel_set_config_save_state(dev, config);
9541 if (ret)
9542 goto out_config;
9543
9544 save_set.crtc = set->crtc;
9545 save_set.mode = &set->crtc->mode;
9546 save_set.x = set->crtc->x;
9547 save_set.y = set->crtc->y;
9548 save_set.fb = set->crtc->fb;
9549
9550 /* Compute whether we need a full modeset, only an fb base update or no
9551 * change at all. In the future we might also check whether only the
9552 * mode changed, e.g. for LVDS where we only change the panel fitter in
9553 * such cases. */
9554 intel_set_config_compute_mode_changes(set, config);
9555
Daniel Vetter9a935852012-07-05 22:34:27 +02009556 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009557 if (ret)
9558 goto fail;
9559
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009560 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009561 ret = intel_set_mode(set->crtc, set->mode,
9562 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009563 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009564 intel_crtc_wait_for_pending_flips(set->crtc);
9565
Daniel Vetter4f660f42012-07-02 09:47:37 +02009566 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009567 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009568 }
9569
Chris Wilson2d05eae2013-05-03 17:36:25 +01009570 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009571 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9572 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009573fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009574 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009575
Chris Wilson2d05eae2013-05-03 17:36:25 +01009576 /* Try to restore the config */
9577 if (config->mode_changed &&
9578 intel_set_mode(save_set.crtc, save_set.mode,
9579 save_set.x, save_set.y, save_set.fb))
9580 DRM_ERROR("failed to restore config after modeset failure\n");
9581 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009582
Daniel Vetterd9e55602012-07-04 22:16:09 +02009583out_config:
9584 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009585 return ret;
9586}
9587
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009588static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009589 .cursor_set = intel_crtc_cursor_set,
9590 .cursor_move = intel_crtc_cursor_move,
9591 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009592 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009593 .destroy = intel_crtc_destroy,
9594 .page_flip = intel_crtc_page_flip,
9595};
9596
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009597static void intel_cpu_pll_init(struct drm_device *dev)
9598{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009599 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009600 intel_ddi_pll_init(dev);
9601}
9602
Daniel Vetter53589012013-06-05 13:34:16 +02009603static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9604 struct intel_shared_dpll *pll,
9605 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009606{
Daniel Vetter53589012013-06-05 13:34:16 +02009607 uint32_t val;
9608
9609 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009610 hw_state->dpll = val;
9611 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9612 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02009613
9614 return val & DPLL_VCO_ENABLE;
9615}
9616
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009617static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9618 struct intel_shared_dpll *pll)
9619{
9620 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9621 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9622}
9623
Daniel Vettere7b903d2013-06-05 13:34:14 +02009624static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9625 struct intel_shared_dpll *pll)
9626{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009627 /* PCH refclock must be enabled first */
9628 assert_pch_refclk_enabled(dev_priv);
9629
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009630 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9631
9632 /* Wait for the clocks to stabilize. */
9633 POSTING_READ(PCH_DPLL(pll->id));
9634 udelay(150);
9635
9636 /* The pixel multiplier can only be updated once the
9637 * DPLL is enabled and the clocks are stable.
9638 *
9639 * So write it again.
9640 */
9641 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9642 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009643 udelay(200);
9644}
9645
9646static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9647 struct intel_shared_dpll *pll)
9648{
9649 struct drm_device *dev = dev_priv->dev;
9650 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009651
9652 /* Make sure no transcoder isn't still depending on us. */
9653 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9654 if (intel_crtc_to_shared_dpll(crtc) == pll)
9655 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9656 }
9657
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009658 I915_WRITE(PCH_DPLL(pll->id), 0);
9659 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009660 udelay(200);
9661}
9662
Daniel Vetter46edb022013-06-05 13:34:12 +02009663static char *ibx_pch_dpll_names[] = {
9664 "PCH DPLL A",
9665 "PCH DPLL B",
9666};
9667
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009668static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009669{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009670 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009671 int i;
9672
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009673 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009674
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009675 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02009676 dev_priv->shared_dplls[i].id = i;
9677 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009678 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009679 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9680 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009681 dev_priv->shared_dplls[i].get_hw_state =
9682 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009683 }
9684}
9685
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009686static void intel_shared_dpll_init(struct drm_device *dev)
9687{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009688 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009689
9690 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9691 ibx_pch_dpll_init(dev);
9692 else
9693 dev_priv->num_shared_dpll = 0;
9694
9695 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9696 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9697 dev_priv->num_shared_dpll);
9698}
9699
Hannes Ederb358d0a2008-12-18 21:18:47 +01009700static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009701{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009702 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009703 struct intel_crtc *intel_crtc;
9704 int i;
9705
Daniel Vetter955382f2013-09-19 14:05:45 +02009706 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009707 if (intel_crtc == NULL)
9708 return;
9709
9710 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9711
9712 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009713 for (i = 0; i < 256; i++) {
9714 intel_crtc->lut_r[i] = i;
9715 intel_crtc->lut_g[i] = i;
9716 intel_crtc->lut_b[i] = i;
9717 }
9718
Jesse Barnes80824002009-09-10 15:28:06 -07009719 /* Swap pipes & planes for FBC on pre-965 */
9720 intel_crtc->pipe = pipe;
9721 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009722 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009723 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009724 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009725 }
9726
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009727 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9728 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9729 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9730 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9731
Jesse Barnes79e53942008-11-07 14:24:08 -08009732 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009733}
9734
Carl Worth08d7b3d2009-04-29 14:43:54 -07009735int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009736 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009737{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009738 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009739 struct drm_mode_object *drmmode_obj;
9740 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009741
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009742 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9743 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009744
Daniel Vetterc05422d2009-08-11 16:05:30 +02009745 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9746 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009747
Daniel Vetterc05422d2009-08-11 16:05:30 +02009748 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009749 DRM_ERROR("no such CRTC id\n");
9750 return -EINVAL;
9751 }
9752
Daniel Vetterc05422d2009-08-11 16:05:30 +02009753 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9754 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009755
Daniel Vetterc05422d2009-08-11 16:05:30 +02009756 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009757}
9758
Daniel Vetter66a92782012-07-12 20:08:18 +02009759static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009760{
Daniel Vetter66a92782012-07-12 20:08:18 +02009761 struct drm_device *dev = encoder->base.dev;
9762 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009763 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009764 int entry = 0;
9765
Daniel Vetter66a92782012-07-12 20:08:18 +02009766 list_for_each_entry(source_encoder,
9767 &dev->mode_config.encoder_list, base.head) {
9768
9769 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009770 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009771
9772 /* Intel hw has only one MUX where enocoders could be cloned. */
9773 if (encoder->cloneable && source_encoder->cloneable)
9774 index_mask |= (1 << entry);
9775
Jesse Barnes79e53942008-11-07 14:24:08 -08009776 entry++;
9777 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009778
Jesse Barnes79e53942008-11-07 14:24:08 -08009779 return index_mask;
9780}
9781
Chris Wilson4d302442010-12-14 19:21:29 +00009782static bool has_edp_a(struct drm_device *dev)
9783{
9784 struct drm_i915_private *dev_priv = dev->dev_private;
9785
9786 if (!IS_MOBILE(dev))
9787 return false;
9788
9789 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9790 return false;
9791
9792 if (IS_GEN5(dev) &&
9793 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9794 return false;
9795
9796 return true;
9797}
9798
Jesse Barnes79e53942008-11-07 14:24:08 -08009799static void intel_setup_outputs(struct drm_device *dev)
9800{
Eric Anholt725e30a2009-01-22 13:01:02 -08009801 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009802 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009803 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009804
Daniel Vetterc9093352013-06-06 22:22:47 +02009805 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009806
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009807 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009808 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009809
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009810 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009811 int found;
9812
9813 /* Haswell uses DDI functions to detect digital outputs */
9814 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9815 /* DDI A only supports eDP */
9816 if (found)
9817 intel_ddi_init(dev, PORT_A);
9818
9819 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9820 * register */
9821 found = I915_READ(SFUSE_STRAP);
9822
9823 if (found & SFUSE_STRAP_DDIB_DETECTED)
9824 intel_ddi_init(dev, PORT_B);
9825 if (found & SFUSE_STRAP_DDIC_DETECTED)
9826 intel_ddi_init(dev, PORT_C);
9827 if (found & SFUSE_STRAP_DDID_DETECTED)
9828 intel_ddi_init(dev, PORT_D);
9829 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009830 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009831 dpd_is_edp = intel_dpd_is_edp(dev);
9832
9833 if (has_edp_a(dev))
9834 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009835
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009836 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009837 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009838 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009839 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009840 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009841 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009842 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009843 }
9844
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009845 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009846 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009847
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009848 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009849 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009850
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009851 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009852 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009853
Daniel Vetter270b3042012-10-27 15:52:05 +02009854 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009855 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009856 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309857 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Jesse Barnes6f6005a2013-08-09 09:34:35 -07009858 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
9859 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
9860 PORT_C);
9861 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9862 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
9863 PORT_C);
9864 }
Gajanan Bhat19c03922012-09-27 19:13:07 +05309865
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009866 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009867 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9868 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009869 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9870 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009871 }
Jani Nikula3cfca972013-08-27 15:12:26 +03009872
9873 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +08009874 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009875 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009876
Paulo Zanonie2debe92013-02-18 19:00:27 -03009877 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009878 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009879 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009880 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9881 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009882 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009883 }
Ma Ling27185ae2009-08-24 13:50:23 +08009884
Imre Deake7281ea2013-05-08 13:14:08 +03009885 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009886 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009887 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009888
9889 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009890
Paulo Zanonie2debe92013-02-18 19:00:27 -03009891 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009892 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009893 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009894 }
Ma Ling27185ae2009-08-24 13:50:23 +08009895
Paulo Zanonie2debe92013-02-18 19:00:27 -03009896 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009897
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009898 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9899 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009900 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009901 }
Imre Deake7281ea2013-05-08 13:14:08 +03009902 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009903 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009904 }
Ma Ling27185ae2009-08-24 13:50:23 +08009905
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009906 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009907 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009908 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009909 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009910 intel_dvo_init(dev);
9911
Zhenyu Wang103a1962009-11-27 11:44:36 +08009912 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009913 intel_tv_init(dev);
9914
Chris Wilson4ef69c72010-09-09 15:14:28 +01009915 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9916 encoder->base.possible_crtcs = encoder->crtc_mask;
9917 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009918 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009919 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009920
Paulo Zanonidde86e22012-12-01 12:04:25 -02009921 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009922
9923 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009924}
9925
Chris Wilsonddfe1562013-08-06 17:43:07 +01009926void intel_framebuffer_fini(struct intel_framebuffer *fb)
9927{
9928 drm_framebuffer_cleanup(&fb->base);
9929 drm_gem_object_unreference_unlocked(&fb->obj->base);
9930}
9931
Jesse Barnes79e53942008-11-07 14:24:08 -08009932static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9933{
9934 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009935
Chris Wilsonddfe1562013-08-06 17:43:07 +01009936 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009937 kfree(intel_fb);
9938}
9939
9940static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009941 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009942 unsigned int *handle)
9943{
9944 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009945 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009946
Chris Wilson05394f32010-11-08 19:18:58 +00009947 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009948}
9949
9950static const struct drm_framebuffer_funcs intel_fb_funcs = {
9951 .destroy = intel_user_framebuffer_destroy,
9952 .create_handle = intel_user_framebuffer_create_handle,
9953};
9954
Dave Airlie38651672010-03-30 05:34:13 +00009955int intel_framebuffer_init(struct drm_device *dev,
9956 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009957 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009958 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009959{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009960 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009961 int ret;
9962
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009963 if (obj->tiling_mode == I915_TILING_Y) {
9964 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009965 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009966 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009967
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009968 if (mode_cmd->pitches[0] & 63) {
9969 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9970 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009971 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009972 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009973
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009974 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9975 pitch_limit = 32*1024;
9976 } else if (INTEL_INFO(dev)->gen >= 4) {
9977 if (obj->tiling_mode)
9978 pitch_limit = 16*1024;
9979 else
9980 pitch_limit = 32*1024;
9981 } else if (INTEL_INFO(dev)->gen >= 3) {
9982 if (obj->tiling_mode)
9983 pitch_limit = 8*1024;
9984 else
9985 pitch_limit = 16*1024;
9986 } else
9987 /* XXX DSPC is limited to 4k tiled */
9988 pitch_limit = 8*1024;
9989
9990 if (mode_cmd->pitches[0] > pitch_limit) {
9991 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9992 obj->tiling_mode ? "tiled" : "linear",
9993 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009994 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009995 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009996
9997 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009998 mode_cmd->pitches[0] != obj->stride) {
9999 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10000 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010001 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010002 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010003
Ville Syrjälä57779d02012-10-31 17:50:14 +020010004 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010005 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010006 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010007 case DRM_FORMAT_RGB565:
10008 case DRM_FORMAT_XRGB8888:
10009 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010010 break;
10011 case DRM_FORMAT_XRGB1555:
10012 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010013 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010014 DRM_DEBUG("unsupported pixel format: %s\n",
10015 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010016 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010017 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010018 break;
10019 case DRM_FORMAT_XBGR8888:
10020 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010021 case DRM_FORMAT_XRGB2101010:
10022 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010023 case DRM_FORMAT_XBGR2101010:
10024 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010025 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010026 DRM_DEBUG("unsupported pixel format: %s\n",
10027 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010028 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010029 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010030 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010031 case DRM_FORMAT_YUYV:
10032 case DRM_FORMAT_UYVY:
10033 case DRM_FORMAT_YVYU:
10034 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010035 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010036 DRM_DEBUG("unsupported pixel format: %s\n",
10037 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010038 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010039 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010040 break;
10041 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010042 DRM_DEBUG("unsupported pixel format: %s\n",
10043 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010044 return -EINVAL;
10045 }
10046
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010047 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10048 if (mode_cmd->offsets[0] != 0)
10049 return -EINVAL;
10050
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010051 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10052 intel_fb->obj = obj;
10053
Jesse Barnes79e53942008-11-07 14:24:08 -080010054 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10055 if (ret) {
10056 DRM_ERROR("framebuffer init failed %d\n", ret);
10057 return ret;
10058 }
10059
Jesse Barnes79e53942008-11-07 14:24:08 -080010060 return 0;
10061}
10062
Jesse Barnes79e53942008-11-07 14:24:08 -080010063static struct drm_framebuffer *
10064intel_user_framebuffer_create(struct drm_device *dev,
10065 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010066 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010067{
Chris Wilson05394f32010-11-08 19:18:58 +000010068 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010069
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010070 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10071 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010072 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010073 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010074
Chris Wilsond2dff872011-04-19 08:36:26 +010010075 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010076}
10077
Jesse Barnes79e53942008-11-07 14:24:08 -080010078static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010079 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +000010080 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010081};
10082
Jesse Barnese70236a2009-09-21 10:42:27 -070010083/* Set up chip specific display functions */
10084static void intel_init_display(struct drm_device *dev)
10085{
10086 struct drm_i915_private *dev_priv = dev->dev_private;
10087
Daniel Vetteree9300b2013-06-03 22:40:22 +020010088 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10089 dev_priv->display.find_dpll = g4x_find_best_dpll;
10090 else if (IS_VALLEYVIEW(dev))
10091 dev_priv->display.find_dpll = vlv_find_best_dpll;
10092 else if (IS_PINEVIEW(dev))
10093 dev_priv->display.find_dpll = pnv_find_best_dpll;
10094 else
10095 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10096
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010097 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010098 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010099 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010100 dev_priv->display.crtc_enable = haswell_crtc_enable;
10101 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010102 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010103 dev_priv->display.update_plane = ironlake_update_plane;
10104 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010105 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010106 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010107 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10108 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010109 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010110 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010111 } else if (IS_VALLEYVIEW(dev)) {
10112 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10113 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10114 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10115 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10116 dev_priv->display.off = i9xx_crtc_off;
10117 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010118 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010119 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010120 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010121 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10122 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010123 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010124 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010125 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010126
Jesse Barnese70236a2009-09-21 10:42:27 -070010127 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010128 if (IS_VALLEYVIEW(dev))
10129 dev_priv->display.get_display_clock_speed =
10130 valleyview_get_display_clock_speed;
10131 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010132 dev_priv->display.get_display_clock_speed =
10133 i945_get_display_clock_speed;
10134 else if (IS_I915G(dev))
10135 dev_priv->display.get_display_clock_speed =
10136 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010137 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010138 dev_priv->display.get_display_clock_speed =
10139 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010140 else if (IS_PINEVIEW(dev))
10141 dev_priv->display.get_display_clock_speed =
10142 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010143 else if (IS_I915GM(dev))
10144 dev_priv->display.get_display_clock_speed =
10145 i915gm_get_display_clock_speed;
10146 else if (IS_I865G(dev))
10147 dev_priv->display.get_display_clock_speed =
10148 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010149 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010150 dev_priv->display.get_display_clock_speed =
10151 i855_get_display_clock_speed;
10152 else /* 852, 830 */
10153 dev_priv->display.get_display_clock_speed =
10154 i830_get_display_clock_speed;
10155
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010156 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010157 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010158 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010159 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010160 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010161 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010162 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010163 } else if (IS_IVYBRIDGE(dev)) {
10164 /* FIXME: detect B0+ stepping and use auto training */
10165 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010166 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010167 dev_priv->display.modeset_global_resources =
10168 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010169 } else if (IS_HASWELL(dev)) {
10170 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010171 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010172 dev_priv->display.modeset_global_resources =
10173 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010174 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010175 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010176 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010177 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010178
10179 /* Default just returns -ENODEV to indicate unsupported */
10180 dev_priv->display.queue_flip = intel_default_queue_flip;
10181
10182 switch (INTEL_INFO(dev)->gen) {
10183 case 2:
10184 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10185 break;
10186
10187 case 3:
10188 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10189 break;
10190
10191 case 4:
10192 case 5:
10193 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10194 break;
10195
10196 case 6:
10197 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10198 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010199 case 7:
10200 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10201 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010202 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010203}
10204
Jesse Barnesb690e962010-07-19 13:53:12 -070010205/*
10206 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10207 * resume, or other times. This quirk makes sure that's the case for
10208 * affected systems.
10209 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010210static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010211{
10212 struct drm_i915_private *dev_priv = dev->dev_private;
10213
10214 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010215 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010216}
10217
Keith Packard435793d2011-07-12 14:56:22 -070010218/*
10219 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10220 */
10221static void quirk_ssc_force_disable(struct drm_device *dev)
10222{
10223 struct drm_i915_private *dev_priv = dev->dev_private;
10224 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010225 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010226}
10227
Carsten Emde4dca20e2012-03-15 15:56:26 +010010228/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010229 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10230 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010231 */
10232static void quirk_invert_brightness(struct drm_device *dev)
10233{
10234 struct drm_i915_private *dev_priv = dev->dev_private;
10235 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010236 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010237}
10238
Kamal Mostafae85843b2013-07-19 15:02:01 -070010239/*
10240 * Some machines (Dell XPS13) suffer broken backlight controls if
10241 * BLM_PCH_PWM_ENABLE is set.
10242 */
10243static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
10244{
10245 struct drm_i915_private *dev_priv = dev->dev_private;
10246 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
10247 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
10248}
10249
Jesse Barnesb690e962010-07-19 13:53:12 -070010250struct intel_quirk {
10251 int device;
10252 int subsystem_vendor;
10253 int subsystem_device;
10254 void (*hook)(struct drm_device *dev);
10255};
10256
Egbert Eich5f85f172012-10-14 15:46:38 +020010257/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10258struct intel_dmi_quirk {
10259 void (*hook)(struct drm_device *dev);
10260 const struct dmi_system_id (*dmi_id_list)[];
10261};
10262
10263static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10264{
10265 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10266 return 1;
10267}
10268
10269static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10270 {
10271 .dmi_id_list = &(const struct dmi_system_id[]) {
10272 {
10273 .callback = intel_dmi_reverse_brightness,
10274 .ident = "NCR Corporation",
10275 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10276 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10277 },
10278 },
10279 { } /* terminating entry */
10280 },
10281 .hook = quirk_invert_brightness,
10282 },
10283};
10284
Ben Widawskyc43b5632012-04-16 14:07:40 -070010285static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010286 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010287 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010288
Jesse Barnesb690e962010-07-19 13:53:12 -070010289 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10290 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10291
Jesse Barnesb690e962010-07-19 13:53:12 -070010292 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10293 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10294
Daniel Vetterccd0d362012-10-10 23:13:59 +020010295 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -070010296 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010297 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010298
10299 /* Lenovo U160 cannot use SSC on LVDS */
10300 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010301
10302 /* Sony Vaio Y cannot use SSC on LVDS */
10303 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010304
Jani Nikulaee1452d2013-09-20 15:05:30 +030010305 /*
10306 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10307 * seem to use inverted backlight PWM.
10308 */
10309 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Kamal Mostafae85843b2013-07-19 15:02:01 -070010310
10311 /* Dell XPS13 HD Sandy Bridge */
10312 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
10313 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
10314 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
Jesse Barnesb690e962010-07-19 13:53:12 -070010315};
10316
10317static void intel_init_quirks(struct drm_device *dev)
10318{
10319 struct pci_dev *d = dev->pdev;
10320 int i;
10321
10322 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10323 struct intel_quirk *q = &intel_quirks[i];
10324
10325 if (d->device == q->device &&
10326 (d->subsystem_vendor == q->subsystem_vendor ||
10327 q->subsystem_vendor == PCI_ANY_ID) &&
10328 (d->subsystem_device == q->subsystem_device ||
10329 q->subsystem_device == PCI_ANY_ID))
10330 q->hook(dev);
10331 }
Egbert Eich5f85f172012-10-14 15:46:38 +020010332 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10333 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10334 intel_dmi_quirks[i].hook(dev);
10335 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010336}
10337
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010338/* Disable the VGA plane that we never use */
10339static void i915_disable_vga(struct drm_device *dev)
10340{
10341 struct drm_i915_private *dev_priv = dev->dev_private;
10342 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010343 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010344
10345 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010346 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010347 sr1 = inb(VGA_SR_DATA);
10348 outb(sr1 | 1<<5, VGA_SR_DATA);
10349 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10350 udelay(300);
10351
10352 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10353 POSTING_READ(vga_reg);
10354}
10355
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010356static void i915_enable_vga_mem(struct drm_device *dev)
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010357{
10358 /* Enable VGA memory on Intel HD */
10359 if (HAS_PCH_SPLIT(dev)) {
10360 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10361 outb(inb(VGA_MSR_READ) | VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10362 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10363 VGA_RSRC_LEGACY_MEM |
10364 VGA_RSRC_NORMAL_IO |
10365 VGA_RSRC_NORMAL_MEM);
10366 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10367 }
10368}
10369
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010370void i915_disable_vga_mem(struct drm_device *dev)
10371{
10372 /* Disable VGA memory on Intel HD */
10373 if (HAS_PCH_SPLIT(dev)) {
10374 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10375 outb(inb(VGA_MSR_READ) & ~VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10376 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10377 VGA_RSRC_NORMAL_IO |
10378 VGA_RSRC_NORMAL_MEM);
10379 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10380 }
10381}
10382
Daniel Vetterf8175862012-04-10 15:50:11 +020010383void intel_modeset_init_hw(struct drm_device *dev)
10384{
Jesse Barnesf6071162013-10-01 10:41:38 -070010385 struct drm_i915_private *dev_priv = dev->dev_private;
10386
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010387 intel_prepare_ddi(dev);
10388
Daniel Vetterf8175862012-04-10 15:50:11 +020010389 intel_init_clock_gating(dev);
10390
Jesse Barnesf6071162013-10-01 10:41:38 -070010391 /* Enable the CRI clock source so we can get at the display */
10392 if (IS_VALLEYVIEW(dev))
10393 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10394 DPLL_INTEGRATED_CRI_CLK_VLV);
10395
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010396 intel_init_dpio(dev);
10397
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010398 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010399 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010400 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010401}
10402
Imre Deak7d708ee2013-04-17 14:04:50 +030010403void intel_modeset_suspend_hw(struct drm_device *dev)
10404{
10405 intel_suspend_hw(dev);
10406}
10407
Jesse Barnes79e53942008-11-07 14:24:08 -080010408void intel_modeset_init(struct drm_device *dev)
10409{
Jesse Barnes652c3932009-08-17 13:31:43 -070010410 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010411 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010412
10413 drm_mode_config_init(dev);
10414
10415 dev->mode_config.min_width = 0;
10416 dev->mode_config.min_height = 0;
10417
Dave Airlie019d96c2011-09-29 16:20:42 +010010418 dev->mode_config.preferred_depth = 24;
10419 dev->mode_config.prefer_shadow = 1;
10420
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010421 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010422
Jesse Barnesb690e962010-07-19 13:53:12 -070010423 intel_init_quirks(dev);
10424
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010425 intel_init_pm(dev);
10426
Ben Widawskye3c74752013-04-05 13:12:39 -070010427 if (INTEL_INFO(dev)->num_pipes == 0)
10428 return;
10429
Jesse Barnese70236a2009-09-21 10:42:27 -070010430 intel_init_display(dev);
10431
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010432 if (IS_GEN2(dev)) {
10433 dev->mode_config.max_width = 2048;
10434 dev->mode_config.max_height = 2048;
10435 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010436 dev->mode_config.max_width = 4096;
10437 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010438 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010439 dev->mode_config.max_width = 8192;
10440 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010441 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010442 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010443
Zhao Yakui28c97732009-10-09 11:39:41 +080010444 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010445 INTEL_INFO(dev)->num_pipes,
10446 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010447
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010448 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010449 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010450 for (j = 0; j < dev_priv->num_plane; j++) {
10451 ret = intel_plane_init(dev, i, j);
10452 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010453 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10454 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010455 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010456 }
10457
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010458 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010459 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010460
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010461 /* Just disable it once at startup */
10462 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010463 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010464
10465 /* Just in case the BIOS is doing something questionable. */
10466 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010467}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010468
Daniel Vetter24929352012-07-02 20:28:59 +020010469static void
10470intel_connector_break_all_links(struct intel_connector *connector)
10471{
10472 connector->base.dpms = DRM_MODE_DPMS_OFF;
10473 connector->base.encoder = NULL;
10474 connector->encoder->connectors_active = false;
10475 connector->encoder->base.crtc = NULL;
10476}
10477
Daniel Vetter7fad7982012-07-04 17:51:47 +020010478static void intel_enable_pipe_a(struct drm_device *dev)
10479{
10480 struct intel_connector *connector;
10481 struct drm_connector *crt = NULL;
10482 struct intel_load_detect_pipe load_detect_temp;
10483
10484 /* We can't just switch on the pipe A, we need to set things up with a
10485 * proper mode and output configuration. As a gross hack, enable pipe A
10486 * by enabling the load detect pipe once. */
10487 list_for_each_entry(connector,
10488 &dev->mode_config.connector_list,
10489 base.head) {
10490 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10491 crt = &connector->base;
10492 break;
10493 }
10494 }
10495
10496 if (!crt)
10497 return;
10498
10499 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10500 intel_release_load_detect_pipe(crt, &load_detect_temp);
10501
10502
10503}
10504
Daniel Vetterfa555832012-10-10 23:14:00 +020010505static bool
10506intel_check_plane_mapping(struct intel_crtc *crtc)
10507{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010508 struct drm_device *dev = crtc->base.dev;
10509 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010510 u32 reg, val;
10511
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010512 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010513 return true;
10514
10515 reg = DSPCNTR(!crtc->plane);
10516 val = I915_READ(reg);
10517
10518 if ((val & DISPLAY_PLANE_ENABLE) &&
10519 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10520 return false;
10521
10522 return true;
10523}
10524
Daniel Vetter24929352012-07-02 20:28:59 +020010525static void intel_sanitize_crtc(struct intel_crtc *crtc)
10526{
10527 struct drm_device *dev = crtc->base.dev;
10528 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010529 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010530
Daniel Vetter24929352012-07-02 20:28:59 +020010531 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010532 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010533 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10534
10535 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010536 * disable the crtc (and hence change the state) if it is wrong. Note
10537 * that gen4+ has a fixed plane -> pipe mapping. */
10538 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010539 struct intel_connector *connector;
10540 bool plane;
10541
Daniel Vetter24929352012-07-02 20:28:59 +020010542 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10543 crtc->base.base.id);
10544
10545 /* Pipe has the wrong plane attached and the plane is active.
10546 * Temporarily change the plane mapping and disable everything
10547 * ... */
10548 plane = crtc->plane;
10549 crtc->plane = !plane;
10550 dev_priv->display.crtc_disable(&crtc->base);
10551 crtc->plane = plane;
10552
10553 /* ... and break all links. */
10554 list_for_each_entry(connector, &dev->mode_config.connector_list,
10555 base.head) {
10556 if (connector->encoder->base.crtc != &crtc->base)
10557 continue;
10558
10559 intel_connector_break_all_links(connector);
10560 }
10561
10562 WARN_ON(crtc->active);
10563 crtc->base.enabled = false;
10564 }
Daniel Vetter24929352012-07-02 20:28:59 +020010565
Daniel Vetter7fad7982012-07-04 17:51:47 +020010566 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10567 crtc->pipe == PIPE_A && !crtc->active) {
10568 /* BIOS forgot to enable pipe A, this mostly happens after
10569 * resume. Force-enable the pipe to fix this, the update_dpms
10570 * call below we restore the pipe to the right state, but leave
10571 * the required bits on. */
10572 intel_enable_pipe_a(dev);
10573 }
10574
Daniel Vetter24929352012-07-02 20:28:59 +020010575 /* Adjust the state of the output pipe according to whether we
10576 * have active connectors/encoders. */
10577 intel_crtc_update_dpms(&crtc->base);
10578
10579 if (crtc->active != crtc->base.enabled) {
10580 struct intel_encoder *encoder;
10581
10582 /* This can happen either due to bugs in the get_hw_state
10583 * functions or because the pipe is force-enabled due to the
10584 * pipe A quirk. */
10585 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10586 crtc->base.base.id,
10587 crtc->base.enabled ? "enabled" : "disabled",
10588 crtc->active ? "enabled" : "disabled");
10589
10590 crtc->base.enabled = crtc->active;
10591
10592 /* Because we only establish the connector -> encoder ->
10593 * crtc links if something is active, this means the
10594 * crtc is now deactivated. Break the links. connector
10595 * -> encoder links are only establish when things are
10596 * actually up, hence no need to break them. */
10597 WARN_ON(crtc->active);
10598
10599 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10600 WARN_ON(encoder->connectors_active);
10601 encoder->base.crtc = NULL;
10602 }
10603 }
10604}
10605
10606static void intel_sanitize_encoder(struct intel_encoder *encoder)
10607{
10608 struct intel_connector *connector;
10609 struct drm_device *dev = encoder->base.dev;
10610
10611 /* We need to check both for a crtc link (meaning that the
10612 * encoder is active and trying to read from a pipe) and the
10613 * pipe itself being active. */
10614 bool has_active_crtc = encoder->base.crtc &&
10615 to_intel_crtc(encoder->base.crtc)->active;
10616
10617 if (encoder->connectors_active && !has_active_crtc) {
10618 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10619 encoder->base.base.id,
10620 drm_get_encoder_name(&encoder->base));
10621
10622 /* Connector is active, but has no active pipe. This is
10623 * fallout from our resume register restoring. Disable
10624 * the encoder manually again. */
10625 if (encoder->base.crtc) {
10626 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10627 encoder->base.base.id,
10628 drm_get_encoder_name(&encoder->base));
10629 encoder->disable(encoder);
10630 }
10631
10632 /* Inconsistent output/port/pipe state happens presumably due to
10633 * a bug in one of the get_hw_state functions. Or someplace else
10634 * in our code, like the register restore mess on resume. Clamp
10635 * things to off as a safer default. */
10636 list_for_each_entry(connector,
10637 &dev->mode_config.connector_list,
10638 base.head) {
10639 if (connector->encoder != encoder)
10640 continue;
10641
10642 intel_connector_break_all_links(connector);
10643 }
10644 }
10645 /* Enabled encoders without active connectors will be fixed in
10646 * the crtc fixup. */
10647}
10648
Daniel Vetter44cec742013-01-25 17:53:21 +010010649void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010650{
10651 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010652 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010653
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010654 /* This function can be called both from intel_modeset_setup_hw_state or
10655 * at a very early point in our resume sequence, where the power well
10656 * structures are not yet restored. Since this function is at a very
10657 * paranoid "someone might have enabled VGA while we were not looking"
10658 * level, just check if the power well is enabled instead of trying to
10659 * follow the "don't touch the power well if we don't need it" policy
10660 * the rest of the driver uses. */
10661 if (HAS_POWER_WELL(dev) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030010662 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010663 return;
10664
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010665 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10666 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020010667 i915_disable_vga(dev);
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010668 i915_disable_vga_mem(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010669 }
10670}
10671
Daniel Vetter30e984d2013-06-05 13:34:17 +020010672static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020010673{
10674 struct drm_i915_private *dev_priv = dev->dev_private;
10675 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020010676 struct intel_crtc *crtc;
10677 struct intel_encoder *encoder;
10678 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020010679 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020010680
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010681 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10682 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010010683 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020010684
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010685 crtc->active = dev_priv->display.get_pipe_config(crtc,
10686 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010687
10688 crtc->base.enabled = crtc->active;
10689
10690 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10691 crtc->base.base.id,
10692 crtc->active ? "enabled" : "disabled");
10693 }
10694
Daniel Vetter53589012013-06-05 13:34:16 +020010695 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010696 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010697 intel_ddi_setup_hw_pll_state(dev);
10698
Daniel Vetter53589012013-06-05 13:34:16 +020010699 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10700 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10701
10702 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10703 pll->active = 0;
10704 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10705 base.head) {
10706 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10707 pll->active++;
10708 }
10709 pll->refcount = pll->active;
10710
Daniel Vetter35c95372013-07-17 06:55:04 +020010711 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10712 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020010713 }
10714
Daniel Vetter24929352012-07-02 20:28:59 +020010715 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10716 base.head) {
10717 pipe = 0;
10718
10719 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010720 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10721 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070010722 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010723 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010724 } else {
10725 encoder->base.crtc = NULL;
10726 }
10727
10728 encoder->connectors_active = false;
10729 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10730 encoder->base.base.id,
10731 drm_get_encoder_name(&encoder->base),
10732 encoder->base.crtc ? "enabled" : "disabled",
10733 pipe);
10734 }
10735
10736 list_for_each_entry(connector, &dev->mode_config.connector_list,
10737 base.head) {
10738 if (connector->get_hw_state(connector)) {
10739 connector->base.dpms = DRM_MODE_DPMS_ON;
10740 connector->encoder->connectors_active = true;
10741 connector->base.encoder = &connector->encoder->base;
10742 } else {
10743 connector->base.dpms = DRM_MODE_DPMS_OFF;
10744 connector->base.encoder = NULL;
10745 }
10746 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10747 connector->base.base.id,
10748 drm_get_connector_name(&connector->base),
10749 connector->base.encoder ? "enabled" : "disabled");
10750 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010751}
10752
10753/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10754 * and i915 state tracking structures. */
10755void intel_modeset_setup_hw_state(struct drm_device *dev,
10756 bool force_restore)
10757{
10758 struct drm_i915_private *dev_priv = dev->dev_private;
10759 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010760 struct intel_crtc *crtc;
10761 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020010762 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010763
10764 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010765
Jesse Barnesbabea612013-06-26 18:57:38 +030010766 /*
10767 * Now that we have the config, copy it to each CRTC struct
10768 * Note that this could go away if we move to using crtc_config
10769 * checking everywhere.
10770 */
10771 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10772 base.head) {
10773 if (crtc->active && i915_fastboot) {
10774 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10775
10776 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10777 crtc->base.base.id);
10778 drm_mode_debug_printmodeline(&crtc->base.mode);
10779 }
10780 }
10781
Daniel Vetter24929352012-07-02 20:28:59 +020010782 /* HW state is read out, now we need to sanitize this mess. */
10783 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10784 base.head) {
10785 intel_sanitize_encoder(encoder);
10786 }
10787
10788 for_each_pipe(pipe) {
10789 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10790 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010791 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010792 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010793
Daniel Vetter35c95372013-07-17 06:55:04 +020010794 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10795 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10796
10797 if (!pll->on || pll->active)
10798 continue;
10799
10800 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10801
10802 pll->disable(dev_priv, pll);
10803 pll->on = false;
10804 }
10805
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010806 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030010807 i915_redisable_vga(dev);
10808
Daniel Vetterf30da182013-04-11 20:22:50 +020010809 /*
10810 * We need to use raw interfaces for restoring state to avoid
10811 * checking (bogus) intermediate states.
10812 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010813 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010814 struct drm_crtc *crtc =
10815 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010816
10817 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10818 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010819 }
10820 } else {
10821 intel_modeset_update_staged_output_state(dev);
10822 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010823
10824 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010825
10826 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010827}
10828
10829void intel_modeset_gem_init(struct drm_device *dev)
10830{
Chris Wilson1833b132012-05-09 11:56:28 +010010831 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010832
10833 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010834
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010835 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010836}
10837
10838void intel_modeset_cleanup(struct drm_device *dev)
10839{
Jesse Barnes652c3932009-08-17 13:31:43 -070010840 struct drm_i915_private *dev_priv = dev->dev_private;
10841 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030010842 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070010843
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010844 /*
10845 * Interrupts and polling as the first thing to avoid creating havoc.
10846 * Too much stuff here (turning of rps, connectors, ...) would
10847 * experience fancy races otherwise.
10848 */
10849 drm_irq_uninstall(dev);
10850 cancel_work_sync(&dev_priv->hotplug_work);
10851 /*
10852 * Due to the hpd irq storm handling the hotplug work can re-arm the
10853 * poll handlers. Hence disable polling after hpd handling is shut down.
10854 */
Keith Packardf87ea762010-10-03 19:36:26 -070010855 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010856
Jesse Barnes652c3932009-08-17 13:31:43 -070010857 mutex_lock(&dev->struct_mutex);
10858
Jesse Barnes723bfd72010-10-07 16:01:13 -070010859 intel_unregister_dsm_handler();
10860
Jesse Barnes652c3932009-08-17 13:31:43 -070010861 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10862 /* Skip inactive CRTCs */
10863 if (!crtc->fb)
10864 continue;
10865
Daniel Vetter3dec0092010-08-20 21:40:52 +020010866 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010867 }
10868
Chris Wilson973d04f2011-07-08 12:22:37 +010010869 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010870
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010871 i915_enable_vga_mem(dev);
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010872
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010873 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010874
Daniel Vetter930ebb42012-06-29 23:32:16 +020010875 ironlake_teardown_rc6(dev);
10876
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010877 mutex_unlock(&dev->struct_mutex);
10878
Chris Wilson1630fe72011-07-08 12:22:42 +010010879 /* flush any delayed tasks or pending work */
10880 flush_scheduled_work();
10881
Jani Nikuladc652f92013-04-12 15:18:38 +030010882 /* destroy backlight, if any, before the connectors */
10883 intel_panel_destroy_backlight(dev);
10884
Paulo Zanonid9255d52013-09-26 20:05:59 -030010885 /* destroy the sysfs files before encoders/connectors */
10886 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
10887 drm_sysfs_connector_remove(connector);
10888
Jesse Barnes79e53942008-11-07 14:24:08 -080010889 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010890
10891 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010892}
10893
Dave Airlie28d52042009-09-21 14:33:58 +100010894/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010895 * Return which encoder is currently attached for connector.
10896 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010897struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010898{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010899 return &intel_attached_encoder(connector)->base;
10900}
Jesse Barnes79e53942008-11-07 14:24:08 -080010901
Chris Wilsondf0e9242010-09-09 16:20:55 +010010902void intel_connector_attach_encoder(struct intel_connector *connector,
10903 struct intel_encoder *encoder)
10904{
10905 connector->encoder = encoder;
10906 drm_mode_connector_attach_encoder(&connector->base,
10907 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010908}
Dave Airlie28d52042009-09-21 14:33:58 +100010909
10910/*
10911 * set vga decode state - true == enable VGA decode
10912 */
10913int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10914{
10915 struct drm_i915_private *dev_priv = dev->dev_private;
10916 u16 gmch_ctrl;
10917
10918 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10919 if (state)
10920 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10921 else
10922 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10923 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10924 return 0;
10925}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010926
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010927struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010928
10929 u32 power_well_driver;
10930
Chris Wilson63b66e52013-08-08 15:12:06 +020010931 int num_transcoders;
10932
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010933 struct intel_cursor_error_state {
10934 u32 control;
10935 u32 position;
10936 u32 base;
10937 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010938 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010939
10940 struct intel_pipe_error_state {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010941 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010010942 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010943
10944 struct intel_plane_error_state {
10945 u32 control;
10946 u32 stride;
10947 u32 size;
10948 u32 pos;
10949 u32 addr;
10950 u32 surface;
10951 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010952 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020010953
10954 struct intel_transcoder_error_state {
10955 enum transcoder cpu_transcoder;
10956
10957 u32 conf;
10958
10959 u32 htotal;
10960 u32 hblank;
10961 u32 hsync;
10962 u32 vtotal;
10963 u32 vblank;
10964 u32 vsync;
10965 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010966};
10967
10968struct intel_display_error_state *
10969intel_display_capture_error_state(struct drm_device *dev)
10970{
Akshay Joshi0206e352011-08-16 15:34:10 -040010971 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010972 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020010973 int transcoders[] = {
10974 TRANSCODER_A,
10975 TRANSCODER_B,
10976 TRANSCODER_C,
10977 TRANSCODER_EDP,
10978 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010979 int i;
10980
Chris Wilson63b66e52013-08-08 15:12:06 +020010981 if (INTEL_INFO(dev)->num_pipes == 0)
10982 return NULL;
10983
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010984 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10985 if (error == NULL)
10986 return NULL;
10987
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010988 if (HAS_POWER_WELL(dev))
10989 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10990
Damien Lespiau52331302012-08-15 19:23:25 +010010991 for_each_pipe(i) {
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010992 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10993 error->cursor[i].control = I915_READ(CURCNTR(i));
10994 error->cursor[i].position = I915_READ(CURPOS(i));
10995 error->cursor[i].base = I915_READ(CURBASE(i));
10996 } else {
10997 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10998 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10999 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11000 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011001
11002 error->plane[i].control = I915_READ(DSPCNTR(i));
11003 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011004 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011005 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011006 error->plane[i].pos = I915_READ(DSPPOS(i));
11007 }
Paulo Zanonica291362013-03-06 20:03:14 -030011008 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11009 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011010 if (INTEL_INFO(dev)->gen >= 4) {
11011 error->plane[i].surface = I915_READ(DSPSURF(i));
11012 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11013 }
11014
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011015 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011016 }
11017
11018 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11019 if (HAS_DDI(dev_priv->dev))
11020 error->num_transcoders++; /* Account for eDP. */
11021
11022 for (i = 0; i < error->num_transcoders; i++) {
11023 enum transcoder cpu_transcoder = transcoders[i];
11024
11025 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11026
11027 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11028 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11029 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11030 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11031 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11032 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11033 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011034 }
11035
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011036 /* In the code above we read the registers without checking if the power
11037 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
11038 * prevent the next I915_WRITE from detecting it and printing an error
11039 * message. */
Chris Wilson907b28c2013-07-19 20:36:52 +010011040 intel_uncore_clear_errors(dev);
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011041
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011042 return error;
11043}
11044
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011045#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11046
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011047void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011048intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011049 struct drm_device *dev,
11050 struct intel_display_error_state *error)
11051{
11052 int i;
11053
Chris Wilson63b66e52013-08-08 15:12:06 +020011054 if (!error)
11055 return;
11056
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011057 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011058 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011059 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011060 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011061 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011062 err_printf(m, "Pipe [%d]:\n", i);
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011063 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011064
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011065 err_printf(m, "Plane [%d]:\n", i);
11066 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11067 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011068 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011069 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11070 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011071 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011072 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011073 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011074 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011075 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11076 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011077 }
11078
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011079 err_printf(m, "Cursor [%d]:\n", i);
11080 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11081 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11082 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011083 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011084
11085 for (i = 0; i < error->num_transcoders; i++) {
11086 err_printf(m, " CPU transcoder: %c\n",
11087 transcoder_name(error->transcoder[i].cpu_transcoder));
11088 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11089 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11090 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11091 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11092 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11093 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11094 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11095 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011096}