blob: 62cfada507358c1c243df4e65f06e2ca12095d0f [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 },
316 .m = { .min = 22, .max = 450 }, /* guess */
317 .m1 = { .min = 2, .max = 3 },
318 .m2 = { .min = 11, .max = 156 },
319 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200320 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700321 .p2 = { .dot_limit = 270000,
322 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700323};
324
325static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200326 .dot = { .min = 25000, .max = 270000 },
327 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700328 .n = { .min = 1, .max = 7 },
329 .m = { .min = 60, .max = 300 }, /* guess */
330 .m1 = { .min = 2, .max = 3 },
331 .m2 = { .min = 11, .max = 156 },
332 .p = { .min = 10, .max = 30 },
333 .p1 = { .min = 2, .max = 3 },
334 .p2 = { .dot_limit = 270000,
335 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700336};
337
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300338static void vlv_clock(int refclk, intel_clock_t *clock)
339{
340 clock->m = clock->m1 * clock->m2;
341 clock->p = clock->p1 * clock->p2;
342 clock->vco = refclk * clock->m / clock->n;
343 clock->dot = clock->vco / clock->p;
344}
345
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300346/**
347 * Returns whether any output on the specified pipe is of the specified type
348 */
349static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
350{
351 struct drm_device *dev = crtc->dev;
352 struct intel_encoder *encoder;
353
354 for_each_encoder_on_crtc(dev, crtc, encoder)
355 if (encoder->type == type)
356 return true;
357
358 return false;
359}
360
Chris Wilson1b894b52010-12-14 20:04:54 +0000361static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
362 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800363{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800364 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800365 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800366
367 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100368 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000369 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800370 limit = &intel_limits_ironlake_dual_lvds_100m;
371 else
372 limit = &intel_limits_ironlake_dual_lvds;
373 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000374 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800375 limit = &intel_limits_ironlake_single_lvds_100m;
376 else
377 limit = &intel_limits_ironlake_single_lvds;
378 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200379 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800380 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800381
382 return limit;
383}
384
Ma Ling044c7c42009-03-18 20:13:23 +0800385static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
386{
387 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800388 const intel_limit_t *limit;
389
390 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100391 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700392 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800393 else
Keith Packarde4b36692009-06-05 19:22:17 -0700394 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800395 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
396 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700397 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800398 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700399 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800400 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700401 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800402
403 return limit;
404}
405
Chris Wilson1b894b52010-12-14 20:04:54 +0000406static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800407{
408 struct drm_device *dev = crtc->dev;
409 const intel_limit_t *limit;
410
Eric Anholtbad720f2009-10-22 16:11:14 -0700411 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000412 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800413 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800414 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500415 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800416 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500417 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800418 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500419 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700420 } else if (IS_VALLEYVIEW(dev)) {
421 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
422 limit = &intel_limits_vlv_dac;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700423 else
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +0800424 limit = &intel_limits_vlv_hdmi;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100425 } else if (!IS_GEN2(dev)) {
426 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
427 limit = &intel_limits_i9xx_lvds;
428 else
429 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800430 } else {
431 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700432 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200433 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700434 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200435 else
436 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800437 }
438 return limit;
439}
440
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500441/* m1 is reserved as 0 in Pineview, n is a ring counter */
442static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800443{
Shaohua Li21778322009-02-23 15:19:16 +0800444 clock->m = clock->m2 + 2;
445 clock->p = clock->p1 * clock->p2;
446 clock->vco = refclk * clock->m / clock->n;
447 clock->dot = clock->vco / clock->p;
448}
449
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200450static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
451{
452 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
453}
454
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200455static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800456{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200457 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800458 clock->p = clock->p1 * clock->p2;
459 clock->vco = refclk * clock->m / (clock->n + 2);
460 clock->dot = clock->vco / clock->p;
461}
462
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800463#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800464/**
465 * Returns whether the given set of divisors are valid for a given refclk with
466 * the given connectors.
467 */
468
Chris Wilson1b894b52010-12-14 20:04:54 +0000469static bool intel_PLL_is_valid(struct drm_device *dev,
470 const intel_limit_t *limit,
471 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800472{
Jesse Barnes79e53942008-11-07 14:24:08 -0800473 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800475 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400476 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800477 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800479 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400480 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500481 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400482 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800483 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400484 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400488 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800489 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
490 * connector, etc., rather than just a single range.
491 */
492 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400493 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800494
495 return true;
496}
497
Ma Lingd4906092009-03-18 20:13:27 +0800498static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200499i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800500 int target, int refclk, intel_clock_t *match_clock,
501 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800502{
503 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800504 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 int err = target;
506
Daniel Vettera210b022012-11-26 17:22:08 +0100507 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800508 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100509 * For LVDS just rely on its current settings for dual-channel.
510 * We haven't figured out how to reliably set up different
511 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100513 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800514 clock.p2 = limit->p2.p2_fast;
515 else
516 clock.p2 = limit->p2.p2_slow;
517 } else {
518 if (target < limit->p2.dot_limit)
519 clock.p2 = limit->p2.p2_slow;
520 else
521 clock.p2 = limit->p2.p2_fast;
522 }
523
Akshay Joshi0206e352011-08-16 15:34:10 -0400524 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800525
Zhao Yakui42158662009-11-20 11:24:18 +0800526 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
527 clock.m1++) {
528 for (clock.m2 = limit->m2.min;
529 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200530 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800531 break;
532 for (clock.n = limit->n.min;
533 clock.n <= limit->n.max; clock.n++) {
534 for (clock.p1 = limit->p1.min;
535 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800536 int this_err;
537
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200538 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000539 if (!intel_PLL_is_valid(dev, limit,
540 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800541 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800542 if (match_clock &&
543 clock.p != match_clock->p)
544 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545
546 this_err = abs(clock.dot - target);
547 if (this_err < err) {
548 *best_clock = clock;
549 err = this_err;
550 }
551 }
552 }
553 }
554 }
555
556 return (err != target);
557}
558
Ma Lingd4906092009-03-18 20:13:27 +0800559static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200560pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
561 int target, int refclk, intel_clock_t *match_clock,
562 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200563{
564 struct drm_device *dev = crtc->dev;
565 intel_clock_t clock;
566 int err = target;
567
568 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
569 /*
570 * For LVDS just rely on its current settings for dual-channel.
571 * We haven't figured out how to reliably set up different
572 * single/dual channel state, if we even can.
573 */
574 if (intel_is_dual_link_lvds(dev))
575 clock.p2 = limit->p2.p2_fast;
576 else
577 clock.p2 = limit->p2.p2_slow;
578 } else {
579 if (target < limit->p2.dot_limit)
580 clock.p2 = limit->p2.p2_slow;
581 else
582 clock.p2 = limit->p2.p2_fast;
583 }
584
585 memset(best_clock, 0, sizeof(*best_clock));
586
587 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
588 clock.m1++) {
589 for (clock.m2 = limit->m2.min;
590 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200591 for (clock.n = limit->n.min;
592 clock.n <= limit->n.max; clock.n++) {
593 for (clock.p1 = limit->p1.min;
594 clock.p1 <= limit->p1.max; clock.p1++) {
595 int this_err;
596
597 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800598 if (!intel_PLL_is_valid(dev, limit,
599 &clock))
600 continue;
601 if (match_clock &&
602 clock.p != match_clock->p)
603 continue;
604
605 this_err = abs(clock.dot - target);
606 if (this_err < err) {
607 *best_clock = clock;
608 err = this_err;
609 }
610 }
611 }
612 }
613 }
614
615 return (err != target);
616}
617
Ma Lingd4906092009-03-18 20:13:27 +0800618static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200619g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
620 int target, int refclk, intel_clock_t *match_clock,
621 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800622{
623 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800624 intel_clock_t clock;
625 int max_n;
626 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400627 /* approximately equals target * 0.00585 */
628 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800629 found = false;
630
631 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100632 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800633 clock.p2 = limit->p2.p2_fast;
634 else
635 clock.p2 = limit->p2.p2_slow;
636 } else {
637 if (target < limit->p2.dot_limit)
638 clock.p2 = limit->p2.p2_slow;
639 else
640 clock.p2 = limit->p2.p2_fast;
641 }
642
643 memset(best_clock, 0, sizeof(*best_clock));
644 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200645 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800646 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200647 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800648 for (clock.m1 = limit->m1.max;
649 clock.m1 >= limit->m1.min; clock.m1--) {
650 for (clock.m2 = limit->m2.max;
651 clock.m2 >= limit->m2.min; clock.m2--) {
652 for (clock.p1 = limit->p1.max;
653 clock.p1 >= limit->p1.min; clock.p1--) {
654 int this_err;
655
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200656 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000657 if (!intel_PLL_is_valid(dev, limit,
658 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800659 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000660
661 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800662 if (this_err < err_most) {
663 *best_clock = clock;
664 err_most = this_err;
665 max_n = clock.n;
666 found = true;
667 }
668 }
669 }
670 }
671 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800672 return found;
673}
Ma Lingd4906092009-03-18 20:13:27 +0800674
Zhenyu Wang2c072452009-06-05 15:38:42 +0800675static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200676vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
677 int target, int refclk, intel_clock_t *match_clock,
678 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700679{
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300680 intel_clock_t clock;
681 u32 minupdate = 19200;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300682 unsigned int bestppm = 1000000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700683
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300684 target *= 5; /* fast clock */
685
686 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700687
688 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300689 for (clock.n = limit->n.min; clock.n <= ((refclk) / minupdate); clock.n++) {
690 for (clock.p1 = limit->p1.max; clock.p1 > limit->p1.min; clock.p1--) {
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300691 for (clock.p2 = limit->p2.p2_fast; clock.p2 > 0;
692 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300693 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700694 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300695 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300696 unsigned int ppm, diff;
697
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300698 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
699 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300700
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300701 vlv_clock(refclk, &clock);
702
703 if (clock.vco < limit->vco.min ||
704 clock.vco >= limit->vco.max)
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300705 continue;
706
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300707 diff = abs(clock.dot - target);
708 ppm = div_u64(1000000ULL * diff, target);
709
710 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300711 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300712 *best_clock = clock;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300713 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300714
Ville Syrjäläc6861222013-09-24 21:26:21 +0300715 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300716 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300717 *best_clock = clock;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700718 }
719 }
720 }
721 }
722 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700723
724 return true;
725}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700726
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300727bool intel_crtc_active(struct drm_crtc *crtc)
728{
729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
730
731 /* Be paranoid as we can arrive here with only partial
732 * state retrieved from the hardware during setup.
733 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100734 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300735 * as Haswell has gained clock readout/fastboot support.
736 *
737 * We can ditch the crtc->fb check as soon as we can
738 * properly reconstruct framebuffers.
739 */
740 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100741 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300742}
743
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200744enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
745 enum pipe pipe)
746{
747 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
749
Daniel Vetter3b117c82013-04-17 20:15:07 +0200750 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200751}
752
Paulo Zanonia928d532012-05-04 17:18:15 -0300753static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
754{
755 struct drm_i915_private *dev_priv = dev->dev_private;
756 u32 frame, frame_reg = PIPEFRAME(pipe);
757
758 frame = I915_READ(frame_reg);
759
760 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
761 DRM_DEBUG_KMS("vblank wait timed out\n");
762}
763
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700764/**
765 * intel_wait_for_vblank - wait for vblank on a given pipe
766 * @dev: drm device
767 * @pipe: pipe to wait for
768 *
769 * Wait for vblank to occur on a given pipe. Needed for various bits of
770 * mode setting code.
771 */
772void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800773{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700774 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800775 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700776
Paulo Zanonia928d532012-05-04 17:18:15 -0300777 if (INTEL_INFO(dev)->gen >= 5) {
778 ironlake_wait_for_vblank(dev, pipe);
779 return;
780 }
781
Chris Wilson300387c2010-09-05 20:25:43 +0100782 /* Clear existing vblank status. Note this will clear any other
783 * sticky status fields as well.
784 *
785 * This races with i915_driver_irq_handler() with the result
786 * that either function could miss a vblank event. Here it is not
787 * fatal, as we will either wait upon the next vblank interrupt or
788 * timeout. Generally speaking intel_wait_for_vblank() is only
789 * called during modeset at which time the GPU should be idle and
790 * should *not* be performing page flips and thus not waiting on
791 * vblanks...
792 * Currently, the result of us stealing a vblank from the irq
793 * handler is that a single frame will be skipped during swapbuffers.
794 */
795 I915_WRITE(pipestat_reg,
796 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
797
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700798 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100799 if (wait_for(I915_READ(pipestat_reg) &
800 PIPE_VBLANK_INTERRUPT_STATUS,
801 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700802 DRM_DEBUG_KMS("vblank wait timed out\n");
803}
804
Keith Packardab7ad7f2010-10-03 00:33:06 -0700805/*
806 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700807 * @dev: drm device
808 * @pipe: pipe to wait for
809 *
810 * After disabling a pipe, we can't wait for vblank in the usual way,
811 * spinning on the vblank interrupt status bit, since we won't actually
812 * see an interrupt when the pipe is disabled.
813 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700814 * On Gen4 and above:
815 * wait for the pipe register state bit to turn off
816 *
817 * Otherwise:
818 * wait for the display line value to settle (it usually
819 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100820 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700821 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100822void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700823{
824 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200825 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
826 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700827
Keith Packardab7ad7f2010-10-03 00:33:06 -0700828 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200829 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700830
Keith Packardab7ad7f2010-10-03 00:33:06 -0700831 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100832 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
833 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200834 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700835 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300836 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100837 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700838 unsigned long timeout = jiffies + msecs_to_jiffies(100);
839
Paulo Zanoni837ba002012-05-04 17:18:14 -0300840 if (IS_GEN2(dev))
841 line_mask = DSL_LINEMASK_GEN2;
842 else
843 line_mask = DSL_LINEMASK_GEN3;
844
Keith Packardab7ad7f2010-10-03 00:33:06 -0700845 /* Wait for the display line to settle */
846 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300847 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700848 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300849 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700850 time_after(timeout, jiffies));
851 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200852 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700853 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800854}
855
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000856/*
857 * ibx_digital_port_connected - is the specified port connected?
858 * @dev_priv: i915 private structure
859 * @port: the port to test
860 *
861 * Returns true if @port is connected, false otherwise.
862 */
863bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
864 struct intel_digital_port *port)
865{
866 u32 bit;
867
Damien Lespiauc36346e2012-12-13 16:09:03 +0000868 if (HAS_PCH_IBX(dev_priv->dev)) {
869 switch(port->port) {
870 case PORT_B:
871 bit = SDE_PORTB_HOTPLUG;
872 break;
873 case PORT_C:
874 bit = SDE_PORTC_HOTPLUG;
875 break;
876 case PORT_D:
877 bit = SDE_PORTD_HOTPLUG;
878 break;
879 default:
880 return true;
881 }
882 } else {
883 switch(port->port) {
884 case PORT_B:
885 bit = SDE_PORTB_HOTPLUG_CPT;
886 break;
887 case PORT_C:
888 bit = SDE_PORTC_HOTPLUG_CPT;
889 break;
890 case PORT_D:
891 bit = SDE_PORTD_HOTPLUG_CPT;
892 break;
893 default:
894 return true;
895 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000896 }
897
898 return I915_READ(SDEISR) & bit;
899}
900
Jesse Barnesb24e7172011-01-04 15:09:30 -0800901static const char *state_string(bool enabled)
902{
903 return enabled ? "on" : "off";
904}
905
906/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200907void assert_pll(struct drm_i915_private *dev_priv,
908 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800909{
910 int reg;
911 u32 val;
912 bool cur_state;
913
914 reg = DPLL(pipe);
915 val = I915_READ(reg);
916 cur_state = !!(val & DPLL_VCO_ENABLE);
917 WARN(cur_state != state,
918 "PLL state assertion failure (expected %s, current %s)\n",
919 state_string(state), state_string(cur_state));
920}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800921
Jani Nikula23538ef2013-08-27 15:12:22 +0300922/* XXX: the dsi pll is shared between MIPI DSI ports */
923static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
924{
925 u32 val;
926 bool cur_state;
927
928 mutex_lock(&dev_priv->dpio_lock);
929 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
930 mutex_unlock(&dev_priv->dpio_lock);
931
932 cur_state = val & DSI_PLL_VCO_EN;
933 WARN(cur_state != state,
934 "DSI PLL state assertion failure (expected %s, current %s)\n",
935 state_string(state), state_string(cur_state));
936}
937#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
938#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
939
Daniel Vetter55607e82013-06-16 21:42:39 +0200940struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200941intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800942{
Daniel Vettere2b78262013-06-07 23:10:03 +0200943 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
944
Daniel Vettera43f6e02013-06-07 23:10:32 +0200945 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200946 return NULL;
947
Daniel Vettera43f6e02013-06-07 23:10:32 +0200948 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200949}
950
Jesse Barnesb24e7172011-01-04 15:09:30 -0800951/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200952void assert_shared_dpll(struct drm_i915_private *dev_priv,
953 struct intel_shared_dpll *pll,
954 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800955{
Jesse Barnes040484a2011-01-03 12:14:26 -0800956 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200957 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800958
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300959 if (HAS_PCH_LPT(dev_priv->dev)) {
960 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
961 return;
962 }
963
Chris Wilson92b27b02012-05-20 18:10:50 +0100964 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200965 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100966 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100967
Daniel Vetter53589012013-06-05 13:34:16 +0200968 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100969 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200970 "%s assertion failure (expected %s, current %s)\n",
971 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800972}
Jesse Barnes040484a2011-01-03 12:14:26 -0800973
974static void assert_fdi_tx(struct drm_i915_private *dev_priv,
975 enum pipe pipe, bool state)
976{
977 int reg;
978 u32 val;
979 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200980 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
981 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800982
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200983 if (HAS_DDI(dev_priv->dev)) {
984 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200985 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300986 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200987 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300988 } else {
989 reg = FDI_TX_CTL(pipe);
990 val = I915_READ(reg);
991 cur_state = !!(val & FDI_TX_ENABLE);
992 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800993 WARN(cur_state != state,
994 "FDI TX state assertion failure (expected %s, current %s)\n",
995 state_string(state), state_string(cur_state));
996}
997#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
998#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
999
1000static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1001 enum pipe pipe, bool state)
1002{
1003 int reg;
1004 u32 val;
1005 bool cur_state;
1006
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001007 reg = FDI_RX_CTL(pipe);
1008 val = I915_READ(reg);
1009 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001010 WARN(cur_state != state,
1011 "FDI RX state assertion failure (expected %s, current %s)\n",
1012 state_string(state), state_string(cur_state));
1013}
1014#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1015#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1016
1017static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1018 enum pipe pipe)
1019{
1020 int reg;
1021 u32 val;
1022
1023 /* ILK FDI PLL is always enabled */
1024 if (dev_priv->info->gen == 5)
1025 return;
1026
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001027 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001028 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001029 return;
1030
Jesse Barnes040484a2011-01-03 12:14:26 -08001031 reg = FDI_TX_CTL(pipe);
1032 val = I915_READ(reg);
1033 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1034}
1035
Daniel Vetter55607e82013-06-16 21:42:39 +02001036void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1037 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001038{
1039 int reg;
1040 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001041 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001042
1043 reg = FDI_RX_CTL(pipe);
1044 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001045 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1046 WARN(cur_state != state,
1047 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1048 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001049}
1050
Jesse Barnesea0760c2011-01-04 15:09:32 -08001051static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1052 enum pipe pipe)
1053{
1054 int pp_reg, lvds_reg;
1055 u32 val;
1056 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001057 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001058
1059 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1060 pp_reg = PCH_PP_CONTROL;
1061 lvds_reg = PCH_LVDS;
1062 } else {
1063 pp_reg = PP_CONTROL;
1064 lvds_reg = LVDS;
1065 }
1066
1067 val = I915_READ(pp_reg);
1068 if (!(val & PANEL_POWER_ON) ||
1069 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1070 locked = false;
1071
1072 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1073 panel_pipe = PIPE_B;
1074
1075 WARN(panel_pipe == pipe && locked,
1076 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001077 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001078}
1079
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001080static void assert_cursor(struct drm_i915_private *dev_priv,
1081 enum pipe pipe, bool state)
1082{
1083 struct drm_device *dev = dev_priv->dev;
1084 bool cur_state;
1085
1086 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1087 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1088 else if (IS_845G(dev) || IS_I865G(dev))
1089 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1090 else
1091 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1092
1093 WARN(cur_state != state,
1094 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1095 pipe_name(pipe), state_string(state), state_string(cur_state));
1096}
1097#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1098#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1099
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001100void assert_pipe(struct drm_i915_private *dev_priv,
1101 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001102{
1103 int reg;
1104 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001105 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001106 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1107 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001108
Daniel Vetter8e636782012-01-22 01:36:48 +01001109 /* if we need the pipe A quirk it must be always on */
1110 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1111 state = true;
1112
Paulo Zanonib97186f2013-05-03 12:15:36 -03001113 if (!intel_display_power_enabled(dev_priv->dev,
1114 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001115 cur_state = false;
1116 } else {
1117 reg = PIPECONF(cpu_transcoder);
1118 val = I915_READ(reg);
1119 cur_state = !!(val & PIPECONF_ENABLE);
1120 }
1121
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001122 WARN(cur_state != state,
1123 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001124 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001125}
1126
Chris Wilson931872f2012-01-16 23:01:13 +00001127static void assert_plane(struct drm_i915_private *dev_priv,
1128 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129{
1130 int reg;
1131 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001132 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133
1134 reg = DSPCNTR(plane);
1135 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001136 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1137 WARN(cur_state != state,
1138 "plane %c assertion failure (expected %s, current %s)\n",
1139 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001140}
1141
Chris Wilson931872f2012-01-16 23:01:13 +00001142#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1143#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1144
Jesse Barnesb24e7172011-01-04 15:09:30 -08001145static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1146 enum pipe pipe)
1147{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001148 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149 int reg, i;
1150 u32 val;
1151 int cur_pipe;
1152
Ville Syrjälä653e1022013-06-04 13:49:05 +03001153 /* Primary planes are fixed to pipes on gen4+ */
1154 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001155 reg = DSPCNTR(pipe);
1156 val = I915_READ(reg);
1157 WARN((val & DISPLAY_PLANE_ENABLE),
1158 "plane %c assertion failure, should be disabled but not\n",
1159 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001160 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001161 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001162
Jesse Barnesb24e7172011-01-04 15:09:30 -08001163 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001164 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001165 reg = DSPCNTR(i);
1166 val = I915_READ(reg);
1167 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1168 DISPPLANE_SEL_PIPE_SHIFT;
1169 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001170 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1171 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001172 }
1173}
1174
Jesse Barnes19332d72013-03-28 09:55:38 -07001175static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1176 enum pipe pipe)
1177{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001178 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001179 int reg, i;
1180 u32 val;
1181
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001182 if (IS_VALLEYVIEW(dev)) {
1183 for (i = 0; i < dev_priv->num_plane; i++) {
1184 reg = SPCNTR(pipe, i);
1185 val = I915_READ(reg);
1186 WARN((val & SP_ENABLE),
1187 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1188 sprite_name(pipe, i), pipe_name(pipe));
1189 }
1190 } else if (INTEL_INFO(dev)->gen >= 7) {
1191 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001192 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001193 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001194 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001195 plane_name(pipe), pipe_name(pipe));
1196 } else if (INTEL_INFO(dev)->gen >= 5) {
1197 reg = DVSCNTR(pipe);
1198 val = I915_READ(reg);
1199 WARN((val & DVS_ENABLE),
1200 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1201 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001202 }
1203}
1204
Jesse Barnes92f25842011-01-04 15:09:34 -08001205static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1206{
1207 u32 val;
1208 bool enabled;
1209
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001210 if (HAS_PCH_LPT(dev_priv->dev)) {
1211 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1212 return;
1213 }
1214
Jesse Barnes92f25842011-01-04 15:09:34 -08001215 val = I915_READ(PCH_DREF_CONTROL);
1216 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1217 DREF_SUPERSPREAD_SOURCE_MASK));
1218 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1219}
1220
Daniel Vetterab9412b2013-05-03 11:49:46 +02001221static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1222 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001223{
1224 int reg;
1225 u32 val;
1226 bool enabled;
1227
Daniel Vetterab9412b2013-05-03 11:49:46 +02001228 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001229 val = I915_READ(reg);
1230 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001231 WARN(enabled,
1232 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1233 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001234}
1235
Keith Packard4e634382011-08-06 10:39:45 -07001236static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1237 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001238{
1239 if ((val & DP_PORT_EN) == 0)
1240 return false;
1241
1242 if (HAS_PCH_CPT(dev_priv->dev)) {
1243 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1244 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1245 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1246 return false;
1247 } else {
1248 if ((val & DP_PIPE_MASK) != (pipe << 30))
1249 return false;
1250 }
1251 return true;
1252}
1253
Keith Packard1519b992011-08-06 10:35:34 -07001254static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1255 enum pipe pipe, u32 val)
1256{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001257 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001258 return false;
1259
1260 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001261 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001262 return false;
1263 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001264 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001265 return false;
1266 }
1267 return true;
1268}
1269
1270static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1271 enum pipe pipe, u32 val)
1272{
1273 if ((val & LVDS_PORT_EN) == 0)
1274 return false;
1275
1276 if (HAS_PCH_CPT(dev_priv->dev)) {
1277 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1278 return false;
1279 } else {
1280 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1281 return false;
1282 }
1283 return true;
1284}
1285
1286static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1287 enum pipe pipe, u32 val)
1288{
1289 if ((val & ADPA_DAC_ENABLE) == 0)
1290 return false;
1291 if (HAS_PCH_CPT(dev_priv->dev)) {
1292 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1293 return false;
1294 } else {
1295 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1296 return false;
1297 }
1298 return true;
1299}
1300
Jesse Barnes291906f2011-02-02 12:28:03 -08001301static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001302 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001303{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001304 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001305 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001306 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001307 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001308
Daniel Vetter75c5da22012-09-10 21:58:29 +02001309 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1310 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001311 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001312}
1313
1314static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1315 enum pipe pipe, int reg)
1316{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001317 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001318 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001319 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001320 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001321
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001322 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001323 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001324 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001325}
1326
1327static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe)
1329{
1330 int reg;
1331 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001332
Keith Packardf0575e92011-07-25 22:12:43 -07001333 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1334 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1335 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001336
1337 reg = PCH_ADPA;
1338 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001339 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001340 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001341 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001342
1343 reg = PCH_LVDS;
1344 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001345 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001346 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001347 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001348
Paulo Zanonie2debe92013-02-18 19:00:27 -03001349 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1350 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1351 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001352}
1353
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001354static void intel_init_dpio(struct drm_device *dev)
1355{
1356 struct drm_i915_private *dev_priv = dev->dev_private;
1357
1358 if (!IS_VALLEYVIEW(dev))
1359 return;
1360
1361 /*
1362 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1363 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1364 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1365 * b. The other bits such as sfr settings / modesel may all be set
1366 * to 0.
1367 *
1368 * This should only be done on init and resume from S3 with both
1369 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1370 */
1371 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1372}
1373
Daniel Vetter426115c2013-07-11 22:13:42 +02001374static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001375{
Daniel Vetter426115c2013-07-11 22:13:42 +02001376 struct drm_device *dev = crtc->base.dev;
1377 struct drm_i915_private *dev_priv = dev->dev_private;
1378 int reg = DPLL(crtc->pipe);
1379 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001380
Daniel Vetter426115c2013-07-11 22:13:42 +02001381 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001382
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001383 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001384 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1385
1386 /* PLL is protected by panel, make sure we can write it */
1387 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001388 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001389
Daniel Vetter426115c2013-07-11 22:13:42 +02001390 I915_WRITE(reg, dpll);
1391 POSTING_READ(reg);
1392 udelay(150);
1393
1394 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1395 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1396
1397 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1398 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001399
1400 /* We do this three times for luck */
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 */
Daniel Vetter426115c2013-07-11 22:13:42 +02001407 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001408 POSTING_READ(reg);
1409 udelay(150); /* wait for warmup */
1410}
1411
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001412static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001413{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001414 struct drm_device *dev = crtc->base.dev;
1415 struct drm_i915_private *dev_priv = dev->dev_private;
1416 int reg = DPLL(crtc->pipe);
1417 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001418
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001419 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001420
1421 /* No really, not for ILK+ */
1422 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001423
1424 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001425 if (IS_MOBILE(dev) && !IS_I830(dev))
1426 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001427
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001428 I915_WRITE(reg, dpll);
1429
1430 /* Wait for the clocks to stabilize. */
1431 POSTING_READ(reg);
1432 udelay(150);
1433
1434 if (INTEL_INFO(dev)->gen >= 4) {
1435 I915_WRITE(DPLL_MD(crtc->pipe),
1436 crtc->config.dpll_hw_state.dpll_md);
1437 } else {
1438 /* The pixel multiplier can only be updated once the
1439 * DPLL is enabled and the clocks are stable.
1440 *
1441 * So write it again.
1442 */
1443 I915_WRITE(reg, dpll);
1444 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001445
1446 /* We do this three times for luck */
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 */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001453 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001454 POSTING_READ(reg);
1455 udelay(150); /* wait for warmup */
1456}
1457
1458/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001459 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001460 * @dev_priv: i915 private structure
1461 * @pipe: pipe PLL to disable
1462 *
1463 * Disable the PLL for @pipe, making sure the pipe is off first.
1464 *
1465 * Note! This is for pre-ILK only.
1466 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001467static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001468{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001469 /* Don't disable pipe A or pipe A PLLs if needed */
1470 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1471 return;
1472
1473 /* Make sure the pipe isn't still relying on us */
1474 assert_pipe_disabled(dev_priv, pipe);
1475
Daniel Vetter50b44a42013-06-05 13:34:33 +02001476 I915_WRITE(DPLL(pipe), 0);
1477 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001478}
1479
Jesse Barnesf6071162013-10-01 10:41:38 -07001480static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1481{
1482 u32 val = 0;
1483
1484 /* Make sure the pipe isn't still relying on us */
1485 assert_pipe_disabled(dev_priv, pipe);
1486
1487 /* Leave integrated clock source enabled */
1488 if (pipe == PIPE_B)
1489 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1490 I915_WRITE(DPLL(pipe), val);
1491 POSTING_READ(DPLL(pipe));
1492}
1493
Jesse Barnes89b667f2013-04-18 14:51:36 -07001494void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1495{
1496 u32 port_mask;
1497
1498 if (!port)
1499 port_mask = DPLL_PORTB_READY_MASK;
1500 else
1501 port_mask = DPLL_PORTC_READY_MASK;
1502
1503 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1504 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1505 'B' + port, I915_READ(DPLL(0)));
1506}
1507
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001508/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001509 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001510 * @dev_priv: i915 private structure
1511 * @pipe: pipe PLL to enable
1512 *
1513 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1514 * drives the transcoder clock.
1515 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001516static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001517{
Daniel Vettere2b78262013-06-07 23:10:03 +02001518 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1519 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001520
Chris Wilson48da64a2012-05-13 20:16:12 +01001521 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001522 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001523 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001524 return;
1525
1526 if (WARN_ON(pll->refcount == 0))
1527 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001528
Daniel Vetter46edb022013-06-05 13:34:12 +02001529 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1530 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001531 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001532
Daniel Vettercdbd2312013-06-05 13:34:03 +02001533 if (pll->active++) {
1534 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001535 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001536 return;
1537 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001538 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001539
Daniel Vetter46edb022013-06-05 13:34:12 +02001540 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001541 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001542 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001543}
1544
Daniel Vettere2b78262013-06-07 23:10:03 +02001545static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001546{
Daniel Vettere2b78262013-06-07 23:10:03 +02001547 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1548 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001549
Jesse Barnes92f25842011-01-04 15:09:34 -08001550 /* PCH only available on ILK+ */
1551 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001552 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001553 return;
1554
Chris Wilson48da64a2012-05-13 20:16:12 +01001555 if (WARN_ON(pll->refcount == 0))
1556 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001557
Daniel Vetter46edb022013-06-05 13:34:12 +02001558 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1559 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001560 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001561
Chris Wilson48da64a2012-05-13 20:16:12 +01001562 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001563 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001564 return;
1565 }
1566
Daniel Vettere9d69442013-06-05 13:34:15 +02001567 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001568 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001569 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001570 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001571
Daniel Vetter46edb022013-06-05 13:34:12 +02001572 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001573 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001574 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001575}
1576
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001577static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1578 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001579{
Daniel Vetter23670b322012-11-01 09:15:30 +01001580 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001581 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001583 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001584
1585 /* PCH only available on ILK+ */
1586 BUG_ON(dev_priv->info->gen < 5);
1587
1588 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001589 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001590 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001591
1592 /* FDI must be feeding us bits for PCH ports */
1593 assert_fdi_tx_enabled(dev_priv, pipe);
1594 assert_fdi_rx_enabled(dev_priv, pipe);
1595
Daniel Vetter23670b322012-11-01 09:15:30 +01001596 if (HAS_PCH_CPT(dev)) {
1597 /* Workaround: Set the timing override bit before enabling the
1598 * pch transcoder. */
1599 reg = TRANS_CHICKEN2(pipe);
1600 val = I915_READ(reg);
1601 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1602 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001603 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001604
Daniel Vetterab9412b2013-05-03 11:49:46 +02001605 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001606 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001607 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001608
1609 if (HAS_PCH_IBX(dev_priv->dev)) {
1610 /*
1611 * make the BPC in transcoder be consistent with
1612 * that in pipeconf reg.
1613 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001614 val &= ~PIPECONF_BPC_MASK;
1615 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001616 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001617
1618 val &= ~TRANS_INTERLACE_MASK;
1619 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001620 if (HAS_PCH_IBX(dev_priv->dev) &&
1621 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1622 val |= TRANS_LEGACY_INTERLACED_ILK;
1623 else
1624 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001625 else
1626 val |= TRANS_PROGRESSIVE;
1627
Jesse Barnes040484a2011-01-03 12:14:26 -08001628 I915_WRITE(reg, val | TRANS_ENABLE);
1629 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001630 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001631}
1632
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001633static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001634 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001635{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001636 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001637
1638 /* PCH only available on ILK+ */
1639 BUG_ON(dev_priv->info->gen < 5);
1640
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001641 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001642 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001643 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001644
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001645 /* Workaround: set timing override bit. */
1646 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001647 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001648 I915_WRITE(_TRANSA_CHICKEN2, val);
1649
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001650 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001651 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001652
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001653 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1654 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001655 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001656 else
1657 val |= TRANS_PROGRESSIVE;
1658
Daniel Vetterab9412b2013-05-03 11:49:46 +02001659 I915_WRITE(LPT_TRANSCONF, val);
1660 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001661 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001662}
1663
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001664static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1665 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001666{
Daniel Vetter23670b322012-11-01 09:15:30 +01001667 struct drm_device *dev = dev_priv->dev;
1668 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001669
1670 /* FDI relies on the transcoder */
1671 assert_fdi_tx_disabled(dev_priv, pipe);
1672 assert_fdi_rx_disabled(dev_priv, pipe);
1673
Jesse Barnes291906f2011-02-02 12:28:03 -08001674 /* Ports must be off as well */
1675 assert_pch_ports_disabled(dev_priv, pipe);
1676
Daniel Vetterab9412b2013-05-03 11:49:46 +02001677 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001678 val = I915_READ(reg);
1679 val &= ~TRANS_ENABLE;
1680 I915_WRITE(reg, val);
1681 /* wait for PCH transcoder off, transcoder state */
1682 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001683 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001684
1685 if (!HAS_PCH_IBX(dev)) {
1686 /* Workaround: Clear the timing override chicken bit again. */
1687 reg = TRANS_CHICKEN2(pipe);
1688 val = I915_READ(reg);
1689 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1690 I915_WRITE(reg, val);
1691 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001692}
1693
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001694static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001695{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001696 u32 val;
1697
Daniel Vetterab9412b2013-05-03 11:49:46 +02001698 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001699 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001700 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001701 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001702 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001703 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001704
1705 /* Workaround: clear timing override bit. */
1706 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001707 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001708 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001709}
1710
1711/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001712 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001713 * @dev_priv: i915 private structure
1714 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001715 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001716 *
1717 * Enable @pipe, making sure that various hardware specific requirements
1718 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1719 *
1720 * @pipe should be %PIPE_A or %PIPE_B.
1721 *
1722 * Will wait until the pipe is actually running (i.e. first vblank) before
1723 * returning.
1724 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001725static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001726 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001727{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001728 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1729 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001730 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001731 int reg;
1732 u32 val;
1733
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001734 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001735 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001736 assert_sprites_disabled(dev_priv, pipe);
1737
Paulo Zanoni681e5812012-12-06 11:12:38 -02001738 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001739 pch_transcoder = TRANSCODER_A;
1740 else
1741 pch_transcoder = pipe;
1742
Jesse Barnesb24e7172011-01-04 15:09:30 -08001743 /*
1744 * A pipe without a PLL won't actually be able to drive bits from
1745 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1746 * need the check.
1747 */
1748 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001749 if (dsi)
1750 assert_dsi_pll_enabled(dev_priv);
1751 else
1752 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001753 else {
1754 if (pch_port) {
1755 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001756 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001757 assert_fdi_tx_pll_enabled(dev_priv,
1758 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001759 }
1760 /* FIXME: assert CPU port conditions for SNB+ */
1761 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001762
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001763 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001764 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001765 if (val & PIPECONF_ENABLE)
1766 return;
1767
1768 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001769 intel_wait_for_vblank(dev_priv->dev, pipe);
1770}
1771
1772/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001773 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001774 * @dev_priv: i915 private structure
1775 * @pipe: pipe to disable
1776 *
1777 * Disable @pipe, making sure that various hardware specific requirements
1778 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1779 *
1780 * @pipe should be %PIPE_A or %PIPE_B.
1781 *
1782 * Will wait until the pipe has shut down before returning.
1783 */
1784static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1785 enum pipe pipe)
1786{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001787 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1788 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001789 int reg;
1790 u32 val;
1791
1792 /*
1793 * Make sure planes won't keep trying to pump pixels to us,
1794 * or we might hang the display.
1795 */
1796 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001797 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001798 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001799
1800 /* Don't disable pipe A or pipe A PLLs if needed */
1801 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1802 return;
1803
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001804 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001805 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001806 if ((val & PIPECONF_ENABLE) == 0)
1807 return;
1808
1809 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1811}
1812
Keith Packardd74362c2011-07-28 14:47:14 -07001813/*
1814 * Plane regs are double buffered, going from enabled->disabled needs a
1815 * trigger in order to latch. The display address reg provides this.
1816 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001817void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001818 enum plane plane)
1819{
Damien Lespiau14f86142012-10-29 15:24:49 +00001820 if (dev_priv->info->gen >= 4)
1821 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1822 else
1823 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001824}
1825
Jesse Barnesb24e7172011-01-04 15:09:30 -08001826/**
1827 * intel_enable_plane - enable a display plane on a given pipe
1828 * @dev_priv: i915 private structure
1829 * @plane: plane to enable
1830 * @pipe: pipe being fed
1831 *
1832 * Enable @plane on @pipe, making sure that @pipe is running first.
1833 */
1834static void intel_enable_plane(struct drm_i915_private *dev_priv,
1835 enum plane plane, enum pipe pipe)
1836{
1837 int reg;
1838 u32 val;
1839
1840 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1841 assert_pipe_enabled(dev_priv, pipe);
1842
1843 reg = DSPCNTR(plane);
1844 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001845 if (val & DISPLAY_PLANE_ENABLE)
1846 return;
1847
1848 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001849 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001850 intel_wait_for_vblank(dev_priv->dev, pipe);
1851}
1852
Jesse Barnesb24e7172011-01-04 15:09:30 -08001853/**
1854 * intel_disable_plane - disable a display plane
1855 * @dev_priv: i915 private structure
1856 * @plane: plane to disable
1857 * @pipe: pipe consuming the data
1858 *
1859 * Disable @plane; should be an independent operation.
1860 */
1861static void intel_disable_plane(struct drm_i915_private *dev_priv,
1862 enum plane plane, enum pipe pipe)
1863{
1864 int reg;
1865 u32 val;
1866
1867 reg = DSPCNTR(plane);
1868 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001869 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1870 return;
1871
1872 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001873 intel_flush_display_plane(dev_priv, plane);
1874 intel_wait_for_vblank(dev_priv->dev, pipe);
1875}
1876
Chris Wilson693db182013-03-05 14:52:39 +00001877static bool need_vtd_wa(struct drm_device *dev)
1878{
1879#ifdef CONFIG_INTEL_IOMMU
1880 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1881 return true;
1882#endif
1883 return false;
1884}
1885
Chris Wilson127bd2a2010-07-23 23:32:05 +01001886int
Chris Wilson48b956c2010-09-14 12:50:34 +01001887intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001888 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001889 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001890{
Chris Wilsonce453d82011-02-21 14:43:56 +00001891 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001892 u32 alignment;
1893 int ret;
1894
Chris Wilson05394f32010-11-08 19:18:58 +00001895 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001896 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001897 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1898 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001899 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001900 alignment = 4 * 1024;
1901 else
1902 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001903 break;
1904 case I915_TILING_X:
1905 /* pin() will align the object as required by fence */
1906 alignment = 0;
1907 break;
1908 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001909 /* Despite that we check this in framebuffer_init userspace can
1910 * screw us over and change the tiling after the fact. Only
1911 * pinned buffers can't change their tiling. */
1912 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001913 return -EINVAL;
1914 default:
1915 BUG();
1916 }
1917
Chris Wilson693db182013-03-05 14:52:39 +00001918 /* Note that the w/a also requires 64 PTE of padding following the
1919 * bo. We currently fill all unused PTE with the shadow page and so
1920 * we should always have valid PTE following the scanout preventing
1921 * the VT-d warning.
1922 */
1923 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1924 alignment = 256 * 1024;
1925
Chris Wilsonce453d82011-02-21 14:43:56 +00001926 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001927 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001928 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001929 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001930
1931 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1932 * fence, whereas 965+ only requires a fence if using
1933 * framebuffer compression. For simplicity, we always install
1934 * a fence as the cost is not that onerous.
1935 */
Chris Wilson06d98132012-04-17 15:31:24 +01001936 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001937 if (ret)
1938 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001939
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001940 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001941
Chris Wilsonce453d82011-02-21 14:43:56 +00001942 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001943 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001944
1945err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001946 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001947err_interruptible:
1948 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001949 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001950}
1951
Chris Wilson1690e1e2011-12-14 13:57:08 +01001952void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1953{
1954 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001955 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001956}
1957
Daniel Vetterc2c75132012-07-05 12:17:30 +02001958/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1959 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001960unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1961 unsigned int tiling_mode,
1962 unsigned int cpp,
1963 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001964{
Chris Wilsonbc752862013-02-21 20:04:31 +00001965 if (tiling_mode != I915_TILING_NONE) {
1966 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001967
Chris Wilsonbc752862013-02-21 20:04:31 +00001968 tile_rows = *y / 8;
1969 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001970
Chris Wilsonbc752862013-02-21 20:04:31 +00001971 tiles = *x / (512/cpp);
1972 *x %= 512/cpp;
1973
1974 return tile_rows * pitch * 8 + tiles * 4096;
1975 } else {
1976 unsigned int offset;
1977
1978 offset = *y * pitch + *x * cpp;
1979 *y = 0;
1980 *x = (offset & 4095) / cpp;
1981 return offset & -4096;
1982 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001983}
1984
Jesse Barnes17638cd2011-06-24 12:19:23 -07001985static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1986 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001987{
1988 struct drm_device *dev = crtc->dev;
1989 struct drm_i915_private *dev_priv = dev->dev_private;
1990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1991 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001992 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001993 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001994 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001995 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001996 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001997
1998 switch (plane) {
1999 case 0:
2000 case 1:
2001 break;
2002 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002003 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002004 return -EINVAL;
2005 }
2006
2007 intel_fb = to_intel_framebuffer(fb);
2008 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002009
Chris Wilson5eddb702010-09-11 13:48:45 +01002010 reg = DSPCNTR(plane);
2011 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002012 /* Mask out pixel format bits in case we change it */
2013 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002014 switch (fb->pixel_format) {
2015 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002016 dspcntr |= DISPPLANE_8BPP;
2017 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002018 case DRM_FORMAT_XRGB1555:
2019 case DRM_FORMAT_ARGB1555:
2020 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002021 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002022 case DRM_FORMAT_RGB565:
2023 dspcntr |= DISPPLANE_BGRX565;
2024 break;
2025 case DRM_FORMAT_XRGB8888:
2026 case DRM_FORMAT_ARGB8888:
2027 dspcntr |= DISPPLANE_BGRX888;
2028 break;
2029 case DRM_FORMAT_XBGR8888:
2030 case DRM_FORMAT_ABGR8888:
2031 dspcntr |= DISPPLANE_RGBX888;
2032 break;
2033 case DRM_FORMAT_XRGB2101010:
2034 case DRM_FORMAT_ARGB2101010:
2035 dspcntr |= DISPPLANE_BGRX101010;
2036 break;
2037 case DRM_FORMAT_XBGR2101010:
2038 case DRM_FORMAT_ABGR2101010:
2039 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002040 break;
2041 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002042 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002043 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002044
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002045 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002046 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002047 dspcntr |= DISPPLANE_TILED;
2048 else
2049 dspcntr &= ~DISPPLANE_TILED;
2050 }
2051
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002052 if (IS_G4X(dev))
2053 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2054
Chris Wilson5eddb702010-09-11 13:48:45 +01002055 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002056
Daniel Vettere506a0c2012-07-05 12:17:29 +02002057 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002058
Daniel Vetterc2c75132012-07-05 12:17:30 +02002059 if (INTEL_INFO(dev)->gen >= 4) {
2060 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002061 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2062 fb->bits_per_pixel / 8,
2063 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002064 linear_offset -= intel_crtc->dspaddr_offset;
2065 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002066 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002067 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002068
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002069 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2070 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2071 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002072 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002073 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002074 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002075 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002076 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002077 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002078 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002079 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002080 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002081
Jesse Barnes17638cd2011-06-24 12:19:23 -07002082 return 0;
2083}
2084
2085static int ironlake_update_plane(struct drm_crtc *crtc,
2086 struct drm_framebuffer *fb, int x, int y)
2087{
2088 struct drm_device *dev = crtc->dev;
2089 struct drm_i915_private *dev_priv = dev->dev_private;
2090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2091 struct intel_framebuffer *intel_fb;
2092 struct drm_i915_gem_object *obj;
2093 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002094 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002095 u32 dspcntr;
2096 u32 reg;
2097
2098 switch (plane) {
2099 case 0:
2100 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002101 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 break;
2103 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002104 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002105 return -EINVAL;
2106 }
2107
2108 intel_fb = to_intel_framebuffer(fb);
2109 obj = intel_fb->obj;
2110
2111 reg = DSPCNTR(plane);
2112 dspcntr = I915_READ(reg);
2113 /* Mask out pixel format bits in case we change it */
2114 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002115 switch (fb->pixel_format) {
2116 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002117 dspcntr |= DISPPLANE_8BPP;
2118 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002119 case DRM_FORMAT_RGB565:
2120 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002121 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002122 case DRM_FORMAT_XRGB8888:
2123 case DRM_FORMAT_ARGB8888:
2124 dspcntr |= DISPPLANE_BGRX888;
2125 break;
2126 case DRM_FORMAT_XBGR8888:
2127 case DRM_FORMAT_ABGR8888:
2128 dspcntr |= DISPPLANE_RGBX888;
2129 break;
2130 case DRM_FORMAT_XRGB2101010:
2131 case DRM_FORMAT_ARGB2101010:
2132 dspcntr |= DISPPLANE_BGRX101010;
2133 break;
2134 case DRM_FORMAT_XBGR2101010:
2135 case DRM_FORMAT_ABGR2101010:
2136 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002137 break;
2138 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002139 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002140 }
2141
2142 if (obj->tiling_mode != I915_TILING_NONE)
2143 dspcntr |= DISPPLANE_TILED;
2144 else
2145 dspcntr &= ~DISPPLANE_TILED;
2146
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002147 if (IS_HASWELL(dev))
2148 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2149 else
2150 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002151
2152 I915_WRITE(reg, dspcntr);
2153
Daniel Vettere506a0c2012-07-05 12:17:29 +02002154 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002155 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002156 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2157 fb->bits_per_pixel / 8,
2158 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002159 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002160
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002161 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2162 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2163 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002164 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002165 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002166 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002167 if (IS_HASWELL(dev)) {
2168 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2169 } else {
2170 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2171 I915_WRITE(DSPLINOFF(plane), linear_offset);
2172 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002173 POSTING_READ(reg);
2174
2175 return 0;
2176}
2177
2178/* Assume fb object is pinned & idle & fenced and just update base pointers */
2179static int
2180intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2181 int x, int y, enum mode_set_atomic state)
2182{
2183 struct drm_device *dev = crtc->dev;
2184 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002185
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002186 if (dev_priv->display.disable_fbc)
2187 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002188 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002189
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002190 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002191}
2192
Ville Syrjälä96a02912013-02-18 19:08:49 +02002193void intel_display_handle_reset(struct drm_device *dev)
2194{
2195 struct drm_i915_private *dev_priv = dev->dev_private;
2196 struct drm_crtc *crtc;
2197
2198 /*
2199 * Flips in the rings have been nuked by the reset,
2200 * so complete all pending flips so that user space
2201 * will get its events and not get stuck.
2202 *
2203 * Also update the base address of all primary
2204 * planes to the the last fb to make sure we're
2205 * showing the correct fb after a reset.
2206 *
2207 * Need to make two loops over the crtcs so that we
2208 * don't try to grab a crtc mutex before the
2209 * pending_flip_queue really got woken up.
2210 */
2211
2212 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2214 enum plane plane = intel_crtc->plane;
2215
2216 intel_prepare_page_flip(dev, plane);
2217 intel_finish_page_flip_plane(dev, plane);
2218 }
2219
2220 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2222
2223 mutex_lock(&crtc->mutex);
2224 if (intel_crtc->active)
2225 dev_priv->display.update_plane(crtc, crtc->fb,
2226 crtc->x, crtc->y);
2227 mutex_unlock(&crtc->mutex);
2228 }
2229}
2230
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002231static int
Chris Wilson14667a42012-04-03 17:58:35 +01002232intel_finish_fb(struct drm_framebuffer *old_fb)
2233{
2234 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2235 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2236 bool was_interruptible = dev_priv->mm.interruptible;
2237 int ret;
2238
Chris Wilson14667a42012-04-03 17:58:35 +01002239 /* Big Hammer, we also need to ensure that any pending
2240 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2241 * current scanout is retired before unpinning the old
2242 * framebuffer.
2243 *
2244 * This should only fail upon a hung GPU, in which case we
2245 * can safely continue.
2246 */
2247 dev_priv->mm.interruptible = false;
2248 ret = i915_gem_object_finish_gpu(obj);
2249 dev_priv->mm.interruptible = was_interruptible;
2250
2251 return ret;
2252}
2253
Ville Syrjälä198598d2012-10-31 17:50:24 +02002254static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2255{
2256 struct drm_device *dev = crtc->dev;
2257 struct drm_i915_master_private *master_priv;
2258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2259
2260 if (!dev->primary->master)
2261 return;
2262
2263 master_priv = dev->primary->master->driver_priv;
2264 if (!master_priv->sarea_priv)
2265 return;
2266
2267 switch (intel_crtc->pipe) {
2268 case 0:
2269 master_priv->sarea_priv->pipeA_x = x;
2270 master_priv->sarea_priv->pipeA_y = y;
2271 break;
2272 case 1:
2273 master_priv->sarea_priv->pipeB_x = x;
2274 master_priv->sarea_priv->pipeB_y = y;
2275 break;
2276 default:
2277 break;
2278 }
2279}
2280
Chris Wilson14667a42012-04-03 17:58:35 +01002281static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002282intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002283 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002284{
2285 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002286 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002288 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002289 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002290
2291 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002292 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002293 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002294 return 0;
2295 }
2296
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002297 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002298 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2299 plane_name(intel_crtc->plane),
2300 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002301 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002302 }
2303
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002304 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002305 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002306 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002307 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002308 if (ret != 0) {
2309 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002310 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002311 return ret;
2312 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002313
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002314 /*
2315 * Update pipe size and adjust fitter if needed: the reason for this is
2316 * that in compute_mode_changes we check the native mode (not the pfit
2317 * mode) to see if we can flip rather than do a full mode set. In the
2318 * fastboot case, we'll flip, but if we don't update the pipesrc and
2319 * pfit state, we'll end up with a big fb scanned out into the wrong
2320 * sized surface.
2321 *
2322 * To fix this properly, we need to hoist the checks up into
2323 * compute_mode_changes (or above), check the actual pfit state and
2324 * whether the platform allows pfit disable with pipe active, and only
2325 * then update the pipesrc and pfit state, even on the flip path.
2326 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002327 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002328 const struct drm_display_mode *adjusted_mode =
2329 &intel_crtc->config.adjusted_mode;
2330
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002331 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002332 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2333 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002334 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002335 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2336 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2337 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2338 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2339 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2340 }
2341 }
2342
Daniel Vetter94352cf2012-07-05 22:51:56 +02002343 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002344 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002345 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002346 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002347 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002348 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002349 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002350
Daniel Vetter94352cf2012-07-05 22:51:56 +02002351 old_fb = crtc->fb;
2352 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002353 crtc->x = x;
2354 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002355
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002356 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002357 if (intel_crtc->active && old_fb != fb)
2358 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002359 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002360 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002361
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002362 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002363 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002364 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002365
Ville Syrjälä198598d2012-10-31 17:50:24 +02002366 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002367
2368 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002369}
2370
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002371static void intel_fdi_normal_train(struct drm_crtc *crtc)
2372{
2373 struct drm_device *dev = crtc->dev;
2374 struct drm_i915_private *dev_priv = dev->dev_private;
2375 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2376 int pipe = intel_crtc->pipe;
2377 u32 reg, temp;
2378
2379 /* enable normal train */
2380 reg = FDI_TX_CTL(pipe);
2381 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002382 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002383 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2384 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002385 } else {
2386 temp &= ~FDI_LINK_TRAIN_NONE;
2387 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002388 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002389 I915_WRITE(reg, temp);
2390
2391 reg = FDI_RX_CTL(pipe);
2392 temp = I915_READ(reg);
2393 if (HAS_PCH_CPT(dev)) {
2394 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2395 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2396 } else {
2397 temp &= ~FDI_LINK_TRAIN_NONE;
2398 temp |= FDI_LINK_TRAIN_NONE;
2399 }
2400 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2401
2402 /* wait one idle pattern time */
2403 POSTING_READ(reg);
2404 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002405
2406 /* IVB wants error correction enabled */
2407 if (IS_IVYBRIDGE(dev))
2408 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2409 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002410}
2411
Daniel Vetter1e833f42013-02-19 22:31:57 +01002412static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2413{
2414 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2415}
2416
Daniel Vetter01a415f2012-10-27 15:58:40 +02002417static void ivb_modeset_global_resources(struct drm_device *dev)
2418{
2419 struct drm_i915_private *dev_priv = dev->dev_private;
2420 struct intel_crtc *pipe_B_crtc =
2421 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2422 struct intel_crtc *pipe_C_crtc =
2423 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2424 uint32_t temp;
2425
Daniel Vetter1e833f42013-02-19 22:31:57 +01002426 /*
2427 * When everything is off disable fdi C so that we could enable fdi B
2428 * with all lanes. Note that we don't care about enabled pipes without
2429 * an enabled pch encoder.
2430 */
2431 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2432 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002433 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2434 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2435
2436 temp = I915_READ(SOUTH_CHICKEN1);
2437 temp &= ~FDI_BC_BIFURCATION_SELECT;
2438 DRM_DEBUG_KMS("disabling fdi C rx\n");
2439 I915_WRITE(SOUTH_CHICKEN1, temp);
2440 }
2441}
2442
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002443/* The FDI link training functions for ILK/Ibexpeak. */
2444static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2445{
2446 struct drm_device *dev = crtc->dev;
2447 struct drm_i915_private *dev_priv = dev->dev_private;
2448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2449 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002450 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002451 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002452
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002453 /* FDI needs bits from pipe & plane first */
2454 assert_pipe_enabled(dev_priv, pipe);
2455 assert_plane_enabled(dev_priv, plane);
2456
Adam Jacksone1a44742010-06-25 15:32:14 -04002457 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2458 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002459 reg = FDI_RX_IMR(pipe);
2460 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002461 temp &= ~FDI_RX_SYMBOL_LOCK;
2462 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002463 I915_WRITE(reg, temp);
2464 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002465 udelay(150);
2466
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002468 reg = FDI_TX_CTL(pipe);
2469 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002470 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2471 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472 temp &= ~FDI_LINK_TRAIN_NONE;
2473 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002474 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002475
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 reg = FDI_RX_CTL(pipe);
2477 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 temp &= ~FDI_LINK_TRAIN_NONE;
2479 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2481
2482 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 udelay(150);
2484
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002485 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002486 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2487 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2488 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002489
Chris Wilson5eddb702010-09-11 13:48:45 +01002490 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002491 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2494
2495 if ((temp & FDI_RX_BIT_LOCK)) {
2496 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002497 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498 break;
2499 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002500 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002501 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002502 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002503
2504 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002505 reg = FDI_TX_CTL(pipe);
2506 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002507 temp &= ~FDI_LINK_TRAIN_NONE;
2508 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002509 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002510
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 reg = FDI_RX_CTL(pipe);
2512 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002513 temp &= ~FDI_LINK_TRAIN_NONE;
2514 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 I915_WRITE(reg, temp);
2516
2517 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002518 udelay(150);
2519
Chris Wilson5eddb702010-09-11 13:48:45 +01002520 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002521 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002522 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002523 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2524
2525 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002526 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002527 DRM_DEBUG_KMS("FDI train 2 done.\n");
2528 break;
2529 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002531 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533
2534 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002535
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536}
2537
Akshay Joshi0206e352011-08-16 15:34:10 -04002538static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2540 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2541 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2542 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2543};
2544
2545/* The FDI link training functions for SNB/Cougarpoint. */
2546static void gen6_fdi_link_train(struct drm_crtc *crtc)
2547{
2548 struct drm_device *dev = crtc->dev;
2549 struct drm_i915_private *dev_priv = dev->dev_private;
2550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2551 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002552 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002553
Adam Jacksone1a44742010-06-25 15:32:14 -04002554 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2555 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 reg = FDI_RX_IMR(pipe);
2557 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002558 temp &= ~FDI_RX_SYMBOL_LOCK;
2559 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002560 I915_WRITE(reg, temp);
2561
2562 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002563 udelay(150);
2564
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002565 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 reg = FDI_TX_CTL(pipe);
2567 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002568 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2569 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002570 temp &= ~FDI_LINK_TRAIN_NONE;
2571 temp |= FDI_LINK_TRAIN_PATTERN_1;
2572 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2573 /* SNB-B */
2574 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002575 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002576
Daniel Vetterd74cf322012-10-26 10:58:13 +02002577 I915_WRITE(FDI_RX_MISC(pipe),
2578 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2579
Chris Wilson5eddb702010-09-11 13:48:45 +01002580 reg = FDI_RX_CTL(pipe);
2581 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002582 if (HAS_PCH_CPT(dev)) {
2583 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2584 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2585 } else {
2586 temp &= ~FDI_LINK_TRAIN_NONE;
2587 temp |= FDI_LINK_TRAIN_PATTERN_1;
2588 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002589 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2590
2591 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002592 udelay(150);
2593
Akshay Joshi0206e352011-08-16 15:34:10 -04002594 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002595 reg = FDI_TX_CTL(pipe);
2596 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2598 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002599 I915_WRITE(reg, temp);
2600
2601 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002602 udelay(500);
2603
Sean Paulfa37d392012-03-02 12:53:39 -05002604 for (retry = 0; retry < 5; retry++) {
2605 reg = FDI_RX_IIR(pipe);
2606 temp = I915_READ(reg);
2607 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2608 if (temp & FDI_RX_BIT_LOCK) {
2609 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2610 DRM_DEBUG_KMS("FDI train 1 done.\n");
2611 break;
2612 }
2613 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002614 }
Sean Paulfa37d392012-03-02 12:53:39 -05002615 if (retry < 5)
2616 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617 }
2618 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002619 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002620
2621 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002622 reg = FDI_TX_CTL(pipe);
2623 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002624 temp &= ~FDI_LINK_TRAIN_NONE;
2625 temp |= FDI_LINK_TRAIN_PATTERN_2;
2626 if (IS_GEN6(dev)) {
2627 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2628 /* SNB-B */
2629 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2630 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002631 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002632
Chris Wilson5eddb702010-09-11 13:48:45 +01002633 reg = FDI_RX_CTL(pipe);
2634 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002635 if (HAS_PCH_CPT(dev)) {
2636 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2637 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2638 } else {
2639 temp &= ~FDI_LINK_TRAIN_NONE;
2640 temp |= FDI_LINK_TRAIN_PATTERN_2;
2641 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002642 I915_WRITE(reg, temp);
2643
2644 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002645 udelay(150);
2646
Akshay Joshi0206e352011-08-16 15:34:10 -04002647 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002648 reg = FDI_TX_CTL(pipe);
2649 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002650 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2651 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002652 I915_WRITE(reg, temp);
2653
2654 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002655 udelay(500);
2656
Sean Paulfa37d392012-03-02 12:53:39 -05002657 for (retry = 0; retry < 5; retry++) {
2658 reg = FDI_RX_IIR(pipe);
2659 temp = I915_READ(reg);
2660 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2661 if (temp & FDI_RX_SYMBOL_LOCK) {
2662 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2663 DRM_DEBUG_KMS("FDI train 2 done.\n");
2664 break;
2665 }
2666 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002667 }
Sean Paulfa37d392012-03-02 12:53:39 -05002668 if (retry < 5)
2669 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002670 }
2671 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002672 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002673
2674 DRM_DEBUG_KMS("FDI train done.\n");
2675}
2676
Jesse Barnes357555c2011-04-28 15:09:55 -07002677/* Manual link training for Ivy Bridge A0 parts */
2678static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2679{
2680 struct drm_device *dev = crtc->dev;
2681 struct drm_i915_private *dev_priv = dev->dev_private;
2682 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2683 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002684 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002685
2686 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2687 for train result */
2688 reg = FDI_RX_IMR(pipe);
2689 temp = I915_READ(reg);
2690 temp &= ~FDI_RX_SYMBOL_LOCK;
2691 temp &= ~FDI_RX_BIT_LOCK;
2692 I915_WRITE(reg, temp);
2693
2694 POSTING_READ(reg);
2695 udelay(150);
2696
Daniel Vetter01a415f2012-10-27 15:58:40 +02002697 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2698 I915_READ(FDI_RX_IIR(pipe)));
2699
Jesse Barnes139ccd32013-08-19 11:04:55 -07002700 /* Try each vswing and preemphasis setting twice before moving on */
2701 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2702 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002703 reg = FDI_TX_CTL(pipe);
2704 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002705 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2706 temp &= ~FDI_TX_ENABLE;
2707 I915_WRITE(reg, temp);
2708
2709 reg = FDI_RX_CTL(pipe);
2710 temp = I915_READ(reg);
2711 temp &= ~FDI_LINK_TRAIN_AUTO;
2712 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2713 temp &= ~FDI_RX_ENABLE;
2714 I915_WRITE(reg, temp);
2715
2716 /* enable CPU FDI TX and PCH FDI RX */
2717 reg = FDI_TX_CTL(pipe);
2718 temp = I915_READ(reg);
2719 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2720 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2721 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002722 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002723 temp |= snb_b_fdi_train_param[j/2];
2724 temp |= FDI_COMPOSITE_SYNC;
2725 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2726
2727 I915_WRITE(FDI_RX_MISC(pipe),
2728 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2729
2730 reg = FDI_RX_CTL(pipe);
2731 temp = I915_READ(reg);
2732 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2733 temp |= FDI_COMPOSITE_SYNC;
2734 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2735
2736 POSTING_READ(reg);
2737 udelay(1); /* should be 0.5us */
2738
2739 for (i = 0; i < 4; i++) {
2740 reg = FDI_RX_IIR(pipe);
2741 temp = I915_READ(reg);
2742 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2743
2744 if (temp & FDI_RX_BIT_LOCK ||
2745 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2746 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2747 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2748 i);
2749 break;
2750 }
2751 udelay(1); /* should be 0.5us */
2752 }
2753 if (i == 4) {
2754 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2755 continue;
2756 }
2757
2758 /* Train 2 */
2759 reg = FDI_TX_CTL(pipe);
2760 temp = I915_READ(reg);
2761 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2762 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2763 I915_WRITE(reg, temp);
2764
2765 reg = FDI_RX_CTL(pipe);
2766 temp = I915_READ(reg);
2767 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2768 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002769 I915_WRITE(reg, temp);
2770
2771 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002772 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002773
Jesse Barnes139ccd32013-08-19 11:04:55 -07002774 for (i = 0; i < 4; i++) {
2775 reg = FDI_RX_IIR(pipe);
2776 temp = I915_READ(reg);
2777 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002778
Jesse Barnes139ccd32013-08-19 11:04:55 -07002779 if (temp & FDI_RX_SYMBOL_LOCK ||
2780 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2781 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2782 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2783 i);
2784 goto train_done;
2785 }
2786 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002787 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002788 if (i == 4)
2789 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002790 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002791
Jesse Barnes139ccd32013-08-19 11:04:55 -07002792train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002793 DRM_DEBUG_KMS("FDI train done.\n");
2794}
2795
Daniel Vetter88cefb62012-08-12 19:27:14 +02002796static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002797{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002798 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002799 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002800 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002801 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002802
Jesse Barnesc64e3112010-09-10 11:27:03 -07002803
Jesse Barnes0e23b992010-09-10 11:10:00 -07002804 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002805 reg = FDI_RX_CTL(pipe);
2806 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002807 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2808 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002809 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002810 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2811
2812 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002813 udelay(200);
2814
2815 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002816 temp = I915_READ(reg);
2817 I915_WRITE(reg, temp | FDI_PCDCLK);
2818
2819 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002820 udelay(200);
2821
Paulo Zanoni20749732012-11-23 15:30:38 -02002822 /* Enable CPU FDI TX PLL, always on for Ironlake */
2823 reg = FDI_TX_CTL(pipe);
2824 temp = I915_READ(reg);
2825 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2826 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002827
Paulo Zanoni20749732012-11-23 15:30:38 -02002828 POSTING_READ(reg);
2829 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002830 }
2831}
2832
Daniel Vetter88cefb62012-08-12 19:27:14 +02002833static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2834{
2835 struct drm_device *dev = intel_crtc->base.dev;
2836 struct drm_i915_private *dev_priv = dev->dev_private;
2837 int pipe = intel_crtc->pipe;
2838 u32 reg, temp;
2839
2840 /* Switch from PCDclk to Rawclk */
2841 reg = FDI_RX_CTL(pipe);
2842 temp = I915_READ(reg);
2843 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2844
2845 /* Disable CPU FDI TX PLL */
2846 reg = FDI_TX_CTL(pipe);
2847 temp = I915_READ(reg);
2848 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2849
2850 POSTING_READ(reg);
2851 udelay(100);
2852
2853 reg = FDI_RX_CTL(pipe);
2854 temp = I915_READ(reg);
2855 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2856
2857 /* Wait for the clocks to turn off. */
2858 POSTING_READ(reg);
2859 udelay(100);
2860}
2861
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002862static void ironlake_fdi_disable(struct drm_crtc *crtc)
2863{
2864 struct drm_device *dev = crtc->dev;
2865 struct drm_i915_private *dev_priv = dev->dev_private;
2866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2867 int pipe = intel_crtc->pipe;
2868 u32 reg, temp;
2869
2870 /* disable CPU FDI tx and PCH FDI rx */
2871 reg = FDI_TX_CTL(pipe);
2872 temp = I915_READ(reg);
2873 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2874 POSTING_READ(reg);
2875
2876 reg = FDI_RX_CTL(pipe);
2877 temp = I915_READ(reg);
2878 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002879 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002880 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2881
2882 POSTING_READ(reg);
2883 udelay(100);
2884
2885 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002886 if (HAS_PCH_IBX(dev)) {
2887 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002888 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002889
2890 /* still set train pattern 1 */
2891 reg = FDI_TX_CTL(pipe);
2892 temp = I915_READ(reg);
2893 temp &= ~FDI_LINK_TRAIN_NONE;
2894 temp |= FDI_LINK_TRAIN_PATTERN_1;
2895 I915_WRITE(reg, temp);
2896
2897 reg = FDI_RX_CTL(pipe);
2898 temp = I915_READ(reg);
2899 if (HAS_PCH_CPT(dev)) {
2900 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2901 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2902 } else {
2903 temp &= ~FDI_LINK_TRAIN_NONE;
2904 temp |= FDI_LINK_TRAIN_PATTERN_1;
2905 }
2906 /* BPC in FDI rx is consistent with that in PIPECONF */
2907 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002908 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002909 I915_WRITE(reg, temp);
2910
2911 POSTING_READ(reg);
2912 udelay(100);
2913}
2914
Chris Wilson5bb61642012-09-27 21:25:58 +01002915static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2916{
2917 struct drm_device *dev = crtc->dev;
2918 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002920 unsigned long flags;
2921 bool pending;
2922
Ville Syrjälä10d83732013-01-29 18:13:34 +02002923 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2924 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002925 return false;
2926
2927 spin_lock_irqsave(&dev->event_lock, flags);
2928 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2929 spin_unlock_irqrestore(&dev->event_lock, flags);
2930
2931 return pending;
2932}
2933
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002934static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2935{
Chris Wilson0f911282012-04-17 10:05:38 +01002936 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002937 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002938
2939 if (crtc->fb == NULL)
2940 return;
2941
Daniel Vetter2c10d572012-12-20 21:24:07 +01002942 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2943
Chris Wilson5bb61642012-09-27 21:25:58 +01002944 wait_event(dev_priv->pending_flip_queue,
2945 !intel_crtc_has_pending_flip(crtc));
2946
Chris Wilson0f911282012-04-17 10:05:38 +01002947 mutex_lock(&dev->struct_mutex);
2948 intel_finish_fb(crtc->fb);
2949 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002950}
2951
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002952/* Program iCLKIP clock to the desired frequency */
2953static void lpt_program_iclkip(struct drm_crtc *crtc)
2954{
2955 struct drm_device *dev = crtc->dev;
2956 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002957 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002958 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2959 u32 temp;
2960
Daniel Vetter09153002012-12-12 14:06:44 +01002961 mutex_lock(&dev_priv->dpio_lock);
2962
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002963 /* It is necessary to ungate the pixclk gate prior to programming
2964 * the divisors, and gate it back when it is done.
2965 */
2966 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2967
2968 /* Disable SSCCTL */
2969 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002970 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2971 SBI_SSCCTL_DISABLE,
2972 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002973
2974 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002975 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002976 auxdiv = 1;
2977 divsel = 0x41;
2978 phaseinc = 0x20;
2979 } else {
2980 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01002981 * but the adjusted_mode->crtc_clock in in KHz. To get the
2982 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002983 * convert the virtual clock precision to KHz here for higher
2984 * precision.
2985 */
2986 u32 iclk_virtual_root_freq = 172800 * 1000;
2987 u32 iclk_pi_range = 64;
2988 u32 desired_divisor, msb_divisor_value, pi_value;
2989
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002990 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002991 msb_divisor_value = desired_divisor / iclk_pi_range;
2992 pi_value = desired_divisor % iclk_pi_range;
2993
2994 auxdiv = 0;
2995 divsel = msb_divisor_value - 2;
2996 phaseinc = pi_value;
2997 }
2998
2999 /* This should not happen with any sane values */
3000 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3001 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3002 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3003 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3004
3005 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 +03003006 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003007 auxdiv,
3008 divsel,
3009 phasedir,
3010 phaseinc);
3011
3012 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003013 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003014 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3015 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3016 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3017 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3018 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3019 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003020 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003021
3022 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003023 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003024 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3025 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003026 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003027
3028 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003029 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003030 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003031 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003032
3033 /* Wait for initialization time */
3034 udelay(24);
3035
3036 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003037
3038 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003039}
3040
Daniel Vetter275f01b22013-05-03 11:49:47 +02003041static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3042 enum pipe pch_transcoder)
3043{
3044 struct drm_device *dev = crtc->base.dev;
3045 struct drm_i915_private *dev_priv = dev->dev_private;
3046 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3047
3048 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3049 I915_READ(HTOTAL(cpu_transcoder)));
3050 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3051 I915_READ(HBLANK(cpu_transcoder)));
3052 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3053 I915_READ(HSYNC(cpu_transcoder)));
3054
3055 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3056 I915_READ(VTOTAL(cpu_transcoder)));
3057 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3058 I915_READ(VBLANK(cpu_transcoder)));
3059 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3060 I915_READ(VSYNC(cpu_transcoder)));
3061 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3062 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3063}
3064
Jesse Barnesf67a5592011-01-05 10:31:48 -08003065/*
3066 * Enable PCH resources required for PCH ports:
3067 * - PCH PLLs
3068 * - FDI training & RX/TX
3069 * - update transcoder timings
3070 * - DP transcoding bits
3071 * - transcoder
3072 */
3073static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003074{
3075 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003076 struct drm_i915_private *dev_priv = dev->dev_private;
3077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3078 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003079 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003080
Daniel Vetterab9412b2013-05-03 11:49:46 +02003081 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003082
Daniel Vettercd986ab2012-10-26 10:58:12 +02003083 /* Write the TU size bits before fdi link training, so that error
3084 * detection works. */
3085 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3086 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3087
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003088 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003089 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003090
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003091 /* We need to program the right clock selection before writing the pixel
3092 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003093 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003094 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003095
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003096 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003097 temp |= TRANS_DPLL_ENABLE(pipe);
3098 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003099 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003100 temp |= sel;
3101 else
3102 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003103 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003104 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003105
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003106 /* XXX: pch pll's can be enabled any time before we enable the PCH
3107 * transcoder, and we actually should do this to not upset any PCH
3108 * transcoder that already use the clock when we share it.
3109 *
3110 * Note that enable_shared_dpll tries to do the right thing, but
3111 * get_shared_dpll unconditionally resets the pll - we need that to have
3112 * the right LVDS enable sequence. */
3113 ironlake_enable_shared_dpll(intel_crtc);
3114
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003115 /* set transcoder timing, panel must allow it */
3116 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003117 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003118
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003119 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003120
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121 /* For PCH DP, enable TRANS_DP_CTL */
3122 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003123 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3124 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003125 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003126 reg = TRANS_DP_CTL(pipe);
3127 temp = I915_READ(reg);
3128 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003129 TRANS_DP_SYNC_MASK |
3130 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003131 temp |= (TRANS_DP_OUTPUT_ENABLE |
3132 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003133 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003134
3135 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003136 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003137 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003138 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003139
3140 switch (intel_trans_dp_port_sel(crtc)) {
3141 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003142 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003143 break;
3144 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003145 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003146 break;
3147 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003148 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003149 break;
3150 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003151 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003152 }
3153
Chris Wilson5eddb702010-09-11 13:48:45 +01003154 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003155 }
3156
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003157 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003158}
3159
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003160static void lpt_pch_enable(struct drm_crtc *crtc)
3161{
3162 struct drm_device *dev = crtc->dev;
3163 struct drm_i915_private *dev_priv = dev->dev_private;
3164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003165 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003166
Daniel Vetterab9412b2013-05-03 11:49:46 +02003167 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003168
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003169 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003170
Paulo Zanoni0540e482012-10-31 18:12:40 -02003171 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003172 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003173
Paulo Zanoni937bb612012-10-31 18:12:47 -02003174 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003175}
3176
Daniel Vettere2b78262013-06-07 23:10:03 +02003177static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003178{
Daniel Vettere2b78262013-06-07 23:10:03 +02003179 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003180
3181 if (pll == NULL)
3182 return;
3183
3184 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003185 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003186 return;
3187 }
3188
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003189 if (--pll->refcount == 0) {
3190 WARN_ON(pll->on);
3191 WARN_ON(pll->active);
3192 }
3193
Daniel Vettera43f6e02013-06-07 23:10:32 +02003194 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003195}
3196
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003197static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003198{
Daniel Vettere2b78262013-06-07 23:10:03 +02003199 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3200 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3201 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003202
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003203 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003204 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3205 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003206 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003207 }
3208
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003209 if (HAS_PCH_IBX(dev_priv->dev)) {
3210 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003211 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003212 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003213
Daniel Vetter46edb022013-06-05 13:34:12 +02003214 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3215 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003216
3217 goto found;
3218 }
3219
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003220 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3221 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003222
3223 /* Only want to check enabled timings first */
3224 if (pll->refcount == 0)
3225 continue;
3226
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003227 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3228 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003229 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003230 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003231 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003232
3233 goto found;
3234 }
3235 }
3236
3237 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003238 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3239 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003240 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003241 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3242 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003243 goto found;
3244 }
3245 }
3246
3247 return NULL;
3248
3249found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003250 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003251 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3252 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003253
Daniel Vettercdbd2312013-06-05 13:34:03 +02003254 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003255 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3256 sizeof(pll->hw_state));
3257
Daniel Vetter46edb022013-06-05 13:34:12 +02003258 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003259 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003260 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003261
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003262 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003263 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003264 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003265
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003266 return pll;
3267}
3268
Daniel Vettera1520312013-05-03 11:49:50 +02003269static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003270{
3271 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003272 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003273 u32 temp;
3274
3275 temp = I915_READ(dslreg);
3276 udelay(500);
3277 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003278 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003279 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003280 }
3281}
3282
Jesse Barnesb074cec2013-04-25 12:55:02 -07003283static void ironlake_pfit_enable(struct intel_crtc *crtc)
3284{
3285 struct drm_device *dev = crtc->base.dev;
3286 struct drm_i915_private *dev_priv = dev->dev_private;
3287 int pipe = crtc->pipe;
3288
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003289 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003290 /* Force use of hard-coded filter coefficients
3291 * as some pre-programmed values are broken,
3292 * e.g. x201.
3293 */
3294 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3295 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3296 PF_PIPE_SEL_IVB(pipe));
3297 else
3298 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3299 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3300 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003301 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003302}
3303
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003304static void intel_enable_planes(struct drm_crtc *crtc)
3305{
3306 struct drm_device *dev = crtc->dev;
3307 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3308 struct intel_plane *intel_plane;
3309
3310 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3311 if (intel_plane->pipe == pipe)
3312 intel_plane_restore(&intel_plane->base);
3313}
3314
3315static void intel_disable_planes(struct drm_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->dev;
3318 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3319 struct intel_plane *intel_plane;
3320
3321 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3322 if (intel_plane->pipe == pipe)
3323 intel_plane_disable(&intel_plane->base);
3324}
3325
Paulo Zanonid77e4532013-09-24 13:52:55 -03003326static void hsw_enable_ips(struct intel_crtc *crtc)
3327{
3328 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3329
3330 if (!crtc->config.ips_enabled)
3331 return;
3332
3333 /* We can only enable IPS after we enable a plane and wait for a vblank.
3334 * We guarantee that the plane is enabled by calling intel_enable_ips
3335 * only after intel_enable_plane. And intel_enable_plane already waits
3336 * for a vblank, so all we need to do here is to enable the IPS bit. */
3337 assert_plane_enabled(dev_priv, crtc->plane);
3338 I915_WRITE(IPS_CTL, IPS_ENABLE);
3339}
3340
3341static void hsw_disable_ips(struct intel_crtc *crtc)
3342{
3343 struct drm_device *dev = crtc->base.dev;
3344 struct drm_i915_private *dev_priv = dev->dev_private;
3345
3346 if (!crtc->config.ips_enabled)
3347 return;
3348
3349 assert_plane_enabled(dev_priv, crtc->plane);
3350 I915_WRITE(IPS_CTL, 0);
3351 POSTING_READ(IPS_CTL);
3352
3353 /* We need to wait for a vblank before we can disable the plane. */
3354 intel_wait_for_vblank(dev, crtc->pipe);
3355}
3356
3357/** Loads the palette/gamma unit for the CRTC with the prepared values */
3358static void intel_crtc_load_lut(struct drm_crtc *crtc)
3359{
3360 struct drm_device *dev = crtc->dev;
3361 struct drm_i915_private *dev_priv = dev->dev_private;
3362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3363 enum pipe pipe = intel_crtc->pipe;
3364 int palreg = PALETTE(pipe);
3365 int i;
3366 bool reenable_ips = false;
3367
3368 /* The clocks have to be on to load the palette. */
3369 if (!crtc->enabled || !intel_crtc->active)
3370 return;
3371
3372 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3373 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3374 assert_dsi_pll_enabled(dev_priv);
3375 else
3376 assert_pll_enabled(dev_priv, pipe);
3377 }
3378
3379 /* use legacy palette for Ironlake */
3380 if (HAS_PCH_SPLIT(dev))
3381 palreg = LGC_PALETTE(pipe);
3382
3383 /* Workaround : Do not read or write the pipe palette/gamma data while
3384 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3385 */
3386 if (intel_crtc->config.ips_enabled &&
3387 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3388 GAMMA_MODE_MODE_SPLIT)) {
3389 hsw_disable_ips(intel_crtc);
3390 reenable_ips = true;
3391 }
3392
3393 for (i = 0; i < 256; i++) {
3394 I915_WRITE(palreg + 4 * i,
3395 (intel_crtc->lut_r[i] << 16) |
3396 (intel_crtc->lut_g[i] << 8) |
3397 intel_crtc->lut_b[i]);
3398 }
3399
3400 if (reenable_ips)
3401 hsw_enable_ips(intel_crtc);
3402}
3403
Jesse Barnesf67a5592011-01-05 10:31:48 -08003404static void ironlake_crtc_enable(struct drm_crtc *crtc)
3405{
3406 struct drm_device *dev = crtc->dev;
3407 struct drm_i915_private *dev_priv = dev->dev_private;
3408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003409 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003410 int pipe = intel_crtc->pipe;
3411 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003412
Daniel Vetter08a48462012-07-02 11:43:47 +02003413 WARN_ON(!crtc->enabled);
3414
Jesse Barnesf67a5592011-01-05 10:31:48 -08003415 if (intel_crtc->active)
3416 return;
3417
3418 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003419
3420 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3421 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3422
Daniel Vetterf6736a12013-06-05 13:34:30 +02003423 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003424 if (encoder->pre_enable)
3425 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003426
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003427 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003428 /* Note: FDI PLL enabling _must_ be done before we enable the
3429 * cpu pipes, hence this is separate from all the other fdi/pch
3430 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003431 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003432 } else {
3433 assert_fdi_tx_disabled(dev_priv, pipe);
3434 assert_fdi_rx_disabled(dev_priv, pipe);
3435 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003436
Jesse Barnesb074cec2013-04-25 12:55:02 -07003437 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003438
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003439 /*
3440 * On ILK+ LUT must be loaded before the pipe is running but with
3441 * clocks enabled
3442 */
3443 intel_crtc_load_lut(crtc);
3444
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003445 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003446 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003447 intel_crtc->config.has_pch_encoder, false);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003448 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003449 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003450 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003451
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003452 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003453 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003454
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003455 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003456 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003457 mutex_unlock(&dev->struct_mutex);
3458
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003459 for_each_encoder_on_crtc(dev, crtc, encoder)
3460 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003461
3462 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003463 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003464
3465 /*
3466 * There seems to be a race in PCH platform hw (at least on some
3467 * outputs) where an enabled pipe still completes any pageflip right
3468 * away (as if the pipe is off) instead of waiting for vblank. As soon
3469 * as the first vblank happend, everything works as expected. Hence just
3470 * wait for one vblank before returning to avoid strange things
3471 * happening.
3472 */
3473 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003474}
3475
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003476/* IPS only exists on ULT machines and is tied to pipe A. */
3477static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3478{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003479 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003480}
3481
Ville Syrjälädda9a662013-09-19 17:00:37 -03003482static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3483{
3484 struct drm_device *dev = crtc->dev;
3485 struct drm_i915_private *dev_priv = dev->dev_private;
3486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3487 int pipe = intel_crtc->pipe;
3488 int plane = intel_crtc->plane;
3489
3490 intel_enable_plane(dev_priv, plane, pipe);
3491 intel_enable_planes(crtc);
3492 intel_crtc_update_cursor(crtc, true);
3493
3494 hsw_enable_ips(intel_crtc);
3495
3496 mutex_lock(&dev->struct_mutex);
3497 intel_update_fbc(dev);
3498 mutex_unlock(&dev->struct_mutex);
3499}
3500
3501static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3502{
3503 struct drm_device *dev = crtc->dev;
3504 struct drm_i915_private *dev_priv = dev->dev_private;
3505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3506 int pipe = intel_crtc->pipe;
3507 int plane = intel_crtc->plane;
3508
3509 intel_crtc_wait_for_pending_flips(crtc);
3510 drm_vblank_off(dev, pipe);
3511
3512 /* FBC must be disabled before disabling the plane on HSW. */
3513 if (dev_priv->fbc.plane == plane)
3514 intel_disable_fbc(dev);
3515
3516 hsw_disable_ips(intel_crtc);
3517
3518 intel_crtc_update_cursor(crtc, false);
3519 intel_disable_planes(crtc);
3520 intel_disable_plane(dev_priv, plane, pipe);
3521}
3522
Paulo Zanonie4916942013-09-20 16:21:19 -03003523/*
3524 * This implements the workaround described in the "notes" section of the mode
3525 * set sequence documentation. When going from no pipes or single pipe to
3526 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3527 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3528 */
3529static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3530{
3531 struct drm_device *dev = crtc->base.dev;
3532 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3533
3534 /* We want to get the other_active_crtc only if there's only 1 other
3535 * active crtc. */
3536 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3537 if (!crtc_it->active || crtc_it == crtc)
3538 continue;
3539
3540 if (other_active_crtc)
3541 return;
3542
3543 other_active_crtc = crtc_it;
3544 }
3545 if (!other_active_crtc)
3546 return;
3547
3548 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3549 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3550}
3551
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003552static void haswell_crtc_enable(struct drm_crtc *crtc)
3553{
3554 struct drm_device *dev = crtc->dev;
3555 struct drm_i915_private *dev_priv = dev->dev_private;
3556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3557 struct intel_encoder *encoder;
3558 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003559
3560 WARN_ON(!crtc->enabled);
3561
3562 if (intel_crtc->active)
3563 return;
3564
3565 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003566
3567 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3568 if (intel_crtc->config.has_pch_encoder)
3569 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3570
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003571 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003572 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003573
3574 for_each_encoder_on_crtc(dev, crtc, encoder)
3575 if (encoder->pre_enable)
3576 encoder->pre_enable(encoder);
3577
Paulo Zanoni1f544382012-10-24 11:32:00 -02003578 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003579
Jesse Barnesb074cec2013-04-25 12:55:02 -07003580 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003581
3582 /*
3583 * On ILK+ LUT must be loaded before the pipe is running but with
3584 * clocks enabled
3585 */
3586 intel_crtc_load_lut(crtc);
3587
Paulo Zanoni1f544382012-10-24 11:32:00 -02003588 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003589 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003590
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003591 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003592 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003593 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003594
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003595 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003596 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003597
Jani Nikula8807e552013-08-30 19:40:32 +03003598 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003599 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003600 intel_opregion_notify_encoder(encoder, true);
3601 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003602
Paulo Zanonie4916942013-09-20 16:21:19 -03003603 /* If we change the relative order between pipe/planes enabling, we need
3604 * to change the workaround. */
3605 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003606 haswell_crtc_enable_planes(crtc);
3607
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003608 /*
3609 * There seems to be a race in PCH platform hw (at least on some
3610 * outputs) where an enabled pipe still completes any pageflip right
3611 * away (as if the pipe is off) instead of waiting for vblank. As soon
3612 * as the first vblank happend, everything works as expected. Hence just
3613 * wait for one vblank before returning to avoid strange things
3614 * happening.
3615 */
3616 intel_wait_for_vblank(dev, intel_crtc->pipe);
3617}
3618
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003619static void ironlake_pfit_disable(struct intel_crtc *crtc)
3620{
3621 struct drm_device *dev = crtc->base.dev;
3622 struct drm_i915_private *dev_priv = dev->dev_private;
3623 int pipe = crtc->pipe;
3624
3625 /* To avoid upsetting the power well on haswell only disable the pfit if
3626 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003627 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003628 I915_WRITE(PF_CTL(pipe), 0);
3629 I915_WRITE(PF_WIN_POS(pipe), 0);
3630 I915_WRITE(PF_WIN_SZ(pipe), 0);
3631 }
3632}
3633
Jesse Barnes6be4a602010-09-10 10:26:01 -07003634static void ironlake_crtc_disable(struct drm_crtc *crtc)
3635{
3636 struct drm_device *dev = crtc->dev;
3637 struct drm_i915_private *dev_priv = dev->dev_private;
3638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003639 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003640 int pipe = intel_crtc->pipe;
3641 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003642 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003643
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003644
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003645 if (!intel_crtc->active)
3646 return;
3647
Daniel Vetterea9d7582012-07-10 10:42:52 +02003648 for_each_encoder_on_crtc(dev, crtc, encoder)
3649 encoder->disable(encoder);
3650
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003651 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003652 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003653
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003654 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003655 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003656
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003657 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003658 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003659 intel_disable_plane(dev_priv, plane, pipe);
3660
Daniel Vetterd925c592013-06-05 13:34:04 +02003661 if (intel_crtc->config.has_pch_encoder)
3662 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3663
Jesse Barnesb24e7172011-01-04 15:09:30 -08003664 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003665
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003666 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003667
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003668 for_each_encoder_on_crtc(dev, crtc, encoder)
3669 if (encoder->post_disable)
3670 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003671
Daniel Vetterd925c592013-06-05 13:34:04 +02003672 if (intel_crtc->config.has_pch_encoder) {
3673 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003674
Daniel Vetterd925c592013-06-05 13:34:04 +02003675 ironlake_disable_pch_transcoder(dev_priv, pipe);
3676 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003677
Daniel Vetterd925c592013-06-05 13:34:04 +02003678 if (HAS_PCH_CPT(dev)) {
3679 /* disable TRANS_DP_CTL */
3680 reg = TRANS_DP_CTL(pipe);
3681 temp = I915_READ(reg);
3682 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3683 TRANS_DP_PORT_SEL_MASK);
3684 temp |= TRANS_DP_PORT_SEL_NONE;
3685 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003686
Daniel Vetterd925c592013-06-05 13:34:04 +02003687 /* disable DPLL_SEL */
3688 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003689 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003690 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003691 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003692
3693 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003694 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003695
3696 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003697 }
3698
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003699 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003700 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003701
3702 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003703 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003704 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003705}
3706
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003707static void haswell_crtc_disable(struct drm_crtc *crtc)
3708{
3709 struct drm_device *dev = crtc->dev;
3710 struct drm_i915_private *dev_priv = dev->dev_private;
3711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3712 struct intel_encoder *encoder;
3713 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003714 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003715
3716 if (!intel_crtc->active)
3717 return;
3718
Ville Syrjälädda9a662013-09-19 17:00:37 -03003719 haswell_crtc_disable_planes(crtc);
3720
Jani Nikula8807e552013-08-30 19:40:32 +03003721 for_each_encoder_on_crtc(dev, crtc, encoder) {
3722 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003723 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003724 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003725
Paulo Zanoni86642812013-04-12 17:57:57 -03003726 if (intel_crtc->config.has_pch_encoder)
3727 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003728 intel_disable_pipe(dev_priv, pipe);
3729
Paulo Zanoniad80a812012-10-24 16:06:19 -02003730 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003731
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003732 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003733
Paulo Zanoni1f544382012-10-24 11:32:00 -02003734 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003735
3736 for_each_encoder_on_crtc(dev, crtc, encoder)
3737 if (encoder->post_disable)
3738 encoder->post_disable(encoder);
3739
Daniel Vetter88adfff2013-03-28 10:42:01 +01003740 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003741 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003742 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003743 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003744 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003745
3746 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003747 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003748
3749 mutex_lock(&dev->struct_mutex);
3750 intel_update_fbc(dev);
3751 mutex_unlock(&dev->struct_mutex);
3752}
3753
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003754static void ironlake_crtc_off(struct drm_crtc *crtc)
3755{
3756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003757 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003758}
3759
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003760static void haswell_crtc_off(struct drm_crtc *crtc)
3761{
3762 intel_ddi_put_crtc_pll(crtc);
3763}
3764
Daniel Vetter02e792f2009-09-15 22:57:34 +02003765static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3766{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003767 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003768 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003769 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003770
Chris Wilson23f09ce2010-08-12 13:53:37 +01003771 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003772 dev_priv->mm.interruptible = false;
3773 (void) intel_overlay_switch_off(intel_crtc->overlay);
3774 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003775 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003776 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003777
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003778 /* Let userspace switch the overlay on again. In most cases userspace
3779 * has to recompute where to put it anyway.
3780 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003781}
3782
Egbert Eich61bc95c2013-03-04 09:24:38 -05003783/**
3784 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3785 * cursor plane briefly if not already running after enabling the display
3786 * plane.
3787 * This workaround avoids occasional blank screens when self refresh is
3788 * enabled.
3789 */
3790static void
3791g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3792{
3793 u32 cntl = I915_READ(CURCNTR(pipe));
3794
3795 if ((cntl & CURSOR_MODE) == 0) {
3796 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3797
3798 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3799 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3800 intel_wait_for_vblank(dev_priv->dev, pipe);
3801 I915_WRITE(CURCNTR(pipe), cntl);
3802 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3803 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3804 }
3805}
3806
Jesse Barnes2dd24552013-04-25 12:55:01 -07003807static void i9xx_pfit_enable(struct intel_crtc *crtc)
3808{
3809 struct drm_device *dev = crtc->base.dev;
3810 struct drm_i915_private *dev_priv = dev->dev_private;
3811 struct intel_crtc_config *pipe_config = &crtc->config;
3812
Daniel Vetter328d8e82013-05-08 10:36:31 +02003813 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003814 return;
3815
Daniel Vetterc0b03412013-05-28 12:05:54 +02003816 /*
3817 * The panel fitter should only be adjusted whilst the pipe is disabled,
3818 * according to register description and PRM.
3819 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003820 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3821 assert_pipe_disabled(dev_priv, crtc->pipe);
3822
Jesse Barnesb074cec2013-04-25 12:55:02 -07003823 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3824 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003825
3826 /* Border color in case we don't scale up to the full screen. Black by
3827 * default, change to something else for debugging. */
3828 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003829}
3830
Jesse Barnes89b667f2013-04-18 14:51:36 -07003831static void valleyview_crtc_enable(struct drm_crtc *crtc)
3832{
3833 struct drm_device *dev = crtc->dev;
3834 struct drm_i915_private *dev_priv = dev->dev_private;
3835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3836 struct intel_encoder *encoder;
3837 int pipe = intel_crtc->pipe;
3838 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03003839 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003840
3841 WARN_ON(!crtc->enabled);
3842
3843 if (intel_crtc->active)
3844 return;
3845
3846 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003847
Jesse Barnes89b667f2013-04-18 14:51:36 -07003848 for_each_encoder_on_crtc(dev, crtc, encoder)
3849 if (encoder->pre_pll_enable)
3850 encoder->pre_pll_enable(encoder);
3851
Jani Nikula23538ef2013-08-27 15:12:22 +03003852 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
3853
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003854 if (!is_dsi)
3855 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003856
3857 for_each_encoder_on_crtc(dev, crtc, encoder)
3858 if (encoder->pre_enable)
3859 encoder->pre_enable(encoder);
3860
Jesse Barnes2dd24552013-04-25 12:55:01 -07003861 i9xx_pfit_enable(intel_crtc);
3862
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003863 intel_crtc_load_lut(crtc);
3864
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003865 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003866 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003867 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003868 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003869 intel_crtc_update_cursor(crtc, true);
3870
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003871 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03003872
3873 for_each_encoder_on_crtc(dev, crtc, encoder)
3874 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003875}
3876
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003877static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003878{
3879 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003880 struct drm_i915_private *dev_priv = dev->dev_private;
3881 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003882 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003883 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003884 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003885
Daniel Vetter08a48462012-07-02 11:43:47 +02003886 WARN_ON(!crtc->enabled);
3887
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003888 if (intel_crtc->active)
3889 return;
3890
3891 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003892
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003893 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003894 if (encoder->pre_enable)
3895 encoder->pre_enable(encoder);
3896
Daniel Vetterf6736a12013-06-05 13:34:30 +02003897 i9xx_enable_pll(intel_crtc);
3898
Jesse Barnes2dd24552013-04-25 12:55:01 -07003899 i9xx_pfit_enable(intel_crtc);
3900
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003901 intel_crtc_load_lut(crtc);
3902
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003903 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003904 intel_enable_pipe(dev_priv, pipe, false, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003905 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003906 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003907 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003908 if (IS_G4X(dev))
3909 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003910 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003911
3912 /* Give the overlay scaler a chance to enable if it's on this pipe */
3913 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003914
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003915 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003916
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003917 for_each_encoder_on_crtc(dev, crtc, encoder)
3918 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003919}
3920
Daniel Vetter87476d62013-04-11 16:29:06 +02003921static void i9xx_pfit_disable(struct intel_crtc *crtc)
3922{
3923 struct drm_device *dev = crtc->base.dev;
3924 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003925
3926 if (!crtc->config.gmch_pfit.control)
3927 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003928
3929 assert_pipe_disabled(dev_priv, crtc->pipe);
3930
Daniel Vetter328d8e82013-05-08 10:36:31 +02003931 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3932 I915_READ(PFIT_CONTROL));
3933 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003934}
3935
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003936static void i9xx_crtc_disable(struct drm_crtc *crtc)
3937{
3938 struct drm_device *dev = crtc->dev;
3939 struct drm_i915_private *dev_priv = dev->dev_private;
3940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003941 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003942 int pipe = intel_crtc->pipe;
3943 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003944
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003945 if (!intel_crtc->active)
3946 return;
3947
Daniel Vetterea9d7582012-07-10 10:42:52 +02003948 for_each_encoder_on_crtc(dev, crtc, encoder)
3949 encoder->disable(encoder);
3950
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003951 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003952 intel_crtc_wait_for_pending_flips(crtc);
3953 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003954
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003955 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003956 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003957
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003958 intel_crtc_dpms_overlay(intel_crtc, false);
3959 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003960 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003961 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003962
Jesse Barnesb24e7172011-01-04 15:09:30 -08003963 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003964
Daniel Vetter87476d62013-04-11 16:29:06 +02003965 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003966
Jesse Barnes89b667f2013-04-18 14:51:36 -07003967 for_each_encoder_on_crtc(dev, crtc, encoder)
3968 if (encoder->post_disable)
3969 encoder->post_disable(encoder);
3970
Jesse Barnesf6071162013-10-01 10:41:38 -07003971 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3972 vlv_disable_pll(dev_priv, pipe);
3973 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003974 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003975
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003976 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003977 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003978
Chris Wilson6b383a72010-09-13 13:54:26 +01003979 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003980}
3981
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003982static void i9xx_crtc_off(struct drm_crtc *crtc)
3983{
3984}
3985
Daniel Vetter976f8a22012-07-08 22:34:21 +02003986static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3987 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003988{
3989 struct drm_device *dev = crtc->dev;
3990 struct drm_i915_master_private *master_priv;
3991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3992 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003993
3994 if (!dev->primary->master)
3995 return;
3996
3997 master_priv = dev->primary->master->driver_priv;
3998 if (!master_priv->sarea_priv)
3999 return;
4000
Jesse Barnes79e53942008-11-07 14:24:08 -08004001 switch (pipe) {
4002 case 0:
4003 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4004 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4005 break;
4006 case 1:
4007 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4008 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4009 break;
4010 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004011 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004012 break;
4013 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004014}
4015
Daniel Vetter976f8a22012-07-08 22:34:21 +02004016/**
4017 * Sets the power management mode of the pipe and plane.
4018 */
4019void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004020{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004021 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004022 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004023 struct intel_encoder *intel_encoder;
4024 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004025
Daniel Vetter976f8a22012-07-08 22:34:21 +02004026 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4027 enable |= intel_encoder->connectors_active;
4028
4029 if (enable)
4030 dev_priv->display.crtc_enable(crtc);
4031 else
4032 dev_priv->display.crtc_disable(crtc);
4033
4034 intel_crtc_update_sarea(crtc, enable);
4035}
4036
Daniel Vetter976f8a22012-07-08 22:34:21 +02004037static void intel_crtc_disable(struct drm_crtc *crtc)
4038{
4039 struct drm_device *dev = crtc->dev;
4040 struct drm_connector *connector;
4041 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004043
4044 /* crtc should still be enabled when we disable it. */
4045 WARN_ON(!crtc->enabled);
4046
4047 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004048 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004049 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004050 dev_priv->display.off(crtc);
4051
Chris Wilson931872f2012-01-16 23:01:13 +00004052 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004053 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004054 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004055
4056 if (crtc->fb) {
4057 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004058 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004059 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004060 crtc->fb = NULL;
4061 }
4062
4063 /* Update computed state. */
4064 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4065 if (!connector->encoder || !connector->encoder->crtc)
4066 continue;
4067
4068 if (connector->encoder->crtc != crtc)
4069 continue;
4070
4071 connector->dpms = DRM_MODE_DPMS_OFF;
4072 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004073 }
4074}
4075
Chris Wilsonea5b2132010-08-04 13:50:23 +01004076void intel_encoder_destroy(struct drm_encoder *encoder)
4077{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004078 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004079
Chris Wilsonea5b2132010-08-04 13:50:23 +01004080 drm_encoder_cleanup(encoder);
4081 kfree(intel_encoder);
4082}
4083
Damien Lespiau92373292013-08-08 22:28:57 +01004084/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004085 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4086 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004087static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004088{
4089 if (mode == DRM_MODE_DPMS_ON) {
4090 encoder->connectors_active = true;
4091
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004092 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004093 } else {
4094 encoder->connectors_active = false;
4095
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004096 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004097 }
4098}
4099
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004100/* Cross check the actual hw state with our own modeset state tracking (and it's
4101 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004102static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004103{
4104 if (connector->get_hw_state(connector)) {
4105 struct intel_encoder *encoder = connector->encoder;
4106 struct drm_crtc *crtc;
4107 bool encoder_enabled;
4108 enum pipe pipe;
4109
4110 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4111 connector->base.base.id,
4112 drm_get_connector_name(&connector->base));
4113
4114 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4115 "wrong connector dpms state\n");
4116 WARN(connector->base.encoder != &encoder->base,
4117 "active connector not linked to encoder\n");
4118 WARN(!encoder->connectors_active,
4119 "encoder->connectors_active not set\n");
4120
4121 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4122 WARN(!encoder_enabled, "encoder not enabled\n");
4123 if (WARN_ON(!encoder->base.crtc))
4124 return;
4125
4126 crtc = encoder->base.crtc;
4127
4128 WARN(!crtc->enabled, "crtc not enabled\n");
4129 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4130 WARN(pipe != to_intel_crtc(crtc)->pipe,
4131 "encoder active on the wrong pipe\n");
4132 }
4133}
4134
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004135/* Even simpler default implementation, if there's really no special case to
4136 * consider. */
4137void intel_connector_dpms(struct drm_connector *connector, int mode)
4138{
4139 struct intel_encoder *encoder = intel_attached_encoder(connector);
4140
4141 /* All the simple cases only support two dpms states. */
4142 if (mode != DRM_MODE_DPMS_ON)
4143 mode = DRM_MODE_DPMS_OFF;
4144
4145 if (mode == connector->dpms)
4146 return;
4147
4148 connector->dpms = mode;
4149
4150 /* Only need to change hw state when actually enabled */
4151 if (encoder->base.crtc)
4152 intel_encoder_dpms(encoder, mode);
4153 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02004154 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004155
Daniel Vetterb9805142012-08-31 17:37:33 +02004156 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004157}
4158
Daniel Vetterf0947c32012-07-02 13:10:34 +02004159/* Simple connector->get_hw_state implementation for encoders that support only
4160 * one connector and no cloning and hence the encoder state determines the state
4161 * of the connector. */
4162bool intel_connector_get_hw_state(struct intel_connector *connector)
4163{
Daniel Vetter24929352012-07-02 20:28:59 +02004164 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004165 struct intel_encoder *encoder = connector->encoder;
4166
4167 return encoder->get_hw_state(encoder, &pipe);
4168}
4169
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004170static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4171 struct intel_crtc_config *pipe_config)
4172{
4173 struct drm_i915_private *dev_priv = dev->dev_private;
4174 struct intel_crtc *pipe_B_crtc =
4175 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4176
4177 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4178 pipe_name(pipe), pipe_config->fdi_lanes);
4179 if (pipe_config->fdi_lanes > 4) {
4180 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4181 pipe_name(pipe), pipe_config->fdi_lanes);
4182 return false;
4183 }
4184
4185 if (IS_HASWELL(dev)) {
4186 if (pipe_config->fdi_lanes > 2) {
4187 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4188 pipe_config->fdi_lanes);
4189 return false;
4190 } else {
4191 return true;
4192 }
4193 }
4194
4195 if (INTEL_INFO(dev)->num_pipes == 2)
4196 return true;
4197
4198 /* Ivybridge 3 pipe is really complicated */
4199 switch (pipe) {
4200 case PIPE_A:
4201 return true;
4202 case PIPE_B:
4203 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4204 pipe_config->fdi_lanes > 2) {
4205 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4206 pipe_name(pipe), pipe_config->fdi_lanes);
4207 return false;
4208 }
4209 return true;
4210 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004211 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004212 pipe_B_crtc->config.fdi_lanes <= 2) {
4213 if (pipe_config->fdi_lanes > 2) {
4214 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4215 pipe_name(pipe), pipe_config->fdi_lanes);
4216 return false;
4217 }
4218 } else {
4219 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4220 return false;
4221 }
4222 return true;
4223 default:
4224 BUG();
4225 }
4226}
4227
Daniel Vettere29c22c2013-02-21 00:00:16 +01004228#define RETRY 1
4229static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4230 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004231{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004232 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004233 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004234 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004235 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004236
Daniel Vettere29c22c2013-02-21 00:00:16 +01004237retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004238 /* FDI is a binary signal running at ~2.7GHz, encoding
4239 * each output octet as 10 bits. The actual frequency
4240 * is stored as a divider into a 100MHz clock, and the
4241 * mode pixel clock is stored in units of 1KHz.
4242 * Hence the bw of each lane in terms of the mode signal
4243 * is:
4244 */
4245 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4246
Damien Lespiau241bfc32013-09-25 16:45:37 +01004247 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004248
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004249 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004250 pipe_config->pipe_bpp);
4251
4252 pipe_config->fdi_lanes = lane;
4253
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004254 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004255 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004256
Daniel Vettere29c22c2013-02-21 00:00:16 +01004257 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4258 intel_crtc->pipe, pipe_config);
4259 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4260 pipe_config->pipe_bpp -= 2*3;
4261 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4262 pipe_config->pipe_bpp);
4263 needs_recompute = true;
4264 pipe_config->bw_constrained = true;
4265
4266 goto retry;
4267 }
4268
4269 if (needs_recompute)
4270 return RETRY;
4271
4272 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004273}
4274
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004275static void hsw_compute_ips_config(struct intel_crtc *crtc,
4276 struct intel_crtc_config *pipe_config)
4277{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004278 pipe_config->ips_enabled = i915_enable_ips &&
4279 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004280 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004281}
4282
Daniel Vettera43f6e02013-06-07 23:10:32 +02004283static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004284 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004285{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004286 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004287 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004288
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004289 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004290 if (INTEL_INFO(dev)->gen < 4) {
4291 struct drm_i915_private *dev_priv = dev->dev_private;
4292 int clock_limit =
4293 dev_priv->display.get_display_clock_speed(dev);
4294
4295 /*
4296 * Enable pixel doubling when the dot clock
4297 * is > 90% of the (display) core speed.
4298 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004299 * GDG double wide on either pipe,
4300 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004301 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004302 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004303 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004304 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004305 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004306 }
4307
Damien Lespiau241bfc32013-09-25 16:45:37 +01004308 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004309 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004310 }
Chris Wilson89749352010-09-12 18:25:19 +01004311
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004312 /*
4313 * Pipe horizontal size must be even in:
4314 * - DVO ganged mode
4315 * - LVDS dual channel mode
4316 * - Double wide pipe
4317 */
4318 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4319 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4320 pipe_config->pipe_src_w &= ~1;
4321
Damien Lespiau8693a822013-05-03 18:48:11 +01004322 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4323 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004324 */
4325 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4326 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004327 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004328
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004329 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004330 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004331 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004332 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4333 * for lvds. */
4334 pipe_config->pipe_bpp = 8*3;
4335 }
4336
Damien Lespiauf5adf942013-06-24 18:29:34 +01004337 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004338 hsw_compute_ips_config(crtc, pipe_config);
4339
4340 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4341 * clock survives for now. */
4342 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4343 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004344
Daniel Vetter877d48d2013-04-19 11:24:43 +02004345 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004346 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004347
Daniel Vettere29c22c2013-02-21 00:00:16 +01004348 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004349}
4350
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004351static int valleyview_get_display_clock_speed(struct drm_device *dev)
4352{
4353 return 400000; /* FIXME */
4354}
4355
Jesse Barnese70236a2009-09-21 10:42:27 -07004356static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004357{
Jesse Barnese70236a2009-09-21 10:42:27 -07004358 return 400000;
4359}
Jesse Barnes79e53942008-11-07 14:24:08 -08004360
Jesse Barnese70236a2009-09-21 10:42:27 -07004361static int i915_get_display_clock_speed(struct drm_device *dev)
4362{
4363 return 333000;
4364}
Jesse Barnes79e53942008-11-07 14:24:08 -08004365
Jesse Barnese70236a2009-09-21 10:42:27 -07004366static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4367{
4368 return 200000;
4369}
Jesse Barnes79e53942008-11-07 14:24:08 -08004370
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004371static int pnv_get_display_clock_speed(struct drm_device *dev)
4372{
4373 u16 gcfgc = 0;
4374
4375 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4376
4377 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4378 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4379 return 267000;
4380 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4381 return 333000;
4382 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4383 return 444000;
4384 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4385 return 200000;
4386 default:
4387 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4388 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4389 return 133000;
4390 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4391 return 167000;
4392 }
4393}
4394
Jesse Barnese70236a2009-09-21 10:42:27 -07004395static int i915gm_get_display_clock_speed(struct drm_device *dev)
4396{
4397 u16 gcfgc = 0;
4398
4399 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4400
4401 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004402 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004403 else {
4404 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4405 case GC_DISPLAY_CLOCK_333_MHZ:
4406 return 333000;
4407 default:
4408 case GC_DISPLAY_CLOCK_190_200_MHZ:
4409 return 190000;
4410 }
4411 }
4412}
Jesse Barnes79e53942008-11-07 14:24:08 -08004413
Jesse Barnese70236a2009-09-21 10:42:27 -07004414static int i865_get_display_clock_speed(struct drm_device *dev)
4415{
4416 return 266000;
4417}
4418
4419static int i855_get_display_clock_speed(struct drm_device *dev)
4420{
4421 u16 hpllcc = 0;
4422 /* Assume that the hardware is in the high speed state. This
4423 * should be the default.
4424 */
4425 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4426 case GC_CLOCK_133_200:
4427 case GC_CLOCK_100_200:
4428 return 200000;
4429 case GC_CLOCK_166_250:
4430 return 250000;
4431 case GC_CLOCK_100_133:
4432 return 133000;
4433 }
4434
4435 /* Shouldn't happen */
4436 return 0;
4437}
4438
4439static int i830_get_display_clock_speed(struct drm_device *dev)
4440{
4441 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004442}
4443
Zhenyu Wang2c072452009-06-05 15:38:42 +08004444static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004445intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004446{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004447 while (*num > DATA_LINK_M_N_MASK ||
4448 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004449 *num >>= 1;
4450 *den >>= 1;
4451 }
4452}
4453
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004454static void compute_m_n(unsigned int m, unsigned int n,
4455 uint32_t *ret_m, uint32_t *ret_n)
4456{
4457 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4458 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4459 intel_reduce_m_n_ratio(ret_m, ret_n);
4460}
4461
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004462void
4463intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4464 int pixel_clock, int link_clock,
4465 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004466{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004467 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004468
4469 compute_m_n(bits_per_pixel * pixel_clock,
4470 link_clock * nlanes * 8,
4471 &m_n->gmch_m, &m_n->gmch_n);
4472
4473 compute_m_n(pixel_clock, link_clock,
4474 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004475}
4476
Chris Wilsona7615032011-01-12 17:04:08 +00004477static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4478{
Keith Packard72bbe58c2011-09-26 16:09:45 -07004479 if (i915_panel_use_ssc >= 0)
4480 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004481 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004482 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004483}
4484
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004485static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4486{
4487 struct drm_device *dev = crtc->dev;
4488 struct drm_i915_private *dev_priv = dev->dev_private;
4489 int refclk;
4490
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004491 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004492 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004493 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004494 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004495 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004496 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4497 refclk / 1000);
4498 } else if (!IS_GEN2(dev)) {
4499 refclk = 96000;
4500 } else {
4501 refclk = 48000;
4502 }
4503
4504 return refclk;
4505}
4506
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004507static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004508{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004509 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004510}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004511
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004512static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4513{
4514 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004515}
4516
Daniel Vetterf47709a2013-03-28 10:42:02 +01004517static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004518 intel_clock_t *reduced_clock)
4519{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004520 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004521 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004522 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004523 u32 fp, fp2 = 0;
4524
4525 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004526 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004527 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004528 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004529 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004530 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004531 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004532 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004533 }
4534
4535 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004536 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004537
Daniel Vetterf47709a2013-03-28 10:42:02 +01004538 crtc->lowfreq_avail = false;
4539 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004540 reduced_clock && i915_powersave) {
4541 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004542 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004543 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004544 } else {
4545 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004546 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004547 }
4548}
4549
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004550static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4551 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004552{
4553 u32 reg_val;
4554
4555 /*
4556 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4557 * and set it to a reasonable value instead.
4558 */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004559 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004560 reg_val &= 0xffffff00;
4561 reg_val |= 0x00000030;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004562 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004563
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004564 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004565 reg_val &= 0x8cffffff;
4566 reg_val = 0x8c000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004567 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004568
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004569 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004570 reg_val &= 0xffffff00;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004571 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004572
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004573 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004574 reg_val &= 0x00ffffff;
4575 reg_val |= 0xb0000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004576 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004577}
4578
Daniel Vetterb5518422013-05-03 11:49:48 +02004579static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4580 struct intel_link_m_n *m_n)
4581{
4582 struct drm_device *dev = crtc->base.dev;
4583 struct drm_i915_private *dev_priv = dev->dev_private;
4584 int pipe = crtc->pipe;
4585
Daniel Vettere3b95f12013-05-03 11:49:49 +02004586 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4587 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4588 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4589 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004590}
4591
4592static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4593 struct intel_link_m_n *m_n)
4594{
4595 struct drm_device *dev = crtc->base.dev;
4596 struct drm_i915_private *dev_priv = dev->dev_private;
4597 int pipe = crtc->pipe;
4598 enum transcoder transcoder = crtc->config.cpu_transcoder;
4599
4600 if (INTEL_INFO(dev)->gen >= 5) {
4601 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4602 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4603 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4604 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4605 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004606 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4607 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4608 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4609 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004610 }
4611}
4612
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004613static void intel_dp_set_m_n(struct intel_crtc *crtc)
4614{
4615 if (crtc->config.has_pch_encoder)
4616 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4617 else
4618 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4619}
4620
Daniel Vetterf47709a2013-03-28 10:42:02 +01004621static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004622{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004623 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004624 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004625 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004626 u32 dpll, mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004627 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004628 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004629
Daniel Vetter09153002012-12-12 14:06:44 +01004630 mutex_lock(&dev_priv->dpio_lock);
4631
Daniel Vetterf47709a2013-03-28 10:42:02 +01004632 bestn = crtc->config.dpll.n;
4633 bestm1 = crtc->config.dpll.m1;
4634 bestm2 = crtc->config.dpll.m2;
4635 bestp1 = crtc->config.dpll.p1;
4636 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004637
Jesse Barnes89b667f2013-04-18 14:51:36 -07004638 /* See eDP HDMI DPIO driver vbios notes doc */
4639
4640 /* PLL B needs special handling */
4641 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004642 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004643
4644 /* Set up Tx target for periodic Rcomp update */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004645 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004646
4647 /* Disable target IRef on PLL */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004648 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004649 reg_val &= 0x00ffffff;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004650 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004651
4652 /* Disable fast lock */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004653 vlv_dpio_write(dev_priv, pipe, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004654
4655 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004656 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4657 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4658 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004659 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004660
4661 /*
4662 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4663 * but we don't support that).
4664 * Note: don't use the DAC post divider as it seems unstable.
4665 */
4666 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004667 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004668
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004669 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004670 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004671
Jesse Barnes89b667f2013-04-18 14:51:36 -07004672 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004673 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004674 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004675 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004676 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004677 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004678 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004679 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004680 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004681
Jesse Barnes89b667f2013-04-18 14:51:36 -07004682 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4683 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4684 /* Use SSC source */
4685 if (!pipe)
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 0x0df40000);
4688 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004689 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004690 0x0df70000);
4691 } else { /* HDMI or VGA */
4692 /* Use bend source */
4693 if (!pipe)
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 0x0df70000);
4696 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004697 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004698 0x0df40000);
4699 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004700
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004701 coreclk = vlv_dpio_read(dev_priv, pipe, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004702 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4703 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4704 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4705 coreclk |= 0x01000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004706 vlv_dpio_write(dev_priv, pipe, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004707
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004708 vlv_dpio_write(dev_priv, pipe, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004709
Jesse Barnes89b667f2013-04-18 14:51:36 -07004710 /* Enable DPIO clock input */
4711 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4712 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004713 /* We should never disable this, set it here for state tracking */
4714 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004715 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004716 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004717 crtc->config.dpll_hw_state.dpll = dpll;
4718
Daniel Vetteref1b4602013-06-01 17:17:04 +02004719 dpll_md = (crtc->config.pixel_multiplier - 1)
4720 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004721 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4722
Daniel Vetterf47709a2013-03-28 10:42:02 +01004723 if (crtc->config.has_dp_encoder)
4724 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304725
Daniel Vetter09153002012-12-12 14:06:44 +01004726 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004727}
4728
Daniel Vetterf47709a2013-03-28 10:42:02 +01004729static void i9xx_update_pll(struct intel_crtc *crtc,
4730 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004731 int num_connectors)
4732{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004733 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004734 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004735 u32 dpll;
4736 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004737 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004738
Daniel Vetterf47709a2013-03-28 10:42:02 +01004739 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304740
Daniel Vetterf47709a2013-03-28 10:42:02 +01004741 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4742 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004743
4744 dpll = DPLL_VGA_MODE_DIS;
4745
Daniel Vetterf47709a2013-03-28 10:42:02 +01004746 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004747 dpll |= DPLLB_MODE_LVDS;
4748 else
4749 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004750
Daniel Vetteref1b4602013-06-01 17:17:04 +02004751 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004752 dpll |= (crtc->config.pixel_multiplier - 1)
4753 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004754 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004755
4756 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004757 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004758
Daniel Vetterf47709a2013-03-28 10:42:02 +01004759 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004760 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004761
4762 /* compute bitmask from p1 value */
4763 if (IS_PINEVIEW(dev))
4764 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4765 else {
4766 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4767 if (IS_G4X(dev) && reduced_clock)
4768 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4769 }
4770 switch (clock->p2) {
4771 case 5:
4772 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4773 break;
4774 case 7:
4775 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4776 break;
4777 case 10:
4778 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4779 break;
4780 case 14:
4781 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4782 break;
4783 }
4784 if (INTEL_INFO(dev)->gen >= 4)
4785 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4786
Daniel Vetter09ede542013-04-30 14:01:45 +02004787 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004788 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004789 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004790 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4791 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4792 else
4793 dpll |= PLL_REF_INPUT_DREFCLK;
4794
4795 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004796 crtc->config.dpll_hw_state.dpll = dpll;
4797
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004798 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004799 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4800 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004801 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004802 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004803
4804 if (crtc->config.has_dp_encoder)
4805 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004806}
4807
Daniel Vetterf47709a2013-03-28 10:42:02 +01004808static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004809 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004810 int num_connectors)
4811{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004812 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004813 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004814 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004815 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004816
Daniel Vetterf47709a2013-03-28 10:42:02 +01004817 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304818
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004819 dpll = DPLL_VGA_MODE_DIS;
4820
Daniel Vetterf47709a2013-03-28 10:42:02 +01004821 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004822 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4823 } else {
4824 if (clock->p1 == 2)
4825 dpll |= PLL_P1_DIVIDE_BY_TWO;
4826 else
4827 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4828 if (clock->p2 == 4)
4829 dpll |= PLL_P2_DIVIDE_BY_4;
4830 }
4831
Daniel Vetter4a33e482013-07-06 12:52:05 +02004832 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4833 dpll |= DPLL_DVO_2X_MODE;
4834
Daniel Vetterf47709a2013-03-28 10:42:02 +01004835 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004836 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4837 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4838 else
4839 dpll |= PLL_REF_INPUT_DREFCLK;
4840
4841 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004842 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004843}
4844
Daniel Vetter8a654f32013-06-01 17:16:22 +02004845static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004846{
4847 struct drm_device *dev = intel_crtc->base.dev;
4848 struct drm_i915_private *dev_priv = dev->dev_private;
4849 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004850 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004851 struct drm_display_mode *adjusted_mode =
4852 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004853 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4854
4855 /* We need to be careful not to changed the adjusted mode, for otherwise
4856 * the hw state checker will get angry at the mismatch. */
4857 crtc_vtotal = adjusted_mode->crtc_vtotal;
4858 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004859
4860 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4861 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004862 crtc_vtotal -= 1;
4863 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004864 vsyncshift = adjusted_mode->crtc_hsync_start
4865 - adjusted_mode->crtc_htotal / 2;
4866 } else {
4867 vsyncshift = 0;
4868 }
4869
4870 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004871 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004872
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004873 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004874 (adjusted_mode->crtc_hdisplay - 1) |
4875 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004876 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004877 (adjusted_mode->crtc_hblank_start - 1) |
4878 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004879 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004880 (adjusted_mode->crtc_hsync_start - 1) |
4881 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4882
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004883 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004884 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004885 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004886 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004887 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004888 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004889 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004890 (adjusted_mode->crtc_vsync_start - 1) |
4891 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4892
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004893 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4894 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4895 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4896 * bits. */
4897 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4898 (pipe == PIPE_B || pipe == PIPE_C))
4899 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4900
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004901 /* pipesrc controls the size that is scaled from, which should
4902 * always be the user's requested size.
4903 */
4904 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004905 ((intel_crtc->config.pipe_src_w - 1) << 16) |
4906 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004907}
4908
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004909static void intel_get_pipe_timings(struct intel_crtc *crtc,
4910 struct intel_crtc_config *pipe_config)
4911{
4912 struct drm_device *dev = crtc->base.dev;
4913 struct drm_i915_private *dev_priv = dev->dev_private;
4914 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4915 uint32_t tmp;
4916
4917 tmp = I915_READ(HTOTAL(cpu_transcoder));
4918 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4919 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4920 tmp = I915_READ(HBLANK(cpu_transcoder));
4921 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4922 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4923 tmp = I915_READ(HSYNC(cpu_transcoder));
4924 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4925 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4926
4927 tmp = I915_READ(VTOTAL(cpu_transcoder));
4928 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4929 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4930 tmp = I915_READ(VBLANK(cpu_transcoder));
4931 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4932 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4933 tmp = I915_READ(VSYNC(cpu_transcoder));
4934 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4935 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4936
4937 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4938 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4939 pipe_config->adjusted_mode.crtc_vtotal += 1;
4940 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4941 }
4942
4943 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004944 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
4945 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
4946
4947 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
4948 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004949}
4950
Jesse Barnesbabea612013-06-26 18:57:38 +03004951static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4952 struct intel_crtc_config *pipe_config)
4953{
4954 struct drm_crtc *crtc = &intel_crtc->base;
4955
4956 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4957 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4958 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4959 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4960
4961 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4962 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4963 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4964 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4965
4966 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4967
Damien Lespiau241bfc32013-09-25 16:45:37 +01004968 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03004969 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4970}
4971
Daniel Vetter84b046f2013-02-19 18:48:54 +01004972static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4973{
4974 struct drm_device *dev = intel_crtc->base.dev;
4975 struct drm_i915_private *dev_priv = dev->dev_private;
4976 uint32_t pipeconf;
4977
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004978 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004979
Daniel Vetter67c72a12013-09-24 11:46:14 +02004980 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
4981 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
4982 pipeconf |= PIPECONF_ENABLE;
4983
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004984 if (intel_crtc->config.double_wide)
4985 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004986
Daniel Vetterff9ce462013-04-24 14:57:17 +02004987 /* only g4x and later have fancy bpc/dither controls */
4988 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004989 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4990 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4991 pipeconf |= PIPECONF_DITHER_EN |
4992 PIPECONF_DITHER_TYPE_SP;
4993
4994 switch (intel_crtc->config.pipe_bpp) {
4995 case 18:
4996 pipeconf |= PIPECONF_6BPC;
4997 break;
4998 case 24:
4999 pipeconf |= PIPECONF_8BPC;
5000 break;
5001 case 30:
5002 pipeconf |= PIPECONF_10BPC;
5003 break;
5004 default:
5005 /* Case prevented by intel_choose_pipe_bpp_dither. */
5006 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005007 }
5008 }
5009
5010 if (HAS_PIPE_CXSR(dev)) {
5011 if (intel_crtc->lowfreq_avail) {
5012 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5013 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5014 } else {
5015 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005016 }
5017 }
5018
Daniel Vetter84b046f2013-02-19 18:48:54 +01005019 if (!IS_GEN2(dev) &&
5020 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5021 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5022 else
5023 pipeconf |= PIPECONF_PROGRESSIVE;
5024
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005025 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5026 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005027
Daniel Vetter84b046f2013-02-19 18:48:54 +01005028 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5029 POSTING_READ(PIPECONF(intel_crtc->pipe));
5030}
5031
Eric Anholtf564048e2011-03-30 13:01:02 -07005032static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005033 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005034 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005035{
5036 struct drm_device *dev = crtc->dev;
5037 struct drm_i915_private *dev_priv = dev->dev_private;
5038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5039 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005040 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005041 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005042 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005043 u32 dspcntr;
Daniel Vettera16af722013-04-30 14:01:44 +02005044 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005045 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005046 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005047 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005048 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005049
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005050 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005051 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005052 case INTEL_OUTPUT_LVDS:
5053 is_lvds = true;
5054 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005055 case INTEL_OUTPUT_DSI:
5056 is_dsi = true;
5057 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005058 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005059
Eric Anholtc751ce42010-03-25 11:48:48 -07005060 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005061 }
5062
Jani Nikulaf2335332013-09-13 11:03:09 +03005063 if (is_dsi)
5064 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005065
Jani Nikulaf2335332013-09-13 11:03:09 +03005066 if (!intel_crtc->config.clock_set) {
5067 refclk = i9xx_get_refclk(crtc, num_connectors);
5068
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005069 /*
5070 * Returns a set of divisors for the desired target clock with
5071 * the given refclk, or FALSE. The returned values represent
5072 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5073 * 2) / p1 / p2.
5074 */
5075 limit = intel_limit(crtc, refclk);
5076 ok = dev_priv->display.find_dpll(limit, crtc,
5077 intel_crtc->config.port_clock,
5078 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005079 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005080 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5081 return -EINVAL;
5082 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005083
Jani Nikulaf2335332013-09-13 11:03:09 +03005084 if (is_lvds && dev_priv->lvds_downclock_avail) {
5085 /*
5086 * Ensure we match the reduced clock's P to the target
5087 * clock. If the clocks don't match, we can't switch
5088 * the display clock by using the FP0/FP1. In such case
5089 * we will disable the LVDS downclock feature.
5090 */
5091 has_reduced_clock =
5092 dev_priv->display.find_dpll(limit, crtc,
5093 dev_priv->lvds_downclock,
5094 refclk, &clock,
5095 &reduced_clock);
5096 }
5097 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005098 intel_crtc->config.dpll.n = clock.n;
5099 intel_crtc->config.dpll.m1 = clock.m1;
5100 intel_crtc->config.dpll.m2 = clock.m2;
5101 intel_crtc->config.dpll.p1 = clock.p1;
5102 intel_crtc->config.dpll.p2 = clock.p2;
5103 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005104
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005105 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005106 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305107 has_reduced_clock ? &reduced_clock : NULL,
5108 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005109 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005110 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005111 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005112 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005113 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005114 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005115 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005116
Jani Nikulaf2335332013-09-13 11:03:09 +03005117skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005118 /* Set up the display plane register */
5119 dspcntr = DISPPLANE_GAMMA_ENABLE;
5120
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005121 if (!IS_VALLEYVIEW(dev)) {
5122 if (pipe == 0)
5123 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5124 else
5125 dspcntr |= DISPPLANE_SEL_PIPE_B;
5126 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005127
Daniel Vetter8a654f32013-06-01 17:16:22 +02005128 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005129
5130 /* pipesrc and dspsize control the size that is scaled from,
5131 * which should always be the user's requested size.
5132 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005133 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005134 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5135 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005136 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005137
Daniel Vetter84b046f2013-02-19 18:48:54 +01005138 i9xx_set_pipeconf(intel_crtc);
5139
Eric Anholtf564048e2011-03-30 13:01:02 -07005140 I915_WRITE(DSPCNTR(plane), dspcntr);
5141 POSTING_READ(DSPCNTR(plane));
5142
Daniel Vetter94352cf2012-07-05 22:51:56 +02005143 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005144
Eric Anholtf564048e2011-03-30 13:01:02 -07005145 return ret;
5146}
5147
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005148static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5149 struct intel_crtc_config *pipe_config)
5150{
5151 struct drm_device *dev = crtc->base.dev;
5152 struct drm_i915_private *dev_priv = dev->dev_private;
5153 uint32_t tmp;
5154
5155 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005156 if (!(tmp & PFIT_ENABLE))
5157 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005158
Daniel Vetter06922822013-07-11 13:35:40 +02005159 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005160 if (INTEL_INFO(dev)->gen < 4) {
5161 if (crtc->pipe != PIPE_B)
5162 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005163 } else {
5164 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5165 return;
5166 }
5167
Daniel Vetter06922822013-07-11 13:35:40 +02005168 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005169 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5170 if (INTEL_INFO(dev)->gen < 5)
5171 pipe_config->gmch_pfit.lvds_border_bits =
5172 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5173}
5174
Jesse Barnesacbec812013-09-20 11:29:32 -07005175static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5176 struct intel_crtc_config *pipe_config)
5177{
5178 struct drm_device *dev = crtc->base.dev;
5179 struct drm_i915_private *dev_priv = dev->dev_private;
5180 int pipe = pipe_config->cpu_transcoder;
5181 intel_clock_t clock;
5182 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005183 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005184
5185 mutex_lock(&dev_priv->dpio_lock);
5186 mdiv = vlv_dpio_read(dev_priv, pipe, DPIO_DIV(pipe));
5187 mutex_unlock(&dev_priv->dpio_lock);
5188
5189 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5190 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5191 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5192 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5193 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5194
Chris Wilson662c6ec2013-09-25 14:24:01 -07005195 clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
5196 clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
Jesse Barnesacbec812013-09-20 11:29:32 -07005197
5198 pipe_config->port_clock = clock.dot / 10;
5199}
5200
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005201static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5202 struct intel_crtc_config *pipe_config)
5203{
5204 struct drm_device *dev = crtc->base.dev;
5205 struct drm_i915_private *dev_priv = dev->dev_private;
5206 uint32_t tmp;
5207
Daniel Vettere143a212013-07-04 12:01:15 +02005208 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005209 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005210
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005211 tmp = I915_READ(PIPECONF(crtc->pipe));
5212 if (!(tmp & PIPECONF_ENABLE))
5213 return false;
5214
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005215 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5216 switch (tmp & PIPECONF_BPC_MASK) {
5217 case PIPECONF_6BPC:
5218 pipe_config->pipe_bpp = 18;
5219 break;
5220 case PIPECONF_8BPC:
5221 pipe_config->pipe_bpp = 24;
5222 break;
5223 case PIPECONF_10BPC:
5224 pipe_config->pipe_bpp = 30;
5225 break;
5226 default:
5227 break;
5228 }
5229 }
5230
Ville Syrjälä282740f2013-09-04 18:30:03 +03005231 if (INTEL_INFO(dev)->gen < 4)
5232 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5233
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005234 intel_get_pipe_timings(crtc, pipe_config);
5235
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005236 i9xx_get_pfit_config(crtc, pipe_config);
5237
Daniel Vetter6c49f242013-06-06 12:45:25 +02005238 if (INTEL_INFO(dev)->gen >= 4) {
5239 tmp = I915_READ(DPLL_MD(crtc->pipe));
5240 pipe_config->pixel_multiplier =
5241 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5242 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005243 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005244 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5245 tmp = I915_READ(DPLL(crtc->pipe));
5246 pipe_config->pixel_multiplier =
5247 ((tmp & SDVO_MULTIPLIER_MASK)
5248 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5249 } else {
5250 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5251 * port and will be fixed up in the encoder->get_config
5252 * function. */
5253 pipe_config->pixel_multiplier = 1;
5254 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005255 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5256 if (!IS_VALLEYVIEW(dev)) {
5257 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5258 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005259 } else {
5260 /* Mask out read-only status bits. */
5261 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5262 DPLL_PORTC_READY_MASK |
5263 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005264 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005265
Jesse Barnesacbec812013-09-20 11:29:32 -07005266 if (IS_VALLEYVIEW(dev))
5267 vlv_crtc_clock_get(crtc, pipe_config);
5268 else
5269 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005270
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005271 return true;
5272}
5273
Paulo Zanonidde86e22012-12-01 12:04:25 -02005274static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005275{
5276 struct drm_i915_private *dev_priv = dev->dev_private;
5277 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005278 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005279 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005280 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005281 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005282 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005283 bool has_ck505 = false;
5284 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005285
5286 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005287 list_for_each_entry(encoder, &mode_config->encoder_list,
5288 base.head) {
5289 switch (encoder->type) {
5290 case INTEL_OUTPUT_LVDS:
5291 has_panel = true;
5292 has_lvds = true;
5293 break;
5294 case INTEL_OUTPUT_EDP:
5295 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005296 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005297 has_cpu_edp = true;
5298 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005299 }
5300 }
5301
Keith Packard99eb6a02011-09-26 14:29:12 -07005302 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005303 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005304 can_ssc = has_ck505;
5305 } else {
5306 has_ck505 = false;
5307 can_ssc = true;
5308 }
5309
Imre Deak2de69052013-05-08 13:14:04 +03005310 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5311 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005312
5313 /* Ironlake: try to setup display ref clock before DPLL
5314 * enabling. This is only under driver's control after
5315 * PCH B stepping, previous chipset stepping should be
5316 * ignoring this setting.
5317 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005318 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005319
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005320 /* As we must carefully and slowly disable/enable each source in turn,
5321 * compute the final state we want first and check if we need to
5322 * make any changes at all.
5323 */
5324 final = val;
5325 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005326 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005327 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005328 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005329 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5330
5331 final &= ~DREF_SSC_SOURCE_MASK;
5332 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5333 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005334
Keith Packard199e5d72011-09-22 12:01:57 -07005335 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005336 final |= DREF_SSC_SOURCE_ENABLE;
5337
5338 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5339 final |= DREF_SSC1_ENABLE;
5340
5341 if (has_cpu_edp) {
5342 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5343 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5344 else
5345 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5346 } else
5347 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5348 } else {
5349 final |= DREF_SSC_SOURCE_DISABLE;
5350 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5351 }
5352
5353 if (final == val)
5354 return;
5355
5356 /* Always enable nonspread source */
5357 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5358
5359 if (has_ck505)
5360 val |= DREF_NONSPREAD_CK505_ENABLE;
5361 else
5362 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5363
5364 if (has_panel) {
5365 val &= ~DREF_SSC_SOURCE_MASK;
5366 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005367
Keith Packard199e5d72011-09-22 12:01:57 -07005368 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005369 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005370 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005371 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005372 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005373 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005374
5375 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005376 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005377 POSTING_READ(PCH_DREF_CONTROL);
5378 udelay(200);
5379
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005380 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005381
5382 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005383 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005384 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005385 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005386 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005387 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005388 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005389 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005390 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005391 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005392
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005393 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005394 POSTING_READ(PCH_DREF_CONTROL);
5395 udelay(200);
5396 } else {
5397 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5398
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005399 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005400
5401 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005402 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005403
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005404 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005405 POSTING_READ(PCH_DREF_CONTROL);
5406 udelay(200);
5407
5408 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005409 val &= ~DREF_SSC_SOURCE_MASK;
5410 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005411
5412 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005413 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005414
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005415 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005416 POSTING_READ(PCH_DREF_CONTROL);
5417 udelay(200);
5418 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005419
5420 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005421}
5422
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005423static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005424{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005425 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005426
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005427 tmp = I915_READ(SOUTH_CHICKEN2);
5428 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5429 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005430
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005431 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5432 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5433 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005434
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005435 tmp = I915_READ(SOUTH_CHICKEN2);
5436 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5437 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005438
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005439 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5440 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5441 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005442}
5443
5444/* WaMPhyProgramming:hsw */
5445static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5446{
5447 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005448
5449 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5450 tmp &= ~(0xFF << 24);
5451 tmp |= (0x12 << 24);
5452 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5453
Paulo Zanonidde86e22012-12-01 12:04:25 -02005454 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5455 tmp |= (1 << 11);
5456 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5457
5458 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5459 tmp |= (1 << 11);
5460 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5461
Paulo Zanonidde86e22012-12-01 12:04:25 -02005462 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5463 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5464 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5465
5466 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5467 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5468 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5469
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005470 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5471 tmp &= ~(7 << 13);
5472 tmp |= (5 << 13);
5473 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005474
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005475 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5476 tmp &= ~(7 << 13);
5477 tmp |= (5 << 13);
5478 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005479
5480 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5481 tmp &= ~0xFF;
5482 tmp |= 0x1C;
5483 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5484
5485 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5486 tmp &= ~0xFF;
5487 tmp |= 0x1C;
5488 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5489
5490 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5491 tmp &= ~(0xFF << 16);
5492 tmp |= (0x1C << 16);
5493 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5494
5495 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5496 tmp &= ~(0xFF << 16);
5497 tmp |= (0x1C << 16);
5498 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5499
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005500 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5501 tmp |= (1 << 27);
5502 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005503
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005504 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5505 tmp |= (1 << 27);
5506 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005507
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005508 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5509 tmp &= ~(0xF << 28);
5510 tmp |= (4 << 28);
5511 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005512
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005513 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5514 tmp &= ~(0xF << 28);
5515 tmp |= (4 << 28);
5516 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005517}
5518
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005519/* Implements 3 different sequences from BSpec chapter "Display iCLK
5520 * Programming" based on the parameters passed:
5521 * - Sequence to enable CLKOUT_DP
5522 * - Sequence to enable CLKOUT_DP without spread
5523 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5524 */
5525static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5526 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005527{
5528 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005529 uint32_t reg, tmp;
5530
5531 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5532 with_spread = true;
5533 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5534 with_fdi, "LP PCH doesn't have FDI\n"))
5535 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005536
5537 mutex_lock(&dev_priv->dpio_lock);
5538
5539 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5540 tmp &= ~SBI_SSCCTL_DISABLE;
5541 tmp |= SBI_SSCCTL_PATHALT;
5542 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5543
5544 udelay(24);
5545
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005546 if (with_spread) {
5547 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5548 tmp &= ~SBI_SSCCTL_PATHALT;
5549 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005550
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005551 if (with_fdi) {
5552 lpt_reset_fdi_mphy(dev_priv);
5553 lpt_program_fdi_mphy(dev_priv);
5554 }
5555 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005556
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005557 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5558 SBI_GEN0 : SBI_DBUFF0;
5559 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5560 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5561 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005562
5563 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005564}
5565
Paulo Zanoni47701c32013-07-23 11:19:25 -03005566/* Sequence to disable CLKOUT_DP */
5567static void lpt_disable_clkout_dp(struct drm_device *dev)
5568{
5569 struct drm_i915_private *dev_priv = dev->dev_private;
5570 uint32_t reg, tmp;
5571
5572 mutex_lock(&dev_priv->dpio_lock);
5573
5574 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5575 SBI_GEN0 : SBI_DBUFF0;
5576 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5577 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5578 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5579
5580 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5581 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5582 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5583 tmp |= SBI_SSCCTL_PATHALT;
5584 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5585 udelay(32);
5586 }
5587 tmp |= SBI_SSCCTL_DISABLE;
5588 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5589 }
5590
5591 mutex_unlock(&dev_priv->dpio_lock);
5592}
5593
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005594static void lpt_init_pch_refclk(struct drm_device *dev)
5595{
5596 struct drm_mode_config *mode_config = &dev->mode_config;
5597 struct intel_encoder *encoder;
5598 bool has_vga = false;
5599
5600 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5601 switch (encoder->type) {
5602 case INTEL_OUTPUT_ANALOG:
5603 has_vga = true;
5604 break;
5605 }
5606 }
5607
Paulo Zanoni47701c32013-07-23 11:19:25 -03005608 if (has_vga)
5609 lpt_enable_clkout_dp(dev, true, true);
5610 else
5611 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005612}
5613
Paulo Zanonidde86e22012-12-01 12:04:25 -02005614/*
5615 * Initialize reference clocks when the driver loads
5616 */
5617void intel_init_pch_refclk(struct drm_device *dev)
5618{
5619 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5620 ironlake_init_pch_refclk(dev);
5621 else if (HAS_PCH_LPT(dev))
5622 lpt_init_pch_refclk(dev);
5623}
5624
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005625static int ironlake_get_refclk(struct drm_crtc *crtc)
5626{
5627 struct drm_device *dev = crtc->dev;
5628 struct drm_i915_private *dev_priv = dev->dev_private;
5629 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005630 int num_connectors = 0;
5631 bool is_lvds = false;
5632
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005633 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005634 switch (encoder->type) {
5635 case INTEL_OUTPUT_LVDS:
5636 is_lvds = true;
5637 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005638 }
5639 num_connectors++;
5640 }
5641
5642 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5643 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005644 dev_priv->vbt.lvds_ssc_freq);
5645 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005646 }
5647
5648 return 120000;
5649}
5650
Daniel Vetter6ff93602013-04-19 11:24:36 +02005651static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005652{
5653 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5655 int pipe = intel_crtc->pipe;
5656 uint32_t val;
5657
Daniel Vetter78114072013-06-13 00:54:57 +02005658 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005659
Daniel Vetter965e0c42013-03-27 00:44:57 +01005660 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005661 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005662 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005663 break;
5664 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005665 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005666 break;
5667 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005668 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005669 break;
5670 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005671 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005672 break;
5673 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005674 /* Case prevented by intel_choose_pipe_bpp_dither. */
5675 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005676 }
5677
Daniel Vetterd8b32242013-04-25 17:54:44 +02005678 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005679 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5680
Daniel Vetter6ff93602013-04-19 11:24:36 +02005681 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005682 val |= PIPECONF_INTERLACED_ILK;
5683 else
5684 val |= PIPECONF_PROGRESSIVE;
5685
Daniel Vetter50f3b012013-03-27 00:44:56 +01005686 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005687 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005688
Paulo Zanonic8203562012-09-12 10:06:29 -03005689 I915_WRITE(PIPECONF(pipe), val);
5690 POSTING_READ(PIPECONF(pipe));
5691}
5692
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005693/*
5694 * Set up the pipe CSC unit.
5695 *
5696 * Currently only full range RGB to limited range RGB conversion
5697 * is supported, but eventually this should handle various
5698 * RGB<->YCbCr scenarios as well.
5699 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005700static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005701{
5702 struct drm_device *dev = crtc->dev;
5703 struct drm_i915_private *dev_priv = dev->dev_private;
5704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5705 int pipe = intel_crtc->pipe;
5706 uint16_t coeff = 0x7800; /* 1.0 */
5707
5708 /*
5709 * TODO: Check what kind of values actually come out of the pipe
5710 * with these coeff/postoff values and adjust to get the best
5711 * accuracy. Perhaps we even need to take the bpc value into
5712 * consideration.
5713 */
5714
Daniel Vetter50f3b012013-03-27 00:44:56 +01005715 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005716 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5717
5718 /*
5719 * GY/GU and RY/RU should be the other way around according
5720 * to BSpec, but reality doesn't agree. Just set them up in
5721 * a way that results in the correct picture.
5722 */
5723 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5724 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5725
5726 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5727 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5728
5729 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5730 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5731
5732 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5733 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5734 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5735
5736 if (INTEL_INFO(dev)->gen > 6) {
5737 uint16_t postoff = 0;
5738
Daniel Vetter50f3b012013-03-27 00:44:56 +01005739 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005740 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5741
5742 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5743 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5744 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5745
5746 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5747 } else {
5748 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5749
Daniel Vetter50f3b012013-03-27 00:44:56 +01005750 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005751 mode |= CSC_BLACK_SCREEN_OFFSET;
5752
5753 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5754 }
5755}
5756
Daniel Vetter6ff93602013-04-19 11:24:36 +02005757static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005758{
5759 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005761 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005762 uint32_t val;
5763
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005764 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005765
Daniel Vetterd8b32242013-04-25 17:54:44 +02005766 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005767 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5768
Daniel Vetter6ff93602013-04-19 11:24:36 +02005769 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005770 val |= PIPECONF_INTERLACED_ILK;
5771 else
5772 val |= PIPECONF_PROGRESSIVE;
5773
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005774 I915_WRITE(PIPECONF(cpu_transcoder), val);
5775 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005776
5777 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5778 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005779}
5780
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005781static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005782 intel_clock_t *clock,
5783 bool *has_reduced_clock,
5784 intel_clock_t *reduced_clock)
5785{
5786 struct drm_device *dev = crtc->dev;
5787 struct drm_i915_private *dev_priv = dev->dev_private;
5788 struct intel_encoder *intel_encoder;
5789 int refclk;
5790 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02005791 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005792
5793 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5794 switch (intel_encoder->type) {
5795 case INTEL_OUTPUT_LVDS:
5796 is_lvds = true;
5797 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005798 }
5799 }
5800
5801 refclk = ironlake_get_refclk(crtc);
5802
5803 /*
5804 * Returns a set of divisors for the desired target clock with the given
5805 * refclk, or FALSE. The returned values represent the clock equation:
5806 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5807 */
5808 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005809 ret = dev_priv->display.find_dpll(limit, crtc,
5810 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005811 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005812 if (!ret)
5813 return false;
5814
5815 if (is_lvds && dev_priv->lvds_downclock_avail) {
5816 /*
5817 * Ensure we match the reduced clock's P to the target clock.
5818 * If the clocks don't match, we can't switch the display clock
5819 * by using the FP0/FP1. In such case we will disable the LVDS
5820 * downclock feature.
5821 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005822 *has_reduced_clock =
5823 dev_priv->display.find_dpll(limit, crtc,
5824 dev_priv->lvds_downclock,
5825 refclk, clock,
5826 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005827 }
5828
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005829 return true;
5830}
5831
Daniel Vetter01a415f2012-10-27 15:58:40 +02005832static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5833{
5834 struct drm_i915_private *dev_priv = dev->dev_private;
5835 uint32_t temp;
5836
5837 temp = I915_READ(SOUTH_CHICKEN1);
5838 if (temp & FDI_BC_BIFURCATION_SELECT)
5839 return;
5840
5841 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5842 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5843
5844 temp |= FDI_BC_BIFURCATION_SELECT;
5845 DRM_DEBUG_KMS("enabling fdi C rx\n");
5846 I915_WRITE(SOUTH_CHICKEN1, temp);
5847 POSTING_READ(SOUTH_CHICKEN1);
5848}
5849
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005850static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005851{
5852 struct drm_device *dev = intel_crtc->base.dev;
5853 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005854
5855 switch (intel_crtc->pipe) {
5856 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005857 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005858 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005859 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005860 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5861 else
5862 cpt_enable_fdi_bc_bifurcation(dev);
5863
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005864 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005865 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005866 cpt_enable_fdi_bc_bifurcation(dev);
5867
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005868 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005869 default:
5870 BUG();
5871 }
5872}
5873
Paulo Zanonid4b19312012-11-29 11:29:32 -02005874int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5875{
5876 /*
5877 * Account for spread spectrum to avoid
5878 * oversubscribing the link. Max center spread
5879 * is 2.5%; use 5% for safety's sake.
5880 */
5881 u32 bps = target_clock * bpp * 21 / 20;
5882 return bps / (link_bw * 8) + 1;
5883}
5884
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005885static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005886{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005887 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005888}
5889
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005890static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005891 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005892 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005893{
5894 struct drm_crtc *crtc = &intel_crtc->base;
5895 struct drm_device *dev = crtc->dev;
5896 struct drm_i915_private *dev_priv = dev->dev_private;
5897 struct intel_encoder *intel_encoder;
5898 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005899 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005900 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005901
5902 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5903 switch (intel_encoder->type) {
5904 case INTEL_OUTPUT_LVDS:
5905 is_lvds = true;
5906 break;
5907 case INTEL_OUTPUT_SDVO:
5908 case INTEL_OUTPUT_HDMI:
5909 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005910 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005911 }
5912
5913 num_connectors++;
5914 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005915
Chris Wilsonc1858122010-12-03 21:35:48 +00005916 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005917 factor = 21;
5918 if (is_lvds) {
5919 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005920 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005921 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005922 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005923 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005924 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005925
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005926 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005927 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005928
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005929 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5930 *fp2 |= FP_CB_TUNE;
5931
Chris Wilson5eddb702010-09-11 13:48:45 +01005932 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005933
Eric Anholta07d6782011-03-30 13:01:08 -07005934 if (is_lvds)
5935 dpll |= DPLLB_MODE_LVDS;
5936 else
5937 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005938
Daniel Vetteref1b4602013-06-01 17:17:04 +02005939 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5940 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005941
5942 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005943 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005944 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005945 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005946
Eric Anholta07d6782011-03-30 13:01:08 -07005947 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005948 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005949 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005950 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005951
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005952 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005953 case 5:
5954 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5955 break;
5956 case 7:
5957 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5958 break;
5959 case 10:
5960 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5961 break;
5962 case 14:
5963 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5964 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005965 }
5966
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005967 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005968 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005969 else
5970 dpll |= PLL_REF_INPUT_DREFCLK;
5971
Daniel Vetter959e16d2013-06-05 13:34:21 +02005972 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005973}
5974
Jesse Barnes79e53942008-11-07 14:24:08 -08005975static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005976 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005977 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005978{
5979 struct drm_device *dev = crtc->dev;
5980 struct drm_i915_private *dev_priv = dev->dev_private;
5981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5982 int pipe = intel_crtc->pipe;
5983 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005984 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005985 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005986 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005987 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005988 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005989 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005990 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005991 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005992
5993 for_each_encoder_on_crtc(dev, crtc, encoder) {
5994 switch (encoder->type) {
5995 case INTEL_OUTPUT_LVDS:
5996 is_lvds = true;
5997 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005998 }
5999
6000 num_connectors++;
6001 }
6002
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006003 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6004 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6005
Daniel Vetterff9a6752013-06-01 17:16:21 +02006006 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006007 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006008 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006009 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6010 return -EINVAL;
6011 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006012 /* Compat-code for transition, will disappear. */
6013 if (!intel_crtc->config.clock_set) {
6014 intel_crtc->config.dpll.n = clock.n;
6015 intel_crtc->config.dpll.m1 = clock.m1;
6016 intel_crtc->config.dpll.m2 = clock.m2;
6017 intel_crtc->config.dpll.p1 = clock.p1;
6018 intel_crtc->config.dpll.p2 = clock.p2;
6019 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006020
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006021 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006022 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006023 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006024 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006025 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006026
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006027 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006028 &fp, &reduced_clock,
6029 has_reduced_clock ? &fp2 : NULL);
6030
Daniel Vetter959e16d2013-06-05 13:34:21 +02006031 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006032 intel_crtc->config.dpll_hw_state.fp0 = fp;
6033 if (has_reduced_clock)
6034 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6035 else
6036 intel_crtc->config.dpll_hw_state.fp1 = fp;
6037
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006038 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006039 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006040 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6041 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006042 return -EINVAL;
6043 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006044 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006045 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006046
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006047 if (intel_crtc->config.has_dp_encoder)
6048 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006049
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006050 if (is_lvds && has_reduced_clock && i915_powersave)
6051 intel_crtc->lowfreq_avail = true;
6052 else
6053 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006054
6055 if (intel_crtc->config.has_pch_encoder) {
6056 pll = intel_crtc_to_shared_dpll(intel_crtc);
6057
Jesse Barnes79e53942008-11-07 14:24:08 -08006058 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006059
Daniel Vetter8a654f32013-06-01 17:16:22 +02006060 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006061
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006062 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006063 intel_cpu_transcoder_set_m_n(intel_crtc,
6064 &intel_crtc->config.fdi_m_n);
6065 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006066
Daniel Vetterebfd86f2013-04-19 11:24:44 +02006067 if (IS_IVYBRIDGE(dev))
6068 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006069
Daniel Vetter6ff93602013-04-19 11:24:36 +02006070 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006071
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006072 /* Set up the display plane register */
6073 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006074 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006075
Daniel Vetter94352cf2012-07-05 22:51:56 +02006076 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006077
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006078 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006079}
6080
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006081static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6082 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006083{
6084 struct drm_device *dev = crtc->base.dev;
6085 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006086 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006087
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006088 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6089 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6090 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6091 & ~TU_SIZE_MASK;
6092 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6093 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6094 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6095}
6096
6097static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6098 enum transcoder transcoder,
6099 struct intel_link_m_n *m_n)
6100{
6101 struct drm_device *dev = crtc->base.dev;
6102 struct drm_i915_private *dev_priv = dev->dev_private;
6103 enum pipe pipe = crtc->pipe;
6104
6105 if (INTEL_INFO(dev)->gen >= 5) {
6106 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6107 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6108 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6109 & ~TU_SIZE_MASK;
6110 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6111 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6112 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6113 } else {
6114 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6115 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6116 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6117 & ~TU_SIZE_MASK;
6118 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6119 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6120 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6121 }
6122}
6123
6124void intel_dp_get_m_n(struct intel_crtc *crtc,
6125 struct intel_crtc_config *pipe_config)
6126{
6127 if (crtc->config.has_pch_encoder)
6128 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6129 else
6130 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6131 &pipe_config->dp_m_n);
6132}
6133
Daniel Vetter72419202013-04-04 13:28:53 +02006134static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6135 struct intel_crtc_config *pipe_config)
6136{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006137 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6138 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006139}
6140
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006141static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6142 struct intel_crtc_config *pipe_config)
6143{
6144 struct drm_device *dev = crtc->base.dev;
6145 struct drm_i915_private *dev_priv = dev->dev_private;
6146 uint32_t tmp;
6147
6148 tmp = I915_READ(PF_CTL(crtc->pipe));
6149
6150 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006151 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006152 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6153 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006154
6155 /* We currently do not free assignements of panel fitters on
6156 * ivb/hsw (since we don't use the higher upscaling modes which
6157 * differentiates them) so just WARN about this case for now. */
6158 if (IS_GEN7(dev)) {
6159 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6160 PF_PIPE_SEL_IVB(crtc->pipe));
6161 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006162 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006163}
6164
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006165static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6166 struct intel_crtc_config *pipe_config)
6167{
6168 struct drm_device *dev = crtc->base.dev;
6169 struct drm_i915_private *dev_priv = dev->dev_private;
6170 uint32_t tmp;
6171
Daniel Vettere143a212013-07-04 12:01:15 +02006172 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006173 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006174
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006175 tmp = I915_READ(PIPECONF(crtc->pipe));
6176 if (!(tmp & PIPECONF_ENABLE))
6177 return false;
6178
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006179 switch (tmp & PIPECONF_BPC_MASK) {
6180 case PIPECONF_6BPC:
6181 pipe_config->pipe_bpp = 18;
6182 break;
6183 case PIPECONF_8BPC:
6184 pipe_config->pipe_bpp = 24;
6185 break;
6186 case PIPECONF_10BPC:
6187 pipe_config->pipe_bpp = 30;
6188 break;
6189 case PIPECONF_12BPC:
6190 pipe_config->pipe_bpp = 36;
6191 break;
6192 default:
6193 break;
6194 }
6195
Daniel Vetterab9412b2013-05-03 11:49:46 +02006196 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006197 struct intel_shared_dpll *pll;
6198
Daniel Vetter88adfff2013-03-28 10:42:01 +01006199 pipe_config->has_pch_encoder = true;
6200
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006201 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6202 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6203 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006204
6205 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006206
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006207 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006208 pipe_config->shared_dpll =
6209 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006210 } else {
6211 tmp = I915_READ(PCH_DPLL_SEL);
6212 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6213 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6214 else
6215 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6216 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006217
6218 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6219
6220 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6221 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006222
6223 tmp = pipe_config->dpll_hw_state.dpll;
6224 pipe_config->pixel_multiplier =
6225 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6226 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006227
6228 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006229 } else {
6230 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006231 }
6232
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006233 intel_get_pipe_timings(crtc, pipe_config);
6234
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006235 ironlake_get_pfit_config(crtc, pipe_config);
6236
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006237 return true;
6238}
6239
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006240static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6241{
6242 struct drm_device *dev = dev_priv->dev;
6243 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6244 struct intel_crtc *crtc;
6245 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006246 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006247
6248 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6249 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6250 pipe_name(crtc->pipe));
6251
6252 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6253 WARN(plls->spll_refcount, "SPLL enabled\n");
6254 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6255 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6256 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6257 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6258 "CPU PWM1 enabled\n");
6259 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6260 "CPU PWM2 enabled\n");
6261 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6262 "PCH PWM1 enabled\n");
6263 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6264 "Utility pin enabled\n");
6265 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6266
6267 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6268 val = I915_READ(DEIMR);
6269 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6270 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6271 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006272 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006273 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6274 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6275}
6276
6277/*
6278 * This function implements pieces of two sequences from BSpec:
6279 * - Sequence for display software to disable LCPLL
6280 * - Sequence for display software to allow package C8+
6281 * The steps implemented here are just the steps that actually touch the LCPLL
6282 * register. Callers should take care of disabling all the display engine
6283 * functions, doing the mode unset, fixing interrupts, etc.
6284 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006285static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6286 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006287{
6288 uint32_t val;
6289
6290 assert_can_disable_lcpll(dev_priv);
6291
6292 val = I915_READ(LCPLL_CTL);
6293
6294 if (switch_to_fclk) {
6295 val |= LCPLL_CD_SOURCE_FCLK;
6296 I915_WRITE(LCPLL_CTL, val);
6297
6298 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6299 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6300 DRM_ERROR("Switching to FCLK failed\n");
6301
6302 val = I915_READ(LCPLL_CTL);
6303 }
6304
6305 val |= LCPLL_PLL_DISABLE;
6306 I915_WRITE(LCPLL_CTL, val);
6307 POSTING_READ(LCPLL_CTL);
6308
6309 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6310 DRM_ERROR("LCPLL still locked\n");
6311
6312 val = I915_READ(D_COMP);
6313 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006314 mutex_lock(&dev_priv->rps.hw_lock);
6315 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6316 DRM_ERROR("Failed to disable D_COMP\n");
6317 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006318 POSTING_READ(D_COMP);
6319 ndelay(100);
6320
6321 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6322 DRM_ERROR("D_COMP RCOMP still in progress\n");
6323
6324 if (allow_power_down) {
6325 val = I915_READ(LCPLL_CTL);
6326 val |= LCPLL_POWER_DOWN_ALLOW;
6327 I915_WRITE(LCPLL_CTL, val);
6328 POSTING_READ(LCPLL_CTL);
6329 }
6330}
6331
6332/*
6333 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6334 * source.
6335 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006336static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006337{
6338 uint32_t val;
6339
6340 val = I915_READ(LCPLL_CTL);
6341
6342 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6343 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6344 return;
6345
Paulo Zanoni215733f2013-08-19 13:18:07 -03006346 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6347 * we'll hang the machine! */
6348 dev_priv->uncore.funcs.force_wake_get(dev_priv);
6349
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006350 if (val & LCPLL_POWER_DOWN_ALLOW) {
6351 val &= ~LCPLL_POWER_DOWN_ALLOW;
6352 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006353 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006354 }
6355
6356 val = I915_READ(D_COMP);
6357 val |= D_COMP_COMP_FORCE;
6358 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006359 mutex_lock(&dev_priv->rps.hw_lock);
6360 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6361 DRM_ERROR("Failed to enable D_COMP\n");
6362 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006363 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006364
6365 val = I915_READ(LCPLL_CTL);
6366 val &= ~LCPLL_PLL_DISABLE;
6367 I915_WRITE(LCPLL_CTL, val);
6368
6369 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6370 DRM_ERROR("LCPLL not locked yet\n");
6371
6372 if (val & LCPLL_CD_SOURCE_FCLK) {
6373 val = I915_READ(LCPLL_CTL);
6374 val &= ~LCPLL_CD_SOURCE_FCLK;
6375 I915_WRITE(LCPLL_CTL, val);
6376
6377 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6378 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6379 DRM_ERROR("Switching back to LCPLL failed\n");
6380 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006381
6382 dev_priv->uncore.funcs.force_wake_put(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006383}
6384
Paulo Zanonic67a4702013-08-19 13:18:09 -03006385void hsw_enable_pc8_work(struct work_struct *__work)
6386{
6387 struct drm_i915_private *dev_priv =
6388 container_of(to_delayed_work(__work), struct drm_i915_private,
6389 pc8.enable_work);
6390 struct drm_device *dev = dev_priv->dev;
6391 uint32_t val;
6392
6393 if (dev_priv->pc8.enabled)
6394 return;
6395
6396 DRM_DEBUG_KMS("Enabling package C8+\n");
6397
6398 dev_priv->pc8.enabled = true;
6399
6400 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6401 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6402 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6403 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6404 }
6405
6406 lpt_disable_clkout_dp(dev);
6407 hsw_pc8_disable_interrupts(dev);
6408 hsw_disable_lcpll(dev_priv, true, true);
6409}
6410
6411static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6412{
6413 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6414 WARN(dev_priv->pc8.disable_count < 1,
6415 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6416
6417 dev_priv->pc8.disable_count--;
6418 if (dev_priv->pc8.disable_count != 0)
6419 return;
6420
6421 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006422 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006423}
6424
6425static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6426{
6427 struct drm_device *dev = dev_priv->dev;
6428 uint32_t val;
6429
6430 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6431 WARN(dev_priv->pc8.disable_count < 0,
6432 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6433
6434 dev_priv->pc8.disable_count++;
6435 if (dev_priv->pc8.disable_count != 1)
6436 return;
6437
6438 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6439 if (!dev_priv->pc8.enabled)
6440 return;
6441
6442 DRM_DEBUG_KMS("Disabling package C8+\n");
6443
6444 hsw_restore_lcpll(dev_priv);
6445 hsw_pc8_restore_interrupts(dev);
6446 lpt_init_pch_refclk(dev);
6447
6448 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6449 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6450 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6451 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6452 }
6453
6454 intel_prepare_ddi(dev);
6455 i915_gem_init_swizzling(dev);
6456 mutex_lock(&dev_priv->rps.hw_lock);
6457 gen6_update_ring_freq(dev);
6458 mutex_unlock(&dev_priv->rps.hw_lock);
6459 dev_priv->pc8.enabled = false;
6460}
6461
6462void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6463{
6464 mutex_lock(&dev_priv->pc8.lock);
6465 __hsw_enable_package_c8(dev_priv);
6466 mutex_unlock(&dev_priv->pc8.lock);
6467}
6468
6469void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6470{
6471 mutex_lock(&dev_priv->pc8.lock);
6472 __hsw_disable_package_c8(dev_priv);
6473 mutex_unlock(&dev_priv->pc8.lock);
6474}
6475
6476static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6477{
6478 struct drm_device *dev = dev_priv->dev;
6479 struct intel_crtc *crtc;
6480 uint32_t val;
6481
6482 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6483 if (crtc->base.enabled)
6484 return false;
6485
6486 /* This case is still possible since we have the i915.disable_power_well
6487 * parameter and also the KVMr or something else might be requesting the
6488 * power well. */
6489 val = I915_READ(HSW_PWR_WELL_DRIVER);
6490 if (val != 0) {
6491 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6492 return false;
6493 }
6494
6495 return true;
6496}
6497
6498/* Since we're called from modeset_global_resources there's no way to
6499 * symmetrically increase and decrease the refcount, so we use
6500 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6501 * or not.
6502 */
6503static void hsw_update_package_c8(struct drm_device *dev)
6504{
6505 struct drm_i915_private *dev_priv = dev->dev_private;
6506 bool allow;
6507
6508 if (!i915_enable_pc8)
6509 return;
6510
6511 mutex_lock(&dev_priv->pc8.lock);
6512
6513 allow = hsw_can_enable_package_c8(dev_priv);
6514
6515 if (allow == dev_priv->pc8.requirements_met)
6516 goto done;
6517
6518 dev_priv->pc8.requirements_met = allow;
6519
6520 if (allow)
6521 __hsw_enable_package_c8(dev_priv);
6522 else
6523 __hsw_disable_package_c8(dev_priv);
6524
6525done:
6526 mutex_unlock(&dev_priv->pc8.lock);
6527}
6528
6529static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6530{
6531 if (!dev_priv->pc8.gpu_idle) {
6532 dev_priv->pc8.gpu_idle = true;
6533 hsw_enable_package_c8(dev_priv);
6534 }
6535}
6536
6537static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6538{
6539 if (dev_priv->pc8.gpu_idle) {
6540 dev_priv->pc8.gpu_idle = false;
6541 hsw_disable_package_c8(dev_priv);
6542 }
Daniel Vetter94352cf2012-07-05 22:51:56 +02006543}
Eric Anholtf564048e2011-03-30 13:01:02 -07006544
6545static void haswell_modeset_global_resources(struct drm_device *dev)
6546{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006547 bool enable = false;
6548 struct intel_crtc *crtc;
Eric Anholt0b701d22011-03-30 13:01:03 -07006549
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006550 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6551 if (!crtc->base.enabled)
6552 continue;
Eric Anholt0b701d22011-03-30 13:01:03 -07006553
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006554 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
Jesse Barnes79e53942008-11-07 14:24:08 -08006555 crtc->config.cpu_transcoder != TRANSCODER_EDP)
6556 enable = true;
6557 }
6558
6559 intel_set_power_well(dev, enable);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006560
6561 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006562}
6563
6564static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6565 int x, int y,
6566 struct drm_framebuffer *fb)
6567{
6568 struct drm_device *dev = crtc->dev;
6569 struct drm_i915_private *dev_priv = dev->dev_private;
6570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6571 int plane = intel_crtc->plane;
6572 int ret;
6573
6574 if (!intel_ddi_pll_mode_set(crtc))
6575 return -EINVAL;
6576
Chris Wilson560b85b2010-08-07 11:01:38 +01006577 if (intel_crtc->config.has_dp_encoder)
6578 intel_dp_set_m_n(intel_crtc);
6579
6580 intel_crtc->lowfreq_avail = false;
6581
6582 intel_set_pipe_timings(intel_crtc);
6583
6584 if (intel_crtc->config.has_pch_encoder) {
6585 intel_cpu_transcoder_set_m_n(intel_crtc,
6586 &intel_crtc->config.fdi_m_n);
6587 }
6588
6589 haswell_set_pipeconf(crtc);
6590
6591 intel_set_pipe_csc(crtc);
6592
6593 /* Set up the display plane register */
6594 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6595 POSTING_READ(DSPCNTR(plane));
6596
6597 ret = intel_pipe_set_base(crtc, x, y, fb);
6598
Chris Wilson560b85b2010-08-07 11:01:38 +01006599 return ret;
6600}
6601
6602static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6603 struct intel_crtc_config *pipe_config)
6604{
6605 struct drm_device *dev = crtc->base.dev;
6606 struct drm_i915_private *dev_priv = dev->dev_private;
6607 enum intel_display_power_domain pfit_domain;
6608 uint32_t tmp;
6609
6610 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6611 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6612
6613 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6614 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6615 enum pipe trans_edp_pipe;
6616 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6617 default:
6618 WARN(1, "unknown pipe linked to edp transcoder\n");
6619 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6620 case TRANS_DDI_EDP_INPUT_A_ON:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006621 trans_edp_pipe = PIPE_A;
Chris Wilson6b383a72010-09-13 13:54:26 +01006622 break;
6623 case TRANS_DDI_EDP_INPUT_B_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006624 trans_edp_pipe = PIPE_B;
6625 break;
6626 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6627 trans_edp_pipe = PIPE_C;
6628 break;
6629 }
6630
Chris Wilson560b85b2010-08-07 11:01:38 +01006631 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006632 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6633 }
6634
6635 if (!intel_display_power_enabled(dev,
Chris Wilson6b383a72010-09-13 13:54:26 +01006636 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006637 return false;
6638
6639 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6640 if (!(tmp & PIPECONF_ENABLE))
6641 return false;
6642
6643 /*
6644 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6645 * DDI E. So just check whether this pipe is wired to DDI E and whether
6646 * the PCH transcoder is on.
6647 */
6648 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6649 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6650 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6651 pipe_config->has_pch_encoder = true;
6652
6653 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6654 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6655 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6656
6657 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6658 }
6659
6660 intel_get_pipe_timings(crtc, pipe_config);
6661
6662 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6663 if (intel_display_power_enabled(dev, pfit_domain))
6664 ironlake_get_pfit_config(crtc, pipe_config);
Chris Wilson560b85b2010-08-07 11:01:38 +01006665
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006666 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6667 (I915_READ(IPS_CTL) & IPS_ENABLE);
6668
Chris Wilson560b85b2010-08-07 11:01:38 +01006669 pipe_config->pixel_multiplier = 1;
6670
6671 return true;
6672}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006673
6674static int intel_crtc_mode_set(struct drm_crtc *crtc,
6675 int x, int y,
6676 struct drm_framebuffer *fb)
6677{
Jesse Barnes79e53942008-11-07 14:24:08 -08006678 struct drm_device *dev = crtc->dev;
Chris Wilson05394f32010-11-08 19:18:58 +00006679 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006680 struct intel_encoder *encoder;
6681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07006682 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6683 int pipe = intel_crtc->pipe;
6684 int ret;
6685
Eric Anholt0b701d22011-03-30 13:01:03 -07006686 drm_vblank_pre_modeset(dev, pipe);
6687
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006688 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6689
Jesse Barnes79e53942008-11-07 14:24:08 -08006690 drm_vblank_post_modeset(dev, pipe);
6691
Daniel Vetter9256aa12012-10-31 19:26:13 +01006692 if (ret != 0)
6693 return ret;
6694
6695 for_each_encoder_on_crtc(dev, crtc, encoder) {
6696 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6697 encoder->base.base.id,
6698 drm_get_encoder_name(&encoder->base),
6699 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02006700 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01006701 }
6702
6703 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006704}
6705
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006706static bool intel_eld_uptodate(struct drm_connector *connector,
6707 int reg_eldv, uint32_t bits_eldv,
6708 int reg_elda, uint32_t bits_elda,
6709 int reg_edid)
6710{
6711 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6712 uint8_t *eld = connector->eld;
6713 uint32_t i;
6714
6715 i = I915_READ(reg_eldv);
6716 i &= bits_eldv;
6717
6718 if (!eld[0])
6719 return !i;
6720
6721 if (!i)
6722 return false;
6723
6724 i = I915_READ(reg_elda);
6725 i &= ~bits_elda;
6726 I915_WRITE(reg_elda, i);
6727
6728 for (i = 0; i < eld[2]; i++)
6729 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6730 return false;
6731
6732 return true;
6733}
6734
Wu Fengguange0dac652011-09-05 14:25:34 +08006735static void g4x_write_eld(struct drm_connector *connector,
6736 struct drm_crtc *crtc)
6737{
6738 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6739 uint8_t *eld = connector->eld;
6740 uint32_t eldv;
6741 uint32_t len;
6742 uint32_t i;
6743
6744 i = I915_READ(G4X_AUD_VID_DID);
6745
6746 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6747 eldv = G4X_ELDV_DEVCL_DEVBLC;
6748 else
6749 eldv = G4X_ELDV_DEVCTG;
6750
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006751 if (intel_eld_uptodate(connector,
6752 G4X_AUD_CNTL_ST, eldv,
6753 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6754 G4X_HDMIW_HDMIEDID))
6755 return;
6756
Wu Fengguange0dac652011-09-05 14:25:34 +08006757 i = I915_READ(G4X_AUD_CNTL_ST);
6758 i &= ~(eldv | G4X_ELD_ADDR);
6759 len = (i >> 9) & 0x1f; /* ELD buffer size */
6760 I915_WRITE(G4X_AUD_CNTL_ST, i);
6761
6762 if (!eld[0])
6763 return;
6764
6765 len = min_t(uint8_t, eld[2], len);
6766 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6767 for (i = 0; i < len; i++)
6768 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6769
6770 i = I915_READ(G4X_AUD_CNTL_ST);
6771 i |= eldv;
6772 I915_WRITE(G4X_AUD_CNTL_ST, i);
6773}
6774
Wang Xingchao83358c852012-08-16 22:43:37 +08006775static void haswell_write_eld(struct drm_connector *connector,
6776 struct drm_crtc *crtc)
6777{
6778 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6779 uint8_t *eld = connector->eld;
6780 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006782 uint32_t eldv;
6783 uint32_t i;
6784 int len;
6785 int pipe = to_intel_crtc(crtc)->pipe;
6786 int tmp;
6787
6788 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6789 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6790 int aud_config = HSW_AUD_CFG(pipe);
6791 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6792
6793
6794 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6795
6796 /* Audio output enable */
6797 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6798 tmp = I915_READ(aud_cntrl_st2);
6799 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6800 I915_WRITE(aud_cntrl_st2, tmp);
6801
6802 /* Wait for 1 vertical blank */
6803 intel_wait_for_vblank(dev, pipe);
6804
6805 /* Set ELD valid state */
6806 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006807 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006808 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6809 I915_WRITE(aud_cntrl_st2, tmp);
6810 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006811 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006812
6813 /* Enable HDMI mode */
6814 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006815 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006816 /* clear N_programing_enable and N_value_index */
6817 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6818 I915_WRITE(aud_config, tmp);
6819
6820 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6821
6822 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006823 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006824
6825 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6826 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6827 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6828 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6829 } else
6830 I915_WRITE(aud_config, 0);
6831
6832 if (intel_eld_uptodate(connector,
6833 aud_cntrl_st2, eldv,
6834 aud_cntl_st, IBX_ELD_ADDRESS,
6835 hdmiw_hdmiedid))
6836 return;
6837
6838 i = I915_READ(aud_cntrl_st2);
6839 i &= ~eldv;
6840 I915_WRITE(aud_cntrl_st2, i);
6841
6842 if (!eld[0])
6843 return;
6844
6845 i = I915_READ(aud_cntl_st);
6846 i &= ~IBX_ELD_ADDRESS;
6847 I915_WRITE(aud_cntl_st, i);
6848 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6849 DRM_DEBUG_DRIVER("port num:%d\n", i);
6850
6851 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6852 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6853 for (i = 0; i < len; i++)
6854 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6855
6856 i = I915_READ(aud_cntrl_st2);
6857 i |= eldv;
6858 I915_WRITE(aud_cntrl_st2, i);
6859
6860}
6861
Wu Fengguange0dac652011-09-05 14:25:34 +08006862static void ironlake_write_eld(struct drm_connector *connector,
6863 struct drm_crtc *crtc)
6864{
6865 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6866 uint8_t *eld = connector->eld;
6867 uint32_t eldv;
6868 uint32_t i;
6869 int len;
6870 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006871 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006872 int aud_cntl_st;
6873 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006874 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006875
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006876 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006877 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6878 aud_config = IBX_AUD_CFG(pipe);
6879 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006880 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006881 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006882 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6883 aud_config = CPT_AUD_CFG(pipe);
6884 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006885 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006886 }
6887
Wang Xingchao9b138a82012-08-09 16:52:18 +08006888 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006889
6890 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006891 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006892 if (!i) {
6893 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6894 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006895 eldv = IBX_ELD_VALIDB;
6896 eldv |= IBX_ELD_VALIDB << 4;
6897 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006898 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006899 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006900 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006901 }
6902
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006903 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6904 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6905 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006906 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6907 } else
6908 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006909
6910 if (intel_eld_uptodate(connector,
6911 aud_cntrl_st2, eldv,
6912 aud_cntl_st, IBX_ELD_ADDRESS,
6913 hdmiw_hdmiedid))
6914 return;
6915
Wu Fengguange0dac652011-09-05 14:25:34 +08006916 i = I915_READ(aud_cntrl_st2);
6917 i &= ~eldv;
6918 I915_WRITE(aud_cntrl_st2, i);
6919
6920 if (!eld[0])
6921 return;
6922
Wu Fengguange0dac652011-09-05 14:25:34 +08006923 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006924 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006925 I915_WRITE(aud_cntl_st, i);
6926
6927 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6928 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6929 for (i = 0; i < len; i++)
6930 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6931
6932 i = I915_READ(aud_cntrl_st2);
6933 i |= eldv;
6934 I915_WRITE(aud_cntrl_st2, i);
6935}
6936
6937void intel_write_eld(struct drm_encoder *encoder,
6938 struct drm_display_mode *mode)
6939{
6940 struct drm_crtc *crtc = encoder->crtc;
6941 struct drm_connector *connector;
6942 struct drm_device *dev = encoder->dev;
6943 struct drm_i915_private *dev_priv = dev->dev_private;
6944
6945 connector = drm_select_eld(encoder, mode);
6946 if (!connector)
6947 return;
6948
6949 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6950 connector->base.id,
6951 drm_get_connector_name(connector),
6952 connector->encoder->base.id,
6953 drm_get_encoder_name(connector->encoder));
6954
6955 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6956
6957 if (dev_priv->display.write_eld)
6958 dev_priv->display.write_eld(connector, crtc);
6959}
6960
Jesse Barnes79e53942008-11-07 14:24:08 -08006961static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6962{
6963 struct drm_device *dev = crtc->dev;
6964 struct drm_i915_private *dev_priv = dev->dev_private;
6965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6966 bool visible = base != 0;
6967 u32 cntl;
6968
6969 if (intel_crtc->cursor_visible == visible)
6970 return;
6971
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006972 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08006973 if (visible) {
6974 /* On these chipsets we can only modify the base whilst
6975 * the cursor is disabled.
6976 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006977 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006978
6979 cntl &= ~(CURSOR_FORMAT_MASK);
6980 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6981 cntl |= CURSOR_ENABLE |
6982 CURSOR_GAMMA_ENABLE |
6983 CURSOR_FORMAT_ARGB;
6984 } else
6985 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006986 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08006987
6988 intel_crtc->cursor_visible = visible;
6989}
6990
6991static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6992{
6993 struct drm_device *dev = crtc->dev;
6994 struct drm_i915_private *dev_priv = dev->dev_private;
6995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6996 int pipe = intel_crtc->pipe;
6997 bool visible = base != 0;
6998
6999 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007000 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007001 if (base) {
7002 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7003 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7004 cntl |= pipe << 28; /* Connect to correct pipe */
7005 } else {
7006 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7007 cntl |= CURSOR_MODE_DISABLE;
7008 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007009 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007010
7011 intel_crtc->cursor_visible = visible;
7012 }
7013 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007014 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007015}
7016
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007017static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7018{
7019 struct drm_device *dev = crtc->dev;
7020 struct drm_i915_private *dev_priv = dev->dev_private;
7021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7022 int pipe = intel_crtc->pipe;
7023 bool visible = base != 0;
7024
7025 if (intel_crtc->cursor_visible != visible) {
7026 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7027 if (base) {
7028 cntl &= ~CURSOR_MODE;
7029 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7030 } else {
7031 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7032 cntl |= CURSOR_MODE_DISABLE;
7033 }
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007034 if (IS_HASWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007035 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007036 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7037 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007038 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7039
7040 intel_crtc->cursor_visible = visible;
7041 }
7042 /* and commit changes on next vblank */
7043 I915_WRITE(CURBASE_IVB(pipe), base);
7044}
7045
Jesse Barnes79e53942008-11-07 14:24:08 -08007046/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7047static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7048 bool on)
7049{
7050 struct drm_device *dev = crtc->dev;
7051 struct drm_i915_private *dev_priv = dev->dev_private;
7052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7053 int pipe = intel_crtc->pipe;
7054 int x = intel_crtc->cursor_x;
7055 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007056 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007057 bool visible;
7058
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007059 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007060 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007061
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007062 if (x >= intel_crtc->config.pipe_src_w)
7063 base = 0;
7064
7065 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007066 base = 0;
7067
7068 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007069 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007070 base = 0;
7071
7072 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7073 x = -x;
7074 }
7075 pos |= x << CURSOR_X_SHIFT;
7076
7077 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007078 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007079 base = 0;
7080
7081 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7082 y = -y;
7083 }
7084 pos |= y << CURSOR_Y_SHIFT;
7085
7086 visible = base != 0;
7087 if (!visible && !intel_crtc->cursor_visible)
7088 return;
7089
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03007090 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007091 I915_WRITE(CURPOS_IVB(pipe), pos);
7092 ivb_update_cursor(crtc, base);
7093 } else {
7094 I915_WRITE(CURPOS(pipe), pos);
7095 if (IS_845G(dev) || IS_I865G(dev))
7096 i845_update_cursor(crtc, base);
7097 else
7098 i9xx_update_cursor(crtc, base);
7099 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007100}
7101
7102static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7103 struct drm_file *file,
7104 uint32_t handle,
7105 uint32_t width, uint32_t height)
7106{
7107 struct drm_device *dev = crtc->dev;
7108 struct drm_i915_private *dev_priv = dev->dev_private;
7109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007110 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007111 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007112 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007113
Jesse Barnes79e53942008-11-07 14:24:08 -08007114 /* if we want to turn off the cursor ignore width and height */
7115 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007116 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007117 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007118 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007119 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007120 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007121 }
7122
7123 /* Currently we only support 64x64 cursors */
7124 if (width != 64 || height != 64) {
7125 DRM_ERROR("we currently only support 64x64 cursors\n");
7126 return -EINVAL;
7127 }
7128
Chris Wilson05394f32010-11-08 19:18:58 +00007129 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007130 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007131 return -ENOENT;
7132
Chris Wilson05394f32010-11-08 19:18:58 +00007133 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007134 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007135 ret = -ENOMEM;
7136 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007137 }
7138
Dave Airlie71acb5e2008-12-30 20:31:46 +10007139 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007140 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007141 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007142 unsigned alignment;
7143
Chris Wilsond9e86c02010-11-10 16:40:20 +00007144 if (obj->tiling_mode) {
7145 DRM_ERROR("cursor cannot be tiled\n");
7146 ret = -EINVAL;
7147 goto fail_locked;
7148 }
7149
Chris Wilson693db182013-03-05 14:52:39 +00007150 /* Note that the w/a also requires 2 PTE of padding following
7151 * the bo. We currently fill all unused PTE with the shadow
7152 * page and so we should always have valid PTE following the
7153 * cursor preventing the VT-d warning.
7154 */
7155 alignment = 0;
7156 if (need_vtd_wa(dev))
7157 alignment = 64*1024;
7158
7159 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007160 if (ret) {
7161 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007162 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007163 }
7164
Chris Wilsond9e86c02010-11-10 16:40:20 +00007165 ret = i915_gem_object_put_fence(obj);
7166 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007167 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007168 goto fail_unpin;
7169 }
7170
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007171 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007172 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007173 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007174 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007175 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7176 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007177 if (ret) {
7178 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007179 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007180 }
Chris Wilson05394f32010-11-08 19:18:58 +00007181 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007182 }
7183
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007184 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04007185 I915_WRITE(CURSIZE, (height << 12) | width);
7186
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007187 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007188 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007189 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007190 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007191 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7192 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007193 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007194 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007195 }
Jesse Barnes80824002009-09-10 15:28:06 -07007196
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007197 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007198
7199 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007200 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007201 intel_crtc->cursor_width = width;
7202 intel_crtc->cursor_height = height;
7203
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03007204 if (intel_crtc->active)
7205 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007206
Jesse Barnes79e53942008-11-07 14:24:08 -08007207 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007208fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007209 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007210fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007211 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007212fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007213 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007214 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007215}
7216
7217static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7218{
Jesse Barnes79e53942008-11-07 14:24:08 -08007219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007220
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007221 intel_crtc->cursor_x = x;
7222 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07007223
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03007224 if (intel_crtc->active)
7225 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007226
7227 return 0;
7228}
7229
Jesse Barnes79e53942008-11-07 14:24:08 -08007230static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007231 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007232{
James Simmons72034252010-08-03 01:33:19 +01007233 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007235
James Simmons72034252010-08-03 01:33:19 +01007236 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007237 intel_crtc->lut_r[i] = red[i] >> 8;
7238 intel_crtc->lut_g[i] = green[i] >> 8;
7239 intel_crtc->lut_b[i] = blue[i] >> 8;
7240 }
7241
7242 intel_crtc_load_lut(crtc);
7243}
7244
Jesse Barnes79e53942008-11-07 14:24:08 -08007245/* VESA 640x480x72Hz mode to set on the pipe */
7246static struct drm_display_mode load_detect_mode = {
7247 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7248 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7249};
7250
Chris Wilsond2dff872011-04-19 08:36:26 +01007251static struct drm_framebuffer *
7252intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007253 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007254 struct drm_i915_gem_object *obj)
7255{
7256 struct intel_framebuffer *intel_fb;
7257 int ret;
7258
7259 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7260 if (!intel_fb) {
7261 drm_gem_object_unreference_unlocked(&obj->base);
7262 return ERR_PTR(-ENOMEM);
7263 }
7264
7265 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
7266 if (ret) {
7267 drm_gem_object_unreference_unlocked(&obj->base);
7268 kfree(intel_fb);
7269 return ERR_PTR(ret);
7270 }
7271
7272 return &intel_fb->base;
7273}
7274
7275static u32
7276intel_framebuffer_pitch_for_width(int width, int bpp)
7277{
7278 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7279 return ALIGN(pitch, 64);
7280}
7281
7282static u32
7283intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7284{
7285 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7286 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7287}
7288
7289static struct drm_framebuffer *
7290intel_framebuffer_create_for_mode(struct drm_device *dev,
7291 struct drm_display_mode *mode,
7292 int depth, int bpp)
7293{
7294 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007295 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007296
7297 obj = i915_gem_alloc_object(dev,
7298 intel_framebuffer_size_for_mode(mode, bpp));
7299 if (obj == NULL)
7300 return ERR_PTR(-ENOMEM);
7301
7302 mode_cmd.width = mode->hdisplay;
7303 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007304 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7305 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007306 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007307
7308 return intel_framebuffer_create(dev, &mode_cmd, obj);
7309}
7310
7311static struct drm_framebuffer *
7312mode_fits_in_fbdev(struct drm_device *dev,
7313 struct drm_display_mode *mode)
7314{
7315 struct drm_i915_private *dev_priv = dev->dev_private;
7316 struct drm_i915_gem_object *obj;
7317 struct drm_framebuffer *fb;
7318
7319 if (dev_priv->fbdev == NULL)
7320 return NULL;
7321
7322 obj = dev_priv->fbdev->ifb.obj;
7323 if (obj == NULL)
7324 return NULL;
7325
7326 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007327 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7328 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007329 return NULL;
7330
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007331 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007332 return NULL;
7333
7334 return fb;
7335}
7336
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007337bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007338 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007339 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007340{
7341 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007342 struct intel_encoder *intel_encoder =
7343 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007344 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007345 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007346 struct drm_crtc *crtc = NULL;
7347 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007348 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007349 int i = -1;
7350
Chris Wilsond2dff872011-04-19 08:36:26 +01007351 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7352 connector->base.id, drm_get_connector_name(connector),
7353 encoder->base.id, drm_get_encoder_name(encoder));
7354
Jesse Barnes79e53942008-11-07 14:24:08 -08007355 /*
7356 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007357 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007358 * - if the connector already has an assigned crtc, use it (but make
7359 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007360 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007361 * - try to find the first unused crtc that can drive this connector,
7362 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007363 */
7364
7365 /* See if we already have a CRTC for this connector */
7366 if (encoder->crtc) {
7367 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007368
Daniel Vetter7b240562012-12-12 00:35:33 +01007369 mutex_lock(&crtc->mutex);
7370
Daniel Vetter24218aa2012-08-12 19:27:11 +02007371 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007372 old->load_detect_temp = false;
7373
7374 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007375 if (connector->dpms != DRM_MODE_DPMS_ON)
7376 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007377
Chris Wilson71731882011-04-19 23:10:58 +01007378 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007379 }
7380
7381 /* Find an unused one (if possible) */
7382 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7383 i++;
7384 if (!(encoder->possible_crtcs & (1 << i)))
7385 continue;
7386 if (!possible_crtc->enabled) {
7387 crtc = possible_crtc;
7388 break;
7389 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007390 }
7391
7392 /*
7393 * If we didn't find an unused CRTC, don't use any.
7394 */
7395 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007396 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7397 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007398 }
7399
Daniel Vetter7b240562012-12-12 00:35:33 +01007400 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007401 intel_encoder->new_crtc = to_intel_crtc(crtc);
7402 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007403
7404 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007405 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007406 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007407 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007408
Chris Wilson64927112011-04-20 07:25:26 +01007409 if (!mode)
7410 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007411
Chris Wilsond2dff872011-04-19 08:36:26 +01007412 /* We need a framebuffer large enough to accommodate all accesses
7413 * that the plane may generate whilst we perform load detection.
7414 * We can not rely on the fbcon either being present (we get called
7415 * during its initialisation to detect all boot displays, or it may
7416 * not even exist) or that it is large enough to satisfy the
7417 * requested mode.
7418 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007419 fb = mode_fits_in_fbdev(dev, mode);
7420 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007421 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007422 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7423 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007424 } else
7425 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007426 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007427 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007428 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007429 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007430 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007431
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007432 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007433 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007434 if (old->release_fb)
7435 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007436 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007437 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007438 }
Chris Wilson71731882011-04-19 23:10:58 +01007439
Jesse Barnes79e53942008-11-07 14:24:08 -08007440 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007441 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007442 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007443}
7444
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007445void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007446 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007447{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007448 struct intel_encoder *intel_encoder =
7449 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007450 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007451 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007452
Chris Wilsond2dff872011-04-19 08:36:26 +01007453 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7454 connector->base.id, drm_get_connector_name(connector),
7455 encoder->base.id, drm_get_encoder_name(encoder));
7456
Chris Wilson8261b192011-04-19 23:18:09 +01007457 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007458 to_intel_connector(connector)->new_encoder = NULL;
7459 intel_encoder->new_crtc = NULL;
7460 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007461
Daniel Vetter36206362012-12-10 20:42:17 +01007462 if (old->release_fb) {
7463 drm_framebuffer_unregister_private(old->release_fb);
7464 drm_framebuffer_unreference(old->release_fb);
7465 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007466
Daniel Vetter67c96402013-01-23 16:25:09 +00007467 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007468 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007469 }
7470
Eric Anholtc751ce42010-03-25 11:48:48 -07007471 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007472 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7473 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007474
7475 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007476}
7477
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007478static int i9xx_pll_refclk(struct drm_device *dev,
7479 const struct intel_crtc_config *pipe_config)
7480{
7481 struct drm_i915_private *dev_priv = dev->dev_private;
7482 u32 dpll = pipe_config->dpll_hw_state.dpll;
7483
7484 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7485 return dev_priv->vbt.lvds_ssc_freq * 1000;
7486 else if (HAS_PCH_SPLIT(dev))
7487 return 120000;
7488 else if (!IS_GEN2(dev))
7489 return 96000;
7490 else
7491 return 48000;
7492}
7493
Jesse Barnes79e53942008-11-07 14:24:08 -08007494/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007495static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7496 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007497{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007498 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007499 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007500 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007501 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007502 u32 fp;
7503 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007504 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007505
7506 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007507 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007508 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007509 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007510
7511 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007512 if (IS_PINEVIEW(dev)) {
7513 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7514 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007515 } else {
7516 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7517 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7518 }
7519
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007520 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007521 if (IS_PINEVIEW(dev))
7522 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7523 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007524 else
7525 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007526 DPLL_FPA01_P1_POST_DIV_SHIFT);
7527
7528 switch (dpll & DPLL_MODE_MASK) {
7529 case DPLLB_MODE_DAC_SERIAL:
7530 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7531 5 : 10;
7532 break;
7533 case DPLLB_MODE_LVDS:
7534 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7535 7 : 14;
7536 break;
7537 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007538 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007539 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007540 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007541 }
7542
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007543 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007544 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007545 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007546 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007547 } else {
7548 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7549
7550 if (is_lvds) {
7551 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7552 DPLL_FPA01_P1_POST_DIV_SHIFT);
7553 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007554 } else {
7555 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7556 clock.p1 = 2;
7557 else {
7558 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7559 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7560 }
7561 if (dpll & PLL_P2_DIVIDE_BY_4)
7562 clock.p2 = 4;
7563 else
7564 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007565 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007566
7567 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007568 }
7569
Ville Syrjälä18442d02013-09-13 16:00:08 +03007570 /*
7571 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007572 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007573 * encoder's get_config() function.
7574 */
7575 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007576}
7577
Ville Syrjälä6878da02013-09-13 15:59:11 +03007578int intel_dotclock_calculate(int link_freq,
7579 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007580{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007581 /*
7582 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007583 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007584 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007585 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007586 *
7587 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007588 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007589 */
7590
Ville Syrjälä6878da02013-09-13 15:59:11 +03007591 if (!m_n->link_n)
7592 return 0;
7593
7594 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7595}
7596
Ville Syrjälä18442d02013-09-13 16:00:08 +03007597static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7598 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007599{
7600 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007601
7602 /* read out port_clock from the DPLL */
7603 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007604
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007605 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007606 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007607 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007608 * agree once we know their relationship in the encoder's
7609 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007610 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007611 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007612 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7613 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007614}
7615
7616/** Returns the currently programmed mode of the given pipe. */
7617struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7618 struct drm_crtc *crtc)
7619{
Jesse Barnes548f2452011-02-17 10:40:53 -08007620 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007622 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007623 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007624 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007625 int htot = I915_READ(HTOTAL(cpu_transcoder));
7626 int hsync = I915_READ(HSYNC(cpu_transcoder));
7627 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7628 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03007629 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08007630
7631 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7632 if (!mode)
7633 return NULL;
7634
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007635 /*
7636 * Construct a pipe_config sufficient for getting the clock info
7637 * back out of crtc_clock_get.
7638 *
7639 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7640 * to use a real value here instead.
7641 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03007642 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007643 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007644 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
7645 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
7646 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007647 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7648
Ville Syrjälä773ae032013-09-23 17:48:20 +03007649 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08007650 mode->hdisplay = (htot & 0xffff) + 1;
7651 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7652 mode->hsync_start = (hsync & 0xffff) + 1;
7653 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7654 mode->vdisplay = (vtot & 0xffff) + 1;
7655 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7656 mode->vsync_start = (vsync & 0xffff) + 1;
7657 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7658
7659 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007660
7661 return mode;
7662}
7663
Daniel Vetter3dec0092010-08-20 21:40:52 +02007664static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007665{
7666 struct drm_device *dev = crtc->dev;
7667 drm_i915_private_t *dev_priv = dev->dev_private;
7668 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7669 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007670 int dpll_reg = DPLL(pipe);
7671 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007672
Eric Anholtbad720f2009-10-22 16:11:14 -07007673 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007674 return;
7675
7676 if (!dev_priv->lvds_downclock_avail)
7677 return;
7678
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007679 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007680 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007681 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007682
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007683 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007684
7685 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7686 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007687 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007688
Jesse Barnes652c3932009-08-17 13:31:43 -07007689 dpll = I915_READ(dpll_reg);
7690 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007691 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007692 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007693}
7694
7695static void intel_decrease_pllclock(struct drm_crtc *crtc)
7696{
7697 struct drm_device *dev = crtc->dev;
7698 drm_i915_private_t *dev_priv = dev->dev_private;
7699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007700
Eric Anholtbad720f2009-10-22 16:11:14 -07007701 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007702 return;
7703
7704 if (!dev_priv->lvds_downclock_avail)
7705 return;
7706
7707 /*
7708 * Since this is called by a timer, we should never get here in
7709 * the manual case.
7710 */
7711 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007712 int pipe = intel_crtc->pipe;
7713 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007714 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007715
Zhao Yakui44d98a62009-10-09 11:39:40 +08007716 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007717
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007718 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007719
Chris Wilson074b5e12012-05-02 12:07:06 +01007720 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007721 dpll |= DISPLAY_RATE_SELECT_FPA1;
7722 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007723 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007724 dpll = I915_READ(dpll_reg);
7725 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007726 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007727 }
7728
7729}
7730
Chris Wilsonf047e392012-07-21 12:31:41 +01007731void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007732{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007733 struct drm_i915_private *dev_priv = dev->dev_private;
7734
7735 hsw_package_c8_gpu_busy(dev_priv);
7736 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01007737}
7738
7739void intel_mark_idle(struct drm_device *dev)
7740{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007741 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00007742 struct drm_crtc *crtc;
7743
Paulo Zanonic67a4702013-08-19 13:18:09 -03007744 hsw_package_c8_gpu_idle(dev_priv);
7745
Chris Wilson725a5b52013-01-08 11:02:57 +00007746 if (!i915_powersave)
7747 return;
7748
7749 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7750 if (!crtc->fb)
7751 continue;
7752
7753 intel_decrease_pllclock(crtc);
7754 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01007755
7756 if (dev_priv->info->gen >= 6)
7757 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01007758}
7759
Chris Wilsonc65355b2013-06-06 16:53:41 -03007760void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7761 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007762{
7763 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007764 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007765
7766 if (!i915_powersave)
7767 return;
7768
Jesse Barnes652c3932009-08-17 13:31:43 -07007769 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007770 if (!crtc->fb)
7771 continue;
7772
Chris Wilsonc65355b2013-06-06 16:53:41 -03007773 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7774 continue;
7775
7776 intel_increase_pllclock(crtc);
7777 if (ring && intel_fbc_enabled(dev))
7778 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007779 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007780}
7781
Jesse Barnes79e53942008-11-07 14:24:08 -08007782static void intel_crtc_destroy(struct drm_crtc *crtc)
7783{
7784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007785 struct drm_device *dev = crtc->dev;
7786 struct intel_unpin_work *work;
7787 unsigned long flags;
7788
7789 spin_lock_irqsave(&dev->event_lock, flags);
7790 work = intel_crtc->unpin_work;
7791 intel_crtc->unpin_work = NULL;
7792 spin_unlock_irqrestore(&dev->event_lock, flags);
7793
7794 if (work) {
7795 cancel_work_sync(&work->work);
7796 kfree(work);
7797 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007798
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007799 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7800
Jesse Barnes79e53942008-11-07 14:24:08 -08007801 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007802
Jesse Barnes79e53942008-11-07 14:24:08 -08007803 kfree(intel_crtc);
7804}
7805
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007806static void intel_unpin_work_fn(struct work_struct *__work)
7807{
7808 struct intel_unpin_work *work =
7809 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007810 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007811
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007812 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007813 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007814 drm_gem_object_unreference(&work->pending_flip_obj->base);
7815 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007816
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007817 intel_update_fbc(dev);
7818 mutex_unlock(&dev->struct_mutex);
7819
7820 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7821 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7822
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007823 kfree(work);
7824}
7825
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007826static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007827 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007828{
7829 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7831 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007832 unsigned long flags;
7833
7834 /* Ignore early vblank irqs */
7835 if (intel_crtc == NULL)
7836 return;
7837
7838 spin_lock_irqsave(&dev->event_lock, flags);
7839 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007840
7841 /* Ensure we don't miss a work->pending update ... */
7842 smp_rmb();
7843
7844 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007845 spin_unlock_irqrestore(&dev->event_lock, flags);
7846 return;
7847 }
7848
Chris Wilsone7d841c2012-12-03 11:36:30 +00007849 /* and that the unpin work is consistent wrt ->pending. */
7850 smp_rmb();
7851
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007852 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007853
Rob Clark45a066e2012-10-08 14:50:40 -05007854 if (work->event)
7855 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007856
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007857 drm_vblank_put(dev, intel_crtc->pipe);
7858
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007859 spin_unlock_irqrestore(&dev->event_lock, flags);
7860
Daniel Vetter2c10d572012-12-20 21:24:07 +01007861 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007862
7863 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007864
7865 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007866}
7867
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007868void intel_finish_page_flip(struct drm_device *dev, int pipe)
7869{
7870 drm_i915_private_t *dev_priv = dev->dev_private;
7871 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7872
Mario Kleiner49b14a52010-12-09 07:00:07 +01007873 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007874}
7875
7876void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7877{
7878 drm_i915_private_t *dev_priv = dev->dev_private;
7879 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7880
Mario Kleiner49b14a52010-12-09 07:00:07 +01007881 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007882}
7883
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007884void intel_prepare_page_flip(struct drm_device *dev, int plane)
7885{
7886 drm_i915_private_t *dev_priv = dev->dev_private;
7887 struct intel_crtc *intel_crtc =
7888 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7889 unsigned long flags;
7890
Chris Wilsone7d841c2012-12-03 11:36:30 +00007891 /* NB: An MMIO update of the plane base pointer will also
7892 * generate a page-flip completion irq, i.e. every modeset
7893 * is also accompanied by a spurious intel_prepare_page_flip().
7894 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007895 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007896 if (intel_crtc->unpin_work)
7897 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007898 spin_unlock_irqrestore(&dev->event_lock, flags);
7899}
7900
Chris Wilsone7d841c2012-12-03 11:36:30 +00007901inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7902{
7903 /* Ensure that the work item is consistent when activating it ... */
7904 smp_wmb();
7905 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7906 /* and that it is marked active as soon as the irq could fire. */
7907 smp_wmb();
7908}
7909
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007910static int intel_gen2_queue_flip(struct drm_device *dev,
7911 struct drm_crtc *crtc,
7912 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007913 struct drm_i915_gem_object *obj,
7914 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007915{
7916 struct drm_i915_private *dev_priv = dev->dev_private;
7917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007918 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007919 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007920 int ret;
7921
Daniel Vetter6d90c952012-04-26 23:28:05 +02007922 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007923 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007924 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007925
Daniel Vetter6d90c952012-04-26 23:28:05 +02007926 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007927 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007928 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007929
7930 /* Can't queue multiple flips, so wait for the previous
7931 * one to finish before executing the next.
7932 */
7933 if (intel_crtc->plane)
7934 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7935 else
7936 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007937 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7938 intel_ring_emit(ring, MI_NOOP);
7939 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7940 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7941 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007942 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007943 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007944
7945 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007946 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007947 return 0;
7948
7949err_unpin:
7950 intel_unpin_fb_obj(obj);
7951err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007952 return ret;
7953}
7954
7955static int intel_gen3_queue_flip(struct drm_device *dev,
7956 struct drm_crtc *crtc,
7957 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007958 struct drm_i915_gem_object *obj,
7959 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007960{
7961 struct drm_i915_private *dev_priv = dev->dev_private;
7962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007963 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007964 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007965 int ret;
7966
Daniel Vetter6d90c952012-04-26 23:28:05 +02007967 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007968 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007969 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007970
Daniel Vetter6d90c952012-04-26 23:28:05 +02007971 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007972 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007973 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007974
7975 if (intel_crtc->plane)
7976 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7977 else
7978 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007979 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7980 intel_ring_emit(ring, MI_NOOP);
7981 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7982 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7983 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007984 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007985 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007986
Chris Wilsone7d841c2012-12-03 11:36:30 +00007987 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007988 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007989 return 0;
7990
7991err_unpin:
7992 intel_unpin_fb_obj(obj);
7993err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007994 return ret;
7995}
7996
7997static int intel_gen4_queue_flip(struct drm_device *dev,
7998 struct drm_crtc *crtc,
7999 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008000 struct drm_i915_gem_object *obj,
8001 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008002{
8003 struct drm_i915_private *dev_priv = dev->dev_private;
8004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8005 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008006 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008007 int ret;
8008
Daniel Vetter6d90c952012-04-26 23:28:05 +02008009 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008010 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008011 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008012
Daniel Vetter6d90c952012-04-26 23:28:05 +02008013 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008014 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008015 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008016
8017 /* i965+ uses the linear or tiled offsets from the
8018 * Display Registers (which do not change across a page-flip)
8019 * so we need only reprogram the base address.
8020 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008021 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8022 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8023 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008024 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008025 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008026 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008027
8028 /* XXX Enabling the panel-fitter across page-flip is so far
8029 * untested on non-native modes, so ignore it for now.
8030 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8031 */
8032 pf = 0;
8033 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008034 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008035
8036 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008037 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008038 return 0;
8039
8040err_unpin:
8041 intel_unpin_fb_obj(obj);
8042err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008043 return ret;
8044}
8045
8046static int intel_gen6_queue_flip(struct drm_device *dev,
8047 struct drm_crtc *crtc,
8048 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008049 struct drm_i915_gem_object *obj,
8050 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008051{
8052 struct drm_i915_private *dev_priv = dev->dev_private;
8053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008054 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008055 uint32_t pf, pipesrc;
8056 int ret;
8057
Daniel Vetter6d90c952012-04-26 23:28:05 +02008058 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008059 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008060 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008061
Daniel Vetter6d90c952012-04-26 23:28:05 +02008062 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008063 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008064 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008065
Daniel Vetter6d90c952012-04-26 23:28:05 +02008066 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8067 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8068 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008069 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008070
Chris Wilson99d9acd2012-04-17 20:37:00 +01008071 /* Contrary to the suggestions in the documentation,
8072 * "Enable Panel Fitter" does not seem to be required when page
8073 * flipping with a non-native mode, and worse causes a normal
8074 * modeset to fail.
8075 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8076 */
8077 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008078 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008079 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008080
8081 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008082 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008083 return 0;
8084
8085err_unpin:
8086 intel_unpin_fb_obj(obj);
8087err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008088 return ret;
8089}
8090
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008091static int intel_gen7_queue_flip(struct drm_device *dev,
8092 struct drm_crtc *crtc,
8093 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008094 struct drm_i915_gem_object *obj,
8095 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008096{
8097 struct drm_i915_private *dev_priv = dev->dev_private;
8098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008099 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008100 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008101 int len, ret;
8102
8103 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008104 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008105 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008106
8107 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8108 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008109 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008110
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008111 switch(intel_crtc->plane) {
8112 case PLANE_A:
8113 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8114 break;
8115 case PLANE_B:
8116 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8117 break;
8118 case PLANE_C:
8119 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8120 break;
8121 default:
8122 WARN_ONCE(1, "unknown plane in flip command\n");
8123 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008124 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008125 }
8126
Chris Wilsonffe74d72013-08-26 20:58:12 +01008127 len = 4;
8128 if (ring->id == RCS)
8129 len += 6;
8130
8131 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008132 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008133 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008134
Chris Wilsonffe74d72013-08-26 20:58:12 +01008135 /* Unmask the flip-done completion message. Note that the bspec says that
8136 * we should do this for both the BCS and RCS, and that we must not unmask
8137 * more than one flip event at any time (or ensure that one flip message
8138 * can be sent by waiting for flip-done prior to queueing new flips).
8139 * Experimentation says that BCS works despite DERRMR masking all
8140 * flip-done completion events and that unmasking all planes at once
8141 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8142 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8143 */
8144 if (ring->id == RCS) {
8145 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8146 intel_ring_emit(ring, DERRMR);
8147 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8148 DERRMR_PIPEB_PRI_FLIP_DONE |
8149 DERRMR_PIPEC_PRI_FLIP_DONE));
8150 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8151 intel_ring_emit(ring, DERRMR);
8152 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8153 }
8154
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008155 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008156 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008157 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008158 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008159
8160 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008161 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008162 return 0;
8163
8164err_unpin:
8165 intel_unpin_fb_obj(obj);
8166err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008167 return ret;
8168}
8169
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008170static int intel_default_queue_flip(struct drm_device *dev,
8171 struct drm_crtc *crtc,
8172 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008173 struct drm_i915_gem_object *obj,
8174 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008175{
8176 return -ENODEV;
8177}
8178
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008179static int intel_crtc_page_flip(struct drm_crtc *crtc,
8180 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008181 struct drm_pending_vblank_event *event,
8182 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008183{
8184 struct drm_device *dev = crtc->dev;
8185 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008186 struct drm_framebuffer *old_fb = crtc->fb;
8187 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8189 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008190 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008191 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008192
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008193 /* Can't change pixel format via MI display flips. */
8194 if (fb->pixel_format != crtc->fb->pixel_format)
8195 return -EINVAL;
8196
8197 /*
8198 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8199 * Note that pitch changes could also affect these register.
8200 */
8201 if (INTEL_INFO(dev)->gen > 3 &&
8202 (fb->offsets[0] != crtc->fb->offsets[0] ||
8203 fb->pitches[0] != crtc->fb->pitches[0]))
8204 return -EINVAL;
8205
Daniel Vetterb14c5672013-09-19 12:18:32 +02008206 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008207 if (work == NULL)
8208 return -ENOMEM;
8209
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008210 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008211 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008212 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008213 INIT_WORK(&work->work, intel_unpin_work_fn);
8214
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008215 ret = drm_vblank_get(dev, intel_crtc->pipe);
8216 if (ret)
8217 goto free_work;
8218
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008219 /* We borrow the event spin lock for protecting unpin_work */
8220 spin_lock_irqsave(&dev->event_lock, flags);
8221 if (intel_crtc->unpin_work) {
8222 spin_unlock_irqrestore(&dev->event_lock, flags);
8223 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008224 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008225
8226 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008227 return -EBUSY;
8228 }
8229 intel_crtc->unpin_work = work;
8230 spin_unlock_irqrestore(&dev->event_lock, flags);
8231
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008232 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8233 flush_workqueue(dev_priv->wq);
8234
Chris Wilson79158102012-05-23 11:13:58 +01008235 ret = i915_mutex_lock_interruptible(dev);
8236 if (ret)
8237 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008238
Jesse Barnes75dfca82010-02-10 15:09:44 -08008239 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008240 drm_gem_object_reference(&work->old_fb_obj->base);
8241 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008242
8243 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008244
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008245 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008246
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008247 work->enable_stall_check = true;
8248
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008249 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008250 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008251
Keith Packarded8d1972013-07-22 18:49:58 -07008252 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008253 if (ret)
8254 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008255
Chris Wilson7782de32011-07-08 12:22:41 +01008256 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008257 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008258 mutex_unlock(&dev->struct_mutex);
8259
Jesse Barnese5510fa2010-07-01 16:48:37 -07008260 trace_i915_flip_request(intel_crtc->plane, obj);
8261
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008262 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008263
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008264cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008265 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008266 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008267 drm_gem_object_unreference(&work->old_fb_obj->base);
8268 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008269 mutex_unlock(&dev->struct_mutex);
8270
Chris Wilson79158102012-05-23 11:13:58 +01008271cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008272 spin_lock_irqsave(&dev->event_lock, flags);
8273 intel_crtc->unpin_work = NULL;
8274 spin_unlock_irqrestore(&dev->event_lock, flags);
8275
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008276 drm_vblank_put(dev, intel_crtc->pipe);
8277free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008278 kfree(work);
8279
8280 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008281}
8282
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008283static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008284 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8285 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008286};
8287
Daniel Vetter50f56112012-07-02 09:35:43 +02008288static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8289 struct drm_crtc *crtc)
8290{
8291 struct drm_device *dev;
8292 struct drm_crtc *tmp;
8293 int crtc_mask = 1;
8294
8295 WARN(!crtc, "checking null crtc?\n");
8296
8297 dev = crtc->dev;
8298
8299 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8300 if (tmp == crtc)
8301 break;
8302 crtc_mask <<= 1;
8303 }
8304
8305 if (encoder->possible_crtcs & crtc_mask)
8306 return true;
8307 return false;
8308}
8309
Daniel Vetter9a935852012-07-05 22:34:27 +02008310/**
8311 * intel_modeset_update_staged_output_state
8312 *
8313 * Updates the staged output configuration state, e.g. after we've read out the
8314 * current hw state.
8315 */
8316static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8317{
8318 struct intel_encoder *encoder;
8319 struct intel_connector *connector;
8320
8321 list_for_each_entry(connector, &dev->mode_config.connector_list,
8322 base.head) {
8323 connector->new_encoder =
8324 to_intel_encoder(connector->base.encoder);
8325 }
8326
8327 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8328 base.head) {
8329 encoder->new_crtc =
8330 to_intel_crtc(encoder->base.crtc);
8331 }
8332}
8333
8334/**
8335 * intel_modeset_commit_output_state
8336 *
8337 * This function copies the stage display pipe configuration to the real one.
8338 */
8339static void intel_modeset_commit_output_state(struct drm_device *dev)
8340{
8341 struct intel_encoder *encoder;
8342 struct intel_connector *connector;
8343
8344 list_for_each_entry(connector, &dev->mode_config.connector_list,
8345 base.head) {
8346 connector->base.encoder = &connector->new_encoder->base;
8347 }
8348
8349 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8350 base.head) {
8351 encoder->base.crtc = &encoder->new_crtc->base;
8352 }
8353}
8354
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008355static void
8356connected_sink_compute_bpp(struct intel_connector * connector,
8357 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008358{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008359 int bpp = pipe_config->pipe_bpp;
8360
8361 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8362 connector->base.base.id,
8363 drm_get_connector_name(&connector->base));
8364
8365 /* Don't use an invalid EDID bpc value */
8366 if (connector->base.display_info.bpc &&
8367 connector->base.display_info.bpc * 3 < bpp) {
8368 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8369 bpp, connector->base.display_info.bpc*3);
8370 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8371 }
8372
8373 /* Clamp bpp to 8 on screens without EDID 1.4 */
8374 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8375 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8376 bpp);
8377 pipe_config->pipe_bpp = 24;
8378 }
8379}
8380
8381static int
8382compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8383 struct drm_framebuffer *fb,
8384 struct intel_crtc_config *pipe_config)
8385{
8386 struct drm_device *dev = crtc->base.dev;
8387 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008388 int bpp;
8389
Daniel Vetterd42264b2013-03-28 16:38:08 +01008390 switch (fb->pixel_format) {
8391 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008392 bpp = 8*3; /* since we go through a colormap */
8393 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008394 case DRM_FORMAT_XRGB1555:
8395 case DRM_FORMAT_ARGB1555:
8396 /* checked in intel_framebuffer_init already */
8397 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8398 return -EINVAL;
8399 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008400 bpp = 6*3; /* min is 18bpp */
8401 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008402 case DRM_FORMAT_XBGR8888:
8403 case DRM_FORMAT_ABGR8888:
8404 /* checked in intel_framebuffer_init already */
8405 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8406 return -EINVAL;
8407 case DRM_FORMAT_XRGB8888:
8408 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008409 bpp = 8*3;
8410 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008411 case DRM_FORMAT_XRGB2101010:
8412 case DRM_FORMAT_ARGB2101010:
8413 case DRM_FORMAT_XBGR2101010:
8414 case DRM_FORMAT_ABGR2101010:
8415 /* checked in intel_framebuffer_init already */
8416 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008417 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008418 bpp = 10*3;
8419 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008420 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008421 default:
8422 DRM_DEBUG_KMS("unsupported depth\n");
8423 return -EINVAL;
8424 }
8425
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008426 pipe_config->pipe_bpp = bpp;
8427
8428 /* Clamp display bpp to EDID value */
8429 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008430 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008431 if (!connector->new_encoder ||
8432 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008433 continue;
8434
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008435 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008436 }
8437
8438 return bpp;
8439}
8440
Daniel Vetter644db712013-09-19 14:53:58 +02008441static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8442{
8443 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8444 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008445 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008446 mode->crtc_hdisplay, mode->crtc_hsync_start,
8447 mode->crtc_hsync_end, mode->crtc_htotal,
8448 mode->crtc_vdisplay, mode->crtc_vsync_start,
8449 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8450}
8451
Daniel Vetterc0b03412013-05-28 12:05:54 +02008452static void intel_dump_pipe_config(struct intel_crtc *crtc,
8453 struct intel_crtc_config *pipe_config,
8454 const char *context)
8455{
8456 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8457 context, pipe_name(crtc->pipe));
8458
8459 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8460 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8461 pipe_config->pipe_bpp, pipe_config->dither);
8462 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8463 pipe_config->has_pch_encoder,
8464 pipe_config->fdi_lanes,
8465 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8466 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8467 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008468 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8469 pipe_config->has_dp_encoder,
8470 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8471 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8472 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008473 DRM_DEBUG_KMS("requested mode:\n");
8474 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8475 DRM_DEBUG_KMS("adjusted mode:\n");
8476 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008477 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008478 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008479 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8480 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008481 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8482 pipe_config->gmch_pfit.control,
8483 pipe_config->gmch_pfit.pgm_ratios,
8484 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008485 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008486 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008487 pipe_config->pch_pfit.size,
8488 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008489 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008490 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008491}
8492
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008493static bool check_encoder_cloning(struct drm_crtc *crtc)
8494{
8495 int num_encoders = 0;
8496 bool uncloneable_encoders = false;
8497 struct intel_encoder *encoder;
8498
8499 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8500 base.head) {
8501 if (&encoder->new_crtc->base != crtc)
8502 continue;
8503
8504 num_encoders++;
8505 if (!encoder->cloneable)
8506 uncloneable_encoders = true;
8507 }
8508
8509 return !(num_encoders > 1 && uncloneable_encoders);
8510}
8511
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008512static struct intel_crtc_config *
8513intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008514 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008515 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008516{
8517 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008518 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008519 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008520 int plane_bpp, ret = -EINVAL;
8521 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008522
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008523 if (!check_encoder_cloning(crtc)) {
8524 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8525 return ERR_PTR(-EINVAL);
8526 }
8527
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008528 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8529 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008530 return ERR_PTR(-ENOMEM);
8531
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008532 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8533 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008534
Daniel Vettere143a212013-07-04 12:01:15 +02008535 pipe_config->cpu_transcoder =
8536 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008537 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008538
Imre Deak2960bc92013-07-30 13:36:32 +03008539 /*
8540 * Sanitize sync polarity flags based on requested ones. If neither
8541 * positive or negative polarity is requested, treat this as meaning
8542 * negative polarity.
8543 */
8544 if (!(pipe_config->adjusted_mode.flags &
8545 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8546 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8547
8548 if (!(pipe_config->adjusted_mode.flags &
8549 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8550 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8551
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008552 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8553 * plane pixel format and any sink constraints into account. Returns the
8554 * source plane bpp so that dithering can be selected on mismatches
8555 * after encoders and crtc also have had their say. */
8556 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8557 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008558 if (plane_bpp < 0)
8559 goto fail;
8560
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008561 /*
8562 * Determine the real pipe dimensions. Note that stereo modes can
8563 * increase the actual pipe size due to the frame doubling and
8564 * insertion of additional space for blanks between the frame. This
8565 * is stored in the crtc timings. We use the requested mode to do this
8566 * computation to clearly distinguish it from the adjusted mode, which
8567 * can be changed by the connectors in the below retry loop.
8568 */
8569 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8570 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8571 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8572
Daniel Vettere29c22c2013-02-21 00:00:16 +01008573encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008574 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008575 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008576 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008577
Daniel Vetter135c81b2013-07-21 21:37:09 +02008578 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008579 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008580
Daniel Vetter7758a112012-07-08 19:40:39 +02008581 /* Pass our mode to the connectors and the CRTC to give them a chance to
8582 * adjust it according to limitations or connector properties, and also
8583 * a chance to reject the mode entirely.
8584 */
8585 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8586 base.head) {
8587
8588 if (&encoder->new_crtc->base != crtc)
8589 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008590
Daniel Vetterefea6e82013-07-21 21:36:59 +02008591 if (!(encoder->compute_config(encoder, pipe_config))) {
8592 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008593 goto fail;
8594 }
8595 }
8596
Daniel Vetterff9a6752013-06-01 17:16:21 +02008597 /* Set default port clock if not overwritten by the encoder. Needs to be
8598 * done afterwards in case the encoder adjusts the mode. */
8599 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008600 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8601 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008602
Daniel Vettera43f6e02013-06-07 23:10:32 +02008603 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008604 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008605 DRM_DEBUG_KMS("CRTC fixup failed\n");
8606 goto fail;
8607 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008608
8609 if (ret == RETRY) {
8610 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8611 ret = -EINVAL;
8612 goto fail;
8613 }
8614
8615 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8616 retry = false;
8617 goto encoder_retry;
8618 }
8619
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008620 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8621 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8622 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8623
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008624 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008625fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008626 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008627 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008628}
8629
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008630/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8631 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8632static void
8633intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8634 unsigned *prepare_pipes, unsigned *disable_pipes)
8635{
8636 struct intel_crtc *intel_crtc;
8637 struct drm_device *dev = crtc->dev;
8638 struct intel_encoder *encoder;
8639 struct intel_connector *connector;
8640 struct drm_crtc *tmp_crtc;
8641
8642 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8643
8644 /* Check which crtcs have changed outputs connected to them, these need
8645 * to be part of the prepare_pipes mask. We don't (yet) support global
8646 * modeset across multiple crtcs, so modeset_pipes will only have one
8647 * bit set at most. */
8648 list_for_each_entry(connector, &dev->mode_config.connector_list,
8649 base.head) {
8650 if (connector->base.encoder == &connector->new_encoder->base)
8651 continue;
8652
8653 if (connector->base.encoder) {
8654 tmp_crtc = connector->base.encoder->crtc;
8655
8656 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8657 }
8658
8659 if (connector->new_encoder)
8660 *prepare_pipes |=
8661 1 << connector->new_encoder->new_crtc->pipe;
8662 }
8663
8664 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8665 base.head) {
8666 if (encoder->base.crtc == &encoder->new_crtc->base)
8667 continue;
8668
8669 if (encoder->base.crtc) {
8670 tmp_crtc = encoder->base.crtc;
8671
8672 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8673 }
8674
8675 if (encoder->new_crtc)
8676 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8677 }
8678
8679 /* Check for any pipes that will be fully disabled ... */
8680 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8681 base.head) {
8682 bool used = false;
8683
8684 /* Don't try to disable disabled crtcs. */
8685 if (!intel_crtc->base.enabled)
8686 continue;
8687
8688 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8689 base.head) {
8690 if (encoder->new_crtc == intel_crtc)
8691 used = true;
8692 }
8693
8694 if (!used)
8695 *disable_pipes |= 1 << intel_crtc->pipe;
8696 }
8697
8698
8699 /* set_mode is also used to update properties on life display pipes. */
8700 intel_crtc = to_intel_crtc(crtc);
8701 if (crtc->enabled)
8702 *prepare_pipes |= 1 << intel_crtc->pipe;
8703
Daniel Vetterb6c51642013-04-12 18:48:43 +02008704 /*
8705 * For simplicity do a full modeset on any pipe where the output routing
8706 * changed. We could be more clever, but that would require us to be
8707 * more careful with calling the relevant encoder->mode_set functions.
8708 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008709 if (*prepare_pipes)
8710 *modeset_pipes = *prepare_pipes;
8711
8712 /* ... and mask these out. */
8713 *modeset_pipes &= ~(*disable_pipes);
8714 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008715
8716 /*
8717 * HACK: We don't (yet) fully support global modesets. intel_set_config
8718 * obies this rule, but the modeset restore mode of
8719 * intel_modeset_setup_hw_state does not.
8720 */
8721 *modeset_pipes &= 1 << intel_crtc->pipe;
8722 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008723
8724 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8725 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008726}
8727
Daniel Vetterea9d7582012-07-10 10:42:52 +02008728static bool intel_crtc_in_use(struct drm_crtc *crtc)
8729{
8730 struct drm_encoder *encoder;
8731 struct drm_device *dev = crtc->dev;
8732
8733 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8734 if (encoder->crtc == crtc)
8735 return true;
8736
8737 return false;
8738}
8739
8740static void
8741intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8742{
8743 struct intel_encoder *intel_encoder;
8744 struct intel_crtc *intel_crtc;
8745 struct drm_connector *connector;
8746
8747 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8748 base.head) {
8749 if (!intel_encoder->base.crtc)
8750 continue;
8751
8752 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8753
8754 if (prepare_pipes & (1 << intel_crtc->pipe))
8755 intel_encoder->connectors_active = false;
8756 }
8757
8758 intel_modeset_commit_output_state(dev);
8759
8760 /* Update computed state. */
8761 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8762 base.head) {
8763 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8764 }
8765
8766 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8767 if (!connector->encoder || !connector->encoder->crtc)
8768 continue;
8769
8770 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8771
8772 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008773 struct drm_property *dpms_property =
8774 dev->mode_config.dpms_property;
8775
Daniel Vetterea9d7582012-07-10 10:42:52 +02008776 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008777 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008778 dpms_property,
8779 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008780
8781 intel_encoder = to_intel_encoder(connector->encoder);
8782 intel_encoder->connectors_active = true;
8783 }
8784 }
8785
8786}
8787
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008788static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008789{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008790 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008791
8792 if (clock1 == clock2)
8793 return true;
8794
8795 if (!clock1 || !clock2)
8796 return false;
8797
8798 diff = abs(clock1 - clock2);
8799
8800 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8801 return true;
8802
8803 return false;
8804}
8805
Daniel Vetter25c5b262012-07-08 22:08:04 +02008806#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8807 list_for_each_entry((intel_crtc), \
8808 &(dev)->mode_config.crtc_list, \
8809 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008810 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008811
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008812static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008813intel_pipe_config_compare(struct drm_device *dev,
8814 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008815 struct intel_crtc_config *pipe_config)
8816{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008817#define PIPE_CONF_CHECK_X(name) \
8818 if (current_config->name != pipe_config->name) { \
8819 DRM_ERROR("mismatch in " #name " " \
8820 "(expected 0x%08x, found 0x%08x)\n", \
8821 current_config->name, \
8822 pipe_config->name); \
8823 return false; \
8824 }
8825
Daniel Vetter08a24032013-04-19 11:25:34 +02008826#define PIPE_CONF_CHECK_I(name) \
8827 if (current_config->name != pipe_config->name) { \
8828 DRM_ERROR("mismatch in " #name " " \
8829 "(expected %i, found %i)\n", \
8830 current_config->name, \
8831 pipe_config->name); \
8832 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008833 }
8834
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008835#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8836 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008837 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008838 "(expected %i, found %i)\n", \
8839 current_config->name & (mask), \
8840 pipe_config->name & (mask)); \
8841 return false; \
8842 }
8843
Ville Syrjälä5e550652013-09-06 23:29:07 +03008844#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
8845 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8846 DRM_ERROR("mismatch in " #name " " \
8847 "(expected %i, found %i)\n", \
8848 current_config->name, \
8849 pipe_config->name); \
8850 return false; \
8851 }
8852
Daniel Vetterbb760062013-06-06 14:55:52 +02008853#define PIPE_CONF_QUIRK(quirk) \
8854 ((current_config->quirks | pipe_config->quirks) & (quirk))
8855
Daniel Vettereccb1402013-05-22 00:50:22 +02008856 PIPE_CONF_CHECK_I(cpu_transcoder);
8857
Daniel Vetter08a24032013-04-19 11:25:34 +02008858 PIPE_CONF_CHECK_I(has_pch_encoder);
8859 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008860 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8861 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8862 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8863 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8864 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008865
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008866 PIPE_CONF_CHECK_I(has_dp_encoder);
8867 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
8868 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
8869 PIPE_CONF_CHECK_I(dp_m_n.link_m);
8870 PIPE_CONF_CHECK_I(dp_m_n.link_n);
8871 PIPE_CONF_CHECK_I(dp_m_n.tu);
8872
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008873 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8874 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8875 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8876 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8877 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8878 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8879
8880 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8881 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8882 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8883 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8884 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8885 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8886
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008887 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008888
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008889 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8890 DRM_MODE_FLAG_INTERLACE);
8891
Daniel Vetterbb760062013-06-06 14:55:52 +02008892 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8893 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8894 DRM_MODE_FLAG_PHSYNC);
8895 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8896 DRM_MODE_FLAG_NHSYNC);
8897 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8898 DRM_MODE_FLAG_PVSYNC);
8899 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8900 DRM_MODE_FLAG_NVSYNC);
8901 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008902
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008903 PIPE_CONF_CHECK_I(pipe_src_w);
8904 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008905
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008906 PIPE_CONF_CHECK_I(gmch_pfit.control);
8907 /* pfit ratios are autocomputed by the hw on gen4+ */
8908 if (INTEL_INFO(dev)->gen < 4)
8909 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8910 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008911 PIPE_CONF_CHECK_I(pch_pfit.enabled);
8912 if (current_config->pch_pfit.enabled) {
8913 PIPE_CONF_CHECK_I(pch_pfit.pos);
8914 PIPE_CONF_CHECK_I(pch_pfit.size);
8915 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008916
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008917 PIPE_CONF_CHECK_I(ips_enabled);
8918
Ville Syrjälä282740f2013-09-04 18:30:03 +03008919 PIPE_CONF_CHECK_I(double_wide);
8920
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008921 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008922 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008923 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008924 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8925 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008926
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008927 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
8928 PIPE_CONF_CHECK_I(pipe_bpp);
8929
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008930 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01008931 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008932 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
8933 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03008934
Daniel Vetter66e985c2013-06-05 13:34:20 +02008935#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008936#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008937#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03008938#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02008939#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008940
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008941 return true;
8942}
8943
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008944static void
8945check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008946{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008947 struct intel_connector *connector;
8948
8949 list_for_each_entry(connector, &dev->mode_config.connector_list,
8950 base.head) {
8951 /* This also checks the encoder/connector hw state with the
8952 * ->get_hw_state callbacks. */
8953 intel_connector_check_state(connector);
8954
8955 WARN(&connector->new_encoder->base != connector->base.encoder,
8956 "connector's staged encoder doesn't match current encoder\n");
8957 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008958}
8959
8960static void
8961check_encoder_state(struct drm_device *dev)
8962{
8963 struct intel_encoder *encoder;
8964 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008965
8966 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8967 base.head) {
8968 bool enabled = false;
8969 bool active = false;
8970 enum pipe pipe, tracked_pipe;
8971
8972 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8973 encoder->base.base.id,
8974 drm_get_encoder_name(&encoder->base));
8975
8976 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8977 "encoder's stage crtc doesn't match current crtc\n");
8978 WARN(encoder->connectors_active && !encoder->base.crtc,
8979 "encoder's active_connectors set, but no crtc\n");
8980
8981 list_for_each_entry(connector, &dev->mode_config.connector_list,
8982 base.head) {
8983 if (connector->base.encoder != &encoder->base)
8984 continue;
8985 enabled = true;
8986 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8987 active = true;
8988 }
8989 WARN(!!encoder->base.crtc != enabled,
8990 "encoder's enabled state mismatch "
8991 "(expected %i, found %i)\n",
8992 !!encoder->base.crtc, enabled);
8993 WARN(active && !encoder->base.crtc,
8994 "active encoder with no crtc\n");
8995
8996 WARN(encoder->connectors_active != active,
8997 "encoder's computed active state doesn't match tracked active state "
8998 "(expected %i, found %i)\n", active, encoder->connectors_active);
8999
9000 active = encoder->get_hw_state(encoder, &pipe);
9001 WARN(active != encoder->connectors_active,
9002 "encoder's hw state doesn't match sw tracking "
9003 "(expected %i, found %i)\n",
9004 encoder->connectors_active, active);
9005
9006 if (!encoder->base.crtc)
9007 continue;
9008
9009 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9010 WARN(active && pipe != tracked_pipe,
9011 "active encoder's pipe doesn't match"
9012 "(expected %i, found %i)\n",
9013 tracked_pipe, pipe);
9014
9015 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009016}
9017
9018static void
9019check_crtc_state(struct drm_device *dev)
9020{
9021 drm_i915_private_t *dev_priv = dev->dev_private;
9022 struct intel_crtc *crtc;
9023 struct intel_encoder *encoder;
9024 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009025
9026 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9027 base.head) {
9028 bool enabled = false;
9029 bool active = false;
9030
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009031 memset(&pipe_config, 0, sizeof(pipe_config));
9032
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009033 DRM_DEBUG_KMS("[CRTC:%d]\n",
9034 crtc->base.base.id);
9035
9036 WARN(crtc->active && !crtc->base.enabled,
9037 "active crtc, but not enabled in sw tracking\n");
9038
9039 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9040 base.head) {
9041 if (encoder->base.crtc != &crtc->base)
9042 continue;
9043 enabled = true;
9044 if (encoder->connectors_active)
9045 active = true;
9046 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009047
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009048 WARN(active != crtc->active,
9049 "crtc's computed active state doesn't match tracked active state "
9050 "(expected %i, found %i)\n", active, crtc->active);
9051 WARN(enabled != crtc->base.enabled,
9052 "crtc's computed enabled state doesn't match tracked enabled state "
9053 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9054
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009055 active = dev_priv->display.get_pipe_config(crtc,
9056 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009057
9058 /* hw state is inconsistent with the pipe A quirk */
9059 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9060 active = crtc->active;
9061
Daniel Vetter6c49f242013-06-06 12:45:25 +02009062 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9063 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009064 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009065 if (encoder->base.crtc != &crtc->base)
9066 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009067 if (encoder->get_config &&
9068 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009069 encoder->get_config(encoder, &pipe_config);
9070 }
9071
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009072 WARN(crtc->active != active,
9073 "crtc active state doesn't match with hw state "
9074 "(expected %i, found %i)\n", crtc->active, active);
9075
Daniel Vetterc0b03412013-05-28 12:05:54 +02009076 if (active &&
9077 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9078 WARN(1, "pipe state doesn't match!\n");
9079 intel_dump_pipe_config(crtc, &pipe_config,
9080 "[hw state]");
9081 intel_dump_pipe_config(crtc, &crtc->config,
9082 "[sw state]");
9083 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009084 }
9085}
9086
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009087static void
9088check_shared_dpll_state(struct drm_device *dev)
9089{
9090 drm_i915_private_t *dev_priv = dev->dev_private;
9091 struct intel_crtc *crtc;
9092 struct intel_dpll_hw_state dpll_hw_state;
9093 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009094
9095 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9096 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9097 int enabled_crtcs = 0, active_crtcs = 0;
9098 bool active;
9099
9100 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9101
9102 DRM_DEBUG_KMS("%s\n", pll->name);
9103
9104 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9105
9106 WARN(pll->active > pll->refcount,
9107 "more active pll users than references: %i vs %i\n",
9108 pll->active, pll->refcount);
9109 WARN(pll->active && !pll->on,
9110 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009111 WARN(pll->on && !pll->active,
9112 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009113 WARN(pll->on != active,
9114 "pll on state mismatch (expected %i, found %i)\n",
9115 pll->on, active);
9116
9117 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9118 base.head) {
9119 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9120 enabled_crtcs++;
9121 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9122 active_crtcs++;
9123 }
9124 WARN(pll->active != active_crtcs,
9125 "pll active crtcs mismatch (expected %i, found %i)\n",
9126 pll->active, active_crtcs);
9127 WARN(pll->refcount != enabled_crtcs,
9128 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9129 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009130
9131 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9132 sizeof(dpll_hw_state)),
9133 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009134 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009135}
9136
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009137void
9138intel_modeset_check_state(struct drm_device *dev)
9139{
9140 check_connector_state(dev);
9141 check_encoder_state(dev);
9142 check_crtc_state(dev);
9143 check_shared_dpll_state(dev);
9144}
9145
Ville Syrjälä18442d02013-09-13 16:00:08 +03009146void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9147 int dotclock)
9148{
9149 /*
9150 * FDI already provided one idea for the dotclock.
9151 * Yell if the encoder disagrees.
9152 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009153 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009154 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009155 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009156}
9157
Daniel Vetterf30da182013-04-11 20:22:50 +02009158static int __intel_set_mode(struct drm_crtc *crtc,
9159 struct drm_display_mode *mode,
9160 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009161{
9162 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009163 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009164 struct drm_display_mode *saved_mode, *saved_hwmode;
9165 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009166 struct intel_crtc *intel_crtc;
9167 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009168 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009169
Daniel Vettera1e22652013-09-21 00:35:38 +02009170 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009171 if (!saved_mode)
9172 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009173 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009174
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009175 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009176 &prepare_pipes, &disable_pipes);
9177
Tim Gardner3ac18232012-12-07 07:54:26 -07009178 *saved_hwmode = crtc->hwmode;
9179 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009180
Daniel Vetter25c5b262012-07-08 22:08:04 +02009181 /* Hack: Because we don't (yet) support global modeset on multiple
9182 * crtcs, we don't keep track of the new mode for more than one crtc.
9183 * Hence simply check whether any bit is set in modeset_pipes in all the
9184 * pieces of code that are not yet converted to deal with mutliple crtcs
9185 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009186 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009187 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009188 if (IS_ERR(pipe_config)) {
9189 ret = PTR_ERR(pipe_config);
9190 pipe_config = NULL;
9191
Tim Gardner3ac18232012-12-07 07:54:26 -07009192 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009193 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009194 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9195 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009196 }
9197
Daniel Vetter460da9162013-03-27 00:44:51 +01009198 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9199 intel_crtc_disable(&intel_crtc->base);
9200
Daniel Vetterea9d7582012-07-10 10:42:52 +02009201 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9202 if (intel_crtc->base.enabled)
9203 dev_priv->display.crtc_disable(&intel_crtc->base);
9204 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009205
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009206 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9207 * to set it here already despite that we pass it down the callchain.
9208 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009209 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009210 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009211 /* mode_set/enable/disable functions rely on a correct pipe
9212 * config. */
9213 to_intel_crtc(crtc)->config = *pipe_config;
9214 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009215
Daniel Vetterea9d7582012-07-10 10:42:52 +02009216 /* Only after disabling all output pipelines that will be changed can we
9217 * update the the output configuration. */
9218 intel_modeset_update_state(dev, prepare_pipes);
9219
Daniel Vetter47fab732012-10-26 10:58:18 +02009220 if (dev_priv->display.modeset_global_resources)
9221 dev_priv->display.modeset_global_resources(dev);
9222
Daniel Vettera6778b32012-07-02 09:56:42 +02009223 /* Set up the DPLL and any encoders state that needs to adjust or depend
9224 * on the DPLL.
9225 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009226 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009227 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009228 x, y, fb);
9229 if (ret)
9230 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009231 }
9232
9233 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009234 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9235 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009236
Daniel Vetter25c5b262012-07-08 22:08:04 +02009237 if (modeset_pipes) {
9238 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009239 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009240
Daniel Vetter25c5b262012-07-08 22:08:04 +02009241 /* Calculate and store various constants which
9242 * are later needed by vblank and swap-completion
9243 * timestamping. They are derived from true hwmode.
9244 */
9245 drm_calc_timestamping_constants(crtc);
9246 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009247
9248 /* FIXME: add subpixel order */
9249done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009250 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009251 crtc->hwmode = *saved_hwmode;
9252 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009253 }
9254
Tim Gardner3ac18232012-12-07 07:54:26 -07009255out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009256 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009257 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009258 return ret;
9259}
9260
Damien Lespiaue7457a92013-08-08 22:28:59 +01009261static int intel_set_mode(struct drm_crtc *crtc,
9262 struct drm_display_mode *mode,
9263 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009264{
9265 int ret;
9266
9267 ret = __intel_set_mode(crtc, mode, x, y, fb);
9268
9269 if (ret == 0)
9270 intel_modeset_check_state(crtc->dev);
9271
9272 return ret;
9273}
9274
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009275void intel_crtc_restore_mode(struct drm_crtc *crtc)
9276{
9277 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9278}
9279
Daniel Vetter25c5b262012-07-08 22:08:04 +02009280#undef for_each_intel_crtc_masked
9281
Daniel Vetterd9e55602012-07-04 22:16:09 +02009282static void intel_set_config_free(struct intel_set_config *config)
9283{
9284 if (!config)
9285 return;
9286
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009287 kfree(config->save_connector_encoders);
9288 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009289 kfree(config);
9290}
9291
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009292static int intel_set_config_save_state(struct drm_device *dev,
9293 struct intel_set_config *config)
9294{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009295 struct drm_encoder *encoder;
9296 struct drm_connector *connector;
9297 int count;
9298
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009299 config->save_encoder_crtcs =
9300 kcalloc(dev->mode_config.num_encoder,
9301 sizeof(struct drm_crtc *), GFP_KERNEL);
9302 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009303 return -ENOMEM;
9304
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009305 config->save_connector_encoders =
9306 kcalloc(dev->mode_config.num_connector,
9307 sizeof(struct drm_encoder *), GFP_KERNEL);
9308 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009309 return -ENOMEM;
9310
9311 /* Copy data. Note that driver private data is not affected.
9312 * Should anything bad happen only the expected state is
9313 * restored, not the drivers personal bookkeeping.
9314 */
9315 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009316 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009317 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009318 }
9319
9320 count = 0;
9321 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009322 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009323 }
9324
9325 return 0;
9326}
9327
9328static void intel_set_config_restore_state(struct drm_device *dev,
9329 struct intel_set_config *config)
9330{
Daniel Vetter9a935852012-07-05 22:34:27 +02009331 struct intel_encoder *encoder;
9332 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009333 int count;
9334
9335 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009336 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9337 encoder->new_crtc =
9338 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009339 }
9340
9341 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009342 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9343 connector->new_encoder =
9344 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009345 }
9346}
9347
Imre Deake3de42b2013-05-03 19:44:07 +02009348static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009349is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009350{
9351 int i;
9352
Chris Wilson2e57f472013-07-17 12:14:40 +01009353 if (set->num_connectors == 0)
9354 return false;
9355
9356 if (WARN_ON(set->connectors == NULL))
9357 return false;
9358
9359 for (i = 0; i < set->num_connectors; i++)
9360 if (set->connectors[i]->encoder &&
9361 set->connectors[i]->encoder->crtc == set->crtc &&
9362 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009363 return true;
9364
9365 return false;
9366}
9367
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009368static void
9369intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9370 struct intel_set_config *config)
9371{
9372
9373 /* We should be able to check here if the fb has the same properties
9374 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009375 if (is_crtc_connector_off(set)) {
9376 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009377 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009378 /* If we have no fb then treat it as a full mode set */
9379 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009380 struct intel_crtc *intel_crtc =
9381 to_intel_crtc(set->crtc);
9382
9383 if (intel_crtc->active && i915_fastboot) {
9384 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9385 config->fb_changed = true;
9386 } else {
9387 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9388 config->mode_changed = true;
9389 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009390 } else if (set->fb == NULL) {
9391 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009392 } else if (set->fb->pixel_format !=
9393 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009394 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009395 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009396 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009397 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009398 }
9399
Daniel Vetter835c5872012-07-10 18:11:08 +02009400 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009401 config->fb_changed = true;
9402
9403 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9404 DRM_DEBUG_KMS("modes are different, full mode set\n");
9405 drm_mode_debug_printmodeline(&set->crtc->mode);
9406 drm_mode_debug_printmodeline(set->mode);
9407 config->mode_changed = true;
9408 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009409
9410 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9411 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009412}
9413
Daniel Vetter2e431052012-07-04 22:42:15 +02009414static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009415intel_modeset_stage_output_state(struct drm_device *dev,
9416 struct drm_mode_set *set,
9417 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009418{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009419 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009420 struct intel_connector *connector;
9421 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009422 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009423
Damien Lespiau9abdda72013-02-13 13:29:23 +00009424 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009425 * of connectors. For paranoia, double-check this. */
9426 WARN_ON(!set->fb && (set->num_connectors != 0));
9427 WARN_ON(set->fb && (set->num_connectors == 0));
9428
Daniel Vetter9a935852012-07-05 22:34:27 +02009429 list_for_each_entry(connector, &dev->mode_config.connector_list,
9430 base.head) {
9431 /* Otherwise traverse passed in connector list and get encoders
9432 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009433 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009434 if (set->connectors[ro] == &connector->base) {
9435 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009436 break;
9437 }
9438 }
9439
Daniel Vetter9a935852012-07-05 22:34:27 +02009440 /* If we disable the crtc, disable all its connectors. Also, if
9441 * the connector is on the changing crtc but not on the new
9442 * connector list, disable it. */
9443 if ((!set->fb || ro == set->num_connectors) &&
9444 connector->base.encoder &&
9445 connector->base.encoder->crtc == set->crtc) {
9446 connector->new_encoder = NULL;
9447
9448 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9449 connector->base.base.id,
9450 drm_get_connector_name(&connector->base));
9451 }
9452
9453
9454 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009455 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009456 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009457 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009458 }
9459 /* connector->new_encoder is now updated for all connectors. */
9460
9461 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009462 list_for_each_entry(connector, &dev->mode_config.connector_list,
9463 base.head) {
9464 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009465 continue;
9466
Daniel Vetter9a935852012-07-05 22:34:27 +02009467 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009468
9469 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009470 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009471 new_crtc = set->crtc;
9472 }
9473
9474 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009475 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9476 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009477 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009478 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009479 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9480
9481 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9482 connector->base.base.id,
9483 drm_get_connector_name(&connector->base),
9484 new_crtc->base.id);
9485 }
9486
9487 /* Check for any encoders that needs to be disabled. */
9488 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9489 base.head) {
9490 list_for_each_entry(connector,
9491 &dev->mode_config.connector_list,
9492 base.head) {
9493 if (connector->new_encoder == encoder) {
9494 WARN_ON(!connector->new_encoder->new_crtc);
9495
9496 goto next_encoder;
9497 }
9498 }
9499 encoder->new_crtc = NULL;
9500next_encoder:
9501 /* Only now check for crtc changes so we don't miss encoders
9502 * that will be disabled. */
9503 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009504 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009505 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009506 }
9507 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009508 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009509
Daniel Vetter2e431052012-07-04 22:42:15 +02009510 return 0;
9511}
9512
9513static int intel_crtc_set_config(struct drm_mode_set *set)
9514{
9515 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009516 struct drm_mode_set save_set;
9517 struct intel_set_config *config;
9518 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009519
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009520 BUG_ON(!set);
9521 BUG_ON(!set->crtc);
9522 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009523
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009524 /* Enforce sane interface api - has been abused by the fb helper. */
9525 BUG_ON(!set->mode && set->fb);
9526 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009527
Daniel Vetter2e431052012-07-04 22:42:15 +02009528 if (set->fb) {
9529 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9530 set->crtc->base.id, set->fb->base.id,
9531 (int)set->num_connectors, set->x, set->y);
9532 } else {
9533 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009534 }
9535
9536 dev = set->crtc->dev;
9537
9538 ret = -ENOMEM;
9539 config = kzalloc(sizeof(*config), GFP_KERNEL);
9540 if (!config)
9541 goto out_config;
9542
9543 ret = intel_set_config_save_state(dev, config);
9544 if (ret)
9545 goto out_config;
9546
9547 save_set.crtc = set->crtc;
9548 save_set.mode = &set->crtc->mode;
9549 save_set.x = set->crtc->x;
9550 save_set.y = set->crtc->y;
9551 save_set.fb = set->crtc->fb;
9552
9553 /* Compute whether we need a full modeset, only an fb base update or no
9554 * change at all. In the future we might also check whether only the
9555 * mode changed, e.g. for LVDS where we only change the panel fitter in
9556 * such cases. */
9557 intel_set_config_compute_mode_changes(set, config);
9558
Daniel Vetter9a935852012-07-05 22:34:27 +02009559 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009560 if (ret)
9561 goto fail;
9562
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009563 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009564 ret = intel_set_mode(set->crtc, set->mode,
9565 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009566 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009567 intel_crtc_wait_for_pending_flips(set->crtc);
9568
Daniel Vetter4f660f42012-07-02 09:47:37 +02009569 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009570 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009571 }
9572
Chris Wilson2d05eae2013-05-03 17:36:25 +01009573 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009574 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9575 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009576fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009577 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009578
Chris Wilson2d05eae2013-05-03 17:36:25 +01009579 /* Try to restore the config */
9580 if (config->mode_changed &&
9581 intel_set_mode(save_set.crtc, save_set.mode,
9582 save_set.x, save_set.y, save_set.fb))
9583 DRM_ERROR("failed to restore config after modeset failure\n");
9584 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009585
Daniel Vetterd9e55602012-07-04 22:16:09 +02009586out_config:
9587 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009588 return ret;
9589}
9590
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009591static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009592 .cursor_set = intel_crtc_cursor_set,
9593 .cursor_move = intel_crtc_cursor_move,
9594 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009595 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009596 .destroy = intel_crtc_destroy,
9597 .page_flip = intel_crtc_page_flip,
9598};
9599
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009600static void intel_cpu_pll_init(struct drm_device *dev)
9601{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009602 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009603 intel_ddi_pll_init(dev);
9604}
9605
Daniel Vetter53589012013-06-05 13:34:16 +02009606static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9607 struct intel_shared_dpll *pll,
9608 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009609{
Daniel Vetter53589012013-06-05 13:34:16 +02009610 uint32_t val;
9611
9612 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009613 hw_state->dpll = val;
9614 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9615 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02009616
9617 return val & DPLL_VCO_ENABLE;
9618}
9619
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009620static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9621 struct intel_shared_dpll *pll)
9622{
9623 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9624 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9625}
9626
Daniel Vettere7b903d2013-06-05 13:34:14 +02009627static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9628 struct intel_shared_dpll *pll)
9629{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009630 /* PCH refclock must be enabled first */
9631 assert_pch_refclk_enabled(dev_priv);
9632
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009633 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9634
9635 /* Wait for the clocks to stabilize. */
9636 POSTING_READ(PCH_DPLL(pll->id));
9637 udelay(150);
9638
9639 /* The pixel multiplier can only be updated once the
9640 * DPLL is enabled and the clocks are stable.
9641 *
9642 * So write it again.
9643 */
9644 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9645 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009646 udelay(200);
9647}
9648
9649static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9650 struct intel_shared_dpll *pll)
9651{
9652 struct drm_device *dev = dev_priv->dev;
9653 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009654
9655 /* Make sure no transcoder isn't still depending on us. */
9656 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9657 if (intel_crtc_to_shared_dpll(crtc) == pll)
9658 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9659 }
9660
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009661 I915_WRITE(PCH_DPLL(pll->id), 0);
9662 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009663 udelay(200);
9664}
9665
Daniel Vetter46edb022013-06-05 13:34:12 +02009666static char *ibx_pch_dpll_names[] = {
9667 "PCH DPLL A",
9668 "PCH DPLL B",
9669};
9670
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009671static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009672{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009673 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009674 int i;
9675
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009676 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009677
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009678 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02009679 dev_priv->shared_dplls[i].id = i;
9680 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009681 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009682 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9683 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009684 dev_priv->shared_dplls[i].get_hw_state =
9685 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009686 }
9687}
9688
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009689static void intel_shared_dpll_init(struct drm_device *dev)
9690{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009691 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009692
9693 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9694 ibx_pch_dpll_init(dev);
9695 else
9696 dev_priv->num_shared_dpll = 0;
9697
9698 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9699 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9700 dev_priv->num_shared_dpll);
9701}
9702
Hannes Ederb358d0a2008-12-18 21:18:47 +01009703static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009704{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009705 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009706 struct intel_crtc *intel_crtc;
9707 int i;
9708
Daniel Vetter955382f2013-09-19 14:05:45 +02009709 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009710 if (intel_crtc == NULL)
9711 return;
9712
9713 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9714
9715 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009716 for (i = 0; i < 256; i++) {
9717 intel_crtc->lut_r[i] = i;
9718 intel_crtc->lut_g[i] = i;
9719 intel_crtc->lut_b[i] = i;
9720 }
9721
Jesse Barnes80824002009-09-10 15:28:06 -07009722 /* Swap pipes & planes for FBC on pre-965 */
9723 intel_crtc->pipe = pipe;
9724 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009725 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009726 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009727 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009728 }
9729
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009730 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9731 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9732 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9733 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9734
Jesse Barnes79e53942008-11-07 14:24:08 -08009735 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009736}
9737
Carl Worth08d7b3d2009-04-29 14:43:54 -07009738int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009739 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009740{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009741 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009742 struct drm_mode_object *drmmode_obj;
9743 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009744
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009745 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9746 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009747
Daniel Vetterc05422d2009-08-11 16:05:30 +02009748 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9749 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009750
Daniel Vetterc05422d2009-08-11 16:05:30 +02009751 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009752 DRM_ERROR("no such CRTC id\n");
9753 return -EINVAL;
9754 }
9755
Daniel Vetterc05422d2009-08-11 16:05:30 +02009756 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9757 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009758
Daniel Vetterc05422d2009-08-11 16:05:30 +02009759 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009760}
9761
Daniel Vetter66a92782012-07-12 20:08:18 +02009762static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009763{
Daniel Vetter66a92782012-07-12 20:08:18 +02009764 struct drm_device *dev = encoder->base.dev;
9765 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009766 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009767 int entry = 0;
9768
Daniel Vetter66a92782012-07-12 20:08:18 +02009769 list_for_each_entry(source_encoder,
9770 &dev->mode_config.encoder_list, base.head) {
9771
9772 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009773 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009774
9775 /* Intel hw has only one MUX where enocoders could be cloned. */
9776 if (encoder->cloneable && source_encoder->cloneable)
9777 index_mask |= (1 << entry);
9778
Jesse Barnes79e53942008-11-07 14:24:08 -08009779 entry++;
9780 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009781
Jesse Barnes79e53942008-11-07 14:24:08 -08009782 return index_mask;
9783}
9784
Chris Wilson4d302442010-12-14 19:21:29 +00009785static bool has_edp_a(struct drm_device *dev)
9786{
9787 struct drm_i915_private *dev_priv = dev->dev_private;
9788
9789 if (!IS_MOBILE(dev))
9790 return false;
9791
9792 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9793 return false;
9794
9795 if (IS_GEN5(dev) &&
9796 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9797 return false;
9798
9799 return true;
9800}
9801
Jesse Barnes79e53942008-11-07 14:24:08 -08009802static void intel_setup_outputs(struct drm_device *dev)
9803{
Eric Anholt725e30a2009-01-22 13:01:02 -08009804 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009805 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009806 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009807
Daniel Vetterc9093352013-06-06 22:22:47 +02009808 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009809
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009810 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009811 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009812
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009813 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009814 int found;
9815
9816 /* Haswell uses DDI functions to detect digital outputs */
9817 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9818 /* DDI A only supports eDP */
9819 if (found)
9820 intel_ddi_init(dev, PORT_A);
9821
9822 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9823 * register */
9824 found = I915_READ(SFUSE_STRAP);
9825
9826 if (found & SFUSE_STRAP_DDIB_DETECTED)
9827 intel_ddi_init(dev, PORT_B);
9828 if (found & SFUSE_STRAP_DDIC_DETECTED)
9829 intel_ddi_init(dev, PORT_C);
9830 if (found & SFUSE_STRAP_DDID_DETECTED)
9831 intel_ddi_init(dev, PORT_D);
9832 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009833 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009834 dpd_is_edp = intel_dpd_is_edp(dev);
9835
9836 if (has_edp_a(dev))
9837 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009838
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009839 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009840 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009841 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009842 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009843 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009844 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009845 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009846 }
9847
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009848 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009849 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009850
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009851 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009852 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009853
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009854 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009855 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009856
Daniel Vetter270b3042012-10-27 15:52:05 +02009857 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009858 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009859 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309860 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Jesse Barnes6f6005a2013-08-09 09:34:35 -07009861 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
9862 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
9863 PORT_C);
9864 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9865 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
9866 PORT_C);
9867 }
Gajanan Bhat19c03922012-09-27 19:13:07 +05309868
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009869 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009870 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9871 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009872 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9873 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009874 }
Jani Nikula3cfca972013-08-27 15:12:26 +03009875
9876 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +08009877 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009878 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009879
Paulo Zanonie2debe92013-02-18 19:00:27 -03009880 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009881 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009882 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009883 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9884 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009885 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009886 }
Ma Ling27185ae2009-08-24 13:50:23 +08009887
Imre Deake7281ea2013-05-08 13:14:08 +03009888 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009889 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009890 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009891
9892 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009893
Paulo Zanonie2debe92013-02-18 19:00:27 -03009894 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009895 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009896 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009897 }
Ma Ling27185ae2009-08-24 13:50:23 +08009898
Paulo Zanonie2debe92013-02-18 19:00:27 -03009899 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009900
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009901 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9902 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009903 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009904 }
Imre Deake7281ea2013-05-08 13:14:08 +03009905 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009906 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009907 }
Ma Ling27185ae2009-08-24 13:50:23 +08009908
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009909 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009910 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009911 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009912 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009913 intel_dvo_init(dev);
9914
Zhenyu Wang103a1962009-11-27 11:44:36 +08009915 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009916 intel_tv_init(dev);
9917
Chris Wilson4ef69c72010-09-09 15:14:28 +01009918 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9919 encoder->base.possible_crtcs = encoder->crtc_mask;
9920 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009921 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009922 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009923
Paulo Zanonidde86e22012-12-01 12:04:25 -02009924 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009925
9926 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009927}
9928
Chris Wilsonddfe1562013-08-06 17:43:07 +01009929void intel_framebuffer_fini(struct intel_framebuffer *fb)
9930{
9931 drm_framebuffer_cleanup(&fb->base);
9932 drm_gem_object_unreference_unlocked(&fb->obj->base);
9933}
9934
Jesse Barnes79e53942008-11-07 14:24:08 -08009935static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9936{
9937 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009938
Chris Wilsonddfe1562013-08-06 17:43:07 +01009939 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009940 kfree(intel_fb);
9941}
9942
9943static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009944 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009945 unsigned int *handle)
9946{
9947 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009948 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009949
Chris Wilson05394f32010-11-08 19:18:58 +00009950 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009951}
9952
9953static const struct drm_framebuffer_funcs intel_fb_funcs = {
9954 .destroy = intel_user_framebuffer_destroy,
9955 .create_handle = intel_user_framebuffer_create_handle,
9956};
9957
Dave Airlie38651672010-03-30 05:34:13 +00009958int intel_framebuffer_init(struct drm_device *dev,
9959 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009960 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009961 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009962{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009963 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009964 int ret;
9965
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009966 if (obj->tiling_mode == I915_TILING_Y) {
9967 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009968 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009969 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009970
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009971 if (mode_cmd->pitches[0] & 63) {
9972 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9973 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009974 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009975 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009976
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009977 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9978 pitch_limit = 32*1024;
9979 } else if (INTEL_INFO(dev)->gen >= 4) {
9980 if (obj->tiling_mode)
9981 pitch_limit = 16*1024;
9982 else
9983 pitch_limit = 32*1024;
9984 } else if (INTEL_INFO(dev)->gen >= 3) {
9985 if (obj->tiling_mode)
9986 pitch_limit = 8*1024;
9987 else
9988 pitch_limit = 16*1024;
9989 } else
9990 /* XXX DSPC is limited to 4k tiled */
9991 pitch_limit = 8*1024;
9992
9993 if (mode_cmd->pitches[0] > pitch_limit) {
9994 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9995 obj->tiling_mode ? "tiled" : "linear",
9996 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009997 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009998 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009999
10000 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010001 mode_cmd->pitches[0] != obj->stride) {
10002 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10003 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010004 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010005 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010006
Ville Syrjälä57779d02012-10-31 17:50:14 +020010007 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010008 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010009 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010010 case DRM_FORMAT_RGB565:
10011 case DRM_FORMAT_XRGB8888:
10012 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010013 break;
10014 case DRM_FORMAT_XRGB1555:
10015 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010016 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010017 DRM_DEBUG("unsupported pixel format: %s\n",
10018 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010019 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010020 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010021 break;
10022 case DRM_FORMAT_XBGR8888:
10023 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010024 case DRM_FORMAT_XRGB2101010:
10025 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010026 case DRM_FORMAT_XBGR2101010:
10027 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010028 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010029 DRM_DEBUG("unsupported pixel format: %s\n",
10030 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010031 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010032 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010033 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010034 case DRM_FORMAT_YUYV:
10035 case DRM_FORMAT_UYVY:
10036 case DRM_FORMAT_YVYU:
10037 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010038 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010039 DRM_DEBUG("unsupported pixel format: %s\n",
10040 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010041 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010042 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010043 break;
10044 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010045 DRM_DEBUG("unsupported pixel format: %s\n",
10046 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010047 return -EINVAL;
10048 }
10049
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010050 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10051 if (mode_cmd->offsets[0] != 0)
10052 return -EINVAL;
10053
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010054 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10055 intel_fb->obj = obj;
10056
Jesse Barnes79e53942008-11-07 14:24:08 -080010057 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10058 if (ret) {
10059 DRM_ERROR("framebuffer init failed %d\n", ret);
10060 return ret;
10061 }
10062
Jesse Barnes79e53942008-11-07 14:24:08 -080010063 return 0;
10064}
10065
Jesse Barnes79e53942008-11-07 14:24:08 -080010066static struct drm_framebuffer *
10067intel_user_framebuffer_create(struct drm_device *dev,
10068 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010069 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010070{
Chris Wilson05394f32010-11-08 19:18:58 +000010071 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010072
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010073 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10074 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010075 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010076 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010077
Chris Wilsond2dff872011-04-19 08:36:26 +010010078 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010079}
10080
Jesse Barnes79e53942008-11-07 14:24:08 -080010081static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010082 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +000010083 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010084};
10085
Jesse Barnese70236a2009-09-21 10:42:27 -070010086/* Set up chip specific display functions */
10087static void intel_init_display(struct drm_device *dev)
10088{
10089 struct drm_i915_private *dev_priv = dev->dev_private;
10090
Daniel Vetteree9300b2013-06-03 22:40:22 +020010091 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10092 dev_priv->display.find_dpll = g4x_find_best_dpll;
10093 else if (IS_VALLEYVIEW(dev))
10094 dev_priv->display.find_dpll = vlv_find_best_dpll;
10095 else if (IS_PINEVIEW(dev))
10096 dev_priv->display.find_dpll = pnv_find_best_dpll;
10097 else
10098 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10099
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010100 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010101 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010102 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010103 dev_priv->display.crtc_enable = haswell_crtc_enable;
10104 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010105 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010106 dev_priv->display.update_plane = ironlake_update_plane;
10107 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010108 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010109 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010110 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10111 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010112 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010113 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010114 } else if (IS_VALLEYVIEW(dev)) {
10115 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10116 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10117 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10118 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10119 dev_priv->display.off = i9xx_crtc_off;
10120 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010121 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010122 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010123 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010124 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10125 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010126 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010127 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010128 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010129
Jesse Barnese70236a2009-09-21 10:42:27 -070010130 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010131 if (IS_VALLEYVIEW(dev))
10132 dev_priv->display.get_display_clock_speed =
10133 valleyview_get_display_clock_speed;
10134 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010135 dev_priv->display.get_display_clock_speed =
10136 i945_get_display_clock_speed;
10137 else if (IS_I915G(dev))
10138 dev_priv->display.get_display_clock_speed =
10139 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010140 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010141 dev_priv->display.get_display_clock_speed =
10142 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010143 else if (IS_PINEVIEW(dev))
10144 dev_priv->display.get_display_clock_speed =
10145 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010146 else if (IS_I915GM(dev))
10147 dev_priv->display.get_display_clock_speed =
10148 i915gm_get_display_clock_speed;
10149 else if (IS_I865G(dev))
10150 dev_priv->display.get_display_clock_speed =
10151 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010152 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010153 dev_priv->display.get_display_clock_speed =
10154 i855_get_display_clock_speed;
10155 else /* 852, 830 */
10156 dev_priv->display.get_display_clock_speed =
10157 i830_get_display_clock_speed;
10158
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010159 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010160 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010161 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010162 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010163 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010164 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010165 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010166 } else if (IS_IVYBRIDGE(dev)) {
10167 /* FIXME: detect B0+ stepping and use auto training */
10168 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010169 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010170 dev_priv->display.modeset_global_resources =
10171 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010172 } else if (IS_HASWELL(dev)) {
10173 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010174 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010175 dev_priv->display.modeset_global_resources =
10176 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010177 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010178 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010179 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010180 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010181
10182 /* Default just returns -ENODEV to indicate unsupported */
10183 dev_priv->display.queue_flip = intel_default_queue_flip;
10184
10185 switch (INTEL_INFO(dev)->gen) {
10186 case 2:
10187 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10188 break;
10189
10190 case 3:
10191 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10192 break;
10193
10194 case 4:
10195 case 5:
10196 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10197 break;
10198
10199 case 6:
10200 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10201 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010202 case 7:
10203 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10204 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010205 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010206}
10207
Jesse Barnesb690e962010-07-19 13:53:12 -070010208/*
10209 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10210 * resume, or other times. This quirk makes sure that's the case for
10211 * affected systems.
10212 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010213static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010214{
10215 struct drm_i915_private *dev_priv = dev->dev_private;
10216
10217 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010218 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010219}
10220
Keith Packard435793d2011-07-12 14:56:22 -070010221/*
10222 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10223 */
10224static void quirk_ssc_force_disable(struct drm_device *dev)
10225{
10226 struct drm_i915_private *dev_priv = dev->dev_private;
10227 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010228 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010229}
10230
Carsten Emde4dca20e2012-03-15 15:56:26 +010010231/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010232 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10233 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010234 */
10235static void quirk_invert_brightness(struct drm_device *dev)
10236{
10237 struct drm_i915_private *dev_priv = dev->dev_private;
10238 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010239 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010240}
10241
Kamal Mostafae85843b2013-07-19 15:02:01 -070010242/*
10243 * Some machines (Dell XPS13) suffer broken backlight controls if
10244 * BLM_PCH_PWM_ENABLE is set.
10245 */
10246static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
10247{
10248 struct drm_i915_private *dev_priv = dev->dev_private;
10249 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
10250 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
10251}
10252
Jesse Barnesb690e962010-07-19 13:53:12 -070010253struct intel_quirk {
10254 int device;
10255 int subsystem_vendor;
10256 int subsystem_device;
10257 void (*hook)(struct drm_device *dev);
10258};
10259
Egbert Eich5f85f172012-10-14 15:46:38 +020010260/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10261struct intel_dmi_quirk {
10262 void (*hook)(struct drm_device *dev);
10263 const struct dmi_system_id (*dmi_id_list)[];
10264};
10265
10266static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10267{
10268 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10269 return 1;
10270}
10271
10272static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10273 {
10274 .dmi_id_list = &(const struct dmi_system_id[]) {
10275 {
10276 .callback = intel_dmi_reverse_brightness,
10277 .ident = "NCR Corporation",
10278 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10279 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10280 },
10281 },
10282 { } /* terminating entry */
10283 },
10284 .hook = quirk_invert_brightness,
10285 },
10286};
10287
Ben Widawskyc43b5632012-04-16 14:07:40 -070010288static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010289 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010290 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010291
Jesse Barnesb690e962010-07-19 13:53:12 -070010292 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10293 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10294
Jesse Barnesb690e962010-07-19 13:53:12 -070010295 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10296 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10297
Daniel Vetterccd0d362012-10-10 23:13:59 +020010298 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -070010299 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010300 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010301
10302 /* Lenovo U160 cannot use SSC on LVDS */
10303 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010304
10305 /* Sony Vaio Y cannot use SSC on LVDS */
10306 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010307
Jani Nikulaee1452d2013-09-20 15:05:30 +030010308 /*
10309 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10310 * seem to use inverted backlight PWM.
10311 */
10312 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Kamal Mostafae85843b2013-07-19 15:02:01 -070010313
10314 /* Dell XPS13 HD Sandy Bridge */
10315 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
10316 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
10317 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
Jesse Barnesb690e962010-07-19 13:53:12 -070010318};
10319
10320static void intel_init_quirks(struct drm_device *dev)
10321{
10322 struct pci_dev *d = dev->pdev;
10323 int i;
10324
10325 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10326 struct intel_quirk *q = &intel_quirks[i];
10327
10328 if (d->device == q->device &&
10329 (d->subsystem_vendor == q->subsystem_vendor ||
10330 q->subsystem_vendor == PCI_ANY_ID) &&
10331 (d->subsystem_device == q->subsystem_device ||
10332 q->subsystem_device == PCI_ANY_ID))
10333 q->hook(dev);
10334 }
Egbert Eich5f85f172012-10-14 15:46:38 +020010335 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10336 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10337 intel_dmi_quirks[i].hook(dev);
10338 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010339}
10340
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010341/* Disable the VGA plane that we never use */
10342static void i915_disable_vga(struct drm_device *dev)
10343{
10344 struct drm_i915_private *dev_priv = dev->dev_private;
10345 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010346 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010347
10348 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010349 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010350 sr1 = inb(VGA_SR_DATA);
10351 outb(sr1 | 1<<5, VGA_SR_DATA);
10352 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10353 udelay(300);
10354
10355 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10356 POSTING_READ(vga_reg);
10357}
10358
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010359static void i915_enable_vga_mem(struct drm_device *dev)
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010360{
10361 /* Enable VGA memory on Intel HD */
10362 if (HAS_PCH_SPLIT(dev)) {
10363 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10364 outb(inb(VGA_MSR_READ) | VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10365 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10366 VGA_RSRC_LEGACY_MEM |
10367 VGA_RSRC_NORMAL_IO |
10368 VGA_RSRC_NORMAL_MEM);
10369 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10370 }
10371}
10372
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010373void i915_disable_vga_mem(struct drm_device *dev)
10374{
10375 /* Disable VGA memory on Intel HD */
10376 if (HAS_PCH_SPLIT(dev)) {
10377 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10378 outb(inb(VGA_MSR_READ) & ~VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10379 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10380 VGA_RSRC_NORMAL_IO |
10381 VGA_RSRC_NORMAL_MEM);
10382 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10383 }
10384}
10385
Daniel Vetterf8175862012-04-10 15:50:11 +020010386void intel_modeset_init_hw(struct drm_device *dev)
10387{
Jesse Barnesf6071162013-10-01 10:41:38 -070010388 struct drm_i915_private *dev_priv = dev->dev_private;
10389
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010390 intel_prepare_ddi(dev);
10391
Daniel Vetterf8175862012-04-10 15:50:11 +020010392 intel_init_clock_gating(dev);
10393
Jesse Barnesf6071162013-10-01 10:41:38 -070010394 /* Enable the CRI clock source so we can get at the display */
10395 if (IS_VALLEYVIEW(dev))
10396 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10397 DPLL_INTEGRATED_CRI_CLK_VLV);
10398
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010399 intel_init_dpio(dev);
10400
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010401 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010402 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010403 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010404}
10405
Imre Deak7d708ee2013-04-17 14:04:50 +030010406void intel_modeset_suspend_hw(struct drm_device *dev)
10407{
10408 intel_suspend_hw(dev);
10409}
10410
Jesse Barnes79e53942008-11-07 14:24:08 -080010411void intel_modeset_init(struct drm_device *dev)
10412{
Jesse Barnes652c3932009-08-17 13:31:43 -070010413 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010414 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010415
10416 drm_mode_config_init(dev);
10417
10418 dev->mode_config.min_width = 0;
10419 dev->mode_config.min_height = 0;
10420
Dave Airlie019d96c2011-09-29 16:20:42 +010010421 dev->mode_config.preferred_depth = 24;
10422 dev->mode_config.prefer_shadow = 1;
10423
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010424 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010425
Jesse Barnesb690e962010-07-19 13:53:12 -070010426 intel_init_quirks(dev);
10427
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010428 intel_init_pm(dev);
10429
Ben Widawskye3c74752013-04-05 13:12:39 -070010430 if (INTEL_INFO(dev)->num_pipes == 0)
10431 return;
10432
Jesse Barnese70236a2009-09-21 10:42:27 -070010433 intel_init_display(dev);
10434
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010435 if (IS_GEN2(dev)) {
10436 dev->mode_config.max_width = 2048;
10437 dev->mode_config.max_height = 2048;
10438 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010439 dev->mode_config.max_width = 4096;
10440 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010441 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010442 dev->mode_config.max_width = 8192;
10443 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010444 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010445 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010446
Zhao Yakui28c97732009-10-09 11:39:41 +080010447 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010448 INTEL_INFO(dev)->num_pipes,
10449 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010450
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010451 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010452 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010453 for (j = 0; j < dev_priv->num_plane; j++) {
10454 ret = intel_plane_init(dev, i, j);
10455 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010456 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10457 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010458 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010459 }
10460
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010461 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010462 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010463
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010464 /* Just disable it once at startup */
10465 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010466 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010467
10468 /* Just in case the BIOS is doing something questionable. */
10469 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010470}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010471
Daniel Vetter24929352012-07-02 20:28:59 +020010472static void
10473intel_connector_break_all_links(struct intel_connector *connector)
10474{
10475 connector->base.dpms = DRM_MODE_DPMS_OFF;
10476 connector->base.encoder = NULL;
10477 connector->encoder->connectors_active = false;
10478 connector->encoder->base.crtc = NULL;
10479}
10480
Daniel Vetter7fad7982012-07-04 17:51:47 +020010481static void intel_enable_pipe_a(struct drm_device *dev)
10482{
10483 struct intel_connector *connector;
10484 struct drm_connector *crt = NULL;
10485 struct intel_load_detect_pipe load_detect_temp;
10486
10487 /* We can't just switch on the pipe A, we need to set things up with a
10488 * proper mode and output configuration. As a gross hack, enable pipe A
10489 * by enabling the load detect pipe once. */
10490 list_for_each_entry(connector,
10491 &dev->mode_config.connector_list,
10492 base.head) {
10493 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10494 crt = &connector->base;
10495 break;
10496 }
10497 }
10498
10499 if (!crt)
10500 return;
10501
10502 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10503 intel_release_load_detect_pipe(crt, &load_detect_temp);
10504
10505
10506}
10507
Daniel Vetterfa555832012-10-10 23:14:00 +020010508static bool
10509intel_check_plane_mapping(struct intel_crtc *crtc)
10510{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010511 struct drm_device *dev = crtc->base.dev;
10512 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010513 u32 reg, val;
10514
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010515 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010516 return true;
10517
10518 reg = DSPCNTR(!crtc->plane);
10519 val = I915_READ(reg);
10520
10521 if ((val & DISPLAY_PLANE_ENABLE) &&
10522 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10523 return false;
10524
10525 return true;
10526}
10527
Daniel Vetter24929352012-07-02 20:28:59 +020010528static void intel_sanitize_crtc(struct intel_crtc *crtc)
10529{
10530 struct drm_device *dev = crtc->base.dev;
10531 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010532 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010533
Daniel Vetter24929352012-07-02 20:28:59 +020010534 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010535 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010536 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10537
10538 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010539 * disable the crtc (and hence change the state) if it is wrong. Note
10540 * that gen4+ has a fixed plane -> pipe mapping. */
10541 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010542 struct intel_connector *connector;
10543 bool plane;
10544
Daniel Vetter24929352012-07-02 20:28:59 +020010545 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10546 crtc->base.base.id);
10547
10548 /* Pipe has the wrong plane attached and the plane is active.
10549 * Temporarily change the plane mapping and disable everything
10550 * ... */
10551 plane = crtc->plane;
10552 crtc->plane = !plane;
10553 dev_priv->display.crtc_disable(&crtc->base);
10554 crtc->plane = plane;
10555
10556 /* ... and break all links. */
10557 list_for_each_entry(connector, &dev->mode_config.connector_list,
10558 base.head) {
10559 if (connector->encoder->base.crtc != &crtc->base)
10560 continue;
10561
10562 intel_connector_break_all_links(connector);
10563 }
10564
10565 WARN_ON(crtc->active);
10566 crtc->base.enabled = false;
10567 }
Daniel Vetter24929352012-07-02 20:28:59 +020010568
Daniel Vetter7fad7982012-07-04 17:51:47 +020010569 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10570 crtc->pipe == PIPE_A && !crtc->active) {
10571 /* BIOS forgot to enable pipe A, this mostly happens after
10572 * resume. Force-enable the pipe to fix this, the update_dpms
10573 * call below we restore the pipe to the right state, but leave
10574 * the required bits on. */
10575 intel_enable_pipe_a(dev);
10576 }
10577
Daniel Vetter24929352012-07-02 20:28:59 +020010578 /* Adjust the state of the output pipe according to whether we
10579 * have active connectors/encoders. */
10580 intel_crtc_update_dpms(&crtc->base);
10581
10582 if (crtc->active != crtc->base.enabled) {
10583 struct intel_encoder *encoder;
10584
10585 /* This can happen either due to bugs in the get_hw_state
10586 * functions or because the pipe is force-enabled due to the
10587 * pipe A quirk. */
10588 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10589 crtc->base.base.id,
10590 crtc->base.enabled ? "enabled" : "disabled",
10591 crtc->active ? "enabled" : "disabled");
10592
10593 crtc->base.enabled = crtc->active;
10594
10595 /* Because we only establish the connector -> encoder ->
10596 * crtc links if something is active, this means the
10597 * crtc is now deactivated. Break the links. connector
10598 * -> encoder links are only establish when things are
10599 * actually up, hence no need to break them. */
10600 WARN_ON(crtc->active);
10601
10602 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10603 WARN_ON(encoder->connectors_active);
10604 encoder->base.crtc = NULL;
10605 }
10606 }
10607}
10608
10609static void intel_sanitize_encoder(struct intel_encoder *encoder)
10610{
10611 struct intel_connector *connector;
10612 struct drm_device *dev = encoder->base.dev;
10613
10614 /* We need to check both for a crtc link (meaning that the
10615 * encoder is active and trying to read from a pipe) and the
10616 * pipe itself being active. */
10617 bool has_active_crtc = encoder->base.crtc &&
10618 to_intel_crtc(encoder->base.crtc)->active;
10619
10620 if (encoder->connectors_active && !has_active_crtc) {
10621 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10622 encoder->base.base.id,
10623 drm_get_encoder_name(&encoder->base));
10624
10625 /* Connector is active, but has no active pipe. This is
10626 * fallout from our resume register restoring. Disable
10627 * the encoder manually again. */
10628 if (encoder->base.crtc) {
10629 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10630 encoder->base.base.id,
10631 drm_get_encoder_name(&encoder->base));
10632 encoder->disable(encoder);
10633 }
10634
10635 /* Inconsistent output/port/pipe state happens presumably due to
10636 * a bug in one of the get_hw_state functions. Or someplace else
10637 * in our code, like the register restore mess on resume. Clamp
10638 * things to off as a safer default. */
10639 list_for_each_entry(connector,
10640 &dev->mode_config.connector_list,
10641 base.head) {
10642 if (connector->encoder != encoder)
10643 continue;
10644
10645 intel_connector_break_all_links(connector);
10646 }
10647 }
10648 /* Enabled encoders without active connectors will be fixed in
10649 * the crtc fixup. */
10650}
10651
Daniel Vetter44cec742013-01-25 17:53:21 +010010652void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010653{
10654 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010655 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010656
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010657 /* This function can be called both from intel_modeset_setup_hw_state or
10658 * at a very early point in our resume sequence, where the power well
10659 * structures are not yet restored. Since this function is at a very
10660 * paranoid "someone might have enabled VGA while we were not looking"
10661 * level, just check if the power well is enabled instead of trying to
10662 * follow the "don't touch the power well if we don't need it" policy
10663 * the rest of the driver uses. */
10664 if (HAS_POWER_WELL(dev) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030010665 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010666 return;
10667
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010668 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10669 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020010670 i915_disable_vga(dev);
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010671 i915_disable_vga_mem(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010672 }
10673}
10674
Daniel Vetter30e984d2013-06-05 13:34:17 +020010675static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020010676{
10677 struct drm_i915_private *dev_priv = dev->dev_private;
10678 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020010679 struct intel_crtc *crtc;
10680 struct intel_encoder *encoder;
10681 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020010682 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020010683
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010684 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10685 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010010686 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020010687
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010688 crtc->active = dev_priv->display.get_pipe_config(crtc,
10689 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010690
10691 crtc->base.enabled = crtc->active;
10692
10693 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10694 crtc->base.base.id,
10695 crtc->active ? "enabled" : "disabled");
10696 }
10697
Daniel Vetter53589012013-06-05 13:34:16 +020010698 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010699 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010700 intel_ddi_setup_hw_pll_state(dev);
10701
Daniel Vetter53589012013-06-05 13:34:16 +020010702 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10703 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10704
10705 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10706 pll->active = 0;
10707 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10708 base.head) {
10709 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10710 pll->active++;
10711 }
10712 pll->refcount = pll->active;
10713
Daniel Vetter35c95372013-07-17 06:55:04 +020010714 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10715 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020010716 }
10717
Daniel Vetter24929352012-07-02 20:28:59 +020010718 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10719 base.head) {
10720 pipe = 0;
10721
10722 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010723 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10724 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070010725 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010726 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010727 } else {
10728 encoder->base.crtc = NULL;
10729 }
10730
10731 encoder->connectors_active = false;
10732 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10733 encoder->base.base.id,
10734 drm_get_encoder_name(&encoder->base),
10735 encoder->base.crtc ? "enabled" : "disabled",
10736 pipe);
10737 }
10738
10739 list_for_each_entry(connector, &dev->mode_config.connector_list,
10740 base.head) {
10741 if (connector->get_hw_state(connector)) {
10742 connector->base.dpms = DRM_MODE_DPMS_ON;
10743 connector->encoder->connectors_active = true;
10744 connector->base.encoder = &connector->encoder->base;
10745 } else {
10746 connector->base.dpms = DRM_MODE_DPMS_OFF;
10747 connector->base.encoder = NULL;
10748 }
10749 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10750 connector->base.base.id,
10751 drm_get_connector_name(&connector->base),
10752 connector->base.encoder ? "enabled" : "disabled");
10753 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010754}
10755
10756/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10757 * and i915 state tracking structures. */
10758void intel_modeset_setup_hw_state(struct drm_device *dev,
10759 bool force_restore)
10760{
10761 struct drm_i915_private *dev_priv = dev->dev_private;
10762 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010763 struct intel_crtc *crtc;
10764 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020010765 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010766
10767 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010768
Jesse Barnesbabea612013-06-26 18:57:38 +030010769 /*
10770 * Now that we have the config, copy it to each CRTC struct
10771 * Note that this could go away if we move to using crtc_config
10772 * checking everywhere.
10773 */
10774 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10775 base.head) {
10776 if (crtc->active && i915_fastboot) {
10777 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10778
10779 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10780 crtc->base.base.id);
10781 drm_mode_debug_printmodeline(&crtc->base.mode);
10782 }
10783 }
10784
Daniel Vetter24929352012-07-02 20:28:59 +020010785 /* HW state is read out, now we need to sanitize this mess. */
10786 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10787 base.head) {
10788 intel_sanitize_encoder(encoder);
10789 }
10790
10791 for_each_pipe(pipe) {
10792 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10793 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010794 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010795 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010796
Daniel Vetter35c95372013-07-17 06:55:04 +020010797 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10798 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10799
10800 if (!pll->on || pll->active)
10801 continue;
10802
10803 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10804
10805 pll->disable(dev_priv, pll);
10806 pll->on = false;
10807 }
10808
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010809 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030010810 i915_redisable_vga(dev);
10811
Daniel Vetterf30da182013-04-11 20:22:50 +020010812 /*
10813 * We need to use raw interfaces for restoring state to avoid
10814 * checking (bogus) intermediate states.
10815 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010816 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010817 struct drm_crtc *crtc =
10818 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010819
10820 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10821 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010822 }
10823 } else {
10824 intel_modeset_update_staged_output_state(dev);
10825 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010826
10827 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010828
10829 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010830}
10831
10832void intel_modeset_gem_init(struct drm_device *dev)
10833{
Chris Wilson1833b132012-05-09 11:56:28 +010010834 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010835
10836 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010837
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010838 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010839}
10840
10841void intel_modeset_cleanup(struct drm_device *dev)
10842{
Jesse Barnes652c3932009-08-17 13:31:43 -070010843 struct drm_i915_private *dev_priv = dev->dev_private;
10844 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030010845 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070010846
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010847 /*
10848 * Interrupts and polling as the first thing to avoid creating havoc.
10849 * Too much stuff here (turning of rps, connectors, ...) would
10850 * experience fancy races otherwise.
10851 */
10852 drm_irq_uninstall(dev);
10853 cancel_work_sync(&dev_priv->hotplug_work);
10854 /*
10855 * Due to the hpd irq storm handling the hotplug work can re-arm the
10856 * poll handlers. Hence disable polling after hpd handling is shut down.
10857 */
Keith Packardf87ea762010-10-03 19:36:26 -070010858 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010859
Jesse Barnes652c3932009-08-17 13:31:43 -070010860 mutex_lock(&dev->struct_mutex);
10861
Jesse Barnes723bfd72010-10-07 16:01:13 -070010862 intel_unregister_dsm_handler();
10863
Jesse Barnes652c3932009-08-17 13:31:43 -070010864 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10865 /* Skip inactive CRTCs */
10866 if (!crtc->fb)
10867 continue;
10868
Daniel Vetter3dec0092010-08-20 21:40:52 +020010869 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010870 }
10871
Chris Wilson973d04f2011-07-08 12:22:37 +010010872 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010873
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010874 i915_enable_vga_mem(dev);
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010875
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010876 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010877
Daniel Vetter930ebb42012-06-29 23:32:16 +020010878 ironlake_teardown_rc6(dev);
10879
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010880 mutex_unlock(&dev->struct_mutex);
10881
Chris Wilson1630fe72011-07-08 12:22:42 +010010882 /* flush any delayed tasks or pending work */
10883 flush_scheduled_work();
10884
Jani Nikuladc652f92013-04-12 15:18:38 +030010885 /* destroy backlight, if any, before the connectors */
10886 intel_panel_destroy_backlight(dev);
10887
Paulo Zanonid9255d52013-09-26 20:05:59 -030010888 /* destroy the sysfs files before encoders/connectors */
10889 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
10890 drm_sysfs_connector_remove(connector);
10891
Jesse Barnes79e53942008-11-07 14:24:08 -080010892 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010893
10894 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010895}
10896
Dave Airlie28d52042009-09-21 14:33:58 +100010897/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010898 * Return which encoder is currently attached for connector.
10899 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010900struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010901{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010902 return &intel_attached_encoder(connector)->base;
10903}
Jesse Barnes79e53942008-11-07 14:24:08 -080010904
Chris Wilsondf0e9242010-09-09 16:20:55 +010010905void intel_connector_attach_encoder(struct intel_connector *connector,
10906 struct intel_encoder *encoder)
10907{
10908 connector->encoder = encoder;
10909 drm_mode_connector_attach_encoder(&connector->base,
10910 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010911}
Dave Airlie28d52042009-09-21 14:33:58 +100010912
10913/*
10914 * set vga decode state - true == enable VGA decode
10915 */
10916int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10917{
10918 struct drm_i915_private *dev_priv = dev->dev_private;
10919 u16 gmch_ctrl;
10920
10921 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10922 if (state)
10923 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10924 else
10925 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10926 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10927 return 0;
10928}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010929
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010930struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010931
10932 u32 power_well_driver;
10933
Chris Wilson63b66e52013-08-08 15:12:06 +020010934 int num_transcoders;
10935
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010936 struct intel_cursor_error_state {
10937 u32 control;
10938 u32 position;
10939 u32 base;
10940 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010941 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010942
10943 struct intel_pipe_error_state {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010944 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010010945 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010946
10947 struct intel_plane_error_state {
10948 u32 control;
10949 u32 stride;
10950 u32 size;
10951 u32 pos;
10952 u32 addr;
10953 u32 surface;
10954 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010955 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020010956
10957 struct intel_transcoder_error_state {
10958 enum transcoder cpu_transcoder;
10959
10960 u32 conf;
10961
10962 u32 htotal;
10963 u32 hblank;
10964 u32 hsync;
10965 u32 vtotal;
10966 u32 vblank;
10967 u32 vsync;
10968 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010969};
10970
10971struct intel_display_error_state *
10972intel_display_capture_error_state(struct drm_device *dev)
10973{
Akshay Joshi0206e352011-08-16 15:34:10 -040010974 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010975 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020010976 int transcoders[] = {
10977 TRANSCODER_A,
10978 TRANSCODER_B,
10979 TRANSCODER_C,
10980 TRANSCODER_EDP,
10981 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010982 int i;
10983
Chris Wilson63b66e52013-08-08 15:12:06 +020010984 if (INTEL_INFO(dev)->num_pipes == 0)
10985 return NULL;
10986
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010987 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10988 if (error == NULL)
10989 return NULL;
10990
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010991 if (HAS_POWER_WELL(dev))
10992 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10993
Damien Lespiau52331302012-08-15 19:23:25 +010010994 for_each_pipe(i) {
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010995 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10996 error->cursor[i].control = I915_READ(CURCNTR(i));
10997 error->cursor[i].position = I915_READ(CURPOS(i));
10998 error->cursor[i].base = I915_READ(CURBASE(i));
10999 } else {
11000 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11001 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11002 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11003 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011004
11005 error->plane[i].control = I915_READ(DSPCNTR(i));
11006 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011007 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011008 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011009 error->plane[i].pos = I915_READ(DSPPOS(i));
11010 }
Paulo Zanonica291362013-03-06 20:03:14 -030011011 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11012 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011013 if (INTEL_INFO(dev)->gen >= 4) {
11014 error->plane[i].surface = I915_READ(DSPSURF(i));
11015 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11016 }
11017
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011018 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011019 }
11020
11021 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11022 if (HAS_DDI(dev_priv->dev))
11023 error->num_transcoders++; /* Account for eDP. */
11024
11025 for (i = 0; i < error->num_transcoders; i++) {
11026 enum transcoder cpu_transcoder = transcoders[i];
11027
11028 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11029
11030 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11031 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11032 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11033 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11034 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11035 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11036 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011037 }
11038
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011039 /* In the code above we read the registers without checking if the power
11040 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
11041 * prevent the next I915_WRITE from detecting it and printing an error
11042 * message. */
Chris Wilson907b28c2013-07-19 20:36:52 +010011043 intel_uncore_clear_errors(dev);
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011044
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011045 return error;
11046}
11047
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011048#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11049
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011050void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011051intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011052 struct drm_device *dev,
11053 struct intel_display_error_state *error)
11054{
11055 int i;
11056
Chris Wilson63b66e52013-08-08 15:12:06 +020011057 if (!error)
11058 return;
11059
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011060 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011061 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011062 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011063 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011064 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011065 err_printf(m, "Pipe [%d]:\n", i);
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011066 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011067
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011068 err_printf(m, "Plane [%d]:\n", i);
11069 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11070 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011071 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011072 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11073 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011074 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011075 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011076 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011077 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011078 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11079 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011080 }
11081
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011082 err_printf(m, "Cursor [%d]:\n", i);
11083 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11084 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11085 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011086 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011087
11088 for (i = 0; i < error->num_transcoders; i++) {
11089 err_printf(m, " CPU transcoder: %c\n",
11090 transcoder_name(error->transcoder[i].cpu_transcoder));
11091 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11092 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11093 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11094 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11095 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11096 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11097 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11098 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011099}