blob: 83739d2401ab3a77c5b8840457416f3773f764da [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Daniel Vetter3dec0092010-08-20 21:40:52 +020044static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010045static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080046
Jesse Barnesf1f644d2013-06-27 00:39:25 +030047static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030049static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030051
Damien Lespiaue7457a92013-08-08 22:28:59 +010052static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
54
55
Jesse Barnes79e53942008-11-07 14:24:08 -080056typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040057 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_range_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int dot_limit;
62 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080063} intel_p2_t;
64
Ma Lingd4906092009-03-18 20:13:27 +080065typedef struct intel_limit intel_limit_t;
66struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 intel_range_t dot, vco, n, m, m1, m2, p, p1;
68 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080069};
Jesse Barnes79e53942008-11-07 14:24:08 -080070
Daniel Vetterd2acd212012-10-20 20:57:43 +020071int
72intel_pch_rawclk(struct drm_device *dev)
73{
74 struct drm_i915_private *dev_priv = dev->dev_private;
75
76 WARN_ON(!HAS_PCH_SPLIT(dev));
77
78 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
79}
80
Chris Wilson021357a2010-09-07 20:54:59 +010081static inline u32 /* units of 100MHz */
82intel_fdi_link_freq(struct drm_device *dev)
83{
Chris Wilson8b99e682010-10-13 09:59:17 +010084 if (IS_GEN5(dev)) {
85 struct drm_i915_private *dev_priv = dev->dev_private;
86 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 } else
88 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010089}
90
Daniel Vetter5d536e22013-07-06 12:52:06 +020091static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040092 .dot = { .min = 25000, .max = 350000 },
93 .vco = { .min = 930000, .max = 1400000 },
94 .n = { .min = 3, .max = 16 },
95 .m = { .min = 96, .max = 140 },
96 .m1 = { .min = 18, .max = 26 },
97 .m2 = { .min = 6, .max = 16 },
98 .p = { .min = 4, .max = 128 },
99 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700100 .p2 = { .dot_limit = 165000,
101 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700102};
103
Daniel Vetter5d536e22013-07-06 12:52:06 +0200104static const intel_limit_t intel_limits_i8xx_dvo = {
105 .dot = { .min = 25000, .max = 350000 },
106 .vco = { .min = 930000, .max = 1400000 },
107 .n = { .min = 3, .max = 16 },
108 .m = { .min = 96, .max = 140 },
109 .m1 = { .min = 18, .max = 26 },
110 .m2 = { .min = 6, .max = 16 },
111 .p = { .min = 4, .max = 128 },
112 .p1 = { .min = 2, .max = 33 },
113 .p2 = { .dot_limit = 165000,
114 .p2_slow = 4, .p2_fast = 4 },
115};
116
Keith Packarde4b36692009-06-05 19:22:17 -0700117static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 .dot = { .min = 25000, .max = 350000 },
119 .vco = { .min = 930000, .max = 1400000 },
120 .n = { .min = 3, .max = 16 },
121 .m = { .min = 96, .max = 140 },
122 .m1 = { .min = 18, .max = 26 },
123 .m2 = { .min = 6, .max = 16 },
124 .p = { .min = 4, .max = 128 },
125 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700126 .p2 = { .dot_limit = 165000,
127 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700128};
Eric Anholt273e27c2011-03-30 13:01:10 -0700129
Keith Packarde4b36692009-06-05 19:22:17 -0700130static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 .dot = { .min = 20000, .max = 400000 },
132 .vco = { .min = 1400000, .max = 2800000 },
133 .n = { .min = 1, .max = 6 },
134 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100135 .m1 = { .min = 8, .max = 18 },
136 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .p = { .min = 5, .max = 80 },
138 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700139 .p2 = { .dot_limit = 200000,
140 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700141};
142
143static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400144 .dot = { .min = 20000, .max = 400000 },
145 .vco = { .min = 1400000, .max = 2800000 },
146 .n = { .min = 1, .max = 6 },
147 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100148 .m1 = { .min = 8, .max = 18 },
149 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .p = { .min = 7, .max = 98 },
151 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700152 .p2 = { .dot_limit = 112000,
153 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700154};
155
Eric Anholt273e27c2011-03-30 13:01:10 -0700156
Keith Packarde4b36692009-06-05 19:22:17 -0700157static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .dot = { .min = 25000, .max = 270000 },
159 .vco = { .min = 1750000, .max = 3500000},
160 .n = { .min = 1, .max = 4 },
161 .m = { .min = 104, .max = 138 },
162 .m1 = { .min = 17, .max = 23 },
163 .m2 = { .min = 5, .max = 11 },
164 .p = { .min = 10, .max = 30 },
165 .p1 = { .min = 1, .max = 3},
166 .p2 = { .dot_limit = 270000,
167 .p2_slow = 10,
168 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800169 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
172static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .dot = { .min = 22000, .max = 400000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 16, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8},
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 20000, .max = 115000 },
187 .vco = { .min = 1750000, .max = 3500000 },
188 .n = { .min = 1, .max = 3 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 28, .max = 112 },
193 .p1 = { .min = 2, .max = 8 },
194 .p2 = { .dot_limit = 0,
195 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800196 },
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
198
199static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 80000, .max = 224000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 14, .max = 42 },
207 .p1 = { .min = 2, .max = 6 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800210 },
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500213static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400214 .dot = { .min = 20000, .max = 400000},
215 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .n = { .min = 3, .max = 6 },
218 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .m1 = { .min = 0, .max = 0 },
221 .m2 = { .min = 0, .max = 254 },
222 .p = { .min = 5, .max = 80 },
223 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700224 .p2 = { .dot_limit = 200000,
225 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500228static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 20000, .max = 400000 },
230 .vco = { .min = 1700000, .max = 3500000 },
231 .n = { .min = 3, .max = 6 },
232 .m = { .min = 2, .max = 256 },
233 .m1 = { .min = 0, .max = 0 },
234 .m2 = { .min = 0, .max = 254 },
235 .p = { .min = 7, .max = 112 },
236 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700237 .p2 = { .dot_limit = 112000,
238 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
Eric Anholt273e27c2011-03-30 13:01:10 -0700241/* Ironlake / Sandybridge
242 *
243 * We calculate clock using (register_value + 2) for N/M1/M2, so here
244 * the range value for them is (actual_value - 2).
245 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800246static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 25000, .max = 350000 },
248 .vco = { .min = 1760000, .max = 3510000 },
249 .n = { .min = 1, .max = 5 },
250 .m = { .min = 79, .max = 127 },
251 .m1 = { .min = 12, .max = 22 },
252 .m2 = { .min = 5, .max = 9 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
255 .p2 = { .dot_limit = 225000,
256 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800259static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .dot = { .min = 25000, .max = 350000 },
261 .vco = { .min = 1760000, .max = 3510000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 79, .max = 118 },
264 .m1 = { .min = 12, .max = 22 },
265 .m2 = { .min = 5, .max = 9 },
266 .p = { .min = 28, .max = 112 },
267 .p1 = { .min = 2, .max = 8 },
268 .p2 = { .dot_limit = 225000,
269 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800270};
271
272static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .dot = { .min = 25000, .max = 350000 },
274 .vco = { .min = 1760000, .max = 3510000 },
275 .n = { .min = 1, .max = 3 },
276 .m = { .min = 79, .max = 127 },
277 .m1 = { .min = 12, .max = 22 },
278 .m2 = { .min = 5, .max = 9 },
279 .p = { .min = 14, .max = 56 },
280 .p1 = { .min = 2, .max = 8 },
281 .p2 = { .dot_limit = 225000,
282 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800286static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .dot = { .min = 25000, .max = 350000 },
288 .vco = { .min = 1760000, .max = 3510000 },
289 .n = { .min = 1, .max = 2 },
290 .m = { .min = 79, .max = 126 },
291 .m1 = { .min = 12, .max = 22 },
292 .m2 = { .min = 5, .max = 9 },
293 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .p2 = { .dot_limit = 225000,
296 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 126 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800310};
311
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700312static const intel_limit_t intel_limits_vlv_dac = {
313 .dot = { .min = 25000, .max = 270000 },
314 .vco = { .min = 4000000, .max = 6000000 },
315 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700316 .m1 = { .min = 2, .max = 3 },
317 .m2 = { .min = 11, .max = 156 },
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300318 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300319 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700320};
321
322static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200323 .dot = { .min = 25000, .max = 270000 },
324 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700325 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700326 .m1 = { .min = 2, .max = 3 },
327 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300328 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300329 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700330};
331
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300332static void vlv_clock(int refclk, intel_clock_t *clock)
333{
334 clock->m = clock->m1 * clock->m2;
335 clock->p = clock->p1 * clock->p2;
336 clock->vco = refclk * clock->m / clock->n;
337 clock->dot = clock->vco / clock->p;
338}
339
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300340/**
341 * Returns whether any output on the specified pipe is of the specified type
342 */
343static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
344{
345 struct drm_device *dev = crtc->dev;
346 struct intel_encoder *encoder;
347
348 for_each_encoder_on_crtc(dev, crtc, encoder)
349 if (encoder->type == type)
350 return true;
351
352 return false;
353}
354
Chris Wilson1b894b52010-12-14 20:04:54 +0000355static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
356 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800357{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800358 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800359 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360
361 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100362 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000363 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800364 limit = &intel_limits_ironlake_dual_lvds_100m;
365 else
366 limit = &intel_limits_ironlake_dual_lvds;
367 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000368 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800369 limit = &intel_limits_ironlake_single_lvds_100m;
370 else
371 limit = &intel_limits_ironlake_single_lvds;
372 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200373 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800374 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800375
376 return limit;
377}
378
Ma Ling044c7c42009-03-18 20:13:23 +0800379static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
380{
381 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800382 const intel_limit_t *limit;
383
384 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100385 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700386 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800387 else
Keith Packarde4b36692009-06-05 19:22:17 -0700388 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800389 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
390 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700391 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800392 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700393 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800394 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700395 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800396
397 return limit;
398}
399
Chris Wilson1b894b52010-12-14 20:04:54 +0000400static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800401{
402 struct drm_device *dev = crtc->dev;
403 const intel_limit_t *limit;
404
Eric Anholtbad720f2009-10-22 16:11:14 -0700405 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000406 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800407 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800408 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500409 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800410 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500411 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800412 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500413 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700414 } else if (IS_VALLEYVIEW(dev)) {
415 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
416 limit = &intel_limits_vlv_dac;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700417 else
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +0800418 limit = &intel_limits_vlv_hdmi;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100419 } else if (!IS_GEN2(dev)) {
420 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
421 limit = &intel_limits_i9xx_lvds;
422 else
423 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800424 } else {
425 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700426 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200427 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700428 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200429 else
430 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800431 }
432 return limit;
433}
434
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500435/* m1 is reserved as 0 in Pineview, n is a ring counter */
436static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800437{
Shaohua Li21778322009-02-23 15:19:16 +0800438 clock->m = clock->m2 + 2;
439 clock->p = clock->p1 * clock->p2;
440 clock->vco = refclk * clock->m / clock->n;
441 clock->dot = clock->vco / clock->p;
442}
443
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200444static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
445{
446 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
447}
448
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200449static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800450{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200451 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800452 clock->p = clock->p1 * clock->p2;
453 clock->vco = refclk * clock->m / (clock->n + 2);
454 clock->dot = clock->vco / clock->p;
455}
456
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800457#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800458/**
459 * Returns whether the given set of divisors are valid for a given refclk with
460 * the given connectors.
461 */
462
Chris Wilson1b894b52010-12-14 20:04:54 +0000463static bool intel_PLL_is_valid(struct drm_device *dev,
464 const intel_limit_t *limit,
465 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800466{
Jesse Barnes79e53942008-11-07 14:24:08 -0800467 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400468 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800469 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400470 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400472 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800473 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500475 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400476 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800477 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800479 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400480 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800481 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400482 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800483 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
484 * connector, etc., rather than just a single range.
485 */
486 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400487 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800488
489 return true;
490}
491
Ma Lingd4906092009-03-18 20:13:27 +0800492static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200493i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800494 int target, int refclk, intel_clock_t *match_clock,
495 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800496{
497 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800498 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800499 int err = target;
500
Daniel Vettera210b022012-11-26 17:22:08 +0100501 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800502 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100503 * For LVDS just rely on its current settings for dual-channel.
504 * We haven't figured out how to reliably set up different
505 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800506 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100507 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800508 clock.p2 = limit->p2.p2_fast;
509 else
510 clock.p2 = limit->p2.p2_slow;
511 } else {
512 if (target < limit->p2.dot_limit)
513 clock.p2 = limit->p2.p2_slow;
514 else
515 clock.p2 = limit->p2.p2_fast;
516 }
517
Akshay Joshi0206e352011-08-16 15:34:10 -0400518 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800519
Zhao Yakui42158662009-11-20 11:24:18 +0800520 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
521 clock.m1++) {
522 for (clock.m2 = limit->m2.min;
523 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200524 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800525 break;
526 for (clock.n = limit->n.min;
527 clock.n <= limit->n.max; clock.n++) {
528 for (clock.p1 = limit->p1.min;
529 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 int this_err;
531
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200532 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000533 if (!intel_PLL_is_valid(dev, limit,
534 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800535 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800536 if (match_clock &&
537 clock.p != match_clock->p)
538 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800539
540 this_err = abs(clock.dot - target);
541 if (this_err < err) {
542 *best_clock = clock;
543 err = this_err;
544 }
545 }
546 }
547 }
548 }
549
550 return (err != target);
551}
552
Ma Lingd4906092009-03-18 20:13:27 +0800553static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200554pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
555 int target, int refclk, intel_clock_t *match_clock,
556 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200557{
558 struct drm_device *dev = crtc->dev;
559 intel_clock_t clock;
560 int err = target;
561
562 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
563 /*
564 * For LVDS just rely on its current settings for dual-channel.
565 * We haven't figured out how to reliably set up different
566 * single/dual channel state, if we even can.
567 */
568 if (intel_is_dual_link_lvds(dev))
569 clock.p2 = limit->p2.p2_fast;
570 else
571 clock.p2 = limit->p2.p2_slow;
572 } else {
573 if (target < limit->p2.dot_limit)
574 clock.p2 = limit->p2.p2_slow;
575 else
576 clock.p2 = limit->p2.p2_fast;
577 }
578
579 memset(best_clock, 0, sizeof(*best_clock));
580
581 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
582 clock.m1++) {
583 for (clock.m2 = limit->m2.min;
584 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200585 for (clock.n = limit->n.min;
586 clock.n <= limit->n.max; clock.n++) {
587 for (clock.p1 = limit->p1.min;
588 clock.p1 <= limit->p1.max; clock.p1++) {
589 int this_err;
590
591 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800592 if (!intel_PLL_is_valid(dev, limit,
593 &clock))
594 continue;
595 if (match_clock &&
596 clock.p != match_clock->p)
597 continue;
598
599 this_err = abs(clock.dot - target);
600 if (this_err < err) {
601 *best_clock = clock;
602 err = this_err;
603 }
604 }
605 }
606 }
607 }
608
609 return (err != target);
610}
611
Ma Lingd4906092009-03-18 20:13:27 +0800612static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200613g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
614 int target, int refclk, intel_clock_t *match_clock,
615 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800616{
617 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800618 intel_clock_t clock;
619 int max_n;
620 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400621 /* approximately equals target * 0.00585 */
622 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800623 found = false;
624
625 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100626 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800627 clock.p2 = limit->p2.p2_fast;
628 else
629 clock.p2 = limit->p2.p2_slow;
630 } else {
631 if (target < limit->p2.dot_limit)
632 clock.p2 = limit->p2.p2_slow;
633 else
634 clock.p2 = limit->p2.p2_fast;
635 }
636
637 memset(best_clock, 0, sizeof(*best_clock));
638 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200639 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800640 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200641 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800642 for (clock.m1 = limit->m1.max;
643 clock.m1 >= limit->m1.min; clock.m1--) {
644 for (clock.m2 = limit->m2.max;
645 clock.m2 >= limit->m2.min; clock.m2--) {
646 for (clock.p1 = limit->p1.max;
647 clock.p1 >= limit->p1.min; clock.p1--) {
648 int this_err;
649
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200650 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000651 if (!intel_PLL_is_valid(dev, limit,
652 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800653 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000654
655 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800656 if (this_err < err_most) {
657 *best_clock = clock;
658 err_most = this_err;
659 max_n = clock.n;
660 found = true;
661 }
662 }
663 }
664 }
665 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800666 return found;
667}
Ma Lingd4906092009-03-18 20:13:27 +0800668
Zhenyu Wang2c072452009-06-05 15:38:42 +0800669static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200670vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
671 int target, int refclk, intel_clock_t *match_clock,
672 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700673{
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300674 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300675 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300676 /* min update 19.2 MHz */
677 int max_n = min(limit->n.max, refclk / 19200);
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700678
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300679 target *= 5; /* fast clock */
680
681 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700682
683 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300684 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300685 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300686 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300687 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300688 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700689 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300690 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300691 unsigned int ppm, diff;
692
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300693 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
694 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300695
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300696 vlv_clock(refclk, &clock);
697
698 if (clock.vco < limit->vco.min ||
699 clock.vco >= limit->vco.max)
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300700 continue;
701
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300702 diff = abs(clock.dot - target);
703 ppm = div_u64(1000000ULL * diff, target);
704
705 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300706 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300707 *best_clock = clock;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300708 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300709
Ville Syrjäläc6861222013-09-24 21:26:21 +0300710 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300711 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300712 *best_clock = clock;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700713 }
714 }
715 }
716 }
717 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700718
719 return true;
720}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700721
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300722bool intel_crtc_active(struct drm_crtc *crtc)
723{
724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
725
726 /* Be paranoid as we can arrive here with only partial
727 * state retrieved from the hardware during setup.
728 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100729 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300730 * as Haswell has gained clock readout/fastboot support.
731 *
732 * We can ditch the crtc->fb check as soon as we can
733 * properly reconstruct framebuffers.
734 */
735 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100736 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300737}
738
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200739enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
740 enum pipe pipe)
741{
742 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
744
Daniel Vetter3b117c82013-04-17 20:15:07 +0200745 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200746}
747
Paulo Zanonia928d532012-05-04 17:18:15 -0300748static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
749{
750 struct drm_i915_private *dev_priv = dev->dev_private;
751 u32 frame, frame_reg = PIPEFRAME(pipe);
752
753 frame = I915_READ(frame_reg);
754
755 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
756 DRM_DEBUG_KMS("vblank wait timed out\n");
757}
758
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700759/**
760 * intel_wait_for_vblank - wait for vblank on a given pipe
761 * @dev: drm device
762 * @pipe: pipe to wait for
763 *
764 * Wait for vblank to occur on a given pipe. Needed for various bits of
765 * mode setting code.
766 */
767void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800768{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700769 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800770 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700771
Paulo Zanonia928d532012-05-04 17:18:15 -0300772 if (INTEL_INFO(dev)->gen >= 5) {
773 ironlake_wait_for_vblank(dev, pipe);
774 return;
775 }
776
Chris Wilson300387c2010-09-05 20:25:43 +0100777 /* Clear existing vblank status. Note this will clear any other
778 * sticky status fields as well.
779 *
780 * This races with i915_driver_irq_handler() with the result
781 * that either function could miss a vblank event. Here it is not
782 * fatal, as we will either wait upon the next vblank interrupt or
783 * timeout. Generally speaking intel_wait_for_vblank() is only
784 * called during modeset at which time the GPU should be idle and
785 * should *not* be performing page flips and thus not waiting on
786 * vblanks...
787 * Currently, the result of us stealing a vblank from the irq
788 * handler is that a single frame will be skipped during swapbuffers.
789 */
790 I915_WRITE(pipestat_reg,
791 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
792
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700793 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100794 if (wait_for(I915_READ(pipestat_reg) &
795 PIPE_VBLANK_INTERRUPT_STATUS,
796 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700797 DRM_DEBUG_KMS("vblank wait timed out\n");
798}
799
Keith Packardab7ad7f2010-10-03 00:33:06 -0700800/*
801 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700802 * @dev: drm device
803 * @pipe: pipe to wait for
804 *
805 * After disabling a pipe, we can't wait for vblank in the usual way,
806 * spinning on the vblank interrupt status bit, since we won't actually
807 * see an interrupt when the pipe is disabled.
808 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700809 * On Gen4 and above:
810 * wait for the pipe register state bit to turn off
811 *
812 * Otherwise:
813 * wait for the display line value to settle (it usually
814 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100815 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700816 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100817void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700818{
819 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200820 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
821 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700822
Keith Packardab7ad7f2010-10-03 00:33:06 -0700823 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200824 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700825
Keith Packardab7ad7f2010-10-03 00:33:06 -0700826 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100827 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
828 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200829 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700830 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300831 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100832 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700833 unsigned long timeout = jiffies + msecs_to_jiffies(100);
834
Paulo Zanoni837ba002012-05-04 17:18:14 -0300835 if (IS_GEN2(dev))
836 line_mask = DSL_LINEMASK_GEN2;
837 else
838 line_mask = DSL_LINEMASK_GEN3;
839
Keith Packardab7ad7f2010-10-03 00:33:06 -0700840 /* Wait for the display line to settle */
841 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300842 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700843 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300844 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700845 time_after(timeout, jiffies));
846 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200847 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700848 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800849}
850
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000851/*
852 * ibx_digital_port_connected - is the specified port connected?
853 * @dev_priv: i915 private structure
854 * @port: the port to test
855 *
856 * Returns true if @port is connected, false otherwise.
857 */
858bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
859 struct intel_digital_port *port)
860{
861 u32 bit;
862
Damien Lespiauc36346e2012-12-13 16:09:03 +0000863 if (HAS_PCH_IBX(dev_priv->dev)) {
864 switch(port->port) {
865 case PORT_B:
866 bit = SDE_PORTB_HOTPLUG;
867 break;
868 case PORT_C:
869 bit = SDE_PORTC_HOTPLUG;
870 break;
871 case PORT_D:
872 bit = SDE_PORTD_HOTPLUG;
873 break;
874 default:
875 return true;
876 }
877 } else {
878 switch(port->port) {
879 case PORT_B:
880 bit = SDE_PORTB_HOTPLUG_CPT;
881 break;
882 case PORT_C:
883 bit = SDE_PORTC_HOTPLUG_CPT;
884 break;
885 case PORT_D:
886 bit = SDE_PORTD_HOTPLUG_CPT;
887 break;
888 default:
889 return true;
890 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000891 }
892
893 return I915_READ(SDEISR) & bit;
894}
895
Jesse Barnesb24e7172011-01-04 15:09:30 -0800896static const char *state_string(bool enabled)
897{
898 return enabled ? "on" : "off";
899}
900
901/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200902void assert_pll(struct drm_i915_private *dev_priv,
903 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800904{
905 int reg;
906 u32 val;
907 bool cur_state;
908
909 reg = DPLL(pipe);
910 val = I915_READ(reg);
911 cur_state = !!(val & DPLL_VCO_ENABLE);
912 WARN(cur_state != state,
913 "PLL state assertion failure (expected %s, current %s)\n",
914 state_string(state), state_string(cur_state));
915}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800916
Jani Nikula23538ef2013-08-27 15:12:22 +0300917/* XXX: the dsi pll is shared between MIPI DSI ports */
918static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
919{
920 u32 val;
921 bool cur_state;
922
923 mutex_lock(&dev_priv->dpio_lock);
924 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
925 mutex_unlock(&dev_priv->dpio_lock);
926
927 cur_state = val & DSI_PLL_VCO_EN;
928 WARN(cur_state != state,
929 "DSI PLL state assertion failure (expected %s, current %s)\n",
930 state_string(state), state_string(cur_state));
931}
932#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
933#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
934
Daniel Vetter55607e82013-06-16 21:42:39 +0200935struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200936intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800937{
Daniel Vettere2b78262013-06-07 23:10:03 +0200938 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
939
Daniel Vettera43f6e02013-06-07 23:10:32 +0200940 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200941 return NULL;
942
Daniel Vettera43f6e02013-06-07 23:10:32 +0200943 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200944}
945
Jesse Barnesb24e7172011-01-04 15:09:30 -0800946/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200947void assert_shared_dpll(struct drm_i915_private *dev_priv,
948 struct intel_shared_dpll *pll,
949 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800950{
Jesse Barnes040484a2011-01-03 12:14:26 -0800951 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200952 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800953
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300954 if (HAS_PCH_LPT(dev_priv->dev)) {
955 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
956 return;
957 }
958
Chris Wilson92b27b02012-05-20 18:10:50 +0100959 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200960 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100961 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100962
Daniel Vetter53589012013-06-05 13:34:16 +0200963 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100964 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200965 "%s assertion failure (expected %s, current %s)\n",
966 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800967}
Jesse Barnes040484a2011-01-03 12:14:26 -0800968
969static void assert_fdi_tx(struct drm_i915_private *dev_priv,
970 enum pipe pipe, bool state)
971{
972 int reg;
973 u32 val;
974 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200975 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
976 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800977
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200978 if (HAS_DDI(dev_priv->dev)) {
979 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200980 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300981 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200982 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300983 } else {
984 reg = FDI_TX_CTL(pipe);
985 val = I915_READ(reg);
986 cur_state = !!(val & FDI_TX_ENABLE);
987 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800988 WARN(cur_state != state,
989 "FDI TX state assertion failure (expected %s, current %s)\n",
990 state_string(state), state_string(cur_state));
991}
992#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
993#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
994
995static void assert_fdi_rx(struct drm_i915_private *dev_priv,
996 enum pipe pipe, bool state)
997{
998 int reg;
999 u32 val;
1000 bool cur_state;
1001
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001002 reg = FDI_RX_CTL(pipe);
1003 val = I915_READ(reg);
1004 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001005 WARN(cur_state != state,
1006 "FDI RX state assertion failure (expected %s, current %s)\n",
1007 state_string(state), state_string(cur_state));
1008}
1009#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1010#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1011
1012static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1013 enum pipe pipe)
1014{
1015 int reg;
1016 u32 val;
1017
1018 /* ILK FDI PLL is always enabled */
1019 if (dev_priv->info->gen == 5)
1020 return;
1021
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001022 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001023 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001024 return;
1025
Jesse Barnes040484a2011-01-03 12:14:26 -08001026 reg = FDI_TX_CTL(pipe);
1027 val = I915_READ(reg);
1028 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1029}
1030
Daniel Vetter55607e82013-06-16 21:42:39 +02001031void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1032 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001033{
1034 int reg;
1035 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001036 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001037
1038 reg = FDI_RX_CTL(pipe);
1039 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001040 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1041 WARN(cur_state != state,
1042 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1043 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001044}
1045
Jesse Barnesea0760c2011-01-04 15:09:32 -08001046static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1047 enum pipe pipe)
1048{
1049 int pp_reg, lvds_reg;
1050 u32 val;
1051 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001052 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001053
1054 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1055 pp_reg = PCH_PP_CONTROL;
1056 lvds_reg = PCH_LVDS;
1057 } else {
1058 pp_reg = PP_CONTROL;
1059 lvds_reg = LVDS;
1060 }
1061
1062 val = I915_READ(pp_reg);
1063 if (!(val & PANEL_POWER_ON) ||
1064 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1065 locked = false;
1066
1067 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1068 panel_pipe = PIPE_B;
1069
1070 WARN(panel_pipe == pipe && locked,
1071 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001072 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001073}
1074
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001075static void assert_cursor(struct drm_i915_private *dev_priv,
1076 enum pipe pipe, bool state)
1077{
1078 struct drm_device *dev = dev_priv->dev;
1079 bool cur_state;
1080
1081 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1082 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1083 else if (IS_845G(dev) || IS_I865G(dev))
1084 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1085 else
1086 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1087
1088 WARN(cur_state != state,
1089 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1090 pipe_name(pipe), state_string(state), state_string(cur_state));
1091}
1092#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1093#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1094
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001095void assert_pipe(struct drm_i915_private *dev_priv,
1096 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097{
1098 int reg;
1099 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001100 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001101 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1102 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103
Daniel Vetter8e636782012-01-22 01:36:48 +01001104 /* if we need the pipe A quirk it must be always on */
1105 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1106 state = true;
1107
Paulo Zanonib97186f2013-05-03 12:15:36 -03001108 if (!intel_display_power_enabled(dev_priv->dev,
1109 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001110 cur_state = false;
1111 } else {
1112 reg = PIPECONF(cpu_transcoder);
1113 val = I915_READ(reg);
1114 cur_state = !!(val & PIPECONF_ENABLE);
1115 }
1116
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001117 WARN(cur_state != state,
1118 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001119 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001120}
1121
Chris Wilson931872f2012-01-16 23:01:13 +00001122static void assert_plane(struct drm_i915_private *dev_priv,
1123 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001124{
1125 int reg;
1126 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001127 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001128
1129 reg = DSPCNTR(plane);
1130 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001131 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1132 WARN(cur_state != state,
1133 "plane %c assertion failure (expected %s, current %s)\n",
1134 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135}
1136
Chris Wilson931872f2012-01-16 23:01:13 +00001137#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1138#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1139
Jesse Barnesb24e7172011-01-04 15:09:30 -08001140static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1141 enum pipe pipe)
1142{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001143 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001144 int reg, i;
1145 u32 val;
1146 int cur_pipe;
1147
Ville Syrjälä653e1022013-06-04 13:49:05 +03001148 /* Primary planes are fixed to pipes on gen4+ */
1149 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001150 reg = DSPCNTR(pipe);
1151 val = I915_READ(reg);
1152 WARN((val & DISPLAY_PLANE_ENABLE),
1153 "plane %c assertion failure, should be disabled but not\n",
1154 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001155 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001156 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001157
Jesse Barnesb24e7172011-01-04 15:09:30 -08001158 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001159 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001160 reg = DSPCNTR(i);
1161 val = I915_READ(reg);
1162 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1163 DISPPLANE_SEL_PIPE_SHIFT;
1164 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001165 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1166 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001167 }
1168}
1169
Jesse Barnes19332d72013-03-28 09:55:38 -07001170static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1171 enum pipe pipe)
1172{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001173 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001174 int reg, i;
1175 u32 val;
1176
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001177 if (IS_VALLEYVIEW(dev)) {
1178 for (i = 0; i < dev_priv->num_plane; i++) {
1179 reg = SPCNTR(pipe, i);
1180 val = I915_READ(reg);
1181 WARN((val & SP_ENABLE),
1182 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1183 sprite_name(pipe, i), pipe_name(pipe));
1184 }
1185 } else if (INTEL_INFO(dev)->gen >= 7) {
1186 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001187 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001188 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001189 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001190 plane_name(pipe), pipe_name(pipe));
1191 } else if (INTEL_INFO(dev)->gen >= 5) {
1192 reg = DVSCNTR(pipe);
1193 val = I915_READ(reg);
1194 WARN((val & DVS_ENABLE),
1195 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1196 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001197 }
1198}
1199
Jesse Barnes92f25842011-01-04 15:09:34 -08001200static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1201{
1202 u32 val;
1203 bool enabled;
1204
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001205 if (HAS_PCH_LPT(dev_priv->dev)) {
1206 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1207 return;
1208 }
1209
Jesse Barnes92f25842011-01-04 15:09:34 -08001210 val = I915_READ(PCH_DREF_CONTROL);
1211 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1212 DREF_SUPERSPREAD_SOURCE_MASK));
1213 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1214}
1215
Daniel Vetterab9412b2013-05-03 11:49:46 +02001216static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1217 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001218{
1219 int reg;
1220 u32 val;
1221 bool enabled;
1222
Daniel Vetterab9412b2013-05-03 11:49:46 +02001223 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001224 val = I915_READ(reg);
1225 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001226 WARN(enabled,
1227 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1228 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001229}
1230
Keith Packard4e634382011-08-06 10:39:45 -07001231static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001233{
1234 if ((val & DP_PORT_EN) == 0)
1235 return false;
1236
1237 if (HAS_PCH_CPT(dev_priv->dev)) {
1238 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1239 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1240 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1241 return false;
1242 } else {
1243 if ((val & DP_PIPE_MASK) != (pipe << 30))
1244 return false;
1245 }
1246 return true;
1247}
1248
Keith Packard1519b992011-08-06 10:35:34 -07001249static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, u32 val)
1251{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001252 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001253 return false;
1254
1255 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001256 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001257 return false;
1258 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001259 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001260 return false;
1261 }
1262 return true;
1263}
1264
1265static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1266 enum pipe pipe, u32 val)
1267{
1268 if ((val & LVDS_PORT_EN) == 0)
1269 return false;
1270
1271 if (HAS_PCH_CPT(dev_priv->dev)) {
1272 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1273 return false;
1274 } else {
1275 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1276 return false;
1277 }
1278 return true;
1279}
1280
1281static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1282 enum pipe pipe, u32 val)
1283{
1284 if ((val & ADPA_DAC_ENABLE) == 0)
1285 return false;
1286 if (HAS_PCH_CPT(dev_priv->dev)) {
1287 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1288 return false;
1289 } else {
1290 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1291 return false;
1292 }
1293 return true;
1294}
1295
Jesse Barnes291906f2011-02-02 12:28:03 -08001296static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001297 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001298{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001299 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001300 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001301 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001302 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001303
Daniel Vetter75c5da22012-09-10 21:58:29 +02001304 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1305 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001306 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001307}
1308
1309static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1310 enum pipe pipe, int reg)
1311{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001312 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001313 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001314 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001315 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001316
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001317 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001318 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001319 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001320}
1321
1322static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1323 enum pipe pipe)
1324{
1325 int reg;
1326 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001327
Keith Packardf0575e92011-07-25 22:12:43 -07001328 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1329 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1330 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001331
1332 reg = PCH_ADPA;
1333 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001334 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001335 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001336 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001337
1338 reg = PCH_LVDS;
1339 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001340 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001341 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001342 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001343
Paulo Zanonie2debe92013-02-18 19:00:27 -03001344 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1345 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1346 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001347}
1348
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001349static void intel_init_dpio(struct drm_device *dev)
1350{
1351 struct drm_i915_private *dev_priv = dev->dev_private;
1352
1353 if (!IS_VALLEYVIEW(dev))
1354 return;
1355
1356 /*
1357 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1358 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1359 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1360 * b. The other bits such as sfr settings / modesel may all be set
1361 * to 0.
1362 *
1363 * This should only be done on init and resume from S3 with both
1364 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1365 */
1366 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1367}
1368
Daniel Vetter426115c2013-07-11 22:13:42 +02001369static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001370{
Daniel Vetter426115c2013-07-11 22:13:42 +02001371 struct drm_device *dev = crtc->base.dev;
1372 struct drm_i915_private *dev_priv = dev->dev_private;
1373 int reg = DPLL(crtc->pipe);
1374 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001375
Daniel Vetter426115c2013-07-11 22:13:42 +02001376 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001377
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001378 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001379 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1380
1381 /* PLL is protected by panel, make sure we can write it */
1382 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001383 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001384
Daniel Vetter426115c2013-07-11 22:13:42 +02001385 I915_WRITE(reg, dpll);
1386 POSTING_READ(reg);
1387 udelay(150);
1388
1389 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1390 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1391
1392 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1393 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001394
1395 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001396 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001397 POSTING_READ(reg);
1398 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001399 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001400 POSTING_READ(reg);
1401 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001402 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001403 POSTING_READ(reg);
1404 udelay(150); /* wait for warmup */
1405}
1406
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001407static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001408{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001409 struct drm_device *dev = crtc->base.dev;
1410 struct drm_i915_private *dev_priv = dev->dev_private;
1411 int reg = DPLL(crtc->pipe);
1412 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001413
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001414 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001415
1416 /* No really, not for ILK+ */
1417 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001418
1419 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001420 if (IS_MOBILE(dev) && !IS_I830(dev))
1421 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001422
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001423 I915_WRITE(reg, dpll);
1424
1425 /* Wait for the clocks to stabilize. */
1426 POSTING_READ(reg);
1427 udelay(150);
1428
1429 if (INTEL_INFO(dev)->gen >= 4) {
1430 I915_WRITE(DPLL_MD(crtc->pipe),
1431 crtc->config.dpll_hw_state.dpll_md);
1432 } else {
1433 /* The pixel multiplier can only be updated once the
1434 * DPLL is enabled and the clocks are stable.
1435 *
1436 * So write it again.
1437 */
1438 I915_WRITE(reg, dpll);
1439 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001440
1441 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001442 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001443 POSTING_READ(reg);
1444 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001445 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001446 POSTING_READ(reg);
1447 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001448 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001449 POSTING_READ(reg);
1450 udelay(150); /* wait for warmup */
1451}
1452
1453/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001454 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001455 * @dev_priv: i915 private structure
1456 * @pipe: pipe PLL to disable
1457 *
1458 * Disable the PLL for @pipe, making sure the pipe is off first.
1459 *
1460 * Note! This is for pre-ILK only.
1461 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001462static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001463{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001464 /* Don't disable pipe A or pipe A PLLs if needed */
1465 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1466 return;
1467
1468 /* Make sure the pipe isn't still relying on us */
1469 assert_pipe_disabled(dev_priv, pipe);
1470
Daniel Vetter50b44a42013-06-05 13:34:33 +02001471 I915_WRITE(DPLL(pipe), 0);
1472 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001473}
1474
Jesse Barnesf6071162013-10-01 10:41:38 -07001475static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1476{
1477 u32 val = 0;
1478
1479 /* Make sure the pipe isn't still relying on us */
1480 assert_pipe_disabled(dev_priv, pipe);
1481
1482 /* Leave integrated clock source enabled */
1483 if (pipe == PIPE_B)
1484 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1485 I915_WRITE(DPLL(pipe), val);
1486 POSTING_READ(DPLL(pipe));
1487}
1488
Jesse Barnes89b667f2013-04-18 14:51:36 -07001489void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1490{
1491 u32 port_mask;
1492
1493 if (!port)
1494 port_mask = DPLL_PORTB_READY_MASK;
1495 else
1496 port_mask = DPLL_PORTC_READY_MASK;
1497
1498 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1499 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1500 'B' + port, I915_READ(DPLL(0)));
1501}
1502
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001503/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001504 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001505 * @dev_priv: i915 private structure
1506 * @pipe: pipe PLL to enable
1507 *
1508 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1509 * drives the transcoder clock.
1510 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001511static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001512{
Daniel Vettere2b78262013-06-07 23:10:03 +02001513 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1514 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001515
Chris Wilson48da64a2012-05-13 20:16:12 +01001516 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001517 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001518 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001519 return;
1520
1521 if (WARN_ON(pll->refcount == 0))
1522 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001523
Daniel Vetter46edb022013-06-05 13:34:12 +02001524 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1525 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001526 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001527
Daniel Vettercdbd2312013-06-05 13:34:03 +02001528 if (pll->active++) {
1529 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001530 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001531 return;
1532 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001533 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001534
Daniel Vetter46edb022013-06-05 13:34:12 +02001535 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001536 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001537 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001538}
1539
Daniel Vettere2b78262013-06-07 23:10:03 +02001540static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001541{
Daniel Vettere2b78262013-06-07 23:10:03 +02001542 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1543 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001544
Jesse Barnes92f25842011-01-04 15:09:34 -08001545 /* PCH only available on ILK+ */
1546 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001547 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001548 return;
1549
Chris Wilson48da64a2012-05-13 20:16:12 +01001550 if (WARN_ON(pll->refcount == 0))
1551 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001552
Daniel Vetter46edb022013-06-05 13:34:12 +02001553 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1554 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001555 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001556
Chris Wilson48da64a2012-05-13 20:16:12 +01001557 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001558 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001559 return;
1560 }
1561
Daniel Vettere9d69442013-06-05 13:34:15 +02001562 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001563 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001564 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001565 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001566
Daniel Vetter46edb022013-06-05 13:34:12 +02001567 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001568 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001569 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001570}
1571
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001572static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1573 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001574{
Daniel Vetter23670b322012-11-01 09:15:30 +01001575 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001576 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001578 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001579
1580 /* PCH only available on ILK+ */
1581 BUG_ON(dev_priv->info->gen < 5);
1582
1583 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001584 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001585 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001586
1587 /* FDI must be feeding us bits for PCH ports */
1588 assert_fdi_tx_enabled(dev_priv, pipe);
1589 assert_fdi_rx_enabled(dev_priv, pipe);
1590
Daniel Vetter23670b322012-11-01 09:15:30 +01001591 if (HAS_PCH_CPT(dev)) {
1592 /* Workaround: Set the timing override bit before enabling the
1593 * pch transcoder. */
1594 reg = TRANS_CHICKEN2(pipe);
1595 val = I915_READ(reg);
1596 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1597 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001598 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001599
Daniel Vetterab9412b2013-05-03 11:49:46 +02001600 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001601 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001602 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001603
1604 if (HAS_PCH_IBX(dev_priv->dev)) {
1605 /*
1606 * make the BPC in transcoder be consistent with
1607 * that in pipeconf reg.
1608 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001609 val &= ~PIPECONF_BPC_MASK;
1610 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001611 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001612
1613 val &= ~TRANS_INTERLACE_MASK;
1614 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001615 if (HAS_PCH_IBX(dev_priv->dev) &&
1616 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1617 val |= TRANS_LEGACY_INTERLACED_ILK;
1618 else
1619 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001620 else
1621 val |= TRANS_PROGRESSIVE;
1622
Jesse Barnes040484a2011-01-03 12:14:26 -08001623 I915_WRITE(reg, val | TRANS_ENABLE);
1624 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001625 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001626}
1627
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001628static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001629 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001630{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001631 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001632
1633 /* PCH only available on ILK+ */
1634 BUG_ON(dev_priv->info->gen < 5);
1635
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001636 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001637 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001638 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001639
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001640 /* Workaround: set timing override bit. */
1641 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001642 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001643 I915_WRITE(_TRANSA_CHICKEN2, val);
1644
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001645 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001646 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001647
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001648 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1649 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001650 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001651 else
1652 val |= TRANS_PROGRESSIVE;
1653
Daniel Vetterab9412b2013-05-03 11:49:46 +02001654 I915_WRITE(LPT_TRANSCONF, val);
1655 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001656 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001657}
1658
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001659static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1660 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001661{
Daniel Vetter23670b322012-11-01 09:15:30 +01001662 struct drm_device *dev = dev_priv->dev;
1663 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001664
1665 /* FDI relies on the transcoder */
1666 assert_fdi_tx_disabled(dev_priv, pipe);
1667 assert_fdi_rx_disabled(dev_priv, pipe);
1668
Jesse Barnes291906f2011-02-02 12:28:03 -08001669 /* Ports must be off as well */
1670 assert_pch_ports_disabled(dev_priv, pipe);
1671
Daniel Vetterab9412b2013-05-03 11:49:46 +02001672 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001673 val = I915_READ(reg);
1674 val &= ~TRANS_ENABLE;
1675 I915_WRITE(reg, val);
1676 /* wait for PCH transcoder off, transcoder state */
1677 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001678 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001679
1680 if (!HAS_PCH_IBX(dev)) {
1681 /* Workaround: Clear the timing override chicken bit again. */
1682 reg = TRANS_CHICKEN2(pipe);
1683 val = I915_READ(reg);
1684 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1685 I915_WRITE(reg, val);
1686 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001687}
1688
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001689static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001690{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001691 u32 val;
1692
Daniel Vetterab9412b2013-05-03 11:49:46 +02001693 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001694 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001695 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001696 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001697 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001698 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001699
1700 /* Workaround: clear timing override bit. */
1701 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001702 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001703 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001704}
1705
1706/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001707 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001708 * @dev_priv: i915 private structure
1709 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001710 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001711 *
1712 * Enable @pipe, making sure that various hardware specific requirements
1713 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1714 *
1715 * @pipe should be %PIPE_A or %PIPE_B.
1716 *
1717 * Will wait until the pipe is actually running (i.e. first vblank) before
1718 * returning.
1719 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001720static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001721 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001722{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001723 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1724 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001725 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001726 int reg;
1727 u32 val;
1728
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001729 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001730 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001731 assert_sprites_disabled(dev_priv, pipe);
1732
Paulo Zanoni681e5812012-12-06 11:12:38 -02001733 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001734 pch_transcoder = TRANSCODER_A;
1735 else
1736 pch_transcoder = pipe;
1737
Jesse Barnesb24e7172011-01-04 15:09:30 -08001738 /*
1739 * A pipe without a PLL won't actually be able to drive bits from
1740 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1741 * need the check.
1742 */
1743 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001744 if (dsi)
1745 assert_dsi_pll_enabled(dev_priv);
1746 else
1747 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001748 else {
1749 if (pch_port) {
1750 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001751 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001752 assert_fdi_tx_pll_enabled(dev_priv,
1753 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001754 }
1755 /* FIXME: assert CPU port conditions for SNB+ */
1756 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001757
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001758 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001759 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001760 if (val & PIPECONF_ENABLE)
1761 return;
1762
1763 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001764 intel_wait_for_vblank(dev_priv->dev, pipe);
1765}
1766
1767/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001768 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001769 * @dev_priv: i915 private structure
1770 * @pipe: pipe to disable
1771 *
1772 * Disable @pipe, making sure that various hardware specific requirements
1773 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1774 *
1775 * @pipe should be %PIPE_A or %PIPE_B.
1776 *
1777 * Will wait until the pipe has shut down before returning.
1778 */
1779static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1780 enum pipe pipe)
1781{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001782 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1783 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001784 int reg;
1785 u32 val;
1786
1787 /*
1788 * Make sure planes won't keep trying to pump pixels to us,
1789 * or we might hang the display.
1790 */
1791 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001792 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001793 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001794
1795 /* Don't disable pipe A or pipe A PLLs if needed */
1796 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1797 return;
1798
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001799 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001800 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001801 if ((val & PIPECONF_ENABLE) == 0)
1802 return;
1803
1804 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001805 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1806}
1807
Keith Packardd74362c2011-07-28 14:47:14 -07001808/*
1809 * Plane regs are double buffered, going from enabled->disabled needs a
1810 * trigger in order to latch. The display address reg provides this.
1811 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001812void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001813 enum plane plane)
1814{
Damien Lespiau14f86142012-10-29 15:24:49 +00001815 if (dev_priv->info->gen >= 4)
1816 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1817 else
1818 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001819}
1820
Jesse Barnesb24e7172011-01-04 15:09:30 -08001821/**
1822 * intel_enable_plane - enable a display plane on a given pipe
1823 * @dev_priv: i915 private structure
1824 * @plane: plane to enable
1825 * @pipe: pipe being fed
1826 *
1827 * Enable @plane on @pipe, making sure that @pipe is running first.
1828 */
1829static void intel_enable_plane(struct drm_i915_private *dev_priv,
1830 enum plane plane, enum pipe pipe)
1831{
1832 int reg;
1833 u32 val;
1834
1835 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1836 assert_pipe_enabled(dev_priv, pipe);
1837
1838 reg = DSPCNTR(plane);
1839 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001840 if (val & DISPLAY_PLANE_ENABLE)
1841 return;
1842
1843 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001844 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001845 intel_wait_for_vblank(dev_priv->dev, pipe);
1846}
1847
Jesse Barnesb24e7172011-01-04 15:09:30 -08001848/**
1849 * intel_disable_plane - disable a display plane
1850 * @dev_priv: i915 private structure
1851 * @plane: plane to disable
1852 * @pipe: pipe consuming the data
1853 *
1854 * Disable @plane; should be an independent operation.
1855 */
1856static void intel_disable_plane(struct drm_i915_private *dev_priv,
1857 enum plane plane, enum pipe pipe)
1858{
1859 int reg;
1860 u32 val;
1861
1862 reg = DSPCNTR(plane);
1863 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001864 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1865 return;
1866
1867 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001868 intel_flush_display_plane(dev_priv, plane);
1869 intel_wait_for_vblank(dev_priv->dev, pipe);
1870}
1871
Chris Wilson693db182013-03-05 14:52:39 +00001872static bool need_vtd_wa(struct drm_device *dev)
1873{
1874#ifdef CONFIG_INTEL_IOMMU
1875 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1876 return true;
1877#endif
1878 return false;
1879}
1880
Chris Wilson127bd2a2010-07-23 23:32:05 +01001881int
Chris Wilson48b956c2010-09-14 12:50:34 +01001882intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001883 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001884 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001885{
Chris Wilsonce453d82011-02-21 14:43:56 +00001886 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001887 u32 alignment;
1888 int ret;
1889
Chris Wilson05394f32010-11-08 19:18:58 +00001890 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001891 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001892 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1893 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001894 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001895 alignment = 4 * 1024;
1896 else
1897 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001898 break;
1899 case I915_TILING_X:
1900 /* pin() will align the object as required by fence */
1901 alignment = 0;
1902 break;
1903 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001904 /* Despite that we check this in framebuffer_init userspace can
1905 * screw us over and change the tiling after the fact. Only
1906 * pinned buffers can't change their tiling. */
1907 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001908 return -EINVAL;
1909 default:
1910 BUG();
1911 }
1912
Chris Wilson693db182013-03-05 14:52:39 +00001913 /* Note that the w/a also requires 64 PTE of padding following the
1914 * bo. We currently fill all unused PTE with the shadow page and so
1915 * we should always have valid PTE following the scanout preventing
1916 * the VT-d warning.
1917 */
1918 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1919 alignment = 256 * 1024;
1920
Chris Wilsonce453d82011-02-21 14:43:56 +00001921 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001922 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001923 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001924 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001925
1926 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1927 * fence, whereas 965+ only requires a fence if using
1928 * framebuffer compression. For simplicity, we always install
1929 * a fence as the cost is not that onerous.
1930 */
Chris Wilson06d98132012-04-17 15:31:24 +01001931 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001932 if (ret)
1933 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001934
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001935 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001936
Chris Wilsonce453d82011-02-21 14:43:56 +00001937 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001938 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001939
1940err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001941 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001942err_interruptible:
1943 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001944 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001945}
1946
Chris Wilson1690e1e2011-12-14 13:57:08 +01001947void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1948{
1949 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001950 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001951}
1952
Daniel Vetterc2c75132012-07-05 12:17:30 +02001953/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1954 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001955unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1956 unsigned int tiling_mode,
1957 unsigned int cpp,
1958 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001959{
Chris Wilsonbc752862013-02-21 20:04:31 +00001960 if (tiling_mode != I915_TILING_NONE) {
1961 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001962
Chris Wilsonbc752862013-02-21 20:04:31 +00001963 tile_rows = *y / 8;
1964 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001965
Chris Wilsonbc752862013-02-21 20:04:31 +00001966 tiles = *x / (512/cpp);
1967 *x %= 512/cpp;
1968
1969 return tile_rows * pitch * 8 + tiles * 4096;
1970 } else {
1971 unsigned int offset;
1972
1973 offset = *y * pitch + *x * cpp;
1974 *y = 0;
1975 *x = (offset & 4095) / cpp;
1976 return offset & -4096;
1977 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001978}
1979
Jesse Barnes17638cd2011-06-24 12:19:23 -07001980static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1981 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001982{
1983 struct drm_device *dev = crtc->dev;
1984 struct drm_i915_private *dev_priv = dev->dev_private;
1985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1986 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001987 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001988 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001989 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001990 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001991 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001992
1993 switch (plane) {
1994 case 0:
1995 case 1:
1996 break;
1997 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001998 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001999 return -EINVAL;
2000 }
2001
2002 intel_fb = to_intel_framebuffer(fb);
2003 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002004
Chris Wilson5eddb702010-09-11 13:48:45 +01002005 reg = DSPCNTR(plane);
2006 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002007 /* Mask out pixel format bits in case we change it */
2008 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002009 switch (fb->pixel_format) {
2010 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002011 dspcntr |= DISPPLANE_8BPP;
2012 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002013 case DRM_FORMAT_XRGB1555:
2014 case DRM_FORMAT_ARGB1555:
2015 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002016 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002017 case DRM_FORMAT_RGB565:
2018 dspcntr |= DISPPLANE_BGRX565;
2019 break;
2020 case DRM_FORMAT_XRGB8888:
2021 case DRM_FORMAT_ARGB8888:
2022 dspcntr |= DISPPLANE_BGRX888;
2023 break;
2024 case DRM_FORMAT_XBGR8888:
2025 case DRM_FORMAT_ABGR8888:
2026 dspcntr |= DISPPLANE_RGBX888;
2027 break;
2028 case DRM_FORMAT_XRGB2101010:
2029 case DRM_FORMAT_ARGB2101010:
2030 dspcntr |= DISPPLANE_BGRX101010;
2031 break;
2032 case DRM_FORMAT_XBGR2101010:
2033 case DRM_FORMAT_ABGR2101010:
2034 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002035 break;
2036 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002037 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002038 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002039
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002040 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002041 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002042 dspcntr |= DISPPLANE_TILED;
2043 else
2044 dspcntr &= ~DISPPLANE_TILED;
2045 }
2046
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002047 if (IS_G4X(dev))
2048 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2049
Chris Wilson5eddb702010-09-11 13:48:45 +01002050 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002051
Daniel Vettere506a0c2012-07-05 12:17:29 +02002052 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002053
Daniel Vetterc2c75132012-07-05 12:17:30 +02002054 if (INTEL_INFO(dev)->gen >= 4) {
2055 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002056 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2057 fb->bits_per_pixel / 8,
2058 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002059 linear_offset -= intel_crtc->dspaddr_offset;
2060 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002061 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002062 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002063
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002064 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2065 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2066 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002067 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002068 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002069 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002070 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002071 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002072 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002073 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002074 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002075 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002076
Jesse Barnes17638cd2011-06-24 12:19:23 -07002077 return 0;
2078}
2079
2080static int ironlake_update_plane(struct drm_crtc *crtc,
2081 struct drm_framebuffer *fb, int x, int y)
2082{
2083 struct drm_device *dev = crtc->dev;
2084 struct drm_i915_private *dev_priv = dev->dev_private;
2085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2086 struct intel_framebuffer *intel_fb;
2087 struct drm_i915_gem_object *obj;
2088 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002089 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002090 u32 dspcntr;
2091 u32 reg;
2092
2093 switch (plane) {
2094 case 0:
2095 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002096 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002097 break;
2098 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002099 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002100 return -EINVAL;
2101 }
2102
2103 intel_fb = to_intel_framebuffer(fb);
2104 obj = intel_fb->obj;
2105
2106 reg = DSPCNTR(plane);
2107 dspcntr = I915_READ(reg);
2108 /* Mask out pixel format bits in case we change it */
2109 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002110 switch (fb->pixel_format) {
2111 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002112 dspcntr |= DISPPLANE_8BPP;
2113 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002114 case DRM_FORMAT_RGB565:
2115 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002116 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002117 case DRM_FORMAT_XRGB8888:
2118 case DRM_FORMAT_ARGB8888:
2119 dspcntr |= DISPPLANE_BGRX888;
2120 break;
2121 case DRM_FORMAT_XBGR8888:
2122 case DRM_FORMAT_ABGR8888:
2123 dspcntr |= DISPPLANE_RGBX888;
2124 break;
2125 case DRM_FORMAT_XRGB2101010:
2126 case DRM_FORMAT_ARGB2101010:
2127 dspcntr |= DISPPLANE_BGRX101010;
2128 break;
2129 case DRM_FORMAT_XBGR2101010:
2130 case DRM_FORMAT_ABGR2101010:
2131 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002132 break;
2133 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002134 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002135 }
2136
2137 if (obj->tiling_mode != I915_TILING_NONE)
2138 dspcntr |= DISPPLANE_TILED;
2139 else
2140 dspcntr &= ~DISPPLANE_TILED;
2141
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002142 if (IS_HASWELL(dev))
2143 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2144 else
2145 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002146
2147 I915_WRITE(reg, dspcntr);
2148
Daniel Vettere506a0c2012-07-05 12:17:29 +02002149 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002150 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002151 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2152 fb->bits_per_pixel / 8,
2153 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002154 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002155
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002156 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2157 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2158 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002159 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002160 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002161 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002162 if (IS_HASWELL(dev)) {
2163 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2164 } else {
2165 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2166 I915_WRITE(DSPLINOFF(plane), linear_offset);
2167 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002168 POSTING_READ(reg);
2169
2170 return 0;
2171}
2172
2173/* Assume fb object is pinned & idle & fenced and just update base pointers */
2174static int
2175intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2176 int x, int y, enum mode_set_atomic state)
2177{
2178 struct drm_device *dev = crtc->dev;
2179 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002180
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002181 if (dev_priv->display.disable_fbc)
2182 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002183 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002184
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002185 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002186}
2187
Ville Syrjälä96a02912013-02-18 19:08:49 +02002188void intel_display_handle_reset(struct drm_device *dev)
2189{
2190 struct drm_i915_private *dev_priv = dev->dev_private;
2191 struct drm_crtc *crtc;
2192
2193 /*
2194 * Flips in the rings have been nuked by the reset,
2195 * so complete all pending flips so that user space
2196 * will get its events and not get stuck.
2197 *
2198 * Also update the base address of all primary
2199 * planes to the the last fb to make sure we're
2200 * showing the correct fb after a reset.
2201 *
2202 * Need to make two loops over the crtcs so that we
2203 * don't try to grab a crtc mutex before the
2204 * pending_flip_queue really got woken up.
2205 */
2206
2207 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2209 enum plane plane = intel_crtc->plane;
2210
2211 intel_prepare_page_flip(dev, plane);
2212 intel_finish_page_flip_plane(dev, plane);
2213 }
2214
2215 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2217
2218 mutex_lock(&crtc->mutex);
2219 if (intel_crtc->active)
2220 dev_priv->display.update_plane(crtc, crtc->fb,
2221 crtc->x, crtc->y);
2222 mutex_unlock(&crtc->mutex);
2223 }
2224}
2225
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002226static int
Chris Wilson14667a42012-04-03 17:58:35 +01002227intel_finish_fb(struct drm_framebuffer *old_fb)
2228{
2229 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2230 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2231 bool was_interruptible = dev_priv->mm.interruptible;
2232 int ret;
2233
Chris Wilson14667a42012-04-03 17:58:35 +01002234 /* Big Hammer, we also need to ensure that any pending
2235 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2236 * current scanout is retired before unpinning the old
2237 * framebuffer.
2238 *
2239 * This should only fail upon a hung GPU, in which case we
2240 * can safely continue.
2241 */
2242 dev_priv->mm.interruptible = false;
2243 ret = i915_gem_object_finish_gpu(obj);
2244 dev_priv->mm.interruptible = was_interruptible;
2245
2246 return ret;
2247}
2248
Ville Syrjälä198598d2012-10-31 17:50:24 +02002249static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2250{
2251 struct drm_device *dev = crtc->dev;
2252 struct drm_i915_master_private *master_priv;
2253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2254
2255 if (!dev->primary->master)
2256 return;
2257
2258 master_priv = dev->primary->master->driver_priv;
2259 if (!master_priv->sarea_priv)
2260 return;
2261
2262 switch (intel_crtc->pipe) {
2263 case 0:
2264 master_priv->sarea_priv->pipeA_x = x;
2265 master_priv->sarea_priv->pipeA_y = y;
2266 break;
2267 case 1:
2268 master_priv->sarea_priv->pipeB_x = x;
2269 master_priv->sarea_priv->pipeB_y = y;
2270 break;
2271 default:
2272 break;
2273 }
2274}
2275
Chris Wilson14667a42012-04-03 17:58:35 +01002276static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002277intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002278 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002279{
2280 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002281 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002283 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002284 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002285
2286 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002287 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002288 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002289 return 0;
2290 }
2291
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002292 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002293 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2294 plane_name(intel_crtc->plane),
2295 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002296 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002297 }
2298
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002299 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002300 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002301 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002302 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002303 if (ret != 0) {
2304 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002305 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002306 return ret;
2307 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002308
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002309 /*
2310 * Update pipe size and adjust fitter if needed: the reason for this is
2311 * that in compute_mode_changes we check the native mode (not the pfit
2312 * mode) to see if we can flip rather than do a full mode set. In the
2313 * fastboot case, we'll flip, but if we don't update the pipesrc and
2314 * pfit state, we'll end up with a big fb scanned out into the wrong
2315 * sized surface.
2316 *
2317 * To fix this properly, we need to hoist the checks up into
2318 * compute_mode_changes (or above), check the actual pfit state and
2319 * whether the platform allows pfit disable with pipe active, and only
2320 * then update the pipesrc and pfit state, even on the flip path.
2321 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002322 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002323 const struct drm_display_mode *adjusted_mode =
2324 &intel_crtc->config.adjusted_mode;
2325
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002326 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002327 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2328 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002329 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002330 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2331 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2332 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2333 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2334 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2335 }
2336 }
2337
Daniel Vetter94352cf2012-07-05 22:51:56 +02002338 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002339 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002340 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002341 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002342 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002343 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002344 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002345
Daniel Vetter94352cf2012-07-05 22:51:56 +02002346 old_fb = crtc->fb;
2347 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002348 crtc->x = x;
2349 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002350
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002351 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002352 if (intel_crtc->active && old_fb != fb)
2353 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002354 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002355 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002356
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002357 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002358 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002359 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002360
Ville Syrjälä198598d2012-10-31 17:50:24 +02002361 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002362
2363 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002364}
2365
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002366static void intel_fdi_normal_train(struct drm_crtc *crtc)
2367{
2368 struct drm_device *dev = crtc->dev;
2369 struct drm_i915_private *dev_priv = dev->dev_private;
2370 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2371 int pipe = intel_crtc->pipe;
2372 u32 reg, temp;
2373
2374 /* enable normal train */
2375 reg = FDI_TX_CTL(pipe);
2376 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002377 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002378 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2379 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002380 } else {
2381 temp &= ~FDI_LINK_TRAIN_NONE;
2382 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002383 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002384 I915_WRITE(reg, temp);
2385
2386 reg = FDI_RX_CTL(pipe);
2387 temp = I915_READ(reg);
2388 if (HAS_PCH_CPT(dev)) {
2389 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2390 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2391 } else {
2392 temp &= ~FDI_LINK_TRAIN_NONE;
2393 temp |= FDI_LINK_TRAIN_NONE;
2394 }
2395 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2396
2397 /* wait one idle pattern time */
2398 POSTING_READ(reg);
2399 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002400
2401 /* IVB wants error correction enabled */
2402 if (IS_IVYBRIDGE(dev))
2403 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2404 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002405}
2406
Daniel Vetter1e833f42013-02-19 22:31:57 +01002407static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2408{
2409 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2410}
2411
Daniel Vetter01a415f2012-10-27 15:58:40 +02002412static void ivb_modeset_global_resources(struct drm_device *dev)
2413{
2414 struct drm_i915_private *dev_priv = dev->dev_private;
2415 struct intel_crtc *pipe_B_crtc =
2416 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2417 struct intel_crtc *pipe_C_crtc =
2418 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2419 uint32_t temp;
2420
Daniel Vetter1e833f42013-02-19 22:31:57 +01002421 /*
2422 * When everything is off disable fdi C so that we could enable fdi B
2423 * with all lanes. Note that we don't care about enabled pipes without
2424 * an enabled pch encoder.
2425 */
2426 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2427 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002428 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2429 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2430
2431 temp = I915_READ(SOUTH_CHICKEN1);
2432 temp &= ~FDI_BC_BIFURCATION_SELECT;
2433 DRM_DEBUG_KMS("disabling fdi C rx\n");
2434 I915_WRITE(SOUTH_CHICKEN1, temp);
2435 }
2436}
2437
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002438/* The FDI link training functions for ILK/Ibexpeak. */
2439static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2440{
2441 struct drm_device *dev = crtc->dev;
2442 struct drm_i915_private *dev_priv = dev->dev_private;
2443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2444 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002445 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002446 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002448 /* FDI needs bits from pipe & plane first */
2449 assert_pipe_enabled(dev_priv, pipe);
2450 assert_plane_enabled(dev_priv, plane);
2451
Adam Jacksone1a44742010-06-25 15:32:14 -04002452 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2453 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002454 reg = FDI_RX_IMR(pipe);
2455 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002456 temp &= ~FDI_RX_SYMBOL_LOCK;
2457 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002458 I915_WRITE(reg, temp);
2459 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002460 udelay(150);
2461
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002462 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002463 reg = FDI_TX_CTL(pipe);
2464 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002465 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2466 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467 temp &= ~FDI_LINK_TRAIN_NONE;
2468 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002469 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002470
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 reg = FDI_RX_CTL(pipe);
2472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002473 temp &= ~FDI_LINK_TRAIN_NONE;
2474 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2476
2477 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 udelay(150);
2479
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002480 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002481 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2482 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2483 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002484
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002486 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2489
2490 if ((temp & FDI_RX_BIT_LOCK)) {
2491 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002492 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 break;
2494 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002496 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002497 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498
2499 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 reg = FDI_TX_CTL(pipe);
2501 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002502 temp &= ~FDI_LINK_TRAIN_NONE;
2503 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002504 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002505
Chris Wilson5eddb702010-09-11 13:48:45 +01002506 reg = FDI_RX_CTL(pipe);
2507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002508 temp &= ~FDI_LINK_TRAIN_NONE;
2509 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 I915_WRITE(reg, temp);
2511
2512 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002513 udelay(150);
2514
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002516 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002517 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002518 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2519
2520 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002521 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002522 DRM_DEBUG_KMS("FDI train 2 done.\n");
2523 break;
2524 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002526 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002527 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528
2529 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002530
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531}
2532
Akshay Joshi0206e352011-08-16 15:34:10 -04002533static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002534 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2535 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2536 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2537 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2538};
2539
2540/* The FDI link training functions for SNB/Cougarpoint. */
2541static void gen6_fdi_link_train(struct drm_crtc *crtc)
2542{
2543 struct drm_device *dev = crtc->dev;
2544 struct drm_i915_private *dev_priv = dev->dev_private;
2545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2546 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002547 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548
Adam Jacksone1a44742010-06-25 15:32:14 -04002549 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2550 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002551 reg = FDI_RX_IMR(pipe);
2552 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002553 temp &= ~FDI_RX_SYMBOL_LOCK;
2554 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002555 I915_WRITE(reg, temp);
2556
2557 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002558 udelay(150);
2559
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002560 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 reg = FDI_TX_CTL(pipe);
2562 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002563 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2564 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002565 temp &= ~FDI_LINK_TRAIN_NONE;
2566 temp |= FDI_LINK_TRAIN_PATTERN_1;
2567 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2568 /* SNB-B */
2569 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002570 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571
Daniel Vetterd74cf322012-10-26 10:58:13 +02002572 I915_WRITE(FDI_RX_MISC(pipe),
2573 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2574
Chris Wilson5eddb702010-09-11 13:48:45 +01002575 reg = FDI_RX_CTL(pipe);
2576 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002577 if (HAS_PCH_CPT(dev)) {
2578 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2579 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2580 } else {
2581 temp &= ~FDI_LINK_TRAIN_NONE;
2582 temp |= FDI_LINK_TRAIN_PATTERN_1;
2583 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002584 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2585
2586 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587 udelay(150);
2588
Akshay Joshi0206e352011-08-16 15:34:10 -04002589 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002590 reg = FDI_TX_CTL(pipe);
2591 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002592 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2593 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002594 I915_WRITE(reg, temp);
2595
2596 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597 udelay(500);
2598
Sean Paulfa37d392012-03-02 12:53:39 -05002599 for (retry = 0; retry < 5; retry++) {
2600 reg = FDI_RX_IIR(pipe);
2601 temp = I915_READ(reg);
2602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2603 if (temp & FDI_RX_BIT_LOCK) {
2604 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2605 DRM_DEBUG_KMS("FDI train 1 done.\n");
2606 break;
2607 }
2608 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002609 }
Sean Paulfa37d392012-03-02 12:53:39 -05002610 if (retry < 5)
2611 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002612 }
2613 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002614 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002615
2616 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002617 reg = FDI_TX_CTL(pipe);
2618 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002619 temp &= ~FDI_LINK_TRAIN_NONE;
2620 temp |= FDI_LINK_TRAIN_PATTERN_2;
2621 if (IS_GEN6(dev)) {
2622 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2623 /* SNB-B */
2624 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2625 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002626 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002627
Chris Wilson5eddb702010-09-11 13:48:45 +01002628 reg = FDI_RX_CTL(pipe);
2629 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002630 if (HAS_PCH_CPT(dev)) {
2631 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2632 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2633 } else {
2634 temp &= ~FDI_LINK_TRAIN_NONE;
2635 temp |= FDI_LINK_TRAIN_PATTERN_2;
2636 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002637 I915_WRITE(reg, temp);
2638
2639 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002640 udelay(150);
2641
Akshay Joshi0206e352011-08-16 15:34:10 -04002642 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002643 reg = FDI_TX_CTL(pipe);
2644 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002645 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2646 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002647 I915_WRITE(reg, temp);
2648
2649 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002650 udelay(500);
2651
Sean Paulfa37d392012-03-02 12:53:39 -05002652 for (retry = 0; retry < 5; retry++) {
2653 reg = FDI_RX_IIR(pipe);
2654 temp = I915_READ(reg);
2655 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2656 if (temp & FDI_RX_SYMBOL_LOCK) {
2657 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2658 DRM_DEBUG_KMS("FDI train 2 done.\n");
2659 break;
2660 }
2661 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002662 }
Sean Paulfa37d392012-03-02 12:53:39 -05002663 if (retry < 5)
2664 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002665 }
2666 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002667 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002668
2669 DRM_DEBUG_KMS("FDI train done.\n");
2670}
2671
Jesse Barnes357555c2011-04-28 15:09:55 -07002672/* Manual link training for Ivy Bridge A0 parts */
2673static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2674{
2675 struct drm_device *dev = crtc->dev;
2676 struct drm_i915_private *dev_priv = dev->dev_private;
2677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2678 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002679 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002680
2681 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2682 for train result */
2683 reg = FDI_RX_IMR(pipe);
2684 temp = I915_READ(reg);
2685 temp &= ~FDI_RX_SYMBOL_LOCK;
2686 temp &= ~FDI_RX_BIT_LOCK;
2687 I915_WRITE(reg, temp);
2688
2689 POSTING_READ(reg);
2690 udelay(150);
2691
Daniel Vetter01a415f2012-10-27 15:58:40 +02002692 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2693 I915_READ(FDI_RX_IIR(pipe)));
2694
Jesse Barnes139ccd32013-08-19 11:04:55 -07002695 /* Try each vswing and preemphasis setting twice before moving on */
2696 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2697 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002698 reg = FDI_TX_CTL(pipe);
2699 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002700 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2701 temp &= ~FDI_TX_ENABLE;
2702 I915_WRITE(reg, temp);
2703
2704 reg = FDI_RX_CTL(pipe);
2705 temp = I915_READ(reg);
2706 temp &= ~FDI_LINK_TRAIN_AUTO;
2707 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2708 temp &= ~FDI_RX_ENABLE;
2709 I915_WRITE(reg, temp);
2710
2711 /* enable CPU FDI TX and PCH FDI RX */
2712 reg = FDI_TX_CTL(pipe);
2713 temp = I915_READ(reg);
2714 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2715 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2716 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002717 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002718 temp |= snb_b_fdi_train_param[j/2];
2719 temp |= FDI_COMPOSITE_SYNC;
2720 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2721
2722 I915_WRITE(FDI_RX_MISC(pipe),
2723 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2724
2725 reg = FDI_RX_CTL(pipe);
2726 temp = I915_READ(reg);
2727 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2728 temp |= FDI_COMPOSITE_SYNC;
2729 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2730
2731 POSTING_READ(reg);
2732 udelay(1); /* should be 0.5us */
2733
2734 for (i = 0; i < 4; i++) {
2735 reg = FDI_RX_IIR(pipe);
2736 temp = I915_READ(reg);
2737 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2738
2739 if (temp & FDI_RX_BIT_LOCK ||
2740 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2741 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2742 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2743 i);
2744 break;
2745 }
2746 udelay(1); /* should be 0.5us */
2747 }
2748 if (i == 4) {
2749 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2750 continue;
2751 }
2752
2753 /* Train 2 */
2754 reg = FDI_TX_CTL(pipe);
2755 temp = I915_READ(reg);
2756 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2757 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2758 I915_WRITE(reg, temp);
2759
2760 reg = FDI_RX_CTL(pipe);
2761 temp = I915_READ(reg);
2762 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2763 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002764 I915_WRITE(reg, temp);
2765
2766 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002767 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002768
Jesse Barnes139ccd32013-08-19 11:04:55 -07002769 for (i = 0; i < 4; i++) {
2770 reg = FDI_RX_IIR(pipe);
2771 temp = I915_READ(reg);
2772 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002773
Jesse Barnes139ccd32013-08-19 11:04:55 -07002774 if (temp & FDI_RX_SYMBOL_LOCK ||
2775 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2776 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2777 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2778 i);
2779 goto train_done;
2780 }
2781 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002782 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002783 if (i == 4)
2784 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002785 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002786
Jesse Barnes139ccd32013-08-19 11:04:55 -07002787train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002788 DRM_DEBUG_KMS("FDI train done.\n");
2789}
2790
Daniel Vetter88cefb62012-08-12 19:27:14 +02002791static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002792{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002793 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002794 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002795 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002796 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002797
Jesse Barnesc64e3112010-09-10 11:27:03 -07002798
Jesse Barnes0e23b992010-09-10 11:10:00 -07002799 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002800 reg = FDI_RX_CTL(pipe);
2801 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002802 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2803 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002804 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002805 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2806
2807 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002808 udelay(200);
2809
2810 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002811 temp = I915_READ(reg);
2812 I915_WRITE(reg, temp | FDI_PCDCLK);
2813
2814 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002815 udelay(200);
2816
Paulo Zanoni20749732012-11-23 15:30:38 -02002817 /* Enable CPU FDI TX PLL, always on for Ironlake */
2818 reg = FDI_TX_CTL(pipe);
2819 temp = I915_READ(reg);
2820 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2821 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002822
Paulo Zanoni20749732012-11-23 15:30:38 -02002823 POSTING_READ(reg);
2824 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002825 }
2826}
2827
Daniel Vetter88cefb62012-08-12 19:27:14 +02002828static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2829{
2830 struct drm_device *dev = intel_crtc->base.dev;
2831 struct drm_i915_private *dev_priv = dev->dev_private;
2832 int pipe = intel_crtc->pipe;
2833 u32 reg, temp;
2834
2835 /* Switch from PCDclk to Rawclk */
2836 reg = FDI_RX_CTL(pipe);
2837 temp = I915_READ(reg);
2838 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2839
2840 /* Disable CPU FDI TX PLL */
2841 reg = FDI_TX_CTL(pipe);
2842 temp = I915_READ(reg);
2843 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2844
2845 POSTING_READ(reg);
2846 udelay(100);
2847
2848 reg = FDI_RX_CTL(pipe);
2849 temp = I915_READ(reg);
2850 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2851
2852 /* Wait for the clocks to turn off. */
2853 POSTING_READ(reg);
2854 udelay(100);
2855}
2856
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002857static void ironlake_fdi_disable(struct drm_crtc *crtc)
2858{
2859 struct drm_device *dev = crtc->dev;
2860 struct drm_i915_private *dev_priv = dev->dev_private;
2861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2862 int pipe = intel_crtc->pipe;
2863 u32 reg, temp;
2864
2865 /* disable CPU FDI tx and PCH FDI rx */
2866 reg = FDI_TX_CTL(pipe);
2867 temp = I915_READ(reg);
2868 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2869 POSTING_READ(reg);
2870
2871 reg = FDI_RX_CTL(pipe);
2872 temp = I915_READ(reg);
2873 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002874 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002875 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2876
2877 POSTING_READ(reg);
2878 udelay(100);
2879
2880 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002881 if (HAS_PCH_IBX(dev)) {
2882 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002883 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002884
2885 /* still set train pattern 1 */
2886 reg = FDI_TX_CTL(pipe);
2887 temp = I915_READ(reg);
2888 temp &= ~FDI_LINK_TRAIN_NONE;
2889 temp |= FDI_LINK_TRAIN_PATTERN_1;
2890 I915_WRITE(reg, temp);
2891
2892 reg = FDI_RX_CTL(pipe);
2893 temp = I915_READ(reg);
2894 if (HAS_PCH_CPT(dev)) {
2895 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2896 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2897 } else {
2898 temp &= ~FDI_LINK_TRAIN_NONE;
2899 temp |= FDI_LINK_TRAIN_PATTERN_1;
2900 }
2901 /* BPC in FDI rx is consistent with that in PIPECONF */
2902 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002903 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002904 I915_WRITE(reg, temp);
2905
2906 POSTING_READ(reg);
2907 udelay(100);
2908}
2909
Chris Wilson5bb61642012-09-27 21:25:58 +01002910static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2911{
2912 struct drm_device *dev = crtc->dev;
2913 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002915 unsigned long flags;
2916 bool pending;
2917
Ville Syrjälä10d83732013-01-29 18:13:34 +02002918 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2919 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002920 return false;
2921
2922 spin_lock_irqsave(&dev->event_lock, flags);
2923 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2924 spin_unlock_irqrestore(&dev->event_lock, flags);
2925
2926 return pending;
2927}
2928
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002929static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2930{
Chris Wilson0f911282012-04-17 10:05:38 +01002931 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002932 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002933
2934 if (crtc->fb == NULL)
2935 return;
2936
Daniel Vetter2c10d572012-12-20 21:24:07 +01002937 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2938
Chris Wilson5bb61642012-09-27 21:25:58 +01002939 wait_event(dev_priv->pending_flip_queue,
2940 !intel_crtc_has_pending_flip(crtc));
2941
Chris Wilson0f911282012-04-17 10:05:38 +01002942 mutex_lock(&dev->struct_mutex);
2943 intel_finish_fb(crtc->fb);
2944 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002945}
2946
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002947/* Program iCLKIP clock to the desired frequency */
2948static void lpt_program_iclkip(struct drm_crtc *crtc)
2949{
2950 struct drm_device *dev = crtc->dev;
2951 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002952 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002953 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2954 u32 temp;
2955
Daniel Vetter09153002012-12-12 14:06:44 +01002956 mutex_lock(&dev_priv->dpio_lock);
2957
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002958 /* It is necessary to ungate the pixclk gate prior to programming
2959 * the divisors, and gate it back when it is done.
2960 */
2961 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2962
2963 /* Disable SSCCTL */
2964 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002965 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2966 SBI_SSCCTL_DISABLE,
2967 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002968
2969 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002970 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002971 auxdiv = 1;
2972 divsel = 0x41;
2973 phaseinc = 0x20;
2974 } else {
2975 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01002976 * but the adjusted_mode->crtc_clock in in KHz. To get the
2977 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002978 * convert the virtual clock precision to KHz here for higher
2979 * precision.
2980 */
2981 u32 iclk_virtual_root_freq = 172800 * 1000;
2982 u32 iclk_pi_range = 64;
2983 u32 desired_divisor, msb_divisor_value, pi_value;
2984
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002985 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002986 msb_divisor_value = desired_divisor / iclk_pi_range;
2987 pi_value = desired_divisor % iclk_pi_range;
2988
2989 auxdiv = 0;
2990 divsel = msb_divisor_value - 2;
2991 phaseinc = pi_value;
2992 }
2993
2994 /* This should not happen with any sane values */
2995 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2996 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2997 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2998 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2999
3000 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 +03003001 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003002 auxdiv,
3003 divsel,
3004 phasedir,
3005 phaseinc);
3006
3007 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003008 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003009 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3010 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3011 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3012 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3013 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3014 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003015 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003016
3017 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003018 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003019 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3020 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003021 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003022
3023 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003024 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003025 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003026 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003027
3028 /* Wait for initialization time */
3029 udelay(24);
3030
3031 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003032
3033 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003034}
3035
Daniel Vetter275f01b22013-05-03 11:49:47 +02003036static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3037 enum pipe pch_transcoder)
3038{
3039 struct drm_device *dev = crtc->base.dev;
3040 struct drm_i915_private *dev_priv = dev->dev_private;
3041 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3042
3043 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3044 I915_READ(HTOTAL(cpu_transcoder)));
3045 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3046 I915_READ(HBLANK(cpu_transcoder)));
3047 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3048 I915_READ(HSYNC(cpu_transcoder)));
3049
3050 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3051 I915_READ(VTOTAL(cpu_transcoder)));
3052 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3053 I915_READ(VBLANK(cpu_transcoder)));
3054 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3055 I915_READ(VSYNC(cpu_transcoder)));
3056 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3057 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3058}
3059
Jesse Barnesf67a5592011-01-05 10:31:48 -08003060/*
3061 * Enable PCH resources required for PCH ports:
3062 * - PCH PLLs
3063 * - FDI training & RX/TX
3064 * - update transcoder timings
3065 * - DP transcoding bits
3066 * - transcoder
3067 */
3068static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003069{
3070 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003071 struct drm_i915_private *dev_priv = dev->dev_private;
3072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3073 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003074 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003075
Daniel Vetterab9412b2013-05-03 11:49:46 +02003076 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003077
Daniel Vettercd986ab2012-10-26 10:58:12 +02003078 /* Write the TU size bits before fdi link training, so that error
3079 * detection works. */
3080 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3081 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3082
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003083 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003084 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003085
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003086 /* We need to program the right clock selection before writing the pixel
3087 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003088 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003089 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003090
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003091 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003092 temp |= TRANS_DPLL_ENABLE(pipe);
3093 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003094 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003095 temp |= sel;
3096 else
3097 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003098 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003099 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003100
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003101 /* XXX: pch pll's can be enabled any time before we enable the PCH
3102 * transcoder, and we actually should do this to not upset any PCH
3103 * transcoder that already use the clock when we share it.
3104 *
3105 * Note that enable_shared_dpll tries to do the right thing, but
3106 * get_shared_dpll unconditionally resets the pll - we need that to have
3107 * the right LVDS enable sequence. */
3108 ironlake_enable_shared_dpll(intel_crtc);
3109
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003110 /* set transcoder timing, panel must allow it */
3111 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003112 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003113
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003114 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003115
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003116 /* For PCH DP, enable TRANS_DP_CTL */
3117 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003118 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3119 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003120 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003121 reg = TRANS_DP_CTL(pipe);
3122 temp = I915_READ(reg);
3123 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003124 TRANS_DP_SYNC_MASK |
3125 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003126 temp |= (TRANS_DP_OUTPUT_ENABLE |
3127 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003128 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003129
3130 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003131 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003132 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003133 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003134
3135 switch (intel_trans_dp_port_sel(crtc)) {
3136 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003137 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003138 break;
3139 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003140 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003141 break;
3142 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003143 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003144 break;
3145 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003146 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003147 }
3148
Chris Wilson5eddb702010-09-11 13:48:45 +01003149 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003150 }
3151
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003152 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003153}
3154
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003155static void lpt_pch_enable(struct drm_crtc *crtc)
3156{
3157 struct drm_device *dev = crtc->dev;
3158 struct drm_i915_private *dev_priv = dev->dev_private;
3159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003160 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003161
Daniel Vetterab9412b2013-05-03 11:49:46 +02003162 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003163
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003164 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003165
Paulo Zanoni0540e482012-10-31 18:12:40 -02003166 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003167 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003168
Paulo Zanoni937bb612012-10-31 18:12:47 -02003169 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003170}
3171
Daniel Vettere2b78262013-06-07 23:10:03 +02003172static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003173{
Daniel Vettere2b78262013-06-07 23:10:03 +02003174 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003175
3176 if (pll == NULL)
3177 return;
3178
3179 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003180 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003181 return;
3182 }
3183
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003184 if (--pll->refcount == 0) {
3185 WARN_ON(pll->on);
3186 WARN_ON(pll->active);
3187 }
3188
Daniel Vettera43f6e02013-06-07 23:10:32 +02003189 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003190}
3191
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003192static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003193{
Daniel Vettere2b78262013-06-07 23:10:03 +02003194 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3195 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3196 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003197
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003198 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003199 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3200 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003201 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003202 }
3203
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003204 if (HAS_PCH_IBX(dev_priv->dev)) {
3205 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003206 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003207 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003208
Daniel Vetter46edb022013-06-05 13:34:12 +02003209 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3210 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003211
3212 goto found;
3213 }
3214
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003215 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3216 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003217
3218 /* Only want to check enabled timings first */
3219 if (pll->refcount == 0)
3220 continue;
3221
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003222 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3223 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003224 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003225 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003226 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003227
3228 goto found;
3229 }
3230 }
3231
3232 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003233 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3234 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003235 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003236 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3237 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003238 goto found;
3239 }
3240 }
3241
3242 return NULL;
3243
3244found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003245 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003246 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3247 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003248
Daniel Vettercdbd2312013-06-05 13:34:03 +02003249 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003250 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3251 sizeof(pll->hw_state));
3252
Daniel Vetter46edb022013-06-05 13:34:12 +02003253 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003254 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003255 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003256
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003257 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003258 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003259 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003260
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003261 return pll;
3262}
3263
Daniel Vettera1520312013-05-03 11:49:50 +02003264static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003265{
3266 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003267 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003268 u32 temp;
3269
3270 temp = I915_READ(dslreg);
3271 udelay(500);
3272 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003273 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003274 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003275 }
3276}
3277
Jesse Barnesb074cec2013-04-25 12:55:02 -07003278static void ironlake_pfit_enable(struct intel_crtc *crtc)
3279{
3280 struct drm_device *dev = crtc->base.dev;
3281 struct drm_i915_private *dev_priv = dev->dev_private;
3282 int pipe = crtc->pipe;
3283
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003284 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003285 /* Force use of hard-coded filter coefficients
3286 * as some pre-programmed values are broken,
3287 * e.g. x201.
3288 */
3289 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3290 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3291 PF_PIPE_SEL_IVB(pipe));
3292 else
3293 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3294 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3295 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003296 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003297}
3298
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003299static void intel_enable_planes(struct drm_crtc *crtc)
3300{
3301 struct drm_device *dev = crtc->dev;
3302 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3303 struct intel_plane *intel_plane;
3304
3305 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3306 if (intel_plane->pipe == pipe)
3307 intel_plane_restore(&intel_plane->base);
3308}
3309
3310static void intel_disable_planes(struct drm_crtc *crtc)
3311{
3312 struct drm_device *dev = crtc->dev;
3313 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3314 struct intel_plane *intel_plane;
3315
3316 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3317 if (intel_plane->pipe == pipe)
3318 intel_plane_disable(&intel_plane->base);
3319}
3320
Paulo Zanonid77e4532013-09-24 13:52:55 -03003321static void hsw_enable_ips(struct intel_crtc *crtc)
3322{
3323 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3324
3325 if (!crtc->config.ips_enabled)
3326 return;
3327
3328 /* We can only enable IPS after we enable a plane and wait for a vblank.
3329 * We guarantee that the plane is enabled by calling intel_enable_ips
3330 * only after intel_enable_plane. And intel_enable_plane already waits
3331 * for a vblank, so all we need to do here is to enable the IPS bit. */
3332 assert_plane_enabled(dev_priv, crtc->plane);
3333 I915_WRITE(IPS_CTL, IPS_ENABLE);
3334}
3335
3336static void hsw_disable_ips(struct intel_crtc *crtc)
3337{
3338 struct drm_device *dev = crtc->base.dev;
3339 struct drm_i915_private *dev_priv = dev->dev_private;
3340
3341 if (!crtc->config.ips_enabled)
3342 return;
3343
3344 assert_plane_enabled(dev_priv, crtc->plane);
3345 I915_WRITE(IPS_CTL, 0);
3346 POSTING_READ(IPS_CTL);
3347
3348 /* We need to wait for a vblank before we can disable the plane. */
3349 intel_wait_for_vblank(dev, crtc->pipe);
3350}
3351
3352/** Loads the palette/gamma unit for the CRTC with the prepared values */
3353static void intel_crtc_load_lut(struct drm_crtc *crtc)
3354{
3355 struct drm_device *dev = crtc->dev;
3356 struct drm_i915_private *dev_priv = dev->dev_private;
3357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3358 enum pipe pipe = intel_crtc->pipe;
3359 int palreg = PALETTE(pipe);
3360 int i;
3361 bool reenable_ips = false;
3362
3363 /* The clocks have to be on to load the palette. */
3364 if (!crtc->enabled || !intel_crtc->active)
3365 return;
3366
3367 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3368 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3369 assert_dsi_pll_enabled(dev_priv);
3370 else
3371 assert_pll_enabled(dev_priv, pipe);
3372 }
3373
3374 /* use legacy palette for Ironlake */
3375 if (HAS_PCH_SPLIT(dev))
3376 palreg = LGC_PALETTE(pipe);
3377
3378 /* Workaround : Do not read or write the pipe palette/gamma data while
3379 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3380 */
3381 if (intel_crtc->config.ips_enabled &&
3382 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3383 GAMMA_MODE_MODE_SPLIT)) {
3384 hsw_disable_ips(intel_crtc);
3385 reenable_ips = true;
3386 }
3387
3388 for (i = 0; i < 256; i++) {
3389 I915_WRITE(palreg + 4 * i,
3390 (intel_crtc->lut_r[i] << 16) |
3391 (intel_crtc->lut_g[i] << 8) |
3392 intel_crtc->lut_b[i]);
3393 }
3394
3395 if (reenable_ips)
3396 hsw_enable_ips(intel_crtc);
3397}
3398
Jesse Barnesf67a5592011-01-05 10:31:48 -08003399static void ironlake_crtc_enable(struct drm_crtc *crtc)
3400{
3401 struct drm_device *dev = crtc->dev;
3402 struct drm_i915_private *dev_priv = dev->dev_private;
3403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003404 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003405 int pipe = intel_crtc->pipe;
3406 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003407
Daniel Vetter08a48462012-07-02 11:43:47 +02003408 WARN_ON(!crtc->enabled);
3409
Jesse Barnesf67a5592011-01-05 10:31:48 -08003410 if (intel_crtc->active)
3411 return;
3412
3413 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003414
3415 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3416 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3417
Daniel Vetterf6736a12013-06-05 13:34:30 +02003418 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003419 if (encoder->pre_enable)
3420 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003421
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003422 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003423 /* Note: FDI PLL enabling _must_ be done before we enable the
3424 * cpu pipes, hence this is separate from all the other fdi/pch
3425 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003426 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003427 } else {
3428 assert_fdi_tx_disabled(dev_priv, pipe);
3429 assert_fdi_rx_disabled(dev_priv, pipe);
3430 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003431
Jesse Barnesb074cec2013-04-25 12:55:02 -07003432 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003433
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003434 /*
3435 * On ILK+ LUT must be loaded before the pipe is running but with
3436 * clocks enabled
3437 */
3438 intel_crtc_load_lut(crtc);
3439
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003440 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003441 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003442 intel_crtc->config.has_pch_encoder, false);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003443 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003444 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003445 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003446
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003447 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003448 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003449
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003450 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003451 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003452 mutex_unlock(&dev->struct_mutex);
3453
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003454 for_each_encoder_on_crtc(dev, crtc, encoder)
3455 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003456
3457 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003458 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003459
3460 /*
3461 * There seems to be a race in PCH platform hw (at least on some
3462 * outputs) where an enabled pipe still completes any pageflip right
3463 * away (as if the pipe is off) instead of waiting for vblank. As soon
3464 * as the first vblank happend, everything works as expected. Hence just
3465 * wait for one vblank before returning to avoid strange things
3466 * happening.
3467 */
3468 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003469}
3470
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003471/* IPS only exists on ULT machines and is tied to pipe A. */
3472static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3473{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003474 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003475}
3476
Ville Syrjälädda9a662013-09-19 17:00:37 -03003477static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3478{
3479 struct drm_device *dev = crtc->dev;
3480 struct drm_i915_private *dev_priv = dev->dev_private;
3481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3482 int pipe = intel_crtc->pipe;
3483 int plane = intel_crtc->plane;
3484
3485 intel_enable_plane(dev_priv, plane, pipe);
3486 intel_enable_planes(crtc);
3487 intel_crtc_update_cursor(crtc, true);
3488
3489 hsw_enable_ips(intel_crtc);
3490
3491 mutex_lock(&dev->struct_mutex);
3492 intel_update_fbc(dev);
3493 mutex_unlock(&dev->struct_mutex);
3494}
3495
3496static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3497{
3498 struct drm_device *dev = crtc->dev;
3499 struct drm_i915_private *dev_priv = dev->dev_private;
3500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3501 int pipe = intel_crtc->pipe;
3502 int plane = intel_crtc->plane;
3503
3504 intel_crtc_wait_for_pending_flips(crtc);
3505 drm_vblank_off(dev, pipe);
3506
3507 /* FBC must be disabled before disabling the plane on HSW. */
3508 if (dev_priv->fbc.plane == plane)
3509 intel_disable_fbc(dev);
3510
3511 hsw_disable_ips(intel_crtc);
3512
3513 intel_crtc_update_cursor(crtc, false);
3514 intel_disable_planes(crtc);
3515 intel_disable_plane(dev_priv, plane, pipe);
3516}
3517
Paulo Zanonie4916942013-09-20 16:21:19 -03003518/*
3519 * This implements the workaround described in the "notes" section of the mode
3520 * set sequence documentation. When going from no pipes or single pipe to
3521 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3522 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3523 */
3524static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3525{
3526 struct drm_device *dev = crtc->base.dev;
3527 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3528
3529 /* We want to get the other_active_crtc only if there's only 1 other
3530 * active crtc. */
3531 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3532 if (!crtc_it->active || crtc_it == crtc)
3533 continue;
3534
3535 if (other_active_crtc)
3536 return;
3537
3538 other_active_crtc = crtc_it;
3539 }
3540 if (!other_active_crtc)
3541 return;
3542
3543 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3544 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3545}
3546
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003547static void haswell_crtc_enable(struct drm_crtc *crtc)
3548{
3549 struct drm_device *dev = crtc->dev;
3550 struct drm_i915_private *dev_priv = dev->dev_private;
3551 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3552 struct intel_encoder *encoder;
3553 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003554
3555 WARN_ON(!crtc->enabled);
3556
3557 if (intel_crtc->active)
3558 return;
3559
3560 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003561
3562 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3563 if (intel_crtc->config.has_pch_encoder)
3564 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3565
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003566 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003567 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003568
3569 for_each_encoder_on_crtc(dev, crtc, encoder)
3570 if (encoder->pre_enable)
3571 encoder->pre_enable(encoder);
3572
Paulo Zanoni1f544382012-10-24 11:32:00 -02003573 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003574
Jesse Barnesb074cec2013-04-25 12:55:02 -07003575 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003576
3577 /*
3578 * On ILK+ LUT must be loaded before the pipe is running but with
3579 * clocks enabled
3580 */
3581 intel_crtc_load_lut(crtc);
3582
Paulo Zanoni1f544382012-10-24 11:32:00 -02003583 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003584 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003585
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003586 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003587 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003588 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003589
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003590 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003591 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003592
Jani Nikula8807e552013-08-30 19:40:32 +03003593 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003594 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003595 intel_opregion_notify_encoder(encoder, true);
3596 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003597
Paulo Zanonie4916942013-09-20 16:21:19 -03003598 /* If we change the relative order between pipe/planes enabling, we need
3599 * to change the workaround. */
3600 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003601 haswell_crtc_enable_planes(crtc);
3602
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003603 /*
3604 * There seems to be a race in PCH platform hw (at least on some
3605 * outputs) where an enabled pipe still completes any pageflip right
3606 * away (as if the pipe is off) instead of waiting for vblank. As soon
3607 * as the first vblank happend, everything works as expected. Hence just
3608 * wait for one vblank before returning to avoid strange things
3609 * happening.
3610 */
3611 intel_wait_for_vblank(dev, intel_crtc->pipe);
3612}
3613
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003614static void ironlake_pfit_disable(struct intel_crtc *crtc)
3615{
3616 struct drm_device *dev = crtc->base.dev;
3617 struct drm_i915_private *dev_priv = dev->dev_private;
3618 int pipe = crtc->pipe;
3619
3620 /* To avoid upsetting the power well on haswell only disable the pfit if
3621 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003622 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003623 I915_WRITE(PF_CTL(pipe), 0);
3624 I915_WRITE(PF_WIN_POS(pipe), 0);
3625 I915_WRITE(PF_WIN_SZ(pipe), 0);
3626 }
3627}
3628
Jesse Barnes6be4a602010-09-10 10:26:01 -07003629static void ironlake_crtc_disable(struct drm_crtc *crtc)
3630{
3631 struct drm_device *dev = crtc->dev;
3632 struct drm_i915_private *dev_priv = dev->dev_private;
3633 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003634 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003635 int pipe = intel_crtc->pipe;
3636 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003637 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003638
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003639
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003640 if (!intel_crtc->active)
3641 return;
3642
Daniel Vetterea9d7582012-07-10 10:42:52 +02003643 for_each_encoder_on_crtc(dev, crtc, encoder)
3644 encoder->disable(encoder);
3645
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003646 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003647 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003648
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003649 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003650 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003651
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003652 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003653 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003654 intel_disable_plane(dev_priv, plane, pipe);
3655
Daniel Vetterd925c592013-06-05 13:34:04 +02003656 if (intel_crtc->config.has_pch_encoder)
3657 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3658
Jesse Barnesb24e7172011-01-04 15:09:30 -08003659 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003660
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003661 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003662
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003663 for_each_encoder_on_crtc(dev, crtc, encoder)
3664 if (encoder->post_disable)
3665 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003666
Daniel Vetterd925c592013-06-05 13:34:04 +02003667 if (intel_crtc->config.has_pch_encoder) {
3668 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003669
Daniel Vetterd925c592013-06-05 13:34:04 +02003670 ironlake_disable_pch_transcoder(dev_priv, pipe);
3671 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003672
Daniel Vetterd925c592013-06-05 13:34:04 +02003673 if (HAS_PCH_CPT(dev)) {
3674 /* disable TRANS_DP_CTL */
3675 reg = TRANS_DP_CTL(pipe);
3676 temp = I915_READ(reg);
3677 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3678 TRANS_DP_PORT_SEL_MASK);
3679 temp |= TRANS_DP_PORT_SEL_NONE;
3680 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003681
Daniel Vetterd925c592013-06-05 13:34:04 +02003682 /* disable DPLL_SEL */
3683 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003684 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003685 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003686 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003687
3688 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003689 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003690
3691 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003692 }
3693
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003694 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003695 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003696
3697 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003698 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003699 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003700}
3701
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003702static void haswell_crtc_disable(struct drm_crtc *crtc)
3703{
3704 struct drm_device *dev = crtc->dev;
3705 struct drm_i915_private *dev_priv = dev->dev_private;
3706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3707 struct intel_encoder *encoder;
3708 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003709 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003710
3711 if (!intel_crtc->active)
3712 return;
3713
Ville Syrjälädda9a662013-09-19 17:00:37 -03003714 haswell_crtc_disable_planes(crtc);
3715
Jani Nikula8807e552013-08-30 19:40:32 +03003716 for_each_encoder_on_crtc(dev, crtc, encoder) {
3717 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003718 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003719 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003720
Paulo Zanoni86642812013-04-12 17:57:57 -03003721 if (intel_crtc->config.has_pch_encoder)
3722 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003723 intel_disable_pipe(dev_priv, pipe);
3724
Paulo Zanoniad80a812012-10-24 16:06:19 -02003725 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003726
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003727 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003728
Paulo Zanoni1f544382012-10-24 11:32:00 -02003729 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003730
3731 for_each_encoder_on_crtc(dev, crtc, encoder)
3732 if (encoder->post_disable)
3733 encoder->post_disable(encoder);
3734
Daniel Vetter88adfff2013-03-28 10:42:01 +01003735 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003736 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003737 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003738 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003739 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003740
3741 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003742 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003743
3744 mutex_lock(&dev->struct_mutex);
3745 intel_update_fbc(dev);
3746 mutex_unlock(&dev->struct_mutex);
3747}
3748
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003749static void ironlake_crtc_off(struct drm_crtc *crtc)
3750{
3751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003752 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003753}
3754
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003755static void haswell_crtc_off(struct drm_crtc *crtc)
3756{
3757 intel_ddi_put_crtc_pll(crtc);
3758}
3759
Daniel Vetter02e792f2009-09-15 22:57:34 +02003760static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3761{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003762 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003763 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003764 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003765
Chris Wilson23f09ce2010-08-12 13:53:37 +01003766 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003767 dev_priv->mm.interruptible = false;
3768 (void) intel_overlay_switch_off(intel_crtc->overlay);
3769 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003770 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003771 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003772
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003773 /* Let userspace switch the overlay on again. In most cases userspace
3774 * has to recompute where to put it anyway.
3775 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003776}
3777
Egbert Eich61bc95c2013-03-04 09:24:38 -05003778/**
3779 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3780 * cursor plane briefly if not already running after enabling the display
3781 * plane.
3782 * This workaround avoids occasional blank screens when self refresh is
3783 * enabled.
3784 */
3785static void
3786g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3787{
3788 u32 cntl = I915_READ(CURCNTR(pipe));
3789
3790 if ((cntl & CURSOR_MODE) == 0) {
3791 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3792
3793 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3794 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3795 intel_wait_for_vblank(dev_priv->dev, pipe);
3796 I915_WRITE(CURCNTR(pipe), cntl);
3797 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3798 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3799 }
3800}
3801
Jesse Barnes2dd24552013-04-25 12:55:01 -07003802static void i9xx_pfit_enable(struct intel_crtc *crtc)
3803{
3804 struct drm_device *dev = crtc->base.dev;
3805 struct drm_i915_private *dev_priv = dev->dev_private;
3806 struct intel_crtc_config *pipe_config = &crtc->config;
3807
Daniel Vetter328d8e82013-05-08 10:36:31 +02003808 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003809 return;
3810
Daniel Vetterc0b03412013-05-28 12:05:54 +02003811 /*
3812 * The panel fitter should only be adjusted whilst the pipe is disabled,
3813 * according to register description and PRM.
3814 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003815 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3816 assert_pipe_disabled(dev_priv, crtc->pipe);
3817
Jesse Barnesb074cec2013-04-25 12:55:02 -07003818 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3819 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003820
3821 /* Border color in case we don't scale up to the full screen. Black by
3822 * default, change to something else for debugging. */
3823 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003824}
3825
Jesse Barnes89b667f2013-04-18 14:51:36 -07003826static void valleyview_crtc_enable(struct drm_crtc *crtc)
3827{
3828 struct drm_device *dev = crtc->dev;
3829 struct drm_i915_private *dev_priv = dev->dev_private;
3830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3831 struct intel_encoder *encoder;
3832 int pipe = intel_crtc->pipe;
3833 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03003834 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003835
3836 WARN_ON(!crtc->enabled);
3837
3838 if (intel_crtc->active)
3839 return;
3840
3841 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003842
Jesse Barnes89b667f2013-04-18 14:51:36 -07003843 for_each_encoder_on_crtc(dev, crtc, encoder)
3844 if (encoder->pre_pll_enable)
3845 encoder->pre_pll_enable(encoder);
3846
Jani Nikula23538ef2013-08-27 15:12:22 +03003847 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
3848
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003849 if (!is_dsi)
3850 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003851
3852 for_each_encoder_on_crtc(dev, crtc, encoder)
3853 if (encoder->pre_enable)
3854 encoder->pre_enable(encoder);
3855
Jesse Barnes2dd24552013-04-25 12:55:01 -07003856 i9xx_pfit_enable(intel_crtc);
3857
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003858 intel_crtc_load_lut(crtc);
3859
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003860 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003861 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003862 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003863 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003864 intel_crtc_update_cursor(crtc, true);
3865
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003866 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03003867
3868 for_each_encoder_on_crtc(dev, crtc, encoder)
3869 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003870}
3871
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003872static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003873{
3874 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003875 struct drm_i915_private *dev_priv = dev->dev_private;
3876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003877 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003878 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003879 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003880
Daniel Vetter08a48462012-07-02 11:43:47 +02003881 WARN_ON(!crtc->enabled);
3882
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003883 if (intel_crtc->active)
3884 return;
3885
3886 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003887
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003888 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003889 if (encoder->pre_enable)
3890 encoder->pre_enable(encoder);
3891
Daniel Vetterf6736a12013-06-05 13:34:30 +02003892 i9xx_enable_pll(intel_crtc);
3893
Jesse Barnes2dd24552013-04-25 12:55:01 -07003894 i9xx_pfit_enable(intel_crtc);
3895
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003896 intel_crtc_load_lut(crtc);
3897
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003898 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003899 intel_enable_pipe(dev_priv, pipe, false, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003900 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003901 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003902 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003903 if (IS_G4X(dev))
3904 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003905 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003906
3907 /* Give the overlay scaler a chance to enable if it's on this pipe */
3908 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003909
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003910 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003911
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003912 for_each_encoder_on_crtc(dev, crtc, encoder)
3913 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003914}
3915
Daniel Vetter87476d62013-04-11 16:29:06 +02003916static void i9xx_pfit_disable(struct intel_crtc *crtc)
3917{
3918 struct drm_device *dev = crtc->base.dev;
3919 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003920
3921 if (!crtc->config.gmch_pfit.control)
3922 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003923
3924 assert_pipe_disabled(dev_priv, crtc->pipe);
3925
Daniel Vetter328d8e82013-05-08 10:36:31 +02003926 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3927 I915_READ(PFIT_CONTROL));
3928 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003929}
3930
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003931static void i9xx_crtc_disable(struct drm_crtc *crtc)
3932{
3933 struct drm_device *dev = crtc->dev;
3934 struct drm_i915_private *dev_priv = dev->dev_private;
3935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003936 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003937 int pipe = intel_crtc->pipe;
3938 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003939
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003940 if (!intel_crtc->active)
3941 return;
3942
Daniel Vetterea9d7582012-07-10 10:42:52 +02003943 for_each_encoder_on_crtc(dev, crtc, encoder)
3944 encoder->disable(encoder);
3945
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003946 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003947 intel_crtc_wait_for_pending_flips(crtc);
3948 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003949
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003950 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003951 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003952
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003953 intel_crtc_dpms_overlay(intel_crtc, false);
3954 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003955 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003956 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003957
Jesse Barnesb24e7172011-01-04 15:09:30 -08003958 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003959
Daniel Vetter87476d62013-04-11 16:29:06 +02003960 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003961
Jesse Barnes89b667f2013-04-18 14:51:36 -07003962 for_each_encoder_on_crtc(dev, crtc, encoder)
3963 if (encoder->post_disable)
3964 encoder->post_disable(encoder);
3965
Jesse Barnesf6071162013-10-01 10:41:38 -07003966 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3967 vlv_disable_pll(dev_priv, pipe);
3968 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003969 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003970
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003971 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003972 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003973
Chris Wilson6b383a72010-09-13 13:54:26 +01003974 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003975}
3976
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003977static void i9xx_crtc_off(struct drm_crtc *crtc)
3978{
3979}
3980
Daniel Vetter976f8a22012-07-08 22:34:21 +02003981static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3982 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003983{
3984 struct drm_device *dev = crtc->dev;
3985 struct drm_i915_master_private *master_priv;
3986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3987 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003988
3989 if (!dev->primary->master)
3990 return;
3991
3992 master_priv = dev->primary->master->driver_priv;
3993 if (!master_priv->sarea_priv)
3994 return;
3995
Jesse Barnes79e53942008-11-07 14:24:08 -08003996 switch (pipe) {
3997 case 0:
3998 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3999 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4000 break;
4001 case 1:
4002 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4003 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4004 break;
4005 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004006 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004007 break;
4008 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004009}
4010
Daniel Vetter976f8a22012-07-08 22:34:21 +02004011/**
4012 * Sets the power management mode of the pipe and plane.
4013 */
4014void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004015{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004016 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004017 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004018 struct intel_encoder *intel_encoder;
4019 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004020
Daniel Vetter976f8a22012-07-08 22:34:21 +02004021 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4022 enable |= intel_encoder->connectors_active;
4023
4024 if (enable)
4025 dev_priv->display.crtc_enable(crtc);
4026 else
4027 dev_priv->display.crtc_disable(crtc);
4028
4029 intel_crtc_update_sarea(crtc, enable);
4030}
4031
Daniel Vetter976f8a22012-07-08 22:34:21 +02004032static void intel_crtc_disable(struct drm_crtc *crtc)
4033{
4034 struct drm_device *dev = crtc->dev;
4035 struct drm_connector *connector;
4036 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004038
4039 /* crtc should still be enabled when we disable it. */
4040 WARN_ON(!crtc->enabled);
4041
4042 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004043 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004044 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004045 dev_priv->display.off(crtc);
4046
Chris Wilson931872f2012-01-16 23:01:13 +00004047 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004048 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004049 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004050
4051 if (crtc->fb) {
4052 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004053 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004054 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004055 crtc->fb = NULL;
4056 }
4057
4058 /* Update computed state. */
4059 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4060 if (!connector->encoder || !connector->encoder->crtc)
4061 continue;
4062
4063 if (connector->encoder->crtc != crtc)
4064 continue;
4065
4066 connector->dpms = DRM_MODE_DPMS_OFF;
4067 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004068 }
4069}
4070
Chris Wilsonea5b2132010-08-04 13:50:23 +01004071void intel_encoder_destroy(struct drm_encoder *encoder)
4072{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004073 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004074
Chris Wilsonea5b2132010-08-04 13:50:23 +01004075 drm_encoder_cleanup(encoder);
4076 kfree(intel_encoder);
4077}
4078
Damien Lespiau92373292013-08-08 22:28:57 +01004079/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004080 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4081 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004082static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004083{
4084 if (mode == DRM_MODE_DPMS_ON) {
4085 encoder->connectors_active = true;
4086
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004087 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004088 } else {
4089 encoder->connectors_active = false;
4090
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004091 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004092 }
4093}
4094
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004095/* Cross check the actual hw state with our own modeset state tracking (and it's
4096 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004097static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004098{
4099 if (connector->get_hw_state(connector)) {
4100 struct intel_encoder *encoder = connector->encoder;
4101 struct drm_crtc *crtc;
4102 bool encoder_enabled;
4103 enum pipe pipe;
4104
4105 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4106 connector->base.base.id,
4107 drm_get_connector_name(&connector->base));
4108
4109 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4110 "wrong connector dpms state\n");
4111 WARN(connector->base.encoder != &encoder->base,
4112 "active connector not linked to encoder\n");
4113 WARN(!encoder->connectors_active,
4114 "encoder->connectors_active not set\n");
4115
4116 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4117 WARN(!encoder_enabled, "encoder not enabled\n");
4118 if (WARN_ON(!encoder->base.crtc))
4119 return;
4120
4121 crtc = encoder->base.crtc;
4122
4123 WARN(!crtc->enabled, "crtc not enabled\n");
4124 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4125 WARN(pipe != to_intel_crtc(crtc)->pipe,
4126 "encoder active on the wrong pipe\n");
4127 }
4128}
4129
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004130/* Even simpler default implementation, if there's really no special case to
4131 * consider. */
4132void intel_connector_dpms(struct drm_connector *connector, int mode)
4133{
4134 struct intel_encoder *encoder = intel_attached_encoder(connector);
4135
4136 /* All the simple cases only support two dpms states. */
4137 if (mode != DRM_MODE_DPMS_ON)
4138 mode = DRM_MODE_DPMS_OFF;
4139
4140 if (mode == connector->dpms)
4141 return;
4142
4143 connector->dpms = mode;
4144
4145 /* Only need to change hw state when actually enabled */
4146 if (encoder->base.crtc)
4147 intel_encoder_dpms(encoder, mode);
4148 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02004149 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004150
Daniel Vetterb9805142012-08-31 17:37:33 +02004151 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004152}
4153
Daniel Vetterf0947c32012-07-02 13:10:34 +02004154/* Simple connector->get_hw_state implementation for encoders that support only
4155 * one connector and no cloning and hence the encoder state determines the state
4156 * of the connector. */
4157bool intel_connector_get_hw_state(struct intel_connector *connector)
4158{
Daniel Vetter24929352012-07-02 20:28:59 +02004159 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004160 struct intel_encoder *encoder = connector->encoder;
4161
4162 return encoder->get_hw_state(encoder, &pipe);
4163}
4164
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004165static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4166 struct intel_crtc_config *pipe_config)
4167{
4168 struct drm_i915_private *dev_priv = dev->dev_private;
4169 struct intel_crtc *pipe_B_crtc =
4170 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4171
4172 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4173 pipe_name(pipe), pipe_config->fdi_lanes);
4174 if (pipe_config->fdi_lanes > 4) {
4175 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4176 pipe_name(pipe), pipe_config->fdi_lanes);
4177 return false;
4178 }
4179
4180 if (IS_HASWELL(dev)) {
4181 if (pipe_config->fdi_lanes > 2) {
4182 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4183 pipe_config->fdi_lanes);
4184 return false;
4185 } else {
4186 return true;
4187 }
4188 }
4189
4190 if (INTEL_INFO(dev)->num_pipes == 2)
4191 return true;
4192
4193 /* Ivybridge 3 pipe is really complicated */
4194 switch (pipe) {
4195 case PIPE_A:
4196 return true;
4197 case PIPE_B:
4198 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4199 pipe_config->fdi_lanes > 2) {
4200 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4201 pipe_name(pipe), pipe_config->fdi_lanes);
4202 return false;
4203 }
4204 return true;
4205 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004206 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004207 pipe_B_crtc->config.fdi_lanes <= 2) {
4208 if (pipe_config->fdi_lanes > 2) {
4209 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4210 pipe_name(pipe), pipe_config->fdi_lanes);
4211 return false;
4212 }
4213 } else {
4214 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4215 return false;
4216 }
4217 return true;
4218 default:
4219 BUG();
4220 }
4221}
4222
Daniel Vettere29c22c2013-02-21 00:00:16 +01004223#define RETRY 1
4224static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4225 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004226{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004227 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004228 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004229 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004230 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004231
Daniel Vettere29c22c2013-02-21 00:00:16 +01004232retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004233 /* FDI is a binary signal running at ~2.7GHz, encoding
4234 * each output octet as 10 bits. The actual frequency
4235 * is stored as a divider into a 100MHz clock, and the
4236 * mode pixel clock is stored in units of 1KHz.
4237 * Hence the bw of each lane in terms of the mode signal
4238 * is:
4239 */
4240 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4241
Damien Lespiau241bfc32013-09-25 16:45:37 +01004242 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004243
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004244 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004245 pipe_config->pipe_bpp);
4246
4247 pipe_config->fdi_lanes = lane;
4248
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004249 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004250 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004251
Daniel Vettere29c22c2013-02-21 00:00:16 +01004252 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4253 intel_crtc->pipe, pipe_config);
4254 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4255 pipe_config->pipe_bpp -= 2*3;
4256 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4257 pipe_config->pipe_bpp);
4258 needs_recompute = true;
4259 pipe_config->bw_constrained = true;
4260
4261 goto retry;
4262 }
4263
4264 if (needs_recompute)
4265 return RETRY;
4266
4267 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004268}
4269
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004270static void hsw_compute_ips_config(struct intel_crtc *crtc,
4271 struct intel_crtc_config *pipe_config)
4272{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004273 pipe_config->ips_enabled = i915_enable_ips &&
4274 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004275 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004276}
4277
Daniel Vettera43f6e02013-06-07 23:10:32 +02004278static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004279 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004280{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004281 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004282 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004283
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004284 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004285 if (INTEL_INFO(dev)->gen < 4) {
4286 struct drm_i915_private *dev_priv = dev->dev_private;
4287 int clock_limit =
4288 dev_priv->display.get_display_clock_speed(dev);
4289
4290 /*
4291 * Enable pixel doubling when the dot clock
4292 * is > 90% of the (display) core speed.
4293 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004294 * GDG double wide on either pipe,
4295 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004296 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004297 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004298 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004299 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004300 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004301 }
4302
Damien Lespiau241bfc32013-09-25 16:45:37 +01004303 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004304 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004305 }
Chris Wilson89749352010-09-12 18:25:19 +01004306
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004307 /*
4308 * Pipe horizontal size must be even in:
4309 * - DVO ganged mode
4310 * - LVDS dual channel mode
4311 * - Double wide pipe
4312 */
4313 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4314 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4315 pipe_config->pipe_src_w &= ~1;
4316
Damien Lespiau8693a822013-05-03 18:48:11 +01004317 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4318 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004319 */
4320 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4321 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004322 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004323
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004324 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004325 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004326 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004327 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4328 * for lvds. */
4329 pipe_config->pipe_bpp = 8*3;
4330 }
4331
Damien Lespiauf5adf942013-06-24 18:29:34 +01004332 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004333 hsw_compute_ips_config(crtc, pipe_config);
4334
4335 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4336 * clock survives for now. */
4337 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4338 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004339
Daniel Vetter877d48d2013-04-19 11:24:43 +02004340 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004341 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004342
Daniel Vettere29c22c2013-02-21 00:00:16 +01004343 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004344}
4345
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004346static int valleyview_get_display_clock_speed(struct drm_device *dev)
4347{
4348 return 400000; /* FIXME */
4349}
4350
Jesse Barnese70236a2009-09-21 10:42:27 -07004351static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004352{
Jesse Barnese70236a2009-09-21 10:42:27 -07004353 return 400000;
4354}
Jesse Barnes79e53942008-11-07 14:24:08 -08004355
Jesse Barnese70236a2009-09-21 10:42:27 -07004356static int i915_get_display_clock_speed(struct drm_device *dev)
4357{
4358 return 333000;
4359}
Jesse Barnes79e53942008-11-07 14:24:08 -08004360
Jesse Barnese70236a2009-09-21 10:42:27 -07004361static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4362{
4363 return 200000;
4364}
Jesse Barnes79e53942008-11-07 14:24:08 -08004365
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004366static int pnv_get_display_clock_speed(struct drm_device *dev)
4367{
4368 u16 gcfgc = 0;
4369
4370 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4371
4372 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4373 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4374 return 267000;
4375 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4376 return 333000;
4377 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4378 return 444000;
4379 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4380 return 200000;
4381 default:
4382 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4383 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4384 return 133000;
4385 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4386 return 167000;
4387 }
4388}
4389
Jesse Barnese70236a2009-09-21 10:42:27 -07004390static int i915gm_get_display_clock_speed(struct drm_device *dev)
4391{
4392 u16 gcfgc = 0;
4393
4394 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4395
4396 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004397 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004398 else {
4399 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4400 case GC_DISPLAY_CLOCK_333_MHZ:
4401 return 333000;
4402 default:
4403 case GC_DISPLAY_CLOCK_190_200_MHZ:
4404 return 190000;
4405 }
4406 }
4407}
Jesse Barnes79e53942008-11-07 14:24:08 -08004408
Jesse Barnese70236a2009-09-21 10:42:27 -07004409static int i865_get_display_clock_speed(struct drm_device *dev)
4410{
4411 return 266000;
4412}
4413
4414static int i855_get_display_clock_speed(struct drm_device *dev)
4415{
4416 u16 hpllcc = 0;
4417 /* Assume that the hardware is in the high speed state. This
4418 * should be the default.
4419 */
4420 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4421 case GC_CLOCK_133_200:
4422 case GC_CLOCK_100_200:
4423 return 200000;
4424 case GC_CLOCK_166_250:
4425 return 250000;
4426 case GC_CLOCK_100_133:
4427 return 133000;
4428 }
4429
4430 /* Shouldn't happen */
4431 return 0;
4432}
4433
4434static int i830_get_display_clock_speed(struct drm_device *dev)
4435{
4436 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004437}
4438
Zhenyu Wang2c072452009-06-05 15:38:42 +08004439static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004440intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004441{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004442 while (*num > DATA_LINK_M_N_MASK ||
4443 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004444 *num >>= 1;
4445 *den >>= 1;
4446 }
4447}
4448
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004449static void compute_m_n(unsigned int m, unsigned int n,
4450 uint32_t *ret_m, uint32_t *ret_n)
4451{
4452 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4453 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4454 intel_reduce_m_n_ratio(ret_m, ret_n);
4455}
4456
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004457void
4458intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4459 int pixel_clock, int link_clock,
4460 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004461{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004462 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004463
4464 compute_m_n(bits_per_pixel * pixel_clock,
4465 link_clock * nlanes * 8,
4466 &m_n->gmch_m, &m_n->gmch_n);
4467
4468 compute_m_n(pixel_clock, link_clock,
4469 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004470}
4471
Chris Wilsona7615032011-01-12 17:04:08 +00004472static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4473{
Keith Packard72bbe58c2011-09-26 16:09:45 -07004474 if (i915_panel_use_ssc >= 0)
4475 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004476 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004477 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004478}
4479
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004480static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4481{
4482 struct drm_device *dev = crtc->dev;
4483 struct drm_i915_private *dev_priv = dev->dev_private;
4484 int refclk;
4485
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004486 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004487 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004488 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004489 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004490 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004491 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4492 refclk / 1000);
4493 } else if (!IS_GEN2(dev)) {
4494 refclk = 96000;
4495 } else {
4496 refclk = 48000;
4497 }
4498
4499 return refclk;
4500}
4501
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004502static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004503{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004504 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004505}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004506
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004507static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4508{
4509 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004510}
4511
Daniel Vetterf47709a2013-03-28 10:42:02 +01004512static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004513 intel_clock_t *reduced_clock)
4514{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004515 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004516 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004517 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004518 u32 fp, fp2 = 0;
4519
4520 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004521 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004522 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004523 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004524 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004525 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004526 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004527 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004528 }
4529
4530 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004531 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004532
Daniel Vetterf47709a2013-03-28 10:42:02 +01004533 crtc->lowfreq_avail = false;
4534 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004535 reduced_clock && i915_powersave) {
4536 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004537 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004538 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004539 } else {
4540 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004541 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004542 }
4543}
4544
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004545static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4546 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004547{
4548 u32 reg_val;
4549
4550 /*
4551 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4552 * and set it to a reasonable value instead.
4553 */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004554 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004555 reg_val &= 0xffffff00;
4556 reg_val |= 0x00000030;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004557 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004558
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004559 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004560 reg_val &= 0x8cffffff;
4561 reg_val = 0x8c000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004562 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, 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_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004565 reg_val &= 0xffffff00;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004566 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004567
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004568 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004569 reg_val &= 0x00ffffff;
4570 reg_val |= 0xb0000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004571 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004572}
4573
Daniel Vetterb5518422013-05-03 11:49:48 +02004574static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4575 struct intel_link_m_n *m_n)
4576{
4577 struct drm_device *dev = crtc->base.dev;
4578 struct drm_i915_private *dev_priv = dev->dev_private;
4579 int pipe = crtc->pipe;
4580
Daniel Vettere3b95f12013-05-03 11:49:49 +02004581 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4582 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4583 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4584 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004585}
4586
4587static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4588 struct intel_link_m_n *m_n)
4589{
4590 struct drm_device *dev = crtc->base.dev;
4591 struct drm_i915_private *dev_priv = dev->dev_private;
4592 int pipe = crtc->pipe;
4593 enum transcoder transcoder = crtc->config.cpu_transcoder;
4594
4595 if (INTEL_INFO(dev)->gen >= 5) {
4596 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4597 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4598 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4599 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4600 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004601 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4602 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4603 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4604 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004605 }
4606}
4607
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004608static void intel_dp_set_m_n(struct intel_crtc *crtc)
4609{
4610 if (crtc->config.has_pch_encoder)
4611 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4612 else
4613 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4614}
4615
Daniel Vetterf47709a2013-03-28 10:42:02 +01004616static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004617{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004618 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004619 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004620 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004621 u32 dpll, mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004622 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004623 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004624
Daniel Vetter09153002012-12-12 14:06:44 +01004625 mutex_lock(&dev_priv->dpio_lock);
4626
Daniel Vetterf47709a2013-03-28 10:42:02 +01004627 bestn = crtc->config.dpll.n;
4628 bestm1 = crtc->config.dpll.m1;
4629 bestm2 = crtc->config.dpll.m2;
4630 bestp1 = crtc->config.dpll.p1;
4631 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004632
Jesse Barnes89b667f2013-04-18 14:51:36 -07004633 /* See eDP HDMI DPIO driver vbios notes doc */
4634
4635 /* PLL B needs special handling */
4636 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004637 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004638
4639 /* Set up Tx target for periodic Rcomp update */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004640 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004641
4642 /* Disable target IRef on PLL */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004643 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004644 reg_val &= 0x00ffffff;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004645 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004646
4647 /* Disable fast lock */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004648 vlv_dpio_write(dev_priv, pipe, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004649
4650 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004651 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4652 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4653 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004654 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004655
4656 /*
4657 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4658 * but we don't support that).
4659 * Note: don't use the DAC post divider as it seems unstable.
4660 */
4661 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004662 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004663
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004664 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004665 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004666
Jesse Barnes89b667f2013-04-18 14:51:36 -07004667 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004668 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004669 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004670 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004671 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004672 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004673 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004674 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004675 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004676
Jesse Barnes89b667f2013-04-18 14:51:36 -07004677 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4678 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4679 /* Use SSC source */
4680 if (!pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004681 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004682 0x0df40000);
4683 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004684 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004685 0x0df70000);
4686 } else { /* HDMI or VGA */
4687 /* Use bend source */
4688 if (!pipe)
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
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004692 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004693 0x0df40000);
4694 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004695
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004696 coreclk = vlv_dpio_read(dev_priv, pipe, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004697 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4698 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4699 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4700 coreclk |= 0x01000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004701 vlv_dpio_write(dev_priv, pipe, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004702
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004703 vlv_dpio_write(dev_priv, pipe, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004704
Jesse Barnes89b667f2013-04-18 14:51:36 -07004705 /* Enable DPIO clock input */
4706 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4707 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004708 /* We should never disable this, set it here for state tracking */
4709 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004710 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004711 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004712 crtc->config.dpll_hw_state.dpll = dpll;
4713
Daniel Vetteref1b4602013-06-01 17:17:04 +02004714 dpll_md = (crtc->config.pixel_multiplier - 1)
4715 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004716 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4717
Daniel Vetterf47709a2013-03-28 10:42:02 +01004718 if (crtc->config.has_dp_encoder)
4719 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304720
Daniel Vetter09153002012-12-12 14:06:44 +01004721 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004722}
4723
Daniel Vetterf47709a2013-03-28 10:42:02 +01004724static void i9xx_update_pll(struct intel_crtc *crtc,
4725 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004726 int num_connectors)
4727{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004728 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004729 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004730 u32 dpll;
4731 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004732 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004733
Daniel Vetterf47709a2013-03-28 10:42:02 +01004734 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304735
Daniel Vetterf47709a2013-03-28 10:42:02 +01004736 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4737 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004738
4739 dpll = DPLL_VGA_MODE_DIS;
4740
Daniel Vetterf47709a2013-03-28 10:42:02 +01004741 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004742 dpll |= DPLLB_MODE_LVDS;
4743 else
4744 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004745
Daniel Vetteref1b4602013-06-01 17:17:04 +02004746 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004747 dpll |= (crtc->config.pixel_multiplier - 1)
4748 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004749 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004750
4751 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004752 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004753
Daniel Vetterf47709a2013-03-28 10:42:02 +01004754 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004755 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004756
4757 /* compute bitmask from p1 value */
4758 if (IS_PINEVIEW(dev))
4759 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4760 else {
4761 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4762 if (IS_G4X(dev) && reduced_clock)
4763 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4764 }
4765 switch (clock->p2) {
4766 case 5:
4767 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4768 break;
4769 case 7:
4770 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4771 break;
4772 case 10:
4773 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4774 break;
4775 case 14:
4776 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4777 break;
4778 }
4779 if (INTEL_INFO(dev)->gen >= 4)
4780 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4781
Daniel Vetter09ede542013-04-30 14:01:45 +02004782 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004783 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004784 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004785 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4786 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4787 else
4788 dpll |= PLL_REF_INPUT_DREFCLK;
4789
4790 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004791 crtc->config.dpll_hw_state.dpll = dpll;
4792
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004793 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004794 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4795 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004796 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004797 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004798
4799 if (crtc->config.has_dp_encoder)
4800 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004801}
4802
Daniel Vetterf47709a2013-03-28 10:42:02 +01004803static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004804 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004805 int num_connectors)
4806{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004807 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004808 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004809 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004810 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004811
Daniel Vetterf47709a2013-03-28 10:42:02 +01004812 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304813
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004814 dpll = DPLL_VGA_MODE_DIS;
4815
Daniel Vetterf47709a2013-03-28 10:42:02 +01004816 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004817 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4818 } else {
4819 if (clock->p1 == 2)
4820 dpll |= PLL_P1_DIVIDE_BY_TWO;
4821 else
4822 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4823 if (clock->p2 == 4)
4824 dpll |= PLL_P2_DIVIDE_BY_4;
4825 }
4826
Daniel Vetter4a33e482013-07-06 12:52:05 +02004827 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4828 dpll |= DPLL_DVO_2X_MODE;
4829
Daniel Vetterf47709a2013-03-28 10:42:02 +01004830 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004831 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4832 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4833 else
4834 dpll |= PLL_REF_INPUT_DREFCLK;
4835
4836 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004837 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004838}
4839
Daniel Vetter8a654f32013-06-01 17:16:22 +02004840static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004841{
4842 struct drm_device *dev = intel_crtc->base.dev;
4843 struct drm_i915_private *dev_priv = dev->dev_private;
4844 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004845 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004846 struct drm_display_mode *adjusted_mode =
4847 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004848 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4849
4850 /* We need to be careful not to changed the adjusted mode, for otherwise
4851 * the hw state checker will get angry at the mismatch. */
4852 crtc_vtotal = adjusted_mode->crtc_vtotal;
4853 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004854
4855 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4856 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004857 crtc_vtotal -= 1;
4858 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004859 vsyncshift = adjusted_mode->crtc_hsync_start
4860 - adjusted_mode->crtc_htotal / 2;
4861 } else {
4862 vsyncshift = 0;
4863 }
4864
4865 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004866 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004867
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004868 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004869 (adjusted_mode->crtc_hdisplay - 1) |
4870 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004871 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004872 (adjusted_mode->crtc_hblank_start - 1) |
4873 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004874 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004875 (adjusted_mode->crtc_hsync_start - 1) |
4876 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4877
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004878 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004879 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004880 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004881 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004882 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004883 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004884 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004885 (adjusted_mode->crtc_vsync_start - 1) |
4886 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4887
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004888 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4889 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4890 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4891 * bits. */
4892 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4893 (pipe == PIPE_B || pipe == PIPE_C))
4894 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4895
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004896 /* pipesrc controls the size that is scaled from, which should
4897 * always be the user's requested size.
4898 */
4899 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004900 ((intel_crtc->config.pipe_src_w - 1) << 16) |
4901 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004902}
4903
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004904static void intel_get_pipe_timings(struct intel_crtc *crtc,
4905 struct intel_crtc_config *pipe_config)
4906{
4907 struct drm_device *dev = crtc->base.dev;
4908 struct drm_i915_private *dev_priv = dev->dev_private;
4909 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4910 uint32_t tmp;
4911
4912 tmp = I915_READ(HTOTAL(cpu_transcoder));
4913 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4914 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4915 tmp = I915_READ(HBLANK(cpu_transcoder));
4916 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4917 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4918 tmp = I915_READ(HSYNC(cpu_transcoder));
4919 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4920 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4921
4922 tmp = I915_READ(VTOTAL(cpu_transcoder));
4923 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4924 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4925 tmp = I915_READ(VBLANK(cpu_transcoder));
4926 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4927 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4928 tmp = I915_READ(VSYNC(cpu_transcoder));
4929 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4930 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4931
4932 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4933 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4934 pipe_config->adjusted_mode.crtc_vtotal += 1;
4935 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4936 }
4937
4938 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004939 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
4940 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
4941
4942 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
4943 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004944}
4945
Jesse Barnesbabea612013-06-26 18:57:38 +03004946static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4947 struct intel_crtc_config *pipe_config)
4948{
4949 struct drm_crtc *crtc = &intel_crtc->base;
4950
4951 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4952 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4953 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4954 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4955
4956 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4957 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4958 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4959 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4960
4961 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4962
Damien Lespiau241bfc32013-09-25 16:45:37 +01004963 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03004964 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4965}
4966
Daniel Vetter84b046f2013-02-19 18:48:54 +01004967static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4968{
4969 struct drm_device *dev = intel_crtc->base.dev;
4970 struct drm_i915_private *dev_priv = dev->dev_private;
4971 uint32_t pipeconf;
4972
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004973 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004974
Daniel Vetter67c72a12013-09-24 11:46:14 +02004975 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
4976 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
4977 pipeconf |= PIPECONF_ENABLE;
4978
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004979 if (intel_crtc->config.double_wide)
4980 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004981
Daniel Vetterff9ce462013-04-24 14:57:17 +02004982 /* only g4x and later have fancy bpc/dither controls */
4983 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004984 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4985 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4986 pipeconf |= PIPECONF_DITHER_EN |
4987 PIPECONF_DITHER_TYPE_SP;
4988
4989 switch (intel_crtc->config.pipe_bpp) {
4990 case 18:
4991 pipeconf |= PIPECONF_6BPC;
4992 break;
4993 case 24:
4994 pipeconf |= PIPECONF_8BPC;
4995 break;
4996 case 30:
4997 pipeconf |= PIPECONF_10BPC;
4998 break;
4999 default:
5000 /* Case prevented by intel_choose_pipe_bpp_dither. */
5001 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005002 }
5003 }
5004
5005 if (HAS_PIPE_CXSR(dev)) {
5006 if (intel_crtc->lowfreq_avail) {
5007 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5008 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5009 } else {
5010 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005011 }
5012 }
5013
Daniel Vetter84b046f2013-02-19 18:48:54 +01005014 if (!IS_GEN2(dev) &&
5015 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5016 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5017 else
5018 pipeconf |= PIPECONF_PROGRESSIVE;
5019
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005020 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5021 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005022
Daniel Vetter84b046f2013-02-19 18:48:54 +01005023 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5024 POSTING_READ(PIPECONF(intel_crtc->pipe));
5025}
5026
Eric Anholtf564048e2011-03-30 13:01:02 -07005027static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005028 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005029 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005030{
5031 struct drm_device *dev = crtc->dev;
5032 struct drm_i915_private *dev_priv = dev->dev_private;
5033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5034 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005035 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005036 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005037 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005038 u32 dspcntr;
Daniel Vettera16af722013-04-30 14:01:44 +02005039 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005040 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005041 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005042 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005043 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005044
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005045 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005046 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005047 case INTEL_OUTPUT_LVDS:
5048 is_lvds = true;
5049 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005050 case INTEL_OUTPUT_DSI:
5051 is_dsi = true;
5052 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005053 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005054
Eric Anholtc751ce42010-03-25 11:48:48 -07005055 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005056 }
5057
Jani Nikulaf2335332013-09-13 11:03:09 +03005058 if (is_dsi)
5059 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005060
Jani Nikulaf2335332013-09-13 11:03:09 +03005061 if (!intel_crtc->config.clock_set) {
5062 refclk = i9xx_get_refclk(crtc, num_connectors);
5063
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005064 /*
5065 * Returns a set of divisors for the desired target clock with
5066 * the given refclk, or FALSE. The returned values represent
5067 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5068 * 2) / p1 / p2.
5069 */
5070 limit = intel_limit(crtc, refclk);
5071 ok = dev_priv->display.find_dpll(limit, crtc,
5072 intel_crtc->config.port_clock,
5073 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005074 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005075 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5076 return -EINVAL;
5077 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005078
Jani Nikulaf2335332013-09-13 11:03:09 +03005079 if (is_lvds && dev_priv->lvds_downclock_avail) {
5080 /*
5081 * Ensure we match the reduced clock's P to the target
5082 * clock. If the clocks don't match, we can't switch
5083 * the display clock by using the FP0/FP1. In such case
5084 * we will disable the LVDS downclock feature.
5085 */
5086 has_reduced_clock =
5087 dev_priv->display.find_dpll(limit, crtc,
5088 dev_priv->lvds_downclock,
5089 refclk, &clock,
5090 &reduced_clock);
5091 }
5092 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005093 intel_crtc->config.dpll.n = clock.n;
5094 intel_crtc->config.dpll.m1 = clock.m1;
5095 intel_crtc->config.dpll.m2 = clock.m2;
5096 intel_crtc->config.dpll.p1 = clock.p1;
5097 intel_crtc->config.dpll.p2 = clock.p2;
5098 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005099
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005100 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005101 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305102 has_reduced_clock ? &reduced_clock : NULL,
5103 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005104 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005105 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005106 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005107 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005108 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005109 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005110 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005111
Jani Nikulaf2335332013-09-13 11:03:09 +03005112skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005113 /* Set up the display plane register */
5114 dspcntr = DISPPLANE_GAMMA_ENABLE;
5115
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005116 if (!IS_VALLEYVIEW(dev)) {
5117 if (pipe == 0)
5118 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5119 else
5120 dspcntr |= DISPPLANE_SEL_PIPE_B;
5121 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005122
Daniel Vetter8a654f32013-06-01 17:16:22 +02005123 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005124
5125 /* pipesrc and dspsize control the size that is scaled from,
5126 * which should always be the user's requested size.
5127 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005128 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005129 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5130 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005131 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005132
Daniel Vetter84b046f2013-02-19 18:48:54 +01005133 i9xx_set_pipeconf(intel_crtc);
5134
Eric Anholtf564048e2011-03-30 13:01:02 -07005135 I915_WRITE(DSPCNTR(plane), dspcntr);
5136 POSTING_READ(DSPCNTR(plane));
5137
Daniel Vetter94352cf2012-07-05 22:51:56 +02005138 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005139
Eric Anholtf564048e2011-03-30 13:01:02 -07005140 return ret;
5141}
5142
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005143static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5144 struct intel_crtc_config *pipe_config)
5145{
5146 struct drm_device *dev = crtc->base.dev;
5147 struct drm_i915_private *dev_priv = dev->dev_private;
5148 uint32_t tmp;
5149
5150 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005151 if (!(tmp & PFIT_ENABLE))
5152 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005153
Daniel Vetter06922822013-07-11 13:35:40 +02005154 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005155 if (INTEL_INFO(dev)->gen < 4) {
5156 if (crtc->pipe != PIPE_B)
5157 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005158 } else {
5159 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5160 return;
5161 }
5162
Daniel Vetter06922822013-07-11 13:35:40 +02005163 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005164 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5165 if (INTEL_INFO(dev)->gen < 5)
5166 pipe_config->gmch_pfit.lvds_border_bits =
5167 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5168}
5169
Jesse Barnesacbec812013-09-20 11:29:32 -07005170static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5171 struct intel_crtc_config *pipe_config)
5172{
5173 struct drm_device *dev = crtc->base.dev;
5174 struct drm_i915_private *dev_priv = dev->dev_private;
5175 int pipe = pipe_config->cpu_transcoder;
5176 intel_clock_t clock;
5177 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005178 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005179
5180 mutex_lock(&dev_priv->dpio_lock);
5181 mdiv = vlv_dpio_read(dev_priv, pipe, DPIO_DIV(pipe));
5182 mutex_unlock(&dev_priv->dpio_lock);
5183
5184 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5185 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5186 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5187 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5188 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5189
Chris Wilson662c6ec2013-09-25 14:24:01 -07005190 clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
5191 clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
Jesse Barnesacbec812013-09-20 11:29:32 -07005192
5193 pipe_config->port_clock = clock.dot / 10;
5194}
5195
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005196static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5197 struct intel_crtc_config *pipe_config)
5198{
5199 struct drm_device *dev = crtc->base.dev;
5200 struct drm_i915_private *dev_priv = dev->dev_private;
5201 uint32_t tmp;
5202
Daniel Vettere143a212013-07-04 12:01:15 +02005203 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005204 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005205
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005206 tmp = I915_READ(PIPECONF(crtc->pipe));
5207 if (!(tmp & PIPECONF_ENABLE))
5208 return false;
5209
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005210 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5211 switch (tmp & PIPECONF_BPC_MASK) {
5212 case PIPECONF_6BPC:
5213 pipe_config->pipe_bpp = 18;
5214 break;
5215 case PIPECONF_8BPC:
5216 pipe_config->pipe_bpp = 24;
5217 break;
5218 case PIPECONF_10BPC:
5219 pipe_config->pipe_bpp = 30;
5220 break;
5221 default:
5222 break;
5223 }
5224 }
5225
Ville Syrjälä282740f2013-09-04 18:30:03 +03005226 if (INTEL_INFO(dev)->gen < 4)
5227 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5228
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005229 intel_get_pipe_timings(crtc, pipe_config);
5230
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005231 i9xx_get_pfit_config(crtc, pipe_config);
5232
Daniel Vetter6c49f242013-06-06 12:45:25 +02005233 if (INTEL_INFO(dev)->gen >= 4) {
5234 tmp = I915_READ(DPLL_MD(crtc->pipe));
5235 pipe_config->pixel_multiplier =
5236 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5237 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005238 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005239 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5240 tmp = I915_READ(DPLL(crtc->pipe));
5241 pipe_config->pixel_multiplier =
5242 ((tmp & SDVO_MULTIPLIER_MASK)
5243 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5244 } else {
5245 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5246 * port and will be fixed up in the encoder->get_config
5247 * function. */
5248 pipe_config->pixel_multiplier = 1;
5249 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005250 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5251 if (!IS_VALLEYVIEW(dev)) {
5252 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5253 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005254 } else {
5255 /* Mask out read-only status bits. */
5256 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5257 DPLL_PORTC_READY_MASK |
5258 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005259 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005260
Jesse Barnesacbec812013-09-20 11:29:32 -07005261 if (IS_VALLEYVIEW(dev))
5262 vlv_crtc_clock_get(crtc, pipe_config);
5263 else
5264 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005265
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005266 return true;
5267}
5268
Paulo Zanonidde86e22012-12-01 12:04:25 -02005269static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005270{
5271 struct drm_i915_private *dev_priv = dev->dev_private;
5272 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005273 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005274 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005275 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005276 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005277 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005278 bool has_ck505 = false;
5279 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005280
5281 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005282 list_for_each_entry(encoder, &mode_config->encoder_list,
5283 base.head) {
5284 switch (encoder->type) {
5285 case INTEL_OUTPUT_LVDS:
5286 has_panel = true;
5287 has_lvds = true;
5288 break;
5289 case INTEL_OUTPUT_EDP:
5290 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005291 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005292 has_cpu_edp = true;
5293 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005294 }
5295 }
5296
Keith Packard99eb6a02011-09-26 14:29:12 -07005297 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005298 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005299 can_ssc = has_ck505;
5300 } else {
5301 has_ck505 = false;
5302 can_ssc = true;
5303 }
5304
Imre Deak2de69052013-05-08 13:14:04 +03005305 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5306 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005307
5308 /* Ironlake: try to setup display ref clock before DPLL
5309 * enabling. This is only under driver's control after
5310 * PCH B stepping, previous chipset stepping should be
5311 * ignoring this setting.
5312 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005313 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005314
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005315 /* As we must carefully and slowly disable/enable each source in turn,
5316 * compute the final state we want first and check if we need to
5317 * make any changes at all.
5318 */
5319 final = val;
5320 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005321 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005322 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005323 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005324 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5325
5326 final &= ~DREF_SSC_SOURCE_MASK;
5327 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5328 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005329
Keith Packard199e5d72011-09-22 12:01:57 -07005330 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005331 final |= DREF_SSC_SOURCE_ENABLE;
5332
5333 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5334 final |= DREF_SSC1_ENABLE;
5335
5336 if (has_cpu_edp) {
5337 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5338 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5339 else
5340 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5341 } else
5342 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5343 } else {
5344 final |= DREF_SSC_SOURCE_DISABLE;
5345 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5346 }
5347
5348 if (final == val)
5349 return;
5350
5351 /* Always enable nonspread source */
5352 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5353
5354 if (has_ck505)
5355 val |= DREF_NONSPREAD_CK505_ENABLE;
5356 else
5357 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5358
5359 if (has_panel) {
5360 val &= ~DREF_SSC_SOURCE_MASK;
5361 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005362
Keith Packard199e5d72011-09-22 12:01:57 -07005363 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005364 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005365 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005366 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005367 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005368 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005369
5370 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005371 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005372 POSTING_READ(PCH_DREF_CONTROL);
5373 udelay(200);
5374
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005375 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005376
5377 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005378 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005379 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005380 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005381 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005382 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005383 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005384 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005385 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005386 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005387
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005388 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005389 POSTING_READ(PCH_DREF_CONTROL);
5390 udelay(200);
5391 } else {
5392 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5393
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005394 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005395
5396 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005397 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005398
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005399 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005400 POSTING_READ(PCH_DREF_CONTROL);
5401 udelay(200);
5402
5403 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005404 val &= ~DREF_SSC_SOURCE_MASK;
5405 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005406
5407 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005408 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005409
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005410 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005411 POSTING_READ(PCH_DREF_CONTROL);
5412 udelay(200);
5413 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005414
5415 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005416}
5417
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005418static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005419{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005420 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005421
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005422 tmp = I915_READ(SOUTH_CHICKEN2);
5423 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5424 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005425
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005426 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5427 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5428 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005429
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005430 tmp = I915_READ(SOUTH_CHICKEN2);
5431 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5432 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005433
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005434 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5435 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5436 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005437}
5438
5439/* WaMPhyProgramming:hsw */
5440static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5441{
5442 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005443
5444 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5445 tmp &= ~(0xFF << 24);
5446 tmp |= (0x12 << 24);
5447 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5448
Paulo Zanonidde86e22012-12-01 12:04:25 -02005449 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5450 tmp |= (1 << 11);
5451 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5452
5453 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5454 tmp |= (1 << 11);
5455 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5456
Paulo Zanonidde86e22012-12-01 12:04:25 -02005457 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5458 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5459 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5460
5461 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5462 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5463 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5464
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005465 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5466 tmp &= ~(7 << 13);
5467 tmp |= (5 << 13);
5468 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005469
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005470 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5471 tmp &= ~(7 << 13);
5472 tmp |= (5 << 13);
5473 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005474
5475 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5476 tmp &= ~0xFF;
5477 tmp |= 0x1C;
5478 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5479
5480 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5481 tmp &= ~0xFF;
5482 tmp |= 0x1C;
5483 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5484
5485 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5486 tmp &= ~(0xFF << 16);
5487 tmp |= (0x1C << 16);
5488 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5489
5490 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5491 tmp &= ~(0xFF << 16);
5492 tmp |= (0x1C << 16);
5493 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5494
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005495 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5496 tmp |= (1 << 27);
5497 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005498
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005499 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5500 tmp |= (1 << 27);
5501 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005502
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005503 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5504 tmp &= ~(0xF << 28);
5505 tmp |= (4 << 28);
5506 intel_sbi_write(dev_priv, 0x20EC, 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, 0x21EC, SBI_MPHY);
5509 tmp &= ~(0xF << 28);
5510 tmp |= (4 << 28);
5511 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005512}
5513
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005514/* Implements 3 different sequences from BSpec chapter "Display iCLK
5515 * Programming" based on the parameters passed:
5516 * - Sequence to enable CLKOUT_DP
5517 * - Sequence to enable CLKOUT_DP without spread
5518 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5519 */
5520static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5521 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005522{
5523 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005524 uint32_t reg, tmp;
5525
5526 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5527 with_spread = true;
5528 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5529 with_fdi, "LP PCH doesn't have FDI\n"))
5530 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005531
5532 mutex_lock(&dev_priv->dpio_lock);
5533
5534 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5535 tmp &= ~SBI_SSCCTL_DISABLE;
5536 tmp |= SBI_SSCCTL_PATHALT;
5537 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5538
5539 udelay(24);
5540
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005541 if (with_spread) {
5542 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5543 tmp &= ~SBI_SSCCTL_PATHALT;
5544 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005545
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005546 if (with_fdi) {
5547 lpt_reset_fdi_mphy(dev_priv);
5548 lpt_program_fdi_mphy(dev_priv);
5549 }
5550 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005551
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005552 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5553 SBI_GEN0 : SBI_DBUFF0;
5554 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5555 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5556 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005557
5558 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005559}
5560
Paulo Zanoni47701c32013-07-23 11:19:25 -03005561/* Sequence to disable CLKOUT_DP */
5562static void lpt_disable_clkout_dp(struct drm_device *dev)
5563{
5564 struct drm_i915_private *dev_priv = dev->dev_private;
5565 uint32_t reg, tmp;
5566
5567 mutex_lock(&dev_priv->dpio_lock);
5568
5569 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5570 SBI_GEN0 : SBI_DBUFF0;
5571 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5572 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5573 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5574
5575 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5576 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5577 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5578 tmp |= SBI_SSCCTL_PATHALT;
5579 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5580 udelay(32);
5581 }
5582 tmp |= SBI_SSCCTL_DISABLE;
5583 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5584 }
5585
5586 mutex_unlock(&dev_priv->dpio_lock);
5587}
5588
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005589static void lpt_init_pch_refclk(struct drm_device *dev)
5590{
5591 struct drm_mode_config *mode_config = &dev->mode_config;
5592 struct intel_encoder *encoder;
5593 bool has_vga = false;
5594
5595 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5596 switch (encoder->type) {
5597 case INTEL_OUTPUT_ANALOG:
5598 has_vga = true;
5599 break;
5600 }
5601 }
5602
Paulo Zanoni47701c32013-07-23 11:19:25 -03005603 if (has_vga)
5604 lpt_enable_clkout_dp(dev, true, true);
5605 else
5606 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005607}
5608
Paulo Zanonidde86e22012-12-01 12:04:25 -02005609/*
5610 * Initialize reference clocks when the driver loads
5611 */
5612void intel_init_pch_refclk(struct drm_device *dev)
5613{
5614 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5615 ironlake_init_pch_refclk(dev);
5616 else if (HAS_PCH_LPT(dev))
5617 lpt_init_pch_refclk(dev);
5618}
5619
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005620static int ironlake_get_refclk(struct drm_crtc *crtc)
5621{
5622 struct drm_device *dev = crtc->dev;
5623 struct drm_i915_private *dev_priv = dev->dev_private;
5624 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005625 int num_connectors = 0;
5626 bool is_lvds = false;
5627
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005628 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005629 switch (encoder->type) {
5630 case INTEL_OUTPUT_LVDS:
5631 is_lvds = true;
5632 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005633 }
5634 num_connectors++;
5635 }
5636
5637 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5638 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005639 dev_priv->vbt.lvds_ssc_freq);
5640 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005641 }
5642
5643 return 120000;
5644}
5645
Daniel Vetter6ff93602013-04-19 11:24:36 +02005646static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005647{
5648 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5650 int pipe = intel_crtc->pipe;
5651 uint32_t val;
5652
Daniel Vetter78114072013-06-13 00:54:57 +02005653 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005654
Daniel Vetter965e0c42013-03-27 00:44:57 +01005655 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005656 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005657 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005658 break;
5659 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005660 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005661 break;
5662 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005663 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005664 break;
5665 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005666 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005667 break;
5668 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005669 /* Case prevented by intel_choose_pipe_bpp_dither. */
5670 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005671 }
5672
Daniel Vetterd8b32242013-04-25 17:54:44 +02005673 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005674 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5675
Daniel Vetter6ff93602013-04-19 11:24:36 +02005676 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005677 val |= PIPECONF_INTERLACED_ILK;
5678 else
5679 val |= PIPECONF_PROGRESSIVE;
5680
Daniel Vetter50f3b012013-03-27 00:44:56 +01005681 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005682 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005683
Paulo Zanonic8203562012-09-12 10:06:29 -03005684 I915_WRITE(PIPECONF(pipe), val);
5685 POSTING_READ(PIPECONF(pipe));
5686}
5687
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005688/*
5689 * Set up the pipe CSC unit.
5690 *
5691 * Currently only full range RGB to limited range RGB conversion
5692 * is supported, but eventually this should handle various
5693 * RGB<->YCbCr scenarios as well.
5694 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005695static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005696{
5697 struct drm_device *dev = crtc->dev;
5698 struct drm_i915_private *dev_priv = dev->dev_private;
5699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5700 int pipe = intel_crtc->pipe;
5701 uint16_t coeff = 0x7800; /* 1.0 */
5702
5703 /*
5704 * TODO: Check what kind of values actually come out of the pipe
5705 * with these coeff/postoff values and adjust to get the best
5706 * accuracy. Perhaps we even need to take the bpc value into
5707 * consideration.
5708 */
5709
Daniel Vetter50f3b012013-03-27 00:44:56 +01005710 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005711 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5712
5713 /*
5714 * GY/GU and RY/RU should be the other way around according
5715 * to BSpec, but reality doesn't agree. Just set them up in
5716 * a way that results in the correct picture.
5717 */
5718 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5719 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5720
5721 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5722 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5723
5724 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5725 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5726
5727 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5728 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5729 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5730
5731 if (INTEL_INFO(dev)->gen > 6) {
5732 uint16_t postoff = 0;
5733
Daniel Vetter50f3b012013-03-27 00:44:56 +01005734 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005735 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5736
5737 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5738 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5739 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5740
5741 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5742 } else {
5743 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5744
Daniel Vetter50f3b012013-03-27 00:44:56 +01005745 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005746 mode |= CSC_BLACK_SCREEN_OFFSET;
5747
5748 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5749 }
5750}
5751
Daniel Vetter6ff93602013-04-19 11:24:36 +02005752static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005753{
5754 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005756 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005757 uint32_t val;
5758
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005759 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005760
Daniel Vetterd8b32242013-04-25 17:54:44 +02005761 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005762 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5763
Daniel Vetter6ff93602013-04-19 11:24:36 +02005764 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005765 val |= PIPECONF_INTERLACED_ILK;
5766 else
5767 val |= PIPECONF_PROGRESSIVE;
5768
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005769 I915_WRITE(PIPECONF(cpu_transcoder), val);
5770 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005771
5772 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5773 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005774}
5775
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005776static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005777 intel_clock_t *clock,
5778 bool *has_reduced_clock,
5779 intel_clock_t *reduced_clock)
5780{
5781 struct drm_device *dev = crtc->dev;
5782 struct drm_i915_private *dev_priv = dev->dev_private;
5783 struct intel_encoder *intel_encoder;
5784 int refclk;
5785 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02005786 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005787
5788 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5789 switch (intel_encoder->type) {
5790 case INTEL_OUTPUT_LVDS:
5791 is_lvds = true;
5792 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005793 }
5794 }
5795
5796 refclk = ironlake_get_refclk(crtc);
5797
5798 /*
5799 * Returns a set of divisors for the desired target clock with the given
5800 * refclk, or FALSE. The returned values represent the clock equation:
5801 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5802 */
5803 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005804 ret = dev_priv->display.find_dpll(limit, crtc,
5805 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005806 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005807 if (!ret)
5808 return false;
5809
5810 if (is_lvds && dev_priv->lvds_downclock_avail) {
5811 /*
5812 * Ensure we match the reduced clock's P to the target clock.
5813 * If the clocks don't match, we can't switch the display clock
5814 * by using the FP0/FP1. In such case we will disable the LVDS
5815 * downclock feature.
5816 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005817 *has_reduced_clock =
5818 dev_priv->display.find_dpll(limit, crtc,
5819 dev_priv->lvds_downclock,
5820 refclk, clock,
5821 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005822 }
5823
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005824 return true;
5825}
5826
Daniel Vetter01a415f2012-10-27 15:58:40 +02005827static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5828{
5829 struct drm_i915_private *dev_priv = dev->dev_private;
5830 uint32_t temp;
5831
5832 temp = I915_READ(SOUTH_CHICKEN1);
5833 if (temp & FDI_BC_BIFURCATION_SELECT)
5834 return;
5835
5836 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5837 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5838
5839 temp |= FDI_BC_BIFURCATION_SELECT;
5840 DRM_DEBUG_KMS("enabling fdi C rx\n");
5841 I915_WRITE(SOUTH_CHICKEN1, temp);
5842 POSTING_READ(SOUTH_CHICKEN1);
5843}
5844
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005845static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005846{
5847 struct drm_device *dev = intel_crtc->base.dev;
5848 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005849
5850 switch (intel_crtc->pipe) {
5851 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005852 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005853 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005854 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005855 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5856 else
5857 cpt_enable_fdi_bc_bifurcation(dev);
5858
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005859 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005860 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005861 cpt_enable_fdi_bc_bifurcation(dev);
5862
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005863 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005864 default:
5865 BUG();
5866 }
5867}
5868
Paulo Zanonid4b19312012-11-29 11:29:32 -02005869int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5870{
5871 /*
5872 * Account for spread spectrum to avoid
5873 * oversubscribing the link. Max center spread
5874 * is 2.5%; use 5% for safety's sake.
5875 */
5876 u32 bps = target_clock * bpp * 21 / 20;
5877 return bps / (link_bw * 8) + 1;
5878}
5879
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005880static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005881{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005882 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005883}
5884
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005885static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005886 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005887 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005888{
5889 struct drm_crtc *crtc = &intel_crtc->base;
5890 struct drm_device *dev = crtc->dev;
5891 struct drm_i915_private *dev_priv = dev->dev_private;
5892 struct intel_encoder *intel_encoder;
5893 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005894 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005895 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005896
5897 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5898 switch (intel_encoder->type) {
5899 case INTEL_OUTPUT_LVDS:
5900 is_lvds = true;
5901 break;
5902 case INTEL_OUTPUT_SDVO:
5903 case INTEL_OUTPUT_HDMI:
5904 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005905 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005906 }
5907
5908 num_connectors++;
5909 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005910
Chris Wilsonc1858122010-12-03 21:35:48 +00005911 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005912 factor = 21;
5913 if (is_lvds) {
5914 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005915 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005916 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005917 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005918 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005919 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005920
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005921 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005922 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005923
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005924 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5925 *fp2 |= FP_CB_TUNE;
5926
Chris Wilson5eddb702010-09-11 13:48:45 +01005927 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005928
Eric Anholta07d6782011-03-30 13:01:08 -07005929 if (is_lvds)
5930 dpll |= DPLLB_MODE_LVDS;
5931 else
5932 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005933
Daniel Vetteref1b4602013-06-01 17:17:04 +02005934 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5935 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005936
5937 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005938 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005939 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005940 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005941
Eric Anholta07d6782011-03-30 13:01:08 -07005942 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005943 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005944 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005945 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005946
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005947 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005948 case 5:
5949 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5950 break;
5951 case 7:
5952 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5953 break;
5954 case 10:
5955 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5956 break;
5957 case 14:
5958 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5959 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005960 }
5961
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005962 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005963 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005964 else
5965 dpll |= PLL_REF_INPUT_DREFCLK;
5966
Daniel Vetter959e16d2013-06-05 13:34:21 +02005967 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005968}
5969
Jesse Barnes79e53942008-11-07 14:24:08 -08005970static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005971 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005972 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005973{
5974 struct drm_device *dev = crtc->dev;
5975 struct drm_i915_private *dev_priv = dev->dev_private;
5976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5977 int pipe = intel_crtc->pipe;
5978 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005979 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005980 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005981 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005982 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005983 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005984 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005985 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005986 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005987
5988 for_each_encoder_on_crtc(dev, crtc, encoder) {
5989 switch (encoder->type) {
5990 case INTEL_OUTPUT_LVDS:
5991 is_lvds = true;
5992 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005993 }
5994
5995 num_connectors++;
5996 }
5997
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005998 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5999 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6000
Daniel Vetterff9a6752013-06-01 17:16:21 +02006001 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006002 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006003 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006004 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6005 return -EINVAL;
6006 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006007 /* Compat-code for transition, will disappear. */
6008 if (!intel_crtc->config.clock_set) {
6009 intel_crtc->config.dpll.n = clock.n;
6010 intel_crtc->config.dpll.m1 = clock.m1;
6011 intel_crtc->config.dpll.m2 = clock.m2;
6012 intel_crtc->config.dpll.p1 = clock.p1;
6013 intel_crtc->config.dpll.p2 = clock.p2;
6014 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006015
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006016 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006017 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006018 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006019 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006020 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006021
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006022 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006023 &fp, &reduced_clock,
6024 has_reduced_clock ? &fp2 : NULL);
6025
Daniel Vetter959e16d2013-06-05 13:34:21 +02006026 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006027 intel_crtc->config.dpll_hw_state.fp0 = fp;
6028 if (has_reduced_clock)
6029 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6030 else
6031 intel_crtc->config.dpll_hw_state.fp1 = fp;
6032
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006033 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006034 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006035 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6036 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006037 return -EINVAL;
6038 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006039 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006040 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006041
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006042 if (intel_crtc->config.has_dp_encoder)
6043 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006044
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006045 if (is_lvds && has_reduced_clock && i915_powersave)
6046 intel_crtc->lowfreq_avail = true;
6047 else
6048 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006049
6050 if (intel_crtc->config.has_pch_encoder) {
6051 pll = intel_crtc_to_shared_dpll(intel_crtc);
6052
Jesse Barnes79e53942008-11-07 14:24:08 -08006053 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006054
Daniel Vetter8a654f32013-06-01 17:16:22 +02006055 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006056
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006057 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006058 intel_cpu_transcoder_set_m_n(intel_crtc,
6059 &intel_crtc->config.fdi_m_n);
6060 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006061
Daniel Vetterebfd86f2013-04-19 11:24:44 +02006062 if (IS_IVYBRIDGE(dev))
6063 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006064
Daniel Vetter6ff93602013-04-19 11:24:36 +02006065 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006066
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006067 /* Set up the display plane register */
6068 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006069 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006070
Daniel Vetter94352cf2012-07-05 22:51:56 +02006071 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006072
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006073 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006074}
6075
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006076static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6077 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006078{
6079 struct drm_device *dev = crtc->base.dev;
6080 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006081 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006082
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006083 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6084 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6085 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6086 & ~TU_SIZE_MASK;
6087 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6088 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6089 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6090}
6091
6092static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6093 enum transcoder transcoder,
6094 struct intel_link_m_n *m_n)
6095{
6096 struct drm_device *dev = crtc->base.dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098 enum pipe pipe = crtc->pipe;
6099
6100 if (INTEL_INFO(dev)->gen >= 5) {
6101 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6102 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6103 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6104 & ~TU_SIZE_MASK;
6105 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6106 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6107 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6108 } else {
6109 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6110 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6111 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6112 & ~TU_SIZE_MASK;
6113 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6114 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6115 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6116 }
6117}
6118
6119void intel_dp_get_m_n(struct intel_crtc *crtc,
6120 struct intel_crtc_config *pipe_config)
6121{
6122 if (crtc->config.has_pch_encoder)
6123 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6124 else
6125 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6126 &pipe_config->dp_m_n);
6127}
6128
Daniel Vetter72419202013-04-04 13:28:53 +02006129static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6130 struct intel_crtc_config *pipe_config)
6131{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006132 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6133 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006134}
6135
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006136static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6137 struct intel_crtc_config *pipe_config)
6138{
6139 struct drm_device *dev = crtc->base.dev;
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141 uint32_t tmp;
6142
6143 tmp = I915_READ(PF_CTL(crtc->pipe));
6144
6145 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006146 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006147 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6148 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006149
6150 /* We currently do not free assignements of panel fitters on
6151 * ivb/hsw (since we don't use the higher upscaling modes which
6152 * differentiates them) so just WARN about this case for now. */
6153 if (IS_GEN7(dev)) {
6154 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6155 PF_PIPE_SEL_IVB(crtc->pipe));
6156 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006157 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006158}
6159
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006160static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6161 struct intel_crtc_config *pipe_config)
6162{
6163 struct drm_device *dev = crtc->base.dev;
6164 struct drm_i915_private *dev_priv = dev->dev_private;
6165 uint32_t tmp;
6166
Daniel Vettere143a212013-07-04 12:01:15 +02006167 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006168 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006169
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006170 tmp = I915_READ(PIPECONF(crtc->pipe));
6171 if (!(tmp & PIPECONF_ENABLE))
6172 return false;
6173
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006174 switch (tmp & PIPECONF_BPC_MASK) {
6175 case PIPECONF_6BPC:
6176 pipe_config->pipe_bpp = 18;
6177 break;
6178 case PIPECONF_8BPC:
6179 pipe_config->pipe_bpp = 24;
6180 break;
6181 case PIPECONF_10BPC:
6182 pipe_config->pipe_bpp = 30;
6183 break;
6184 case PIPECONF_12BPC:
6185 pipe_config->pipe_bpp = 36;
6186 break;
6187 default:
6188 break;
6189 }
6190
Daniel Vetterab9412b2013-05-03 11:49:46 +02006191 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006192 struct intel_shared_dpll *pll;
6193
Daniel Vetter88adfff2013-03-28 10:42:01 +01006194 pipe_config->has_pch_encoder = true;
6195
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006196 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6197 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6198 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006199
6200 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006201
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006202 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006203 pipe_config->shared_dpll =
6204 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006205 } else {
6206 tmp = I915_READ(PCH_DPLL_SEL);
6207 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6208 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6209 else
6210 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6211 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006212
6213 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6214
6215 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6216 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006217
6218 tmp = pipe_config->dpll_hw_state.dpll;
6219 pipe_config->pixel_multiplier =
6220 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6221 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006222
6223 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006224 } else {
6225 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006226 }
6227
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006228 intel_get_pipe_timings(crtc, pipe_config);
6229
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006230 ironlake_get_pfit_config(crtc, pipe_config);
6231
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006232 return true;
6233}
6234
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006235static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6236{
6237 struct drm_device *dev = dev_priv->dev;
6238 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6239 struct intel_crtc *crtc;
6240 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006241 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006242
6243 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6244 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6245 pipe_name(crtc->pipe));
6246
6247 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6248 WARN(plls->spll_refcount, "SPLL enabled\n");
6249 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6250 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6251 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6252 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6253 "CPU PWM1 enabled\n");
6254 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6255 "CPU PWM2 enabled\n");
6256 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6257 "PCH PWM1 enabled\n");
6258 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6259 "Utility pin enabled\n");
6260 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6261
6262 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6263 val = I915_READ(DEIMR);
6264 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6265 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6266 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006267 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006268 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6269 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6270}
6271
6272/*
6273 * This function implements pieces of two sequences from BSpec:
6274 * - Sequence for display software to disable LCPLL
6275 * - Sequence for display software to allow package C8+
6276 * The steps implemented here are just the steps that actually touch the LCPLL
6277 * register. Callers should take care of disabling all the display engine
6278 * functions, doing the mode unset, fixing interrupts, etc.
6279 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006280static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6281 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006282{
6283 uint32_t val;
6284
6285 assert_can_disable_lcpll(dev_priv);
6286
6287 val = I915_READ(LCPLL_CTL);
6288
6289 if (switch_to_fclk) {
6290 val |= LCPLL_CD_SOURCE_FCLK;
6291 I915_WRITE(LCPLL_CTL, val);
6292
6293 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6294 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6295 DRM_ERROR("Switching to FCLK failed\n");
6296
6297 val = I915_READ(LCPLL_CTL);
6298 }
6299
6300 val |= LCPLL_PLL_DISABLE;
6301 I915_WRITE(LCPLL_CTL, val);
6302 POSTING_READ(LCPLL_CTL);
6303
6304 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6305 DRM_ERROR("LCPLL still locked\n");
6306
6307 val = I915_READ(D_COMP);
6308 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006309 mutex_lock(&dev_priv->rps.hw_lock);
6310 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6311 DRM_ERROR("Failed to disable D_COMP\n");
6312 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006313 POSTING_READ(D_COMP);
6314 ndelay(100);
6315
6316 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6317 DRM_ERROR("D_COMP RCOMP still in progress\n");
6318
6319 if (allow_power_down) {
6320 val = I915_READ(LCPLL_CTL);
6321 val |= LCPLL_POWER_DOWN_ALLOW;
6322 I915_WRITE(LCPLL_CTL, val);
6323 POSTING_READ(LCPLL_CTL);
6324 }
6325}
6326
6327/*
6328 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6329 * source.
6330 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006331static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006332{
6333 uint32_t val;
6334
6335 val = I915_READ(LCPLL_CTL);
6336
6337 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6338 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6339 return;
6340
Paulo Zanoni215733f2013-08-19 13:18:07 -03006341 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6342 * we'll hang the machine! */
6343 dev_priv->uncore.funcs.force_wake_get(dev_priv);
6344
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006345 if (val & LCPLL_POWER_DOWN_ALLOW) {
6346 val &= ~LCPLL_POWER_DOWN_ALLOW;
6347 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006348 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006349 }
6350
6351 val = I915_READ(D_COMP);
6352 val |= D_COMP_COMP_FORCE;
6353 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006354 mutex_lock(&dev_priv->rps.hw_lock);
6355 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6356 DRM_ERROR("Failed to enable D_COMP\n");
6357 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006358 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006359
6360 val = I915_READ(LCPLL_CTL);
6361 val &= ~LCPLL_PLL_DISABLE;
6362 I915_WRITE(LCPLL_CTL, val);
6363
6364 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6365 DRM_ERROR("LCPLL not locked yet\n");
6366
6367 if (val & LCPLL_CD_SOURCE_FCLK) {
6368 val = I915_READ(LCPLL_CTL);
6369 val &= ~LCPLL_CD_SOURCE_FCLK;
6370 I915_WRITE(LCPLL_CTL, val);
6371
6372 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6373 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6374 DRM_ERROR("Switching back to LCPLL failed\n");
6375 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006376
6377 dev_priv->uncore.funcs.force_wake_put(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006378}
6379
Paulo Zanonic67a4702013-08-19 13:18:09 -03006380void hsw_enable_pc8_work(struct work_struct *__work)
6381{
6382 struct drm_i915_private *dev_priv =
6383 container_of(to_delayed_work(__work), struct drm_i915_private,
6384 pc8.enable_work);
6385 struct drm_device *dev = dev_priv->dev;
6386 uint32_t val;
6387
6388 if (dev_priv->pc8.enabled)
6389 return;
6390
6391 DRM_DEBUG_KMS("Enabling package C8+\n");
6392
6393 dev_priv->pc8.enabled = true;
6394
6395 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6396 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6397 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6398 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6399 }
6400
6401 lpt_disable_clkout_dp(dev);
6402 hsw_pc8_disable_interrupts(dev);
6403 hsw_disable_lcpll(dev_priv, true, true);
6404}
6405
6406static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6407{
6408 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6409 WARN(dev_priv->pc8.disable_count < 1,
6410 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6411
6412 dev_priv->pc8.disable_count--;
6413 if (dev_priv->pc8.disable_count != 0)
6414 return;
6415
6416 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006417 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006418}
6419
6420static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6421{
6422 struct drm_device *dev = dev_priv->dev;
6423 uint32_t val;
6424
6425 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6426 WARN(dev_priv->pc8.disable_count < 0,
6427 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6428
6429 dev_priv->pc8.disable_count++;
6430 if (dev_priv->pc8.disable_count != 1)
6431 return;
6432
6433 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6434 if (!dev_priv->pc8.enabled)
6435 return;
6436
6437 DRM_DEBUG_KMS("Disabling package C8+\n");
6438
6439 hsw_restore_lcpll(dev_priv);
6440 hsw_pc8_restore_interrupts(dev);
6441 lpt_init_pch_refclk(dev);
6442
6443 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6444 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6445 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6446 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6447 }
6448
6449 intel_prepare_ddi(dev);
6450 i915_gem_init_swizzling(dev);
6451 mutex_lock(&dev_priv->rps.hw_lock);
6452 gen6_update_ring_freq(dev);
6453 mutex_unlock(&dev_priv->rps.hw_lock);
6454 dev_priv->pc8.enabled = false;
6455}
6456
6457void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6458{
6459 mutex_lock(&dev_priv->pc8.lock);
6460 __hsw_enable_package_c8(dev_priv);
6461 mutex_unlock(&dev_priv->pc8.lock);
6462}
6463
6464void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6465{
6466 mutex_lock(&dev_priv->pc8.lock);
6467 __hsw_disable_package_c8(dev_priv);
6468 mutex_unlock(&dev_priv->pc8.lock);
6469}
6470
6471static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6472{
6473 struct drm_device *dev = dev_priv->dev;
6474 struct intel_crtc *crtc;
6475 uint32_t val;
6476
6477 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6478 if (crtc->base.enabled)
6479 return false;
6480
6481 /* This case is still possible since we have the i915.disable_power_well
6482 * parameter and also the KVMr or something else might be requesting the
6483 * power well. */
6484 val = I915_READ(HSW_PWR_WELL_DRIVER);
6485 if (val != 0) {
6486 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6487 return false;
6488 }
6489
6490 return true;
6491}
6492
6493/* Since we're called from modeset_global_resources there's no way to
6494 * symmetrically increase and decrease the refcount, so we use
6495 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6496 * or not.
6497 */
6498static void hsw_update_package_c8(struct drm_device *dev)
6499{
6500 struct drm_i915_private *dev_priv = dev->dev_private;
6501 bool allow;
6502
6503 if (!i915_enable_pc8)
6504 return;
6505
6506 mutex_lock(&dev_priv->pc8.lock);
6507
6508 allow = hsw_can_enable_package_c8(dev_priv);
6509
6510 if (allow == dev_priv->pc8.requirements_met)
6511 goto done;
6512
6513 dev_priv->pc8.requirements_met = allow;
6514
6515 if (allow)
6516 __hsw_enable_package_c8(dev_priv);
6517 else
6518 __hsw_disable_package_c8(dev_priv);
6519
6520done:
6521 mutex_unlock(&dev_priv->pc8.lock);
6522}
6523
6524static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6525{
6526 if (!dev_priv->pc8.gpu_idle) {
6527 dev_priv->pc8.gpu_idle = true;
6528 hsw_enable_package_c8(dev_priv);
6529 }
6530}
6531
6532static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6533{
6534 if (dev_priv->pc8.gpu_idle) {
6535 dev_priv->pc8.gpu_idle = false;
6536 hsw_disable_package_c8(dev_priv);
6537 }
Daniel Vetter94352cf2012-07-05 22:51:56 +02006538}
Eric Anholtf564048e2011-03-30 13:01:02 -07006539
6540static void haswell_modeset_global_resources(struct drm_device *dev)
6541{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006542 bool enable = false;
6543 struct intel_crtc *crtc;
Eric Anholt0b701d22011-03-30 13:01:03 -07006544
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006545 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6546 if (!crtc->base.enabled)
6547 continue;
Eric Anholt0b701d22011-03-30 13:01:03 -07006548
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006549 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
Jesse Barnes79e53942008-11-07 14:24:08 -08006550 crtc->config.cpu_transcoder != TRANSCODER_EDP)
6551 enable = true;
6552 }
6553
6554 intel_set_power_well(dev, enable);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006555
6556 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006557}
6558
6559static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6560 int x, int y,
6561 struct drm_framebuffer *fb)
6562{
6563 struct drm_device *dev = crtc->dev;
6564 struct drm_i915_private *dev_priv = dev->dev_private;
6565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6566 int plane = intel_crtc->plane;
6567 int ret;
6568
6569 if (!intel_ddi_pll_mode_set(crtc))
6570 return -EINVAL;
6571
Chris Wilson560b85b2010-08-07 11:01:38 +01006572 if (intel_crtc->config.has_dp_encoder)
6573 intel_dp_set_m_n(intel_crtc);
6574
6575 intel_crtc->lowfreq_avail = false;
6576
6577 intel_set_pipe_timings(intel_crtc);
6578
6579 if (intel_crtc->config.has_pch_encoder) {
6580 intel_cpu_transcoder_set_m_n(intel_crtc,
6581 &intel_crtc->config.fdi_m_n);
6582 }
6583
6584 haswell_set_pipeconf(crtc);
6585
6586 intel_set_pipe_csc(crtc);
6587
6588 /* Set up the display plane register */
6589 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6590 POSTING_READ(DSPCNTR(plane));
6591
6592 ret = intel_pipe_set_base(crtc, x, y, fb);
6593
Chris Wilson560b85b2010-08-07 11:01:38 +01006594 return ret;
6595}
6596
6597static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6598 struct intel_crtc_config *pipe_config)
6599{
6600 struct drm_device *dev = crtc->base.dev;
6601 struct drm_i915_private *dev_priv = dev->dev_private;
6602 enum intel_display_power_domain pfit_domain;
6603 uint32_t tmp;
6604
6605 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6606 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6607
6608 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6609 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6610 enum pipe trans_edp_pipe;
6611 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6612 default:
6613 WARN(1, "unknown pipe linked to edp transcoder\n");
6614 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6615 case TRANS_DDI_EDP_INPUT_A_ON:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006616 trans_edp_pipe = PIPE_A;
Chris Wilson6b383a72010-09-13 13:54:26 +01006617 break;
6618 case TRANS_DDI_EDP_INPUT_B_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006619 trans_edp_pipe = PIPE_B;
6620 break;
6621 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6622 trans_edp_pipe = PIPE_C;
6623 break;
6624 }
6625
Chris Wilson560b85b2010-08-07 11:01:38 +01006626 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006627 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6628 }
6629
6630 if (!intel_display_power_enabled(dev,
Chris Wilson6b383a72010-09-13 13:54:26 +01006631 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006632 return false;
6633
6634 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6635 if (!(tmp & PIPECONF_ENABLE))
6636 return false;
6637
6638 /*
6639 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6640 * DDI E. So just check whether this pipe is wired to DDI E and whether
6641 * the PCH transcoder is on.
6642 */
6643 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6644 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6645 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6646 pipe_config->has_pch_encoder = true;
6647
6648 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6649 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6650 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6651
6652 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6653 }
6654
6655 intel_get_pipe_timings(crtc, pipe_config);
6656
6657 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6658 if (intel_display_power_enabled(dev, pfit_domain))
6659 ironlake_get_pfit_config(crtc, pipe_config);
Chris Wilson560b85b2010-08-07 11:01:38 +01006660
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006661 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6662 (I915_READ(IPS_CTL) & IPS_ENABLE);
6663
Chris Wilson560b85b2010-08-07 11:01:38 +01006664 pipe_config->pixel_multiplier = 1;
6665
6666 return true;
6667}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006668
6669static int intel_crtc_mode_set(struct drm_crtc *crtc,
6670 int x, int y,
6671 struct drm_framebuffer *fb)
6672{
Jesse Barnes79e53942008-11-07 14:24:08 -08006673 struct drm_device *dev = crtc->dev;
Chris Wilson05394f32010-11-08 19:18:58 +00006674 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006675 struct intel_encoder *encoder;
6676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07006677 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6678 int pipe = intel_crtc->pipe;
6679 int ret;
6680
Eric Anholt0b701d22011-03-30 13:01:03 -07006681 drm_vblank_pre_modeset(dev, pipe);
6682
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006683 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6684
Jesse Barnes79e53942008-11-07 14:24:08 -08006685 drm_vblank_post_modeset(dev, pipe);
6686
Daniel Vetter9256aa12012-10-31 19:26:13 +01006687 if (ret != 0)
6688 return ret;
6689
6690 for_each_encoder_on_crtc(dev, crtc, encoder) {
6691 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6692 encoder->base.base.id,
6693 drm_get_encoder_name(&encoder->base),
6694 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02006695 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01006696 }
6697
6698 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006699}
6700
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006701static bool intel_eld_uptodate(struct drm_connector *connector,
6702 int reg_eldv, uint32_t bits_eldv,
6703 int reg_elda, uint32_t bits_elda,
6704 int reg_edid)
6705{
6706 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6707 uint8_t *eld = connector->eld;
6708 uint32_t i;
6709
6710 i = I915_READ(reg_eldv);
6711 i &= bits_eldv;
6712
6713 if (!eld[0])
6714 return !i;
6715
6716 if (!i)
6717 return false;
6718
6719 i = I915_READ(reg_elda);
6720 i &= ~bits_elda;
6721 I915_WRITE(reg_elda, i);
6722
6723 for (i = 0; i < eld[2]; i++)
6724 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6725 return false;
6726
6727 return true;
6728}
6729
Wu Fengguange0dac652011-09-05 14:25:34 +08006730static void g4x_write_eld(struct drm_connector *connector,
6731 struct drm_crtc *crtc)
6732{
6733 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6734 uint8_t *eld = connector->eld;
6735 uint32_t eldv;
6736 uint32_t len;
6737 uint32_t i;
6738
6739 i = I915_READ(G4X_AUD_VID_DID);
6740
6741 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6742 eldv = G4X_ELDV_DEVCL_DEVBLC;
6743 else
6744 eldv = G4X_ELDV_DEVCTG;
6745
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006746 if (intel_eld_uptodate(connector,
6747 G4X_AUD_CNTL_ST, eldv,
6748 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6749 G4X_HDMIW_HDMIEDID))
6750 return;
6751
Wu Fengguange0dac652011-09-05 14:25:34 +08006752 i = I915_READ(G4X_AUD_CNTL_ST);
6753 i &= ~(eldv | G4X_ELD_ADDR);
6754 len = (i >> 9) & 0x1f; /* ELD buffer size */
6755 I915_WRITE(G4X_AUD_CNTL_ST, i);
6756
6757 if (!eld[0])
6758 return;
6759
6760 len = min_t(uint8_t, eld[2], len);
6761 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6762 for (i = 0; i < len; i++)
6763 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6764
6765 i = I915_READ(G4X_AUD_CNTL_ST);
6766 i |= eldv;
6767 I915_WRITE(G4X_AUD_CNTL_ST, i);
6768}
6769
Wang Xingchao83358c852012-08-16 22:43:37 +08006770static void haswell_write_eld(struct drm_connector *connector,
6771 struct drm_crtc *crtc)
6772{
6773 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6774 uint8_t *eld = connector->eld;
6775 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006777 uint32_t eldv;
6778 uint32_t i;
6779 int len;
6780 int pipe = to_intel_crtc(crtc)->pipe;
6781 int tmp;
6782
6783 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6784 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6785 int aud_config = HSW_AUD_CFG(pipe);
6786 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6787
6788
6789 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6790
6791 /* Audio output enable */
6792 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6793 tmp = I915_READ(aud_cntrl_st2);
6794 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6795 I915_WRITE(aud_cntrl_st2, tmp);
6796
6797 /* Wait for 1 vertical blank */
6798 intel_wait_for_vblank(dev, pipe);
6799
6800 /* Set ELD valid state */
6801 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006802 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006803 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6804 I915_WRITE(aud_cntrl_st2, tmp);
6805 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006806 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006807
6808 /* Enable HDMI mode */
6809 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006810 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006811 /* clear N_programing_enable and N_value_index */
6812 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6813 I915_WRITE(aud_config, tmp);
6814
6815 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6816
6817 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006818 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006819
6820 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6821 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6822 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6823 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6824 } else
6825 I915_WRITE(aud_config, 0);
6826
6827 if (intel_eld_uptodate(connector,
6828 aud_cntrl_st2, eldv,
6829 aud_cntl_st, IBX_ELD_ADDRESS,
6830 hdmiw_hdmiedid))
6831 return;
6832
6833 i = I915_READ(aud_cntrl_st2);
6834 i &= ~eldv;
6835 I915_WRITE(aud_cntrl_st2, i);
6836
6837 if (!eld[0])
6838 return;
6839
6840 i = I915_READ(aud_cntl_st);
6841 i &= ~IBX_ELD_ADDRESS;
6842 I915_WRITE(aud_cntl_st, i);
6843 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6844 DRM_DEBUG_DRIVER("port num:%d\n", i);
6845
6846 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6847 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6848 for (i = 0; i < len; i++)
6849 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6850
6851 i = I915_READ(aud_cntrl_st2);
6852 i |= eldv;
6853 I915_WRITE(aud_cntrl_st2, i);
6854
6855}
6856
Wu Fengguange0dac652011-09-05 14:25:34 +08006857static void ironlake_write_eld(struct drm_connector *connector,
6858 struct drm_crtc *crtc)
6859{
6860 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6861 uint8_t *eld = connector->eld;
6862 uint32_t eldv;
6863 uint32_t i;
6864 int len;
6865 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006866 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006867 int aud_cntl_st;
6868 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006869 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006870
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006871 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006872 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6873 aud_config = IBX_AUD_CFG(pipe);
6874 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006875 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006876 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006877 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6878 aud_config = CPT_AUD_CFG(pipe);
6879 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006880 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006881 }
6882
Wang Xingchao9b138a82012-08-09 16:52:18 +08006883 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006884
6885 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006886 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006887 if (!i) {
6888 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6889 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006890 eldv = IBX_ELD_VALIDB;
6891 eldv |= IBX_ELD_VALIDB << 4;
6892 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006893 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006894 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006895 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006896 }
6897
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006898 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6899 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6900 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006901 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6902 } else
6903 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006904
6905 if (intel_eld_uptodate(connector,
6906 aud_cntrl_st2, eldv,
6907 aud_cntl_st, IBX_ELD_ADDRESS,
6908 hdmiw_hdmiedid))
6909 return;
6910
Wu Fengguange0dac652011-09-05 14:25:34 +08006911 i = I915_READ(aud_cntrl_st2);
6912 i &= ~eldv;
6913 I915_WRITE(aud_cntrl_st2, i);
6914
6915 if (!eld[0])
6916 return;
6917
Wu Fengguange0dac652011-09-05 14:25:34 +08006918 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006919 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006920 I915_WRITE(aud_cntl_st, i);
6921
6922 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6923 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6924 for (i = 0; i < len; i++)
6925 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6926
6927 i = I915_READ(aud_cntrl_st2);
6928 i |= eldv;
6929 I915_WRITE(aud_cntrl_st2, i);
6930}
6931
6932void intel_write_eld(struct drm_encoder *encoder,
6933 struct drm_display_mode *mode)
6934{
6935 struct drm_crtc *crtc = encoder->crtc;
6936 struct drm_connector *connector;
6937 struct drm_device *dev = encoder->dev;
6938 struct drm_i915_private *dev_priv = dev->dev_private;
6939
6940 connector = drm_select_eld(encoder, mode);
6941 if (!connector)
6942 return;
6943
6944 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6945 connector->base.id,
6946 drm_get_connector_name(connector),
6947 connector->encoder->base.id,
6948 drm_get_encoder_name(connector->encoder));
6949
6950 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6951
6952 if (dev_priv->display.write_eld)
6953 dev_priv->display.write_eld(connector, crtc);
6954}
6955
Jesse Barnes79e53942008-11-07 14:24:08 -08006956static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6957{
6958 struct drm_device *dev = crtc->dev;
6959 struct drm_i915_private *dev_priv = dev->dev_private;
6960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6961 bool visible = base != 0;
6962 u32 cntl;
6963
6964 if (intel_crtc->cursor_visible == visible)
6965 return;
6966
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006967 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08006968 if (visible) {
6969 /* On these chipsets we can only modify the base whilst
6970 * the cursor is disabled.
6971 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006972 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006973
6974 cntl &= ~(CURSOR_FORMAT_MASK);
6975 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6976 cntl |= CURSOR_ENABLE |
6977 CURSOR_GAMMA_ENABLE |
6978 CURSOR_FORMAT_ARGB;
6979 } else
6980 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006981 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08006982
6983 intel_crtc->cursor_visible = visible;
6984}
6985
6986static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6987{
6988 struct drm_device *dev = crtc->dev;
6989 struct drm_i915_private *dev_priv = dev->dev_private;
6990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6991 int pipe = intel_crtc->pipe;
6992 bool visible = base != 0;
6993
6994 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006995 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006996 if (base) {
6997 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6998 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6999 cntl |= pipe << 28; /* Connect to correct pipe */
7000 } else {
7001 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7002 cntl |= CURSOR_MODE_DISABLE;
7003 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007004 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007005
7006 intel_crtc->cursor_visible = visible;
7007 }
7008 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007009 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007010}
7011
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007012static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7013{
7014 struct drm_device *dev = crtc->dev;
7015 struct drm_i915_private *dev_priv = dev->dev_private;
7016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7017 int pipe = intel_crtc->pipe;
7018 bool visible = base != 0;
7019
7020 if (intel_crtc->cursor_visible != visible) {
7021 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7022 if (base) {
7023 cntl &= ~CURSOR_MODE;
7024 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7025 } else {
7026 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7027 cntl |= CURSOR_MODE_DISABLE;
7028 }
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007029 if (IS_HASWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007030 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007031 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7032 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007033 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7034
7035 intel_crtc->cursor_visible = visible;
7036 }
7037 /* and commit changes on next vblank */
7038 I915_WRITE(CURBASE_IVB(pipe), base);
7039}
7040
Jesse Barnes79e53942008-11-07 14:24:08 -08007041/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7042static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7043 bool on)
7044{
7045 struct drm_device *dev = crtc->dev;
7046 struct drm_i915_private *dev_priv = dev->dev_private;
7047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7048 int pipe = intel_crtc->pipe;
7049 int x = intel_crtc->cursor_x;
7050 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007051 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007052 bool visible;
7053
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007054 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007055 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007056
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007057 if (x >= intel_crtc->config.pipe_src_w)
7058 base = 0;
7059
7060 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007061 base = 0;
7062
7063 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007064 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007065 base = 0;
7066
7067 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7068 x = -x;
7069 }
7070 pos |= x << CURSOR_X_SHIFT;
7071
7072 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007073 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007074 base = 0;
7075
7076 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7077 y = -y;
7078 }
7079 pos |= y << CURSOR_Y_SHIFT;
7080
7081 visible = base != 0;
7082 if (!visible && !intel_crtc->cursor_visible)
7083 return;
7084
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03007085 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007086 I915_WRITE(CURPOS_IVB(pipe), pos);
7087 ivb_update_cursor(crtc, base);
7088 } else {
7089 I915_WRITE(CURPOS(pipe), pos);
7090 if (IS_845G(dev) || IS_I865G(dev))
7091 i845_update_cursor(crtc, base);
7092 else
7093 i9xx_update_cursor(crtc, base);
7094 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007095}
7096
7097static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7098 struct drm_file *file,
7099 uint32_t handle,
7100 uint32_t width, uint32_t height)
7101{
7102 struct drm_device *dev = crtc->dev;
7103 struct drm_i915_private *dev_priv = dev->dev_private;
7104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007105 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007106 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007107 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007108
Jesse Barnes79e53942008-11-07 14:24:08 -08007109 /* if we want to turn off the cursor ignore width and height */
7110 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007111 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007112 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007113 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007114 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007115 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007116 }
7117
7118 /* Currently we only support 64x64 cursors */
7119 if (width != 64 || height != 64) {
7120 DRM_ERROR("we currently only support 64x64 cursors\n");
7121 return -EINVAL;
7122 }
7123
Chris Wilson05394f32010-11-08 19:18:58 +00007124 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007125 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007126 return -ENOENT;
7127
Chris Wilson05394f32010-11-08 19:18:58 +00007128 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007129 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007130 ret = -ENOMEM;
7131 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007132 }
7133
Dave Airlie71acb5e2008-12-30 20:31:46 +10007134 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007135 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007136 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007137 unsigned alignment;
7138
Chris Wilsond9e86c02010-11-10 16:40:20 +00007139 if (obj->tiling_mode) {
7140 DRM_ERROR("cursor cannot be tiled\n");
7141 ret = -EINVAL;
7142 goto fail_locked;
7143 }
7144
Chris Wilson693db182013-03-05 14:52:39 +00007145 /* Note that the w/a also requires 2 PTE of padding following
7146 * the bo. We currently fill all unused PTE with the shadow
7147 * page and so we should always have valid PTE following the
7148 * cursor preventing the VT-d warning.
7149 */
7150 alignment = 0;
7151 if (need_vtd_wa(dev))
7152 alignment = 64*1024;
7153
7154 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007155 if (ret) {
7156 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007157 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007158 }
7159
Chris Wilsond9e86c02010-11-10 16:40:20 +00007160 ret = i915_gem_object_put_fence(obj);
7161 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007162 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007163 goto fail_unpin;
7164 }
7165
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007166 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007167 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007168 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007169 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007170 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7171 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007172 if (ret) {
7173 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007174 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007175 }
Chris Wilson05394f32010-11-08 19:18:58 +00007176 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007177 }
7178
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007179 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04007180 I915_WRITE(CURSIZE, (height << 12) | width);
7181
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007182 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007183 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007184 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007185 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007186 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7187 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007188 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007189 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007190 }
Jesse Barnes80824002009-09-10 15:28:06 -07007191
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007192 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007193
7194 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007195 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007196 intel_crtc->cursor_width = width;
7197 intel_crtc->cursor_height = height;
7198
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03007199 if (intel_crtc->active)
7200 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007201
Jesse Barnes79e53942008-11-07 14:24:08 -08007202 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007203fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007204 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007205fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007206 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007207fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007208 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007209 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007210}
7211
7212static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7213{
Jesse Barnes79e53942008-11-07 14:24:08 -08007214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007215
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007216 intel_crtc->cursor_x = x;
7217 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07007218
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03007219 if (intel_crtc->active)
7220 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007221
7222 return 0;
7223}
7224
Jesse Barnes79e53942008-11-07 14:24:08 -08007225static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007226 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007227{
James Simmons72034252010-08-03 01:33:19 +01007228 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007230
James Simmons72034252010-08-03 01:33:19 +01007231 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007232 intel_crtc->lut_r[i] = red[i] >> 8;
7233 intel_crtc->lut_g[i] = green[i] >> 8;
7234 intel_crtc->lut_b[i] = blue[i] >> 8;
7235 }
7236
7237 intel_crtc_load_lut(crtc);
7238}
7239
Jesse Barnes79e53942008-11-07 14:24:08 -08007240/* VESA 640x480x72Hz mode to set on the pipe */
7241static struct drm_display_mode load_detect_mode = {
7242 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7243 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7244};
7245
Chris Wilsond2dff872011-04-19 08:36:26 +01007246static struct drm_framebuffer *
7247intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007248 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007249 struct drm_i915_gem_object *obj)
7250{
7251 struct intel_framebuffer *intel_fb;
7252 int ret;
7253
7254 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7255 if (!intel_fb) {
7256 drm_gem_object_unreference_unlocked(&obj->base);
7257 return ERR_PTR(-ENOMEM);
7258 }
7259
7260 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
7261 if (ret) {
7262 drm_gem_object_unreference_unlocked(&obj->base);
7263 kfree(intel_fb);
7264 return ERR_PTR(ret);
7265 }
7266
7267 return &intel_fb->base;
7268}
7269
7270static u32
7271intel_framebuffer_pitch_for_width(int width, int bpp)
7272{
7273 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7274 return ALIGN(pitch, 64);
7275}
7276
7277static u32
7278intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7279{
7280 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7281 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7282}
7283
7284static struct drm_framebuffer *
7285intel_framebuffer_create_for_mode(struct drm_device *dev,
7286 struct drm_display_mode *mode,
7287 int depth, int bpp)
7288{
7289 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007290 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007291
7292 obj = i915_gem_alloc_object(dev,
7293 intel_framebuffer_size_for_mode(mode, bpp));
7294 if (obj == NULL)
7295 return ERR_PTR(-ENOMEM);
7296
7297 mode_cmd.width = mode->hdisplay;
7298 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007299 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7300 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007301 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007302
7303 return intel_framebuffer_create(dev, &mode_cmd, obj);
7304}
7305
7306static struct drm_framebuffer *
7307mode_fits_in_fbdev(struct drm_device *dev,
7308 struct drm_display_mode *mode)
7309{
7310 struct drm_i915_private *dev_priv = dev->dev_private;
7311 struct drm_i915_gem_object *obj;
7312 struct drm_framebuffer *fb;
7313
7314 if (dev_priv->fbdev == NULL)
7315 return NULL;
7316
7317 obj = dev_priv->fbdev->ifb.obj;
7318 if (obj == NULL)
7319 return NULL;
7320
7321 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007322 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7323 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007324 return NULL;
7325
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007326 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007327 return NULL;
7328
7329 return fb;
7330}
7331
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007332bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007333 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007334 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007335{
7336 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007337 struct intel_encoder *intel_encoder =
7338 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007339 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007340 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007341 struct drm_crtc *crtc = NULL;
7342 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007343 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007344 int i = -1;
7345
Chris Wilsond2dff872011-04-19 08:36:26 +01007346 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7347 connector->base.id, drm_get_connector_name(connector),
7348 encoder->base.id, drm_get_encoder_name(encoder));
7349
Jesse Barnes79e53942008-11-07 14:24:08 -08007350 /*
7351 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007352 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007353 * - if the connector already has an assigned crtc, use it (but make
7354 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007355 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007356 * - try to find the first unused crtc that can drive this connector,
7357 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007358 */
7359
7360 /* See if we already have a CRTC for this connector */
7361 if (encoder->crtc) {
7362 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007363
Daniel Vetter7b240562012-12-12 00:35:33 +01007364 mutex_lock(&crtc->mutex);
7365
Daniel Vetter24218aa2012-08-12 19:27:11 +02007366 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007367 old->load_detect_temp = false;
7368
7369 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007370 if (connector->dpms != DRM_MODE_DPMS_ON)
7371 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007372
Chris Wilson71731882011-04-19 23:10:58 +01007373 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007374 }
7375
7376 /* Find an unused one (if possible) */
7377 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7378 i++;
7379 if (!(encoder->possible_crtcs & (1 << i)))
7380 continue;
7381 if (!possible_crtc->enabled) {
7382 crtc = possible_crtc;
7383 break;
7384 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007385 }
7386
7387 /*
7388 * If we didn't find an unused CRTC, don't use any.
7389 */
7390 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007391 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7392 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007393 }
7394
Daniel Vetter7b240562012-12-12 00:35:33 +01007395 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007396 intel_encoder->new_crtc = to_intel_crtc(crtc);
7397 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007398
7399 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007400 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007401 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007402 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007403
Chris Wilson64927112011-04-20 07:25:26 +01007404 if (!mode)
7405 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007406
Chris Wilsond2dff872011-04-19 08:36:26 +01007407 /* We need a framebuffer large enough to accommodate all accesses
7408 * that the plane may generate whilst we perform load detection.
7409 * We can not rely on the fbcon either being present (we get called
7410 * during its initialisation to detect all boot displays, or it may
7411 * not even exist) or that it is large enough to satisfy the
7412 * requested mode.
7413 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007414 fb = mode_fits_in_fbdev(dev, mode);
7415 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007416 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007417 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7418 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007419 } else
7420 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007421 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007422 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007423 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007424 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007425 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007426
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007427 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007428 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007429 if (old->release_fb)
7430 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007431 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007432 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007433 }
Chris Wilson71731882011-04-19 23:10:58 +01007434
Jesse Barnes79e53942008-11-07 14:24:08 -08007435 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007436 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007437 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007438}
7439
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007440void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007441 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007442{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007443 struct intel_encoder *intel_encoder =
7444 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007445 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007446 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007447
Chris Wilsond2dff872011-04-19 08:36:26 +01007448 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7449 connector->base.id, drm_get_connector_name(connector),
7450 encoder->base.id, drm_get_encoder_name(encoder));
7451
Chris Wilson8261b192011-04-19 23:18:09 +01007452 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007453 to_intel_connector(connector)->new_encoder = NULL;
7454 intel_encoder->new_crtc = NULL;
7455 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007456
Daniel Vetter36206362012-12-10 20:42:17 +01007457 if (old->release_fb) {
7458 drm_framebuffer_unregister_private(old->release_fb);
7459 drm_framebuffer_unreference(old->release_fb);
7460 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007461
Daniel Vetter67c96402013-01-23 16:25:09 +00007462 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007463 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007464 }
7465
Eric Anholtc751ce42010-03-25 11:48:48 -07007466 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007467 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7468 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007469
7470 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007471}
7472
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007473static int i9xx_pll_refclk(struct drm_device *dev,
7474 const struct intel_crtc_config *pipe_config)
7475{
7476 struct drm_i915_private *dev_priv = dev->dev_private;
7477 u32 dpll = pipe_config->dpll_hw_state.dpll;
7478
7479 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7480 return dev_priv->vbt.lvds_ssc_freq * 1000;
7481 else if (HAS_PCH_SPLIT(dev))
7482 return 120000;
7483 else if (!IS_GEN2(dev))
7484 return 96000;
7485 else
7486 return 48000;
7487}
7488
Jesse Barnes79e53942008-11-07 14:24:08 -08007489/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007490static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7491 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007492{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007493 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007494 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007495 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007496 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007497 u32 fp;
7498 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007499 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007500
7501 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007502 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007503 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007504 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007505
7506 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007507 if (IS_PINEVIEW(dev)) {
7508 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7509 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007510 } else {
7511 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7512 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7513 }
7514
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007515 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007516 if (IS_PINEVIEW(dev))
7517 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7518 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007519 else
7520 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007521 DPLL_FPA01_P1_POST_DIV_SHIFT);
7522
7523 switch (dpll & DPLL_MODE_MASK) {
7524 case DPLLB_MODE_DAC_SERIAL:
7525 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7526 5 : 10;
7527 break;
7528 case DPLLB_MODE_LVDS:
7529 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7530 7 : 14;
7531 break;
7532 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007533 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007534 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007535 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007536 }
7537
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007538 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007539 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007540 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007541 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007542 } else {
7543 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7544
7545 if (is_lvds) {
7546 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7547 DPLL_FPA01_P1_POST_DIV_SHIFT);
7548 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007549 } else {
7550 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7551 clock.p1 = 2;
7552 else {
7553 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7554 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7555 }
7556 if (dpll & PLL_P2_DIVIDE_BY_4)
7557 clock.p2 = 4;
7558 else
7559 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007560 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007561
7562 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007563 }
7564
Ville Syrjälä18442d02013-09-13 16:00:08 +03007565 /*
7566 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007567 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007568 * encoder's get_config() function.
7569 */
7570 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007571}
7572
Ville Syrjälä6878da02013-09-13 15:59:11 +03007573int intel_dotclock_calculate(int link_freq,
7574 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007575{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007576 /*
7577 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007578 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007579 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007580 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007581 *
7582 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007583 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007584 */
7585
Ville Syrjälä6878da02013-09-13 15:59:11 +03007586 if (!m_n->link_n)
7587 return 0;
7588
7589 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7590}
7591
Ville Syrjälä18442d02013-09-13 16:00:08 +03007592static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7593 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007594{
7595 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007596
7597 /* read out port_clock from the DPLL */
7598 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007599
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007600 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007601 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007602 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007603 * agree once we know their relationship in the encoder's
7604 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007605 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007606 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007607 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7608 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007609}
7610
7611/** Returns the currently programmed mode of the given pipe. */
7612struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7613 struct drm_crtc *crtc)
7614{
Jesse Barnes548f2452011-02-17 10:40:53 -08007615 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007617 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007618 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007619 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007620 int htot = I915_READ(HTOTAL(cpu_transcoder));
7621 int hsync = I915_READ(HSYNC(cpu_transcoder));
7622 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7623 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03007624 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08007625
7626 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7627 if (!mode)
7628 return NULL;
7629
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007630 /*
7631 * Construct a pipe_config sufficient for getting the clock info
7632 * back out of crtc_clock_get.
7633 *
7634 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7635 * to use a real value here instead.
7636 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03007637 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007638 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007639 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
7640 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
7641 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007642 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7643
Ville Syrjälä773ae032013-09-23 17:48:20 +03007644 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08007645 mode->hdisplay = (htot & 0xffff) + 1;
7646 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7647 mode->hsync_start = (hsync & 0xffff) + 1;
7648 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7649 mode->vdisplay = (vtot & 0xffff) + 1;
7650 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7651 mode->vsync_start = (vsync & 0xffff) + 1;
7652 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7653
7654 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007655
7656 return mode;
7657}
7658
Daniel Vetter3dec0092010-08-20 21:40:52 +02007659static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007660{
7661 struct drm_device *dev = crtc->dev;
7662 drm_i915_private_t *dev_priv = dev->dev_private;
7663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7664 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007665 int dpll_reg = DPLL(pipe);
7666 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007667
Eric Anholtbad720f2009-10-22 16:11:14 -07007668 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007669 return;
7670
7671 if (!dev_priv->lvds_downclock_avail)
7672 return;
7673
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007674 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007675 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007676 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007677
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007678 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007679
7680 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7681 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007682 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007683
Jesse Barnes652c3932009-08-17 13:31:43 -07007684 dpll = I915_READ(dpll_reg);
7685 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007686 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007687 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007688}
7689
7690static void intel_decrease_pllclock(struct drm_crtc *crtc)
7691{
7692 struct drm_device *dev = crtc->dev;
7693 drm_i915_private_t *dev_priv = dev->dev_private;
7694 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007695
Eric Anholtbad720f2009-10-22 16:11:14 -07007696 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007697 return;
7698
7699 if (!dev_priv->lvds_downclock_avail)
7700 return;
7701
7702 /*
7703 * Since this is called by a timer, we should never get here in
7704 * the manual case.
7705 */
7706 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007707 int pipe = intel_crtc->pipe;
7708 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007709 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007710
Zhao Yakui44d98a62009-10-09 11:39:40 +08007711 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007712
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007713 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007714
Chris Wilson074b5e12012-05-02 12:07:06 +01007715 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007716 dpll |= DISPLAY_RATE_SELECT_FPA1;
7717 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007718 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007719 dpll = I915_READ(dpll_reg);
7720 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007721 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007722 }
7723
7724}
7725
Chris Wilsonf047e392012-07-21 12:31:41 +01007726void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007727{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007728 struct drm_i915_private *dev_priv = dev->dev_private;
7729
7730 hsw_package_c8_gpu_busy(dev_priv);
7731 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01007732}
7733
7734void intel_mark_idle(struct drm_device *dev)
7735{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007736 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00007737 struct drm_crtc *crtc;
7738
Paulo Zanonic67a4702013-08-19 13:18:09 -03007739 hsw_package_c8_gpu_idle(dev_priv);
7740
Chris Wilson725a5b52013-01-08 11:02:57 +00007741 if (!i915_powersave)
7742 return;
7743
7744 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7745 if (!crtc->fb)
7746 continue;
7747
7748 intel_decrease_pllclock(crtc);
7749 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01007750
7751 if (dev_priv->info->gen >= 6)
7752 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01007753}
7754
Chris Wilsonc65355b2013-06-06 16:53:41 -03007755void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7756 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007757{
7758 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007759 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007760
7761 if (!i915_powersave)
7762 return;
7763
Jesse Barnes652c3932009-08-17 13:31:43 -07007764 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007765 if (!crtc->fb)
7766 continue;
7767
Chris Wilsonc65355b2013-06-06 16:53:41 -03007768 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7769 continue;
7770
7771 intel_increase_pllclock(crtc);
7772 if (ring && intel_fbc_enabled(dev))
7773 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007774 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007775}
7776
Jesse Barnes79e53942008-11-07 14:24:08 -08007777static void intel_crtc_destroy(struct drm_crtc *crtc)
7778{
7779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007780 struct drm_device *dev = crtc->dev;
7781 struct intel_unpin_work *work;
7782 unsigned long flags;
7783
7784 spin_lock_irqsave(&dev->event_lock, flags);
7785 work = intel_crtc->unpin_work;
7786 intel_crtc->unpin_work = NULL;
7787 spin_unlock_irqrestore(&dev->event_lock, flags);
7788
7789 if (work) {
7790 cancel_work_sync(&work->work);
7791 kfree(work);
7792 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007793
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007794 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7795
Jesse Barnes79e53942008-11-07 14:24:08 -08007796 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007797
Jesse Barnes79e53942008-11-07 14:24:08 -08007798 kfree(intel_crtc);
7799}
7800
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007801static void intel_unpin_work_fn(struct work_struct *__work)
7802{
7803 struct intel_unpin_work *work =
7804 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007805 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007806
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007807 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007808 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007809 drm_gem_object_unreference(&work->pending_flip_obj->base);
7810 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007811
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007812 intel_update_fbc(dev);
7813 mutex_unlock(&dev->struct_mutex);
7814
7815 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7816 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7817
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007818 kfree(work);
7819}
7820
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007821static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007822 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007823{
7824 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7826 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007827 unsigned long flags;
7828
7829 /* Ignore early vblank irqs */
7830 if (intel_crtc == NULL)
7831 return;
7832
7833 spin_lock_irqsave(&dev->event_lock, flags);
7834 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007835
7836 /* Ensure we don't miss a work->pending update ... */
7837 smp_rmb();
7838
7839 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007840 spin_unlock_irqrestore(&dev->event_lock, flags);
7841 return;
7842 }
7843
Chris Wilsone7d841c2012-12-03 11:36:30 +00007844 /* and that the unpin work is consistent wrt ->pending. */
7845 smp_rmb();
7846
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007847 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007848
Rob Clark45a066e2012-10-08 14:50:40 -05007849 if (work->event)
7850 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007851
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007852 drm_vblank_put(dev, intel_crtc->pipe);
7853
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007854 spin_unlock_irqrestore(&dev->event_lock, flags);
7855
Daniel Vetter2c10d572012-12-20 21:24:07 +01007856 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007857
7858 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007859
7860 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007861}
7862
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007863void intel_finish_page_flip(struct drm_device *dev, int pipe)
7864{
7865 drm_i915_private_t *dev_priv = dev->dev_private;
7866 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7867
Mario Kleiner49b14a52010-12-09 07:00:07 +01007868 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007869}
7870
7871void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7872{
7873 drm_i915_private_t *dev_priv = dev->dev_private;
7874 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7875
Mario Kleiner49b14a52010-12-09 07:00:07 +01007876 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007877}
7878
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007879void intel_prepare_page_flip(struct drm_device *dev, int plane)
7880{
7881 drm_i915_private_t *dev_priv = dev->dev_private;
7882 struct intel_crtc *intel_crtc =
7883 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7884 unsigned long flags;
7885
Chris Wilsone7d841c2012-12-03 11:36:30 +00007886 /* NB: An MMIO update of the plane base pointer will also
7887 * generate a page-flip completion irq, i.e. every modeset
7888 * is also accompanied by a spurious intel_prepare_page_flip().
7889 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007890 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007891 if (intel_crtc->unpin_work)
7892 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007893 spin_unlock_irqrestore(&dev->event_lock, flags);
7894}
7895
Chris Wilsone7d841c2012-12-03 11:36:30 +00007896inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7897{
7898 /* Ensure that the work item is consistent when activating it ... */
7899 smp_wmb();
7900 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7901 /* and that it is marked active as soon as the irq could fire. */
7902 smp_wmb();
7903}
7904
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007905static int intel_gen2_queue_flip(struct drm_device *dev,
7906 struct drm_crtc *crtc,
7907 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007908 struct drm_i915_gem_object *obj,
7909 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007910{
7911 struct drm_i915_private *dev_priv = dev->dev_private;
7912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007913 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007914 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007915 int ret;
7916
Daniel Vetter6d90c952012-04-26 23:28:05 +02007917 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007918 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007919 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007920
Daniel Vetter6d90c952012-04-26 23:28:05 +02007921 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007922 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007923 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007924
7925 /* Can't queue multiple flips, so wait for the previous
7926 * one to finish before executing the next.
7927 */
7928 if (intel_crtc->plane)
7929 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7930 else
7931 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007932 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7933 intel_ring_emit(ring, MI_NOOP);
7934 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7935 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7936 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007937 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007938 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007939
7940 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007941 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007942 return 0;
7943
7944err_unpin:
7945 intel_unpin_fb_obj(obj);
7946err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007947 return ret;
7948}
7949
7950static int intel_gen3_queue_flip(struct drm_device *dev,
7951 struct drm_crtc *crtc,
7952 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007953 struct drm_i915_gem_object *obj,
7954 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007955{
7956 struct drm_i915_private *dev_priv = dev->dev_private;
7957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007958 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007959 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007960 int ret;
7961
Daniel Vetter6d90c952012-04-26 23:28:05 +02007962 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007963 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007964 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007965
Daniel Vetter6d90c952012-04-26 23:28:05 +02007966 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007967 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007968 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007969
7970 if (intel_crtc->plane)
7971 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7972 else
7973 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007974 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7975 intel_ring_emit(ring, MI_NOOP);
7976 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7977 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7978 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007979 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007980 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007981
Chris Wilsone7d841c2012-12-03 11:36:30 +00007982 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007983 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007984 return 0;
7985
7986err_unpin:
7987 intel_unpin_fb_obj(obj);
7988err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007989 return ret;
7990}
7991
7992static int intel_gen4_queue_flip(struct drm_device *dev,
7993 struct drm_crtc *crtc,
7994 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007995 struct drm_i915_gem_object *obj,
7996 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007997{
7998 struct drm_i915_private *dev_priv = dev->dev_private;
7999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8000 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008001 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008002 int ret;
8003
Daniel Vetter6d90c952012-04-26 23:28:05 +02008004 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008005 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008006 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008007
Daniel Vetter6d90c952012-04-26 23:28:05 +02008008 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008009 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008010 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008011
8012 /* i965+ uses the linear or tiled offsets from the
8013 * Display Registers (which do not change across a page-flip)
8014 * so we need only reprogram the base address.
8015 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008016 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8017 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8018 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008019 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008020 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008021 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008022
8023 /* XXX Enabling the panel-fitter across page-flip is so far
8024 * untested on non-native modes, so ignore it for now.
8025 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8026 */
8027 pf = 0;
8028 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008029 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008030
8031 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008032 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008033 return 0;
8034
8035err_unpin:
8036 intel_unpin_fb_obj(obj);
8037err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008038 return ret;
8039}
8040
8041static int intel_gen6_queue_flip(struct drm_device *dev,
8042 struct drm_crtc *crtc,
8043 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008044 struct drm_i915_gem_object *obj,
8045 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008046{
8047 struct drm_i915_private *dev_priv = dev->dev_private;
8048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008049 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008050 uint32_t pf, pipesrc;
8051 int ret;
8052
Daniel Vetter6d90c952012-04-26 23:28:05 +02008053 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008054 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008055 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008056
Daniel Vetter6d90c952012-04-26 23:28:05 +02008057 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008058 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008059 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008060
Daniel Vetter6d90c952012-04-26 23:28:05 +02008061 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8062 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8063 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008064 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008065
Chris Wilson99d9acd2012-04-17 20:37:00 +01008066 /* Contrary to the suggestions in the documentation,
8067 * "Enable Panel Fitter" does not seem to be required when page
8068 * flipping with a non-native mode, and worse causes a normal
8069 * modeset to fail.
8070 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8071 */
8072 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008073 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008074 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008075
8076 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008077 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008078 return 0;
8079
8080err_unpin:
8081 intel_unpin_fb_obj(obj);
8082err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008083 return ret;
8084}
8085
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008086static int intel_gen7_queue_flip(struct drm_device *dev,
8087 struct drm_crtc *crtc,
8088 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008089 struct drm_i915_gem_object *obj,
8090 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008091{
8092 struct drm_i915_private *dev_priv = dev->dev_private;
8093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008094 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008095 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008096 int len, ret;
8097
8098 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008099 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008100 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008101
8102 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8103 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008104 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008105
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008106 switch(intel_crtc->plane) {
8107 case PLANE_A:
8108 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8109 break;
8110 case PLANE_B:
8111 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8112 break;
8113 case PLANE_C:
8114 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8115 break;
8116 default:
8117 WARN_ONCE(1, "unknown plane in flip command\n");
8118 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008119 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008120 }
8121
Chris Wilsonffe74d72013-08-26 20:58:12 +01008122 len = 4;
8123 if (ring->id == RCS)
8124 len += 6;
8125
8126 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008127 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008128 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008129
Chris Wilsonffe74d72013-08-26 20:58:12 +01008130 /* Unmask the flip-done completion message. Note that the bspec says that
8131 * we should do this for both the BCS and RCS, and that we must not unmask
8132 * more than one flip event at any time (or ensure that one flip message
8133 * can be sent by waiting for flip-done prior to queueing new flips).
8134 * Experimentation says that BCS works despite DERRMR masking all
8135 * flip-done completion events and that unmasking all planes at once
8136 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8137 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8138 */
8139 if (ring->id == RCS) {
8140 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8141 intel_ring_emit(ring, DERRMR);
8142 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8143 DERRMR_PIPEB_PRI_FLIP_DONE |
8144 DERRMR_PIPEC_PRI_FLIP_DONE));
8145 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8146 intel_ring_emit(ring, DERRMR);
8147 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8148 }
8149
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008150 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008151 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008152 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008153 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008154
8155 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008156 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008157 return 0;
8158
8159err_unpin:
8160 intel_unpin_fb_obj(obj);
8161err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008162 return ret;
8163}
8164
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008165static int intel_default_queue_flip(struct drm_device *dev,
8166 struct drm_crtc *crtc,
8167 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008168 struct drm_i915_gem_object *obj,
8169 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008170{
8171 return -ENODEV;
8172}
8173
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008174static int intel_crtc_page_flip(struct drm_crtc *crtc,
8175 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008176 struct drm_pending_vblank_event *event,
8177 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008178{
8179 struct drm_device *dev = crtc->dev;
8180 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008181 struct drm_framebuffer *old_fb = crtc->fb;
8182 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8184 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008185 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008186 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008187
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008188 /* Can't change pixel format via MI display flips. */
8189 if (fb->pixel_format != crtc->fb->pixel_format)
8190 return -EINVAL;
8191
8192 /*
8193 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8194 * Note that pitch changes could also affect these register.
8195 */
8196 if (INTEL_INFO(dev)->gen > 3 &&
8197 (fb->offsets[0] != crtc->fb->offsets[0] ||
8198 fb->pitches[0] != crtc->fb->pitches[0]))
8199 return -EINVAL;
8200
Daniel Vetterb14c5672013-09-19 12:18:32 +02008201 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008202 if (work == NULL)
8203 return -ENOMEM;
8204
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008205 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008206 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008207 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008208 INIT_WORK(&work->work, intel_unpin_work_fn);
8209
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008210 ret = drm_vblank_get(dev, intel_crtc->pipe);
8211 if (ret)
8212 goto free_work;
8213
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008214 /* We borrow the event spin lock for protecting unpin_work */
8215 spin_lock_irqsave(&dev->event_lock, flags);
8216 if (intel_crtc->unpin_work) {
8217 spin_unlock_irqrestore(&dev->event_lock, flags);
8218 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008219 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008220
8221 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008222 return -EBUSY;
8223 }
8224 intel_crtc->unpin_work = work;
8225 spin_unlock_irqrestore(&dev->event_lock, flags);
8226
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008227 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8228 flush_workqueue(dev_priv->wq);
8229
Chris Wilson79158102012-05-23 11:13:58 +01008230 ret = i915_mutex_lock_interruptible(dev);
8231 if (ret)
8232 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008233
Jesse Barnes75dfca82010-02-10 15:09:44 -08008234 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008235 drm_gem_object_reference(&work->old_fb_obj->base);
8236 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008237
8238 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008239
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008240 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008241
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008242 work->enable_stall_check = true;
8243
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008244 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008245 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008246
Keith Packarded8d1972013-07-22 18:49:58 -07008247 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008248 if (ret)
8249 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008250
Chris Wilson7782de32011-07-08 12:22:41 +01008251 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008252 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008253 mutex_unlock(&dev->struct_mutex);
8254
Jesse Barnese5510fa2010-07-01 16:48:37 -07008255 trace_i915_flip_request(intel_crtc->plane, obj);
8256
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008257 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008258
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008259cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008260 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008261 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008262 drm_gem_object_unreference(&work->old_fb_obj->base);
8263 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008264 mutex_unlock(&dev->struct_mutex);
8265
Chris Wilson79158102012-05-23 11:13:58 +01008266cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008267 spin_lock_irqsave(&dev->event_lock, flags);
8268 intel_crtc->unpin_work = NULL;
8269 spin_unlock_irqrestore(&dev->event_lock, flags);
8270
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008271 drm_vblank_put(dev, intel_crtc->pipe);
8272free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008273 kfree(work);
8274
8275 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008276}
8277
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008278static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008279 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8280 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008281};
8282
Daniel Vetter50f56112012-07-02 09:35:43 +02008283static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8284 struct drm_crtc *crtc)
8285{
8286 struct drm_device *dev;
8287 struct drm_crtc *tmp;
8288 int crtc_mask = 1;
8289
8290 WARN(!crtc, "checking null crtc?\n");
8291
8292 dev = crtc->dev;
8293
8294 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8295 if (tmp == crtc)
8296 break;
8297 crtc_mask <<= 1;
8298 }
8299
8300 if (encoder->possible_crtcs & crtc_mask)
8301 return true;
8302 return false;
8303}
8304
Daniel Vetter9a935852012-07-05 22:34:27 +02008305/**
8306 * intel_modeset_update_staged_output_state
8307 *
8308 * Updates the staged output configuration state, e.g. after we've read out the
8309 * current hw state.
8310 */
8311static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8312{
8313 struct intel_encoder *encoder;
8314 struct intel_connector *connector;
8315
8316 list_for_each_entry(connector, &dev->mode_config.connector_list,
8317 base.head) {
8318 connector->new_encoder =
8319 to_intel_encoder(connector->base.encoder);
8320 }
8321
8322 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8323 base.head) {
8324 encoder->new_crtc =
8325 to_intel_crtc(encoder->base.crtc);
8326 }
8327}
8328
8329/**
8330 * intel_modeset_commit_output_state
8331 *
8332 * This function copies the stage display pipe configuration to the real one.
8333 */
8334static void intel_modeset_commit_output_state(struct drm_device *dev)
8335{
8336 struct intel_encoder *encoder;
8337 struct intel_connector *connector;
8338
8339 list_for_each_entry(connector, &dev->mode_config.connector_list,
8340 base.head) {
8341 connector->base.encoder = &connector->new_encoder->base;
8342 }
8343
8344 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8345 base.head) {
8346 encoder->base.crtc = &encoder->new_crtc->base;
8347 }
8348}
8349
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008350static void
8351connected_sink_compute_bpp(struct intel_connector * connector,
8352 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008353{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008354 int bpp = pipe_config->pipe_bpp;
8355
8356 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8357 connector->base.base.id,
8358 drm_get_connector_name(&connector->base));
8359
8360 /* Don't use an invalid EDID bpc value */
8361 if (connector->base.display_info.bpc &&
8362 connector->base.display_info.bpc * 3 < bpp) {
8363 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8364 bpp, connector->base.display_info.bpc*3);
8365 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8366 }
8367
8368 /* Clamp bpp to 8 on screens without EDID 1.4 */
8369 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8370 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8371 bpp);
8372 pipe_config->pipe_bpp = 24;
8373 }
8374}
8375
8376static int
8377compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8378 struct drm_framebuffer *fb,
8379 struct intel_crtc_config *pipe_config)
8380{
8381 struct drm_device *dev = crtc->base.dev;
8382 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008383 int bpp;
8384
Daniel Vetterd42264b2013-03-28 16:38:08 +01008385 switch (fb->pixel_format) {
8386 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008387 bpp = 8*3; /* since we go through a colormap */
8388 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008389 case DRM_FORMAT_XRGB1555:
8390 case DRM_FORMAT_ARGB1555:
8391 /* checked in intel_framebuffer_init already */
8392 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8393 return -EINVAL;
8394 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008395 bpp = 6*3; /* min is 18bpp */
8396 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008397 case DRM_FORMAT_XBGR8888:
8398 case DRM_FORMAT_ABGR8888:
8399 /* checked in intel_framebuffer_init already */
8400 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8401 return -EINVAL;
8402 case DRM_FORMAT_XRGB8888:
8403 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008404 bpp = 8*3;
8405 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008406 case DRM_FORMAT_XRGB2101010:
8407 case DRM_FORMAT_ARGB2101010:
8408 case DRM_FORMAT_XBGR2101010:
8409 case DRM_FORMAT_ABGR2101010:
8410 /* checked in intel_framebuffer_init already */
8411 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008412 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008413 bpp = 10*3;
8414 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008415 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008416 default:
8417 DRM_DEBUG_KMS("unsupported depth\n");
8418 return -EINVAL;
8419 }
8420
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008421 pipe_config->pipe_bpp = bpp;
8422
8423 /* Clamp display bpp to EDID value */
8424 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008425 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008426 if (!connector->new_encoder ||
8427 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008428 continue;
8429
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008430 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008431 }
8432
8433 return bpp;
8434}
8435
Daniel Vetter644db712013-09-19 14:53:58 +02008436static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8437{
8438 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8439 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008440 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008441 mode->crtc_hdisplay, mode->crtc_hsync_start,
8442 mode->crtc_hsync_end, mode->crtc_htotal,
8443 mode->crtc_vdisplay, mode->crtc_vsync_start,
8444 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8445}
8446
Daniel Vetterc0b03412013-05-28 12:05:54 +02008447static void intel_dump_pipe_config(struct intel_crtc *crtc,
8448 struct intel_crtc_config *pipe_config,
8449 const char *context)
8450{
8451 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8452 context, pipe_name(crtc->pipe));
8453
8454 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8455 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8456 pipe_config->pipe_bpp, pipe_config->dither);
8457 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8458 pipe_config->has_pch_encoder,
8459 pipe_config->fdi_lanes,
8460 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8461 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8462 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008463 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8464 pipe_config->has_dp_encoder,
8465 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8466 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8467 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008468 DRM_DEBUG_KMS("requested mode:\n");
8469 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8470 DRM_DEBUG_KMS("adjusted mode:\n");
8471 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008472 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008473 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008474 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8475 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008476 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8477 pipe_config->gmch_pfit.control,
8478 pipe_config->gmch_pfit.pgm_ratios,
8479 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008480 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008481 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008482 pipe_config->pch_pfit.size,
8483 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008484 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008485 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008486}
8487
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008488static bool check_encoder_cloning(struct drm_crtc *crtc)
8489{
8490 int num_encoders = 0;
8491 bool uncloneable_encoders = false;
8492 struct intel_encoder *encoder;
8493
8494 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8495 base.head) {
8496 if (&encoder->new_crtc->base != crtc)
8497 continue;
8498
8499 num_encoders++;
8500 if (!encoder->cloneable)
8501 uncloneable_encoders = true;
8502 }
8503
8504 return !(num_encoders > 1 && uncloneable_encoders);
8505}
8506
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008507static struct intel_crtc_config *
8508intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008509 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008510 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008511{
8512 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008513 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008514 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008515 int plane_bpp, ret = -EINVAL;
8516 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008517
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008518 if (!check_encoder_cloning(crtc)) {
8519 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8520 return ERR_PTR(-EINVAL);
8521 }
8522
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008523 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8524 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008525 return ERR_PTR(-ENOMEM);
8526
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008527 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8528 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008529
Daniel Vettere143a212013-07-04 12:01:15 +02008530 pipe_config->cpu_transcoder =
8531 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008532 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008533
Imre Deak2960bc92013-07-30 13:36:32 +03008534 /*
8535 * Sanitize sync polarity flags based on requested ones. If neither
8536 * positive or negative polarity is requested, treat this as meaning
8537 * negative polarity.
8538 */
8539 if (!(pipe_config->adjusted_mode.flags &
8540 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8541 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8542
8543 if (!(pipe_config->adjusted_mode.flags &
8544 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8545 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8546
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008547 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8548 * plane pixel format and any sink constraints into account. Returns the
8549 * source plane bpp so that dithering can be selected on mismatches
8550 * after encoders and crtc also have had their say. */
8551 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8552 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008553 if (plane_bpp < 0)
8554 goto fail;
8555
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008556 /*
8557 * Determine the real pipe dimensions. Note that stereo modes can
8558 * increase the actual pipe size due to the frame doubling and
8559 * insertion of additional space for blanks between the frame. This
8560 * is stored in the crtc timings. We use the requested mode to do this
8561 * computation to clearly distinguish it from the adjusted mode, which
8562 * can be changed by the connectors in the below retry loop.
8563 */
8564 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8565 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8566 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8567
Daniel Vettere29c22c2013-02-21 00:00:16 +01008568encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008569 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008570 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008571 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008572
Daniel Vetter135c81b2013-07-21 21:37:09 +02008573 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008574 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008575
Daniel Vetter7758a112012-07-08 19:40:39 +02008576 /* Pass our mode to the connectors and the CRTC to give them a chance to
8577 * adjust it according to limitations or connector properties, and also
8578 * a chance to reject the mode entirely.
8579 */
8580 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8581 base.head) {
8582
8583 if (&encoder->new_crtc->base != crtc)
8584 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008585
Daniel Vetterefea6e82013-07-21 21:36:59 +02008586 if (!(encoder->compute_config(encoder, pipe_config))) {
8587 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008588 goto fail;
8589 }
8590 }
8591
Daniel Vetterff9a6752013-06-01 17:16:21 +02008592 /* Set default port clock if not overwritten by the encoder. Needs to be
8593 * done afterwards in case the encoder adjusts the mode. */
8594 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008595 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8596 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008597
Daniel Vettera43f6e02013-06-07 23:10:32 +02008598 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008599 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008600 DRM_DEBUG_KMS("CRTC fixup failed\n");
8601 goto fail;
8602 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008603
8604 if (ret == RETRY) {
8605 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8606 ret = -EINVAL;
8607 goto fail;
8608 }
8609
8610 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8611 retry = false;
8612 goto encoder_retry;
8613 }
8614
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008615 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8616 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8617 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8618
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008619 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008620fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008621 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008622 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008623}
8624
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008625/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8626 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8627static void
8628intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8629 unsigned *prepare_pipes, unsigned *disable_pipes)
8630{
8631 struct intel_crtc *intel_crtc;
8632 struct drm_device *dev = crtc->dev;
8633 struct intel_encoder *encoder;
8634 struct intel_connector *connector;
8635 struct drm_crtc *tmp_crtc;
8636
8637 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8638
8639 /* Check which crtcs have changed outputs connected to them, these need
8640 * to be part of the prepare_pipes mask. We don't (yet) support global
8641 * modeset across multiple crtcs, so modeset_pipes will only have one
8642 * bit set at most. */
8643 list_for_each_entry(connector, &dev->mode_config.connector_list,
8644 base.head) {
8645 if (connector->base.encoder == &connector->new_encoder->base)
8646 continue;
8647
8648 if (connector->base.encoder) {
8649 tmp_crtc = connector->base.encoder->crtc;
8650
8651 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8652 }
8653
8654 if (connector->new_encoder)
8655 *prepare_pipes |=
8656 1 << connector->new_encoder->new_crtc->pipe;
8657 }
8658
8659 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8660 base.head) {
8661 if (encoder->base.crtc == &encoder->new_crtc->base)
8662 continue;
8663
8664 if (encoder->base.crtc) {
8665 tmp_crtc = encoder->base.crtc;
8666
8667 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8668 }
8669
8670 if (encoder->new_crtc)
8671 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8672 }
8673
8674 /* Check for any pipes that will be fully disabled ... */
8675 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8676 base.head) {
8677 bool used = false;
8678
8679 /* Don't try to disable disabled crtcs. */
8680 if (!intel_crtc->base.enabled)
8681 continue;
8682
8683 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8684 base.head) {
8685 if (encoder->new_crtc == intel_crtc)
8686 used = true;
8687 }
8688
8689 if (!used)
8690 *disable_pipes |= 1 << intel_crtc->pipe;
8691 }
8692
8693
8694 /* set_mode is also used to update properties on life display pipes. */
8695 intel_crtc = to_intel_crtc(crtc);
8696 if (crtc->enabled)
8697 *prepare_pipes |= 1 << intel_crtc->pipe;
8698
Daniel Vetterb6c51642013-04-12 18:48:43 +02008699 /*
8700 * For simplicity do a full modeset on any pipe where the output routing
8701 * changed. We could be more clever, but that would require us to be
8702 * more careful with calling the relevant encoder->mode_set functions.
8703 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008704 if (*prepare_pipes)
8705 *modeset_pipes = *prepare_pipes;
8706
8707 /* ... and mask these out. */
8708 *modeset_pipes &= ~(*disable_pipes);
8709 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008710
8711 /*
8712 * HACK: We don't (yet) fully support global modesets. intel_set_config
8713 * obies this rule, but the modeset restore mode of
8714 * intel_modeset_setup_hw_state does not.
8715 */
8716 *modeset_pipes &= 1 << intel_crtc->pipe;
8717 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008718
8719 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8720 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008721}
8722
Daniel Vetterea9d7582012-07-10 10:42:52 +02008723static bool intel_crtc_in_use(struct drm_crtc *crtc)
8724{
8725 struct drm_encoder *encoder;
8726 struct drm_device *dev = crtc->dev;
8727
8728 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8729 if (encoder->crtc == crtc)
8730 return true;
8731
8732 return false;
8733}
8734
8735static void
8736intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8737{
8738 struct intel_encoder *intel_encoder;
8739 struct intel_crtc *intel_crtc;
8740 struct drm_connector *connector;
8741
8742 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8743 base.head) {
8744 if (!intel_encoder->base.crtc)
8745 continue;
8746
8747 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8748
8749 if (prepare_pipes & (1 << intel_crtc->pipe))
8750 intel_encoder->connectors_active = false;
8751 }
8752
8753 intel_modeset_commit_output_state(dev);
8754
8755 /* Update computed state. */
8756 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8757 base.head) {
8758 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8759 }
8760
8761 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8762 if (!connector->encoder || !connector->encoder->crtc)
8763 continue;
8764
8765 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8766
8767 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008768 struct drm_property *dpms_property =
8769 dev->mode_config.dpms_property;
8770
Daniel Vetterea9d7582012-07-10 10:42:52 +02008771 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008772 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008773 dpms_property,
8774 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008775
8776 intel_encoder = to_intel_encoder(connector->encoder);
8777 intel_encoder->connectors_active = true;
8778 }
8779 }
8780
8781}
8782
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008783static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008784{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008785 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008786
8787 if (clock1 == clock2)
8788 return true;
8789
8790 if (!clock1 || !clock2)
8791 return false;
8792
8793 diff = abs(clock1 - clock2);
8794
8795 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8796 return true;
8797
8798 return false;
8799}
8800
Daniel Vetter25c5b262012-07-08 22:08:04 +02008801#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8802 list_for_each_entry((intel_crtc), \
8803 &(dev)->mode_config.crtc_list, \
8804 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008805 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008806
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008807static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008808intel_pipe_config_compare(struct drm_device *dev,
8809 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008810 struct intel_crtc_config *pipe_config)
8811{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008812#define PIPE_CONF_CHECK_X(name) \
8813 if (current_config->name != pipe_config->name) { \
8814 DRM_ERROR("mismatch in " #name " " \
8815 "(expected 0x%08x, found 0x%08x)\n", \
8816 current_config->name, \
8817 pipe_config->name); \
8818 return false; \
8819 }
8820
Daniel Vetter08a24032013-04-19 11:25:34 +02008821#define PIPE_CONF_CHECK_I(name) \
8822 if (current_config->name != pipe_config->name) { \
8823 DRM_ERROR("mismatch in " #name " " \
8824 "(expected %i, found %i)\n", \
8825 current_config->name, \
8826 pipe_config->name); \
8827 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008828 }
8829
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008830#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8831 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008832 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008833 "(expected %i, found %i)\n", \
8834 current_config->name & (mask), \
8835 pipe_config->name & (mask)); \
8836 return false; \
8837 }
8838
Ville Syrjälä5e550652013-09-06 23:29:07 +03008839#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
8840 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8841 DRM_ERROR("mismatch in " #name " " \
8842 "(expected %i, found %i)\n", \
8843 current_config->name, \
8844 pipe_config->name); \
8845 return false; \
8846 }
8847
Daniel Vetterbb760062013-06-06 14:55:52 +02008848#define PIPE_CONF_QUIRK(quirk) \
8849 ((current_config->quirks | pipe_config->quirks) & (quirk))
8850
Daniel Vettereccb1402013-05-22 00:50:22 +02008851 PIPE_CONF_CHECK_I(cpu_transcoder);
8852
Daniel Vetter08a24032013-04-19 11:25:34 +02008853 PIPE_CONF_CHECK_I(has_pch_encoder);
8854 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008855 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8856 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8857 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8858 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8859 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008860
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008861 PIPE_CONF_CHECK_I(has_dp_encoder);
8862 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
8863 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
8864 PIPE_CONF_CHECK_I(dp_m_n.link_m);
8865 PIPE_CONF_CHECK_I(dp_m_n.link_n);
8866 PIPE_CONF_CHECK_I(dp_m_n.tu);
8867
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008868 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8869 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8870 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8871 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8872 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8873 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8874
8875 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8876 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8877 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8878 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8879 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8880 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8881
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008882 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008883
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008884 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8885 DRM_MODE_FLAG_INTERLACE);
8886
Daniel Vetterbb760062013-06-06 14:55:52 +02008887 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8888 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8889 DRM_MODE_FLAG_PHSYNC);
8890 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8891 DRM_MODE_FLAG_NHSYNC);
8892 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8893 DRM_MODE_FLAG_PVSYNC);
8894 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8895 DRM_MODE_FLAG_NVSYNC);
8896 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008897
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008898 PIPE_CONF_CHECK_I(pipe_src_w);
8899 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008900
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008901 PIPE_CONF_CHECK_I(gmch_pfit.control);
8902 /* pfit ratios are autocomputed by the hw on gen4+ */
8903 if (INTEL_INFO(dev)->gen < 4)
8904 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8905 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008906 PIPE_CONF_CHECK_I(pch_pfit.enabled);
8907 if (current_config->pch_pfit.enabled) {
8908 PIPE_CONF_CHECK_I(pch_pfit.pos);
8909 PIPE_CONF_CHECK_I(pch_pfit.size);
8910 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008911
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008912 PIPE_CONF_CHECK_I(ips_enabled);
8913
Ville Syrjälä282740f2013-09-04 18:30:03 +03008914 PIPE_CONF_CHECK_I(double_wide);
8915
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008916 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008917 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008918 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008919 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8920 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008921
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008922 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
8923 PIPE_CONF_CHECK_I(pipe_bpp);
8924
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008925 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01008926 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008927 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
8928 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03008929
Daniel Vetter66e985c2013-06-05 13:34:20 +02008930#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008931#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008932#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03008933#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02008934#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008935
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008936 return true;
8937}
8938
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008939static void
8940check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008941{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008942 struct intel_connector *connector;
8943
8944 list_for_each_entry(connector, &dev->mode_config.connector_list,
8945 base.head) {
8946 /* This also checks the encoder/connector hw state with the
8947 * ->get_hw_state callbacks. */
8948 intel_connector_check_state(connector);
8949
8950 WARN(&connector->new_encoder->base != connector->base.encoder,
8951 "connector's staged encoder doesn't match current encoder\n");
8952 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008953}
8954
8955static void
8956check_encoder_state(struct drm_device *dev)
8957{
8958 struct intel_encoder *encoder;
8959 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008960
8961 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8962 base.head) {
8963 bool enabled = false;
8964 bool active = false;
8965 enum pipe pipe, tracked_pipe;
8966
8967 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8968 encoder->base.base.id,
8969 drm_get_encoder_name(&encoder->base));
8970
8971 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8972 "encoder's stage crtc doesn't match current crtc\n");
8973 WARN(encoder->connectors_active && !encoder->base.crtc,
8974 "encoder's active_connectors set, but no crtc\n");
8975
8976 list_for_each_entry(connector, &dev->mode_config.connector_list,
8977 base.head) {
8978 if (connector->base.encoder != &encoder->base)
8979 continue;
8980 enabled = true;
8981 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8982 active = true;
8983 }
8984 WARN(!!encoder->base.crtc != enabled,
8985 "encoder's enabled state mismatch "
8986 "(expected %i, found %i)\n",
8987 !!encoder->base.crtc, enabled);
8988 WARN(active && !encoder->base.crtc,
8989 "active encoder with no crtc\n");
8990
8991 WARN(encoder->connectors_active != active,
8992 "encoder's computed active state doesn't match tracked active state "
8993 "(expected %i, found %i)\n", active, encoder->connectors_active);
8994
8995 active = encoder->get_hw_state(encoder, &pipe);
8996 WARN(active != encoder->connectors_active,
8997 "encoder's hw state doesn't match sw tracking "
8998 "(expected %i, found %i)\n",
8999 encoder->connectors_active, active);
9000
9001 if (!encoder->base.crtc)
9002 continue;
9003
9004 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9005 WARN(active && pipe != tracked_pipe,
9006 "active encoder's pipe doesn't match"
9007 "(expected %i, found %i)\n",
9008 tracked_pipe, pipe);
9009
9010 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009011}
9012
9013static void
9014check_crtc_state(struct drm_device *dev)
9015{
9016 drm_i915_private_t *dev_priv = dev->dev_private;
9017 struct intel_crtc *crtc;
9018 struct intel_encoder *encoder;
9019 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009020
9021 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9022 base.head) {
9023 bool enabled = false;
9024 bool active = false;
9025
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009026 memset(&pipe_config, 0, sizeof(pipe_config));
9027
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009028 DRM_DEBUG_KMS("[CRTC:%d]\n",
9029 crtc->base.base.id);
9030
9031 WARN(crtc->active && !crtc->base.enabled,
9032 "active crtc, but not enabled in sw tracking\n");
9033
9034 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9035 base.head) {
9036 if (encoder->base.crtc != &crtc->base)
9037 continue;
9038 enabled = true;
9039 if (encoder->connectors_active)
9040 active = true;
9041 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009042
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009043 WARN(active != crtc->active,
9044 "crtc's computed active state doesn't match tracked active state "
9045 "(expected %i, found %i)\n", active, crtc->active);
9046 WARN(enabled != crtc->base.enabled,
9047 "crtc's computed enabled state doesn't match tracked enabled state "
9048 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9049
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009050 active = dev_priv->display.get_pipe_config(crtc,
9051 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009052
9053 /* hw state is inconsistent with the pipe A quirk */
9054 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9055 active = crtc->active;
9056
Daniel Vetter6c49f242013-06-06 12:45:25 +02009057 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9058 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009059 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009060 if (encoder->base.crtc != &crtc->base)
9061 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009062 if (encoder->get_config &&
9063 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009064 encoder->get_config(encoder, &pipe_config);
9065 }
9066
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009067 WARN(crtc->active != active,
9068 "crtc active state doesn't match with hw state "
9069 "(expected %i, found %i)\n", crtc->active, active);
9070
Daniel Vetterc0b03412013-05-28 12:05:54 +02009071 if (active &&
9072 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9073 WARN(1, "pipe state doesn't match!\n");
9074 intel_dump_pipe_config(crtc, &pipe_config,
9075 "[hw state]");
9076 intel_dump_pipe_config(crtc, &crtc->config,
9077 "[sw state]");
9078 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009079 }
9080}
9081
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009082static void
9083check_shared_dpll_state(struct drm_device *dev)
9084{
9085 drm_i915_private_t *dev_priv = dev->dev_private;
9086 struct intel_crtc *crtc;
9087 struct intel_dpll_hw_state dpll_hw_state;
9088 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009089
9090 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9091 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9092 int enabled_crtcs = 0, active_crtcs = 0;
9093 bool active;
9094
9095 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9096
9097 DRM_DEBUG_KMS("%s\n", pll->name);
9098
9099 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9100
9101 WARN(pll->active > pll->refcount,
9102 "more active pll users than references: %i vs %i\n",
9103 pll->active, pll->refcount);
9104 WARN(pll->active && !pll->on,
9105 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009106 WARN(pll->on && !pll->active,
9107 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009108 WARN(pll->on != active,
9109 "pll on state mismatch (expected %i, found %i)\n",
9110 pll->on, active);
9111
9112 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9113 base.head) {
9114 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9115 enabled_crtcs++;
9116 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9117 active_crtcs++;
9118 }
9119 WARN(pll->active != active_crtcs,
9120 "pll active crtcs mismatch (expected %i, found %i)\n",
9121 pll->active, active_crtcs);
9122 WARN(pll->refcount != enabled_crtcs,
9123 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9124 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009125
9126 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9127 sizeof(dpll_hw_state)),
9128 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009129 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009130}
9131
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009132void
9133intel_modeset_check_state(struct drm_device *dev)
9134{
9135 check_connector_state(dev);
9136 check_encoder_state(dev);
9137 check_crtc_state(dev);
9138 check_shared_dpll_state(dev);
9139}
9140
Ville Syrjälä18442d02013-09-13 16:00:08 +03009141void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9142 int dotclock)
9143{
9144 /*
9145 * FDI already provided one idea for the dotclock.
9146 * Yell if the encoder disagrees.
9147 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009148 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009149 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009150 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009151}
9152
Daniel Vetterf30da182013-04-11 20:22:50 +02009153static int __intel_set_mode(struct drm_crtc *crtc,
9154 struct drm_display_mode *mode,
9155 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009156{
9157 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009158 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009159 struct drm_display_mode *saved_mode, *saved_hwmode;
9160 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009161 struct intel_crtc *intel_crtc;
9162 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009163 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009164
Daniel Vettera1e22652013-09-21 00:35:38 +02009165 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009166 if (!saved_mode)
9167 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009168 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009169
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009170 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009171 &prepare_pipes, &disable_pipes);
9172
Tim Gardner3ac18232012-12-07 07:54:26 -07009173 *saved_hwmode = crtc->hwmode;
9174 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009175
Daniel Vetter25c5b262012-07-08 22:08:04 +02009176 /* Hack: Because we don't (yet) support global modeset on multiple
9177 * crtcs, we don't keep track of the new mode for more than one crtc.
9178 * Hence simply check whether any bit is set in modeset_pipes in all the
9179 * pieces of code that are not yet converted to deal with mutliple crtcs
9180 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009181 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009182 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009183 if (IS_ERR(pipe_config)) {
9184 ret = PTR_ERR(pipe_config);
9185 pipe_config = NULL;
9186
Tim Gardner3ac18232012-12-07 07:54:26 -07009187 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009188 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009189 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9190 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009191 }
9192
Daniel Vetter460da9162013-03-27 00:44:51 +01009193 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9194 intel_crtc_disable(&intel_crtc->base);
9195
Daniel Vetterea9d7582012-07-10 10:42:52 +02009196 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9197 if (intel_crtc->base.enabled)
9198 dev_priv->display.crtc_disable(&intel_crtc->base);
9199 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009200
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009201 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9202 * to set it here already despite that we pass it down the callchain.
9203 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009204 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009205 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009206 /* mode_set/enable/disable functions rely on a correct pipe
9207 * config. */
9208 to_intel_crtc(crtc)->config = *pipe_config;
9209 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009210
Daniel Vetterea9d7582012-07-10 10:42:52 +02009211 /* Only after disabling all output pipelines that will be changed can we
9212 * update the the output configuration. */
9213 intel_modeset_update_state(dev, prepare_pipes);
9214
Daniel Vetter47fab732012-10-26 10:58:18 +02009215 if (dev_priv->display.modeset_global_resources)
9216 dev_priv->display.modeset_global_resources(dev);
9217
Daniel Vettera6778b32012-07-02 09:56:42 +02009218 /* Set up the DPLL and any encoders state that needs to adjust or depend
9219 * on the DPLL.
9220 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009221 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009222 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009223 x, y, fb);
9224 if (ret)
9225 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009226 }
9227
9228 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009229 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9230 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009231
Daniel Vetter25c5b262012-07-08 22:08:04 +02009232 if (modeset_pipes) {
9233 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009234 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009235
Daniel Vetter25c5b262012-07-08 22:08:04 +02009236 /* Calculate and store various constants which
9237 * are later needed by vblank and swap-completion
9238 * timestamping. They are derived from true hwmode.
9239 */
9240 drm_calc_timestamping_constants(crtc);
9241 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009242
9243 /* FIXME: add subpixel order */
9244done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009245 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009246 crtc->hwmode = *saved_hwmode;
9247 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009248 }
9249
Tim Gardner3ac18232012-12-07 07:54:26 -07009250out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009251 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009252 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009253 return ret;
9254}
9255
Damien Lespiaue7457a92013-08-08 22:28:59 +01009256static int intel_set_mode(struct drm_crtc *crtc,
9257 struct drm_display_mode *mode,
9258 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009259{
9260 int ret;
9261
9262 ret = __intel_set_mode(crtc, mode, x, y, fb);
9263
9264 if (ret == 0)
9265 intel_modeset_check_state(crtc->dev);
9266
9267 return ret;
9268}
9269
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009270void intel_crtc_restore_mode(struct drm_crtc *crtc)
9271{
9272 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9273}
9274
Daniel Vetter25c5b262012-07-08 22:08:04 +02009275#undef for_each_intel_crtc_masked
9276
Daniel Vetterd9e55602012-07-04 22:16:09 +02009277static void intel_set_config_free(struct intel_set_config *config)
9278{
9279 if (!config)
9280 return;
9281
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009282 kfree(config->save_connector_encoders);
9283 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009284 kfree(config);
9285}
9286
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009287static int intel_set_config_save_state(struct drm_device *dev,
9288 struct intel_set_config *config)
9289{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009290 struct drm_encoder *encoder;
9291 struct drm_connector *connector;
9292 int count;
9293
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009294 config->save_encoder_crtcs =
9295 kcalloc(dev->mode_config.num_encoder,
9296 sizeof(struct drm_crtc *), GFP_KERNEL);
9297 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009298 return -ENOMEM;
9299
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009300 config->save_connector_encoders =
9301 kcalloc(dev->mode_config.num_connector,
9302 sizeof(struct drm_encoder *), GFP_KERNEL);
9303 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009304 return -ENOMEM;
9305
9306 /* Copy data. Note that driver private data is not affected.
9307 * Should anything bad happen only the expected state is
9308 * restored, not the drivers personal bookkeeping.
9309 */
9310 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009311 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009312 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009313 }
9314
9315 count = 0;
9316 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009317 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009318 }
9319
9320 return 0;
9321}
9322
9323static void intel_set_config_restore_state(struct drm_device *dev,
9324 struct intel_set_config *config)
9325{
Daniel Vetter9a935852012-07-05 22:34:27 +02009326 struct intel_encoder *encoder;
9327 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009328 int count;
9329
9330 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009331 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9332 encoder->new_crtc =
9333 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009334 }
9335
9336 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009337 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9338 connector->new_encoder =
9339 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009340 }
9341}
9342
Imre Deake3de42b2013-05-03 19:44:07 +02009343static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009344is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009345{
9346 int i;
9347
Chris Wilson2e57f472013-07-17 12:14:40 +01009348 if (set->num_connectors == 0)
9349 return false;
9350
9351 if (WARN_ON(set->connectors == NULL))
9352 return false;
9353
9354 for (i = 0; i < set->num_connectors; i++)
9355 if (set->connectors[i]->encoder &&
9356 set->connectors[i]->encoder->crtc == set->crtc &&
9357 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009358 return true;
9359
9360 return false;
9361}
9362
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009363static void
9364intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9365 struct intel_set_config *config)
9366{
9367
9368 /* We should be able to check here if the fb has the same properties
9369 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009370 if (is_crtc_connector_off(set)) {
9371 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009372 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009373 /* If we have no fb then treat it as a full mode set */
9374 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009375 struct intel_crtc *intel_crtc =
9376 to_intel_crtc(set->crtc);
9377
9378 if (intel_crtc->active && i915_fastboot) {
9379 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9380 config->fb_changed = true;
9381 } else {
9382 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9383 config->mode_changed = true;
9384 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009385 } else if (set->fb == NULL) {
9386 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009387 } else if (set->fb->pixel_format !=
9388 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009389 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009390 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009391 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009392 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009393 }
9394
Daniel Vetter835c5872012-07-10 18:11:08 +02009395 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009396 config->fb_changed = true;
9397
9398 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9399 DRM_DEBUG_KMS("modes are different, full mode set\n");
9400 drm_mode_debug_printmodeline(&set->crtc->mode);
9401 drm_mode_debug_printmodeline(set->mode);
9402 config->mode_changed = true;
9403 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009404
9405 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9406 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009407}
9408
Daniel Vetter2e431052012-07-04 22:42:15 +02009409static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009410intel_modeset_stage_output_state(struct drm_device *dev,
9411 struct drm_mode_set *set,
9412 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009413{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009414 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009415 struct intel_connector *connector;
9416 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009417 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009418
Damien Lespiau9abdda72013-02-13 13:29:23 +00009419 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009420 * of connectors. For paranoia, double-check this. */
9421 WARN_ON(!set->fb && (set->num_connectors != 0));
9422 WARN_ON(set->fb && (set->num_connectors == 0));
9423
Daniel Vetter9a935852012-07-05 22:34:27 +02009424 list_for_each_entry(connector, &dev->mode_config.connector_list,
9425 base.head) {
9426 /* Otherwise traverse passed in connector list and get encoders
9427 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009428 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009429 if (set->connectors[ro] == &connector->base) {
9430 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009431 break;
9432 }
9433 }
9434
Daniel Vetter9a935852012-07-05 22:34:27 +02009435 /* If we disable the crtc, disable all its connectors. Also, if
9436 * the connector is on the changing crtc but not on the new
9437 * connector list, disable it. */
9438 if ((!set->fb || ro == set->num_connectors) &&
9439 connector->base.encoder &&
9440 connector->base.encoder->crtc == set->crtc) {
9441 connector->new_encoder = NULL;
9442
9443 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9444 connector->base.base.id,
9445 drm_get_connector_name(&connector->base));
9446 }
9447
9448
9449 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009450 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009451 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009452 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009453 }
9454 /* connector->new_encoder is now updated for all connectors. */
9455
9456 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009457 list_for_each_entry(connector, &dev->mode_config.connector_list,
9458 base.head) {
9459 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009460 continue;
9461
Daniel Vetter9a935852012-07-05 22:34:27 +02009462 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009463
9464 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009465 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009466 new_crtc = set->crtc;
9467 }
9468
9469 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009470 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9471 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009472 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009473 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009474 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9475
9476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9477 connector->base.base.id,
9478 drm_get_connector_name(&connector->base),
9479 new_crtc->base.id);
9480 }
9481
9482 /* Check for any encoders that needs to be disabled. */
9483 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9484 base.head) {
9485 list_for_each_entry(connector,
9486 &dev->mode_config.connector_list,
9487 base.head) {
9488 if (connector->new_encoder == encoder) {
9489 WARN_ON(!connector->new_encoder->new_crtc);
9490
9491 goto next_encoder;
9492 }
9493 }
9494 encoder->new_crtc = NULL;
9495next_encoder:
9496 /* Only now check for crtc changes so we don't miss encoders
9497 * that will be disabled. */
9498 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009499 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009500 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009501 }
9502 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009503 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009504
Daniel Vetter2e431052012-07-04 22:42:15 +02009505 return 0;
9506}
9507
9508static int intel_crtc_set_config(struct drm_mode_set *set)
9509{
9510 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009511 struct drm_mode_set save_set;
9512 struct intel_set_config *config;
9513 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009514
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009515 BUG_ON(!set);
9516 BUG_ON(!set->crtc);
9517 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009518
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009519 /* Enforce sane interface api - has been abused by the fb helper. */
9520 BUG_ON(!set->mode && set->fb);
9521 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009522
Daniel Vetter2e431052012-07-04 22:42:15 +02009523 if (set->fb) {
9524 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9525 set->crtc->base.id, set->fb->base.id,
9526 (int)set->num_connectors, set->x, set->y);
9527 } else {
9528 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009529 }
9530
9531 dev = set->crtc->dev;
9532
9533 ret = -ENOMEM;
9534 config = kzalloc(sizeof(*config), GFP_KERNEL);
9535 if (!config)
9536 goto out_config;
9537
9538 ret = intel_set_config_save_state(dev, config);
9539 if (ret)
9540 goto out_config;
9541
9542 save_set.crtc = set->crtc;
9543 save_set.mode = &set->crtc->mode;
9544 save_set.x = set->crtc->x;
9545 save_set.y = set->crtc->y;
9546 save_set.fb = set->crtc->fb;
9547
9548 /* Compute whether we need a full modeset, only an fb base update or no
9549 * change at all. In the future we might also check whether only the
9550 * mode changed, e.g. for LVDS where we only change the panel fitter in
9551 * such cases. */
9552 intel_set_config_compute_mode_changes(set, config);
9553
Daniel Vetter9a935852012-07-05 22:34:27 +02009554 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009555 if (ret)
9556 goto fail;
9557
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009558 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009559 ret = intel_set_mode(set->crtc, set->mode,
9560 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009561 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009562 intel_crtc_wait_for_pending_flips(set->crtc);
9563
Daniel Vetter4f660f42012-07-02 09:47:37 +02009564 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009565 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009566 }
9567
Chris Wilson2d05eae2013-05-03 17:36:25 +01009568 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009569 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9570 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009571fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009572 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009573
Chris Wilson2d05eae2013-05-03 17:36:25 +01009574 /* Try to restore the config */
9575 if (config->mode_changed &&
9576 intel_set_mode(save_set.crtc, save_set.mode,
9577 save_set.x, save_set.y, save_set.fb))
9578 DRM_ERROR("failed to restore config after modeset failure\n");
9579 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009580
Daniel Vetterd9e55602012-07-04 22:16:09 +02009581out_config:
9582 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009583 return ret;
9584}
9585
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009586static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009587 .cursor_set = intel_crtc_cursor_set,
9588 .cursor_move = intel_crtc_cursor_move,
9589 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009590 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009591 .destroy = intel_crtc_destroy,
9592 .page_flip = intel_crtc_page_flip,
9593};
9594
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009595static void intel_cpu_pll_init(struct drm_device *dev)
9596{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009597 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009598 intel_ddi_pll_init(dev);
9599}
9600
Daniel Vetter53589012013-06-05 13:34:16 +02009601static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9602 struct intel_shared_dpll *pll,
9603 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009604{
Daniel Vetter53589012013-06-05 13:34:16 +02009605 uint32_t val;
9606
9607 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009608 hw_state->dpll = val;
9609 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9610 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02009611
9612 return val & DPLL_VCO_ENABLE;
9613}
9614
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009615static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9616 struct intel_shared_dpll *pll)
9617{
9618 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9619 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9620}
9621
Daniel Vettere7b903d2013-06-05 13:34:14 +02009622static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9623 struct intel_shared_dpll *pll)
9624{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009625 /* PCH refclock must be enabled first */
9626 assert_pch_refclk_enabled(dev_priv);
9627
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009628 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9629
9630 /* Wait for the clocks to stabilize. */
9631 POSTING_READ(PCH_DPLL(pll->id));
9632 udelay(150);
9633
9634 /* The pixel multiplier can only be updated once the
9635 * DPLL is enabled and the clocks are stable.
9636 *
9637 * So write it again.
9638 */
9639 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9640 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009641 udelay(200);
9642}
9643
9644static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9645 struct intel_shared_dpll *pll)
9646{
9647 struct drm_device *dev = dev_priv->dev;
9648 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009649
9650 /* Make sure no transcoder isn't still depending on us. */
9651 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9652 if (intel_crtc_to_shared_dpll(crtc) == pll)
9653 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9654 }
9655
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009656 I915_WRITE(PCH_DPLL(pll->id), 0);
9657 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009658 udelay(200);
9659}
9660
Daniel Vetter46edb022013-06-05 13:34:12 +02009661static char *ibx_pch_dpll_names[] = {
9662 "PCH DPLL A",
9663 "PCH DPLL B",
9664};
9665
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009666static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009667{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009668 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009669 int i;
9670
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009671 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009672
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009673 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02009674 dev_priv->shared_dplls[i].id = i;
9675 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009676 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009677 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9678 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009679 dev_priv->shared_dplls[i].get_hw_state =
9680 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009681 }
9682}
9683
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009684static void intel_shared_dpll_init(struct drm_device *dev)
9685{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009686 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009687
9688 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9689 ibx_pch_dpll_init(dev);
9690 else
9691 dev_priv->num_shared_dpll = 0;
9692
9693 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9694 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9695 dev_priv->num_shared_dpll);
9696}
9697
Hannes Ederb358d0a2008-12-18 21:18:47 +01009698static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009699{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009700 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009701 struct intel_crtc *intel_crtc;
9702 int i;
9703
Daniel Vetter955382f2013-09-19 14:05:45 +02009704 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009705 if (intel_crtc == NULL)
9706 return;
9707
9708 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9709
9710 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009711 for (i = 0; i < 256; i++) {
9712 intel_crtc->lut_r[i] = i;
9713 intel_crtc->lut_g[i] = i;
9714 intel_crtc->lut_b[i] = i;
9715 }
9716
Jesse Barnes80824002009-09-10 15:28:06 -07009717 /* Swap pipes & planes for FBC on pre-965 */
9718 intel_crtc->pipe = pipe;
9719 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009720 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009721 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009722 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009723 }
9724
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009725 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9726 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9727 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9728 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9729
Jesse Barnes79e53942008-11-07 14:24:08 -08009730 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009731}
9732
Carl Worth08d7b3d2009-04-29 14:43:54 -07009733int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009734 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009735{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009736 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009737 struct drm_mode_object *drmmode_obj;
9738 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009739
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009740 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9741 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009742
Daniel Vetterc05422d2009-08-11 16:05:30 +02009743 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9744 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009745
Daniel Vetterc05422d2009-08-11 16:05:30 +02009746 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009747 DRM_ERROR("no such CRTC id\n");
9748 return -EINVAL;
9749 }
9750
Daniel Vetterc05422d2009-08-11 16:05:30 +02009751 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9752 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009753
Daniel Vetterc05422d2009-08-11 16:05:30 +02009754 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009755}
9756
Daniel Vetter66a92782012-07-12 20:08:18 +02009757static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009758{
Daniel Vetter66a92782012-07-12 20:08:18 +02009759 struct drm_device *dev = encoder->base.dev;
9760 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009761 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009762 int entry = 0;
9763
Daniel Vetter66a92782012-07-12 20:08:18 +02009764 list_for_each_entry(source_encoder,
9765 &dev->mode_config.encoder_list, base.head) {
9766
9767 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009768 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009769
9770 /* Intel hw has only one MUX where enocoders could be cloned. */
9771 if (encoder->cloneable && source_encoder->cloneable)
9772 index_mask |= (1 << entry);
9773
Jesse Barnes79e53942008-11-07 14:24:08 -08009774 entry++;
9775 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009776
Jesse Barnes79e53942008-11-07 14:24:08 -08009777 return index_mask;
9778}
9779
Chris Wilson4d302442010-12-14 19:21:29 +00009780static bool has_edp_a(struct drm_device *dev)
9781{
9782 struct drm_i915_private *dev_priv = dev->dev_private;
9783
9784 if (!IS_MOBILE(dev))
9785 return false;
9786
9787 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9788 return false;
9789
9790 if (IS_GEN5(dev) &&
9791 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9792 return false;
9793
9794 return true;
9795}
9796
Jesse Barnes79e53942008-11-07 14:24:08 -08009797static void intel_setup_outputs(struct drm_device *dev)
9798{
Eric Anholt725e30a2009-01-22 13:01:02 -08009799 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009800 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009801 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009802
Daniel Vetterc9093352013-06-06 22:22:47 +02009803 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009804
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009805 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009806 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009807
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009808 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009809 int found;
9810
9811 /* Haswell uses DDI functions to detect digital outputs */
9812 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9813 /* DDI A only supports eDP */
9814 if (found)
9815 intel_ddi_init(dev, PORT_A);
9816
9817 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9818 * register */
9819 found = I915_READ(SFUSE_STRAP);
9820
9821 if (found & SFUSE_STRAP_DDIB_DETECTED)
9822 intel_ddi_init(dev, PORT_B);
9823 if (found & SFUSE_STRAP_DDIC_DETECTED)
9824 intel_ddi_init(dev, PORT_C);
9825 if (found & SFUSE_STRAP_DDID_DETECTED)
9826 intel_ddi_init(dev, PORT_D);
9827 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009828 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009829 dpd_is_edp = intel_dpd_is_edp(dev);
9830
9831 if (has_edp_a(dev))
9832 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009833
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009834 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009835 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009836 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009837 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009838 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009839 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009840 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009841 }
9842
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009843 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009844 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009845
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009846 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009847 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009848
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009849 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009850 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009851
Daniel Vetter270b3042012-10-27 15:52:05 +02009852 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009853 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009854 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309855 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Jesse Barnes6f6005a2013-08-09 09:34:35 -07009856 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
9857 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
9858 PORT_C);
9859 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9860 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
9861 PORT_C);
9862 }
Gajanan Bhat19c03922012-09-27 19:13:07 +05309863
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009864 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009865 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9866 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009867 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9868 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009869 }
Jani Nikula3cfca972013-08-27 15:12:26 +03009870
9871 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +08009872 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009873 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009874
Paulo Zanonie2debe92013-02-18 19:00:27 -03009875 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009876 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009877 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009878 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9879 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009880 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009881 }
Ma Ling27185ae2009-08-24 13:50:23 +08009882
Imre Deake7281ea2013-05-08 13:14:08 +03009883 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009884 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009885 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009886
9887 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009888
Paulo Zanonie2debe92013-02-18 19:00:27 -03009889 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009890 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009891 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009892 }
Ma Ling27185ae2009-08-24 13:50:23 +08009893
Paulo Zanonie2debe92013-02-18 19:00:27 -03009894 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009895
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009896 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9897 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009898 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009899 }
Imre Deake7281ea2013-05-08 13:14:08 +03009900 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009901 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009902 }
Ma Ling27185ae2009-08-24 13:50:23 +08009903
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009904 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009905 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009906 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009907 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009908 intel_dvo_init(dev);
9909
Zhenyu Wang103a1962009-11-27 11:44:36 +08009910 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009911 intel_tv_init(dev);
9912
Chris Wilson4ef69c72010-09-09 15:14:28 +01009913 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9914 encoder->base.possible_crtcs = encoder->crtc_mask;
9915 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009916 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009917 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009918
Paulo Zanonidde86e22012-12-01 12:04:25 -02009919 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009920
9921 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009922}
9923
Chris Wilsonddfe1562013-08-06 17:43:07 +01009924void intel_framebuffer_fini(struct intel_framebuffer *fb)
9925{
9926 drm_framebuffer_cleanup(&fb->base);
9927 drm_gem_object_unreference_unlocked(&fb->obj->base);
9928}
9929
Jesse Barnes79e53942008-11-07 14:24:08 -08009930static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9931{
9932 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009933
Chris Wilsonddfe1562013-08-06 17:43:07 +01009934 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009935 kfree(intel_fb);
9936}
9937
9938static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009939 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009940 unsigned int *handle)
9941{
9942 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009943 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009944
Chris Wilson05394f32010-11-08 19:18:58 +00009945 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009946}
9947
9948static const struct drm_framebuffer_funcs intel_fb_funcs = {
9949 .destroy = intel_user_framebuffer_destroy,
9950 .create_handle = intel_user_framebuffer_create_handle,
9951};
9952
Dave Airlie38651672010-03-30 05:34:13 +00009953int intel_framebuffer_init(struct drm_device *dev,
9954 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009955 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009956 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009957{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009958 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009959 int ret;
9960
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009961 if (obj->tiling_mode == I915_TILING_Y) {
9962 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009963 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009964 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009965
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009966 if (mode_cmd->pitches[0] & 63) {
9967 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9968 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009969 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009970 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009971
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009972 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9973 pitch_limit = 32*1024;
9974 } else if (INTEL_INFO(dev)->gen >= 4) {
9975 if (obj->tiling_mode)
9976 pitch_limit = 16*1024;
9977 else
9978 pitch_limit = 32*1024;
9979 } else if (INTEL_INFO(dev)->gen >= 3) {
9980 if (obj->tiling_mode)
9981 pitch_limit = 8*1024;
9982 else
9983 pitch_limit = 16*1024;
9984 } else
9985 /* XXX DSPC is limited to 4k tiled */
9986 pitch_limit = 8*1024;
9987
9988 if (mode_cmd->pitches[0] > pitch_limit) {
9989 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9990 obj->tiling_mode ? "tiled" : "linear",
9991 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009992 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009993 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009994
9995 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009996 mode_cmd->pitches[0] != obj->stride) {
9997 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9998 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009999 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010000 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010001
Ville Syrjälä57779d02012-10-31 17:50:14 +020010002 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010003 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010004 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010005 case DRM_FORMAT_RGB565:
10006 case DRM_FORMAT_XRGB8888:
10007 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010008 break;
10009 case DRM_FORMAT_XRGB1555:
10010 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010011 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010012 DRM_DEBUG("unsupported pixel format: %s\n",
10013 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010014 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010015 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010016 break;
10017 case DRM_FORMAT_XBGR8888:
10018 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010019 case DRM_FORMAT_XRGB2101010:
10020 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010021 case DRM_FORMAT_XBGR2101010:
10022 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010023 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010024 DRM_DEBUG("unsupported pixel format: %s\n",
10025 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010026 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010027 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010028 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010029 case DRM_FORMAT_YUYV:
10030 case DRM_FORMAT_UYVY:
10031 case DRM_FORMAT_YVYU:
10032 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010033 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010034 DRM_DEBUG("unsupported pixel format: %s\n",
10035 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010036 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010037 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010038 break;
10039 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010040 DRM_DEBUG("unsupported pixel format: %s\n",
10041 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010042 return -EINVAL;
10043 }
10044
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010045 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10046 if (mode_cmd->offsets[0] != 0)
10047 return -EINVAL;
10048
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010049 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10050 intel_fb->obj = obj;
10051
Jesse Barnes79e53942008-11-07 14:24:08 -080010052 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10053 if (ret) {
10054 DRM_ERROR("framebuffer init failed %d\n", ret);
10055 return ret;
10056 }
10057
Jesse Barnes79e53942008-11-07 14:24:08 -080010058 return 0;
10059}
10060
Jesse Barnes79e53942008-11-07 14:24:08 -080010061static struct drm_framebuffer *
10062intel_user_framebuffer_create(struct drm_device *dev,
10063 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010064 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010065{
Chris Wilson05394f32010-11-08 19:18:58 +000010066 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010067
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010068 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10069 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010070 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010071 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010072
Chris Wilsond2dff872011-04-19 08:36:26 +010010073 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010074}
10075
Jesse Barnes79e53942008-11-07 14:24:08 -080010076static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010077 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +000010078 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010079};
10080
Jesse Barnese70236a2009-09-21 10:42:27 -070010081/* Set up chip specific display functions */
10082static void intel_init_display(struct drm_device *dev)
10083{
10084 struct drm_i915_private *dev_priv = dev->dev_private;
10085
Daniel Vetteree9300b2013-06-03 22:40:22 +020010086 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10087 dev_priv->display.find_dpll = g4x_find_best_dpll;
10088 else if (IS_VALLEYVIEW(dev))
10089 dev_priv->display.find_dpll = vlv_find_best_dpll;
10090 else if (IS_PINEVIEW(dev))
10091 dev_priv->display.find_dpll = pnv_find_best_dpll;
10092 else
10093 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10094
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010095 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010096 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010097 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010098 dev_priv->display.crtc_enable = haswell_crtc_enable;
10099 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010100 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010101 dev_priv->display.update_plane = ironlake_update_plane;
10102 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010103 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010104 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010105 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10106 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010107 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010108 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010109 } else if (IS_VALLEYVIEW(dev)) {
10110 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10111 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10112 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10113 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10114 dev_priv->display.off = i9xx_crtc_off;
10115 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010116 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010117 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010118 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010119 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10120 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010121 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010122 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010123 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010124
Jesse Barnese70236a2009-09-21 10:42:27 -070010125 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010126 if (IS_VALLEYVIEW(dev))
10127 dev_priv->display.get_display_clock_speed =
10128 valleyview_get_display_clock_speed;
10129 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010130 dev_priv->display.get_display_clock_speed =
10131 i945_get_display_clock_speed;
10132 else if (IS_I915G(dev))
10133 dev_priv->display.get_display_clock_speed =
10134 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010135 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010136 dev_priv->display.get_display_clock_speed =
10137 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010138 else if (IS_PINEVIEW(dev))
10139 dev_priv->display.get_display_clock_speed =
10140 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010141 else if (IS_I915GM(dev))
10142 dev_priv->display.get_display_clock_speed =
10143 i915gm_get_display_clock_speed;
10144 else if (IS_I865G(dev))
10145 dev_priv->display.get_display_clock_speed =
10146 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010147 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010148 dev_priv->display.get_display_clock_speed =
10149 i855_get_display_clock_speed;
10150 else /* 852, 830 */
10151 dev_priv->display.get_display_clock_speed =
10152 i830_get_display_clock_speed;
10153
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010154 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010155 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010156 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010157 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010158 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010159 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010160 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010161 } else if (IS_IVYBRIDGE(dev)) {
10162 /* FIXME: detect B0+ stepping and use auto training */
10163 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010164 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010165 dev_priv->display.modeset_global_resources =
10166 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010167 } else if (IS_HASWELL(dev)) {
10168 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010169 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010170 dev_priv->display.modeset_global_resources =
10171 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010172 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010173 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010174 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010175 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010176
10177 /* Default just returns -ENODEV to indicate unsupported */
10178 dev_priv->display.queue_flip = intel_default_queue_flip;
10179
10180 switch (INTEL_INFO(dev)->gen) {
10181 case 2:
10182 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10183 break;
10184
10185 case 3:
10186 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10187 break;
10188
10189 case 4:
10190 case 5:
10191 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10192 break;
10193
10194 case 6:
10195 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10196 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010197 case 7:
10198 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10199 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010200 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010201}
10202
Jesse Barnesb690e962010-07-19 13:53:12 -070010203/*
10204 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10205 * resume, or other times. This quirk makes sure that's the case for
10206 * affected systems.
10207 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010208static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010209{
10210 struct drm_i915_private *dev_priv = dev->dev_private;
10211
10212 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010213 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010214}
10215
Keith Packard435793d2011-07-12 14:56:22 -070010216/*
10217 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10218 */
10219static void quirk_ssc_force_disable(struct drm_device *dev)
10220{
10221 struct drm_i915_private *dev_priv = dev->dev_private;
10222 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010223 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010224}
10225
Carsten Emde4dca20e2012-03-15 15:56:26 +010010226/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010227 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10228 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010229 */
10230static void quirk_invert_brightness(struct drm_device *dev)
10231{
10232 struct drm_i915_private *dev_priv = dev->dev_private;
10233 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010234 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010235}
10236
Kamal Mostafae85843b2013-07-19 15:02:01 -070010237/*
10238 * Some machines (Dell XPS13) suffer broken backlight controls if
10239 * BLM_PCH_PWM_ENABLE is set.
10240 */
10241static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
10242{
10243 struct drm_i915_private *dev_priv = dev->dev_private;
10244 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
10245 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
10246}
10247
Jesse Barnesb690e962010-07-19 13:53:12 -070010248struct intel_quirk {
10249 int device;
10250 int subsystem_vendor;
10251 int subsystem_device;
10252 void (*hook)(struct drm_device *dev);
10253};
10254
Egbert Eich5f85f172012-10-14 15:46:38 +020010255/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10256struct intel_dmi_quirk {
10257 void (*hook)(struct drm_device *dev);
10258 const struct dmi_system_id (*dmi_id_list)[];
10259};
10260
10261static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10262{
10263 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10264 return 1;
10265}
10266
10267static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10268 {
10269 .dmi_id_list = &(const struct dmi_system_id[]) {
10270 {
10271 .callback = intel_dmi_reverse_brightness,
10272 .ident = "NCR Corporation",
10273 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10274 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10275 },
10276 },
10277 { } /* terminating entry */
10278 },
10279 .hook = quirk_invert_brightness,
10280 },
10281};
10282
Ben Widawskyc43b5632012-04-16 14:07:40 -070010283static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010284 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010285 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010286
Jesse Barnesb690e962010-07-19 13:53:12 -070010287 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10288 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10289
Jesse Barnesb690e962010-07-19 13:53:12 -070010290 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10291 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10292
Daniel Vetterccd0d362012-10-10 23:13:59 +020010293 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -070010294 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010295 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010296
10297 /* Lenovo U160 cannot use SSC on LVDS */
10298 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010299
10300 /* Sony Vaio Y cannot use SSC on LVDS */
10301 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010302
Jani Nikulaee1452d2013-09-20 15:05:30 +030010303 /*
10304 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10305 * seem to use inverted backlight PWM.
10306 */
10307 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Kamal Mostafae85843b2013-07-19 15:02:01 -070010308
10309 /* Dell XPS13 HD Sandy Bridge */
10310 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
10311 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
10312 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
Jesse Barnesb690e962010-07-19 13:53:12 -070010313};
10314
10315static void intel_init_quirks(struct drm_device *dev)
10316{
10317 struct pci_dev *d = dev->pdev;
10318 int i;
10319
10320 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10321 struct intel_quirk *q = &intel_quirks[i];
10322
10323 if (d->device == q->device &&
10324 (d->subsystem_vendor == q->subsystem_vendor ||
10325 q->subsystem_vendor == PCI_ANY_ID) &&
10326 (d->subsystem_device == q->subsystem_device ||
10327 q->subsystem_device == PCI_ANY_ID))
10328 q->hook(dev);
10329 }
Egbert Eich5f85f172012-10-14 15:46:38 +020010330 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10331 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10332 intel_dmi_quirks[i].hook(dev);
10333 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010334}
10335
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010336/* Disable the VGA plane that we never use */
10337static void i915_disable_vga(struct drm_device *dev)
10338{
10339 struct drm_i915_private *dev_priv = dev->dev_private;
10340 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010341 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010342
10343 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010344 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010345 sr1 = inb(VGA_SR_DATA);
10346 outb(sr1 | 1<<5, VGA_SR_DATA);
10347 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10348 udelay(300);
10349
10350 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10351 POSTING_READ(vga_reg);
10352}
10353
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010354static void i915_enable_vga_mem(struct drm_device *dev)
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010355{
10356 /* Enable VGA memory on Intel HD */
10357 if (HAS_PCH_SPLIT(dev)) {
10358 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10359 outb(inb(VGA_MSR_READ) | VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10360 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10361 VGA_RSRC_LEGACY_MEM |
10362 VGA_RSRC_NORMAL_IO |
10363 VGA_RSRC_NORMAL_MEM);
10364 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10365 }
10366}
10367
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010368void i915_disable_vga_mem(struct drm_device *dev)
10369{
10370 /* Disable VGA memory on Intel HD */
10371 if (HAS_PCH_SPLIT(dev)) {
10372 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10373 outb(inb(VGA_MSR_READ) & ~VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10374 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10375 VGA_RSRC_NORMAL_IO |
10376 VGA_RSRC_NORMAL_MEM);
10377 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10378 }
10379}
10380
Daniel Vetterf8175862012-04-10 15:50:11 +020010381void intel_modeset_init_hw(struct drm_device *dev)
10382{
Jesse Barnesf6071162013-10-01 10:41:38 -070010383 struct drm_i915_private *dev_priv = dev->dev_private;
10384
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010385 intel_prepare_ddi(dev);
10386
Daniel Vetterf8175862012-04-10 15:50:11 +020010387 intel_init_clock_gating(dev);
10388
Jesse Barnesf6071162013-10-01 10:41:38 -070010389 /* Enable the CRI clock source so we can get at the display */
10390 if (IS_VALLEYVIEW(dev))
10391 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10392 DPLL_INTEGRATED_CRI_CLK_VLV);
10393
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010394 intel_init_dpio(dev);
10395
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010396 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010397 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010398 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010399}
10400
Imre Deak7d708ee2013-04-17 14:04:50 +030010401void intel_modeset_suspend_hw(struct drm_device *dev)
10402{
10403 intel_suspend_hw(dev);
10404}
10405
Jesse Barnes79e53942008-11-07 14:24:08 -080010406void intel_modeset_init(struct drm_device *dev)
10407{
Jesse Barnes652c3932009-08-17 13:31:43 -070010408 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010409 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010410
10411 drm_mode_config_init(dev);
10412
10413 dev->mode_config.min_width = 0;
10414 dev->mode_config.min_height = 0;
10415
Dave Airlie019d96c2011-09-29 16:20:42 +010010416 dev->mode_config.preferred_depth = 24;
10417 dev->mode_config.prefer_shadow = 1;
10418
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010419 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010420
Jesse Barnesb690e962010-07-19 13:53:12 -070010421 intel_init_quirks(dev);
10422
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010423 intel_init_pm(dev);
10424
Ben Widawskye3c74752013-04-05 13:12:39 -070010425 if (INTEL_INFO(dev)->num_pipes == 0)
10426 return;
10427
Jesse Barnese70236a2009-09-21 10:42:27 -070010428 intel_init_display(dev);
10429
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010430 if (IS_GEN2(dev)) {
10431 dev->mode_config.max_width = 2048;
10432 dev->mode_config.max_height = 2048;
10433 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010434 dev->mode_config.max_width = 4096;
10435 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010436 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010437 dev->mode_config.max_width = 8192;
10438 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010439 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010440 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010441
Zhao Yakui28c97732009-10-09 11:39:41 +080010442 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010443 INTEL_INFO(dev)->num_pipes,
10444 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010445
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010446 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010447 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010448 for (j = 0; j < dev_priv->num_plane; j++) {
10449 ret = intel_plane_init(dev, i, j);
10450 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010451 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10452 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010453 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010454 }
10455
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010456 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010457 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010458
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010459 /* Just disable it once at startup */
10460 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010461 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010462
10463 /* Just in case the BIOS is doing something questionable. */
10464 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010465}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010466
Daniel Vetter24929352012-07-02 20:28:59 +020010467static void
10468intel_connector_break_all_links(struct intel_connector *connector)
10469{
10470 connector->base.dpms = DRM_MODE_DPMS_OFF;
10471 connector->base.encoder = NULL;
10472 connector->encoder->connectors_active = false;
10473 connector->encoder->base.crtc = NULL;
10474}
10475
Daniel Vetter7fad7982012-07-04 17:51:47 +020010476static void intel_enable_pipe_a(struct drm_device *dev)
10477{
10478 struct intel_connector *connector;
10479 struct drm_connector *crt = NULL;
10480 struct intel_load_detect_pipe load_detect_temp;
10481
10482 /* We can't just switch on the pipe A, we need to set things up with a
10483 * proper mode and output configuration. As a gross hack, enable pipe A
10484 * by enabling the load detect pipe once. */
10485 list_for_each_entry(connector,
10486 &dev->mode_config.connector_list,
10487 base.head) {
10488 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10489 crt = &connector->base;
10490 break;
10491 }
10492 }
10493
10494 if (!crt)
10495 return;
10496
10497 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10498 intel_release_load_detect_pipe(crt, &load_detect_temp);
10499
10500
10501}
10502
Daniel Vetterfa555832012-10-10 23:14:00 +020010503static bool
10504intel_check_plane_mapping(struct intel_crtc *crtc)
10505{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010506 struct drm_device *dev = crtc->base.dev;
10507 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010508 u32 reg, val;
10509
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010510 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010511 return true;
10512
10513 reg = DSPCNTR(!crtc->plane);
10514 val = I915_READ(reg);
10515
10516 if ((val & DISPLAY_PLANE_ENABLE) &&
10517 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10518 return false;
10519
10520 return true;
10521}
10522
Daniel Vetter24929352012-07-02 20:28:59 +020010523static void intel_sanitize_crtc(struct intel_crtc *crtc)
10524{
10525 struct drm_device *dev = crtc->base.dev;
10526 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010527 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010528
Daniel Vetter24929352012-07-02 20:28:59 +020010529 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010530 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010531 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10532
10533 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010534 * disable the crtc (and hence change the state) if it is wrong. Note
10535 * that gen4+ has a fixed plane -> pipe mapping. */
10536 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010537 struct intel_connector *connector;
10538 bool plane;
10539
Daniel Vetter24929352012-07-02 20:28:59 +020010540 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10541 crtc->base.base.id);
10542
10543 /* Pipe has the wrong plane attached and the plane is active.
10544 * Temporarily change the plane mapping and disable everything
10545 * ... */
10546 plane = crtc->plane;
10547 crtc->plane = !plane;
10548 dev_priv->display.crtc_disable(&crtc->base);
10549 crtc->plane = plane;
10550
10551 /* ... and break all links. */
10552 list_for_each_entry(connector, &dev->mode_config.connector_list,
10553 base.head) {
10554 if (connector->encoder->base.crtc != &crtc->base)
10555 continue;
10556
10557 intel_connector_break_all_links(connector);
10558 }
10559
10560 WARN_ON(crtc->active);
10561 crtc->base.enabled = false;
10562 }
Daniel Vetter24929352012-07-02 20:28:59 +020010563
Daniel Vetter7fad7982012-07-04 17:51:47 +020010564 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10565 crtc->pipe == PIPE_A && !crtc->active) {
10566 /* BIOS forgot to enable pipe A, this mostly happens after
10567 * resume. Force-enable the pipe to fix this, the update_dpms
10568 * call below we restore the pipe to the right state, but leave
10569 * the required bits on. */
10570 intel_enable_pipe_a(dev);
10571 }
10572
Daniel Vetter24929352012-07-02 20:28:59 +020010573 /* Adjust the state of the output pipe according to whether we
10574 * have active connectors/encoders. */
10575 intel_crtc_update_dpms(&crtc->base);
10576
10577 if (crtc->active != crtc->base.enabled) {
10578 struct intel_encoder *encoder;
10579
10580 /* This can happen either due to bugs in the get_hw_state
10581 * functions or because the pipe is force-enabled due to the
10582 * pipe A quirk. */
10583 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10584 crtc->base.base.id,
10585 crtc->base.enabled ? "enabled" : "disabled",
10586 crtc->active ? "enabled" : "disabled");
10587
10588 crtc->base.enabled = crtc->active;
10589
10590 /* Because we only establish the connector -> encoder ->
10591 * crtc links if something is active, this means the
10592 * crtc is now deactivated. Break the links. connector
10593 * -> encoder links are only establish when things are
10594 * actually up, hence no need to break them. */
10595 WARN_ON(crtc->active);
10596
10597 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10598 WARN_ON(encoder->connectors_active);
10599 encoder->base.crtc = NULL;
10600 }
10601 }
10602}
10603
10604static void intel_sanitize_encoder(struct intel_encoder *encoder)
10605{
10606 struct intel_connector *connector;
10607 struct drm_device *dev = encoder->base.dev;
10608
10609 /* We need to check both for a crtc link (meaning that the
10610 * encoder is active and trying to read from a pipe) and the
10611 * pipe itself being active. */
10612 bool has_active_crtc = encoder->base.crtc &&
10613 to_intel_crtc(encoder->base.crtc)->active;
10614
10615 if (encoder->connectors_active && !has_active_crtc) {
10616 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10617 encoder->base.base.id,
10618 drm_get_encoder_name(&encoder->base));
10619
10620 /* Connector is active, but has no active pipe. This is
10621 * fallout from our resume register restoring. Disable
10622 * the encoder manually again. */
10623 if (encoder->base.crtc) {
10624 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10625 encoder->base.base.id,
10626 drm_get_encoder_name(&encoder->base));
10627 encoder->disable(encoder);
10628 }
10629
10630 /* Inconsistent output/port/pipe state happens presumably due to
10631 * a bug in one of the get_hw_state functions. Or someplace else
10632 * in our code, like the register restore mess on resume. Clamp
10633 * things to off as a safer default. */
10634 list_for_each_entry(connector,
10635 &dev->mode_config.connector_list,
10636 base.head) {
10637 if (connector->encoder != encoder)
10638 continue;
10639
10640 intel_connector_break_all_links(connector);
10641 }
10642 }
10643 /* Enabled encoders without active connectors will be fixed in
10644 * the crtc fixup. */
10645}
10646
Daniel Vetter44cec742013-01-25 17:53:21 +010010647void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010648{
10649 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010650 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010651
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010652 /* This function can be called both from intel_modeset_setup_hw_state or
10653 * at a very early point in our resume sequence, where the power well
10654 * structures are not yet restored. Since this function is at a very
10655 * paranoid "someone might have enabled VGA while we were not looking"
10656 * level, just check if the power well is enabled instead of trying to
10657 * follow the "don't touch the power well if we don't need it" policy
10658 * the rest of the driver uses. */
10659 if (HAS_POWER_WELL(dev) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030010660 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010661 return;
10662
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010663 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10664 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020010665 i915_disable_vga(dev);
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010666 i915_disable_vga_mem(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010667 }
10668}
10669
Daniel Vetter30e984d2013-06-05 13:34:17 +020010670static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020010671{
10672 struct drm_i915_private *dev_priv = dev->dev_private;
10673 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020010674 struct intel_crtc *crtc;
10675 struct intel_encoder *encoder;
10676 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020010677 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020010678
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010679 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10680 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010010681 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020010682
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010683 crtc->active = dev_priv->display.get_pipe_config(crtc,
10684 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010685
10686 crtc->base.enabled = crtc->active;
10687
10688 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10689 crtc->base.base.id,
10690 crtc->active ? "enabled" : "disabled");
10691 }
10692
Daniel Vetter53589012013-06-05 13:34:16 +020010693 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010694 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010695 intel_ddi_setup_hw_pll_state(dev);
10696
Daniel Vetter53589012013-06-05 13:34:16 +020010697 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10698 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10699
10700 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10701 pll->active = 0;
10702 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10703 base.head) {
10704 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10705 pll->active++;
10706 }
10707 pll->refcount = pll->active;
10708
Daniel Vetter35c95372013-07-17 06:55:04 +020010709 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10710 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020010711 }
10712
Daniel Vetter24929352012-07-02 20:28:59 +020010713 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10714 base.head) {
10715 pipe = 0;
10716
10717 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010718 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10719 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070010720 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010721 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010722 } else {
10723 encoder->base.crtc = NULL;
10724 }
10725
10726 encoder->connectors_active = false;
10727 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10728 encoder->base.base.id,
10729 drm_get_encoder_name(&encoder->base),
10730 encoder->base.crtc ? "enabled" : "disabled",
10731 pipe);
10732 }
10733
10734 list_for_each_entry(connector, &dev->mode_config.connector_list,
10735 base.head) {
10736 if (connector->get_hw_state(connector)) {
10737 connector->base.dpms = DRM_MODE_DPMS_ON;
10738 connector->encoder->connectors_active = true;
10739 connector->base.encoder = &connector->encoder->base;
10740 } else {
10741 connector->base.dpms = DRM_MODE_DPMS_OFF;
10742 connector->base.encoder = NULL;
10743 }
10744 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10745 connector->base.base.id,
10746 drm_get_connector_name(&connector->base),
10747 connector->base.encoder ? "enabled" : "disabled");
10748 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010749}
10750
10751/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10752 * and i915 state tracking structures. */
10753void intel_modeset_setup_hw_state(struct drm_device *dev,
10754 bool force_restore)
10755{
10756 struct drm_i915_private *dev_priv = dev->dev_private;
10757 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010758 struct intel_crtc *crtc;
10759 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020010760 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010761
10762 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010763
Jesse Barnesbabea612013-06-26 18:57:38 +030010764 /*
10765 * Now that we have the config, copy it to each CRTC struct
10766 * Note that this could go away if we move to using crtc_config
10767 * checking everywhere.
10768 */
10769 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10770 base.head) {
10771 if (crtc->active && i915_fastboot) {
10772 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10773
10774 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10775 crtc->base.base.id);
10776 drm_mode_debug_printmodeline(&crtc->base.mode);
10777 }
10778 }
10779
Daniel Vetter24929352012-07-02 20:28:59 +020010780 /* HW state is read out, now we need to sanitize this mess. */
10781 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10782 base.head) {
10783 intel_sanitize_encoder(encoder);
10784 }
10785
10786 for_each_pipe(pipe) {
10787 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10788 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010789 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010790 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010791
Daniel Vetter35c95372013-07-17 06:55:04 +020010792 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10793 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10794
10795 if (!pll->on || pll->active)
10796 continue;
10797
10798 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10799
10800 pll->disable(dev_priv, pll);
10801 pll->on = false;
10802 }
10803
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010804 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030010805 i915_redisable_vga(dev);
10806
Daniel Vetterf30da182013-04-11 20:22:50 +020010807 /*
10808 * We need to use raw interfaces for restoring state to avoid
10809 * checking (bogus) intermediate states.
10810 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010811 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010812 struct drm_crtc *crtc =
10813 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010814
10815 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10816 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010817 }
10818 } else {
10819 intel_modeset_update_staged_output_state(dev);
10820 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010821
10822 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010823
10824 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010825}
10826
10827void intel_modeset_gem_init(struct drm_device *dev)
10828{
Chris Wilson1833b132012-05-09 11:56:28 +010010829 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010830
10831 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010832
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010833 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010834}
10835
10836void intel_modeset_cleanup(struct drm_device *dev)
10837{
Jesse Barnes652c3932009-08-17 13:31:43 -070010838 struct drm_i915_private *dev_priv = dev->dev_private;
10839 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030010840 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070010841
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010842 /*
10843 * Interrupts and polling as the first thing to avoid creating havoc.
10844 * Too much stuff here (turning of rps, connectors, ...) would
10845 * experience fancy races otherwise.
10846 */
10847 drm_irq_uninstall(dev);
10848 cancel_work_sync(&dev_priv->hotplug_work);
10849 /*
10850 * Due to the hpd irq storm handling the hotplug work can re-arm the
10851 * poll handlers. Hence disable polling after hpd handling is shut down.
10852 */
Keith Packardf87ea762010-10-03 19:36:26 -070010853 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010854
Jesse Barnes652c3932009-08-17 13:31:43 -070010855 mutex_lock(&dev->struct_mutex);
10856
Jesse Barnes723bfd72010-10-07 16:01:13 -070010857 intel_unregister_dsm_handler();
10858
Jesse Barnes652c3932009-08-17 13:31:43 -070010859 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10860 /* Skip inactive CRTCs */
10861 if (!crtc->fb)
10862 continue;
10863
Daniel Vetter3dec0092010-08-20 21:40:52 +020010864 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010865 }
10866
Chris Wilson973d04f2011-07-08 12:22:37 +010010867 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010868
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010869 i915_enable_vga_mem(dev);
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010870
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010871 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010872
Daniel Vetter930ebb42012-06-29 23:32:16 +020010873 ironlake_teardown_rc6(dev);
10874
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010875 mutex_unlock(&dev->struct_mutex);
10876
Chris Wilson1630fe72011-07-08 12:22:42 +010010877 /* flush any delayed tasks or pending work */
10878 flush_scheduled_work();
10879
Jani Nikuladc652f92013-04-12 15:18:38 +030010880 /* destroy backlight, if any, before the connectors */
10881 intel_panel_destroy_backlight(dev);
10882
Paulo Zanonid9255d52013-09-26 20:05:59 -030010883 /* destroy the sysfs files before encoders/connectors */
10884 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
10885 drm_sysfs_connector_remove(connector);
10886
Jesse Barnes79e53942008-11-07 14:24:08 -080010887 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010888
10889 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010890}
10891
Dave Airlie28d52042009-09-21 14:33:58 +100010892/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010893 * Return which encoder is currently attached for connector.
10894 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010895struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010896{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010897 return &intel_attached_encoder(connector)->base;
10898}
Jesse Barnes79e53942008-11-07 14:24:08 -080010899
Chris Wilsondf0e9242010-09-09 16:20:55 +010010900void intel_connector_attach_encoder(struct intel_connector *connector,
10901 struct intel_encoder *encoder)
10902{
10903 connector->encoder = encoder;
10904 drm_mode_connector_attach_encoder(&connector->base,
10905 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010906}
Dave Airlie28d52042009-09-21 14:33:58 +100010907
10908/*
10909 * set vga decode state - true == enable VGA decode
10910 */
10911int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10912{
10913 struct drm_i915_private *dev_priv = dev->dev_private;
10914 u16 gmch_ctrl;
10915
10916 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10917 if (state)
10918 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10919 else
10920 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10921 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10922 return 0;
10923}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010924
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010925struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010926
10927 u32 power_well_driver;
10928
Chris Wilson63b66e52013-08-08 15:12:06 +020010929 int num_transcoders;
10930
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010931 struct intel_cursor_error_state {
10932 u32 control;
10933 u32 position;
10934 u32 base;
10935 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010936 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010937
10938 struct intel_pipe_error_state {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010939 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010010940 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010941
10942 struct intel_plane_error_state {
10943 u32 control;
10944 u32 stride;
10945 u32 size;
10946 u32 pos;
10947 u32 addr;
10948 u32 surface;
10949 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010950 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020010951
10952 struct intel_transcoder_error_state {
10953 enum transcoder cpu_transcoder;
10954
10955 u32 conf;
10956
10957 u32 htotal;
10958 u32 hblank;
10959 u32 hsync;
10960 u32 vtotal;
10961 u32 vblank;
10962 u32 vsync;
10963 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010964};
10965
10966struct intel_display_error_state *
10967intel_display_capture_error_state(struct drm_device *dev)
10968{
Akshay Joshi0206e352011-08-16 15:34:10 -040010969 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010970 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020010971 int transcoders[] = {
10972 TRANSCODER_A,
10973 TRANSCODER_B,
10974 TRANSCODER_C,
10975 TRANSCODER_EDP,
10976 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010977 int i;
10978
Chris Wilson63b66e52013-08-08 15:12:06 +020010979 if (INTEL_INFO(dev)->num_pipes == 0)
10980 return NULL;
10981
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010982 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10983 if (error == NULL)
10984 return NULL;
10985
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010986 if (HAS_POWER_WELL(dev))
10987 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10988
Damien Lespiau52331302012-08-15 19:23:25 +010010989 for_each_pipe(i) {
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010990 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10991 error->cursor[i].control = I915_READ(CURCNTR(i));
10992 error->cursor[i].position = I915_READ(CURPOS(i));
10993 error->cursor[i].base = I915_READ(CURBASE(i));
10994 } else {
10995 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10996 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10997 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10998 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010999
11000 error->plane[i].control = I915_READ(DSPCNTR(i));
11001 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011002 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011003 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011004 error->plane[i].pos = I915_READ(DSPPOS(i));
11005 }
Paulo Zanonica291362013-03-06 20:03:14 -030011006 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11007 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011008 if (INTEL_INFO(dev)->gen >= 4) {
11009 error->plane[i].surface = I915_READ(DSPSURF(i));
11010 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11011 }
11012
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011013 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011014 }
11015
11016 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11017 if (HAS_DDI(dev_priv->dev))
11018 error->num_transcoders++; /* Account for eDP. */
11019
11020 for (i = 0; i < error->num_transcoders; i++) {
11021 enum transcoder cpu_transcoder = transcoders[i];
11022
11023 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11024
11025 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11026 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11027 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11028 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11029 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11030 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11031 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011032 }
11033
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011034 /* In the code above we read the registers without checking if the power
11035 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
11036 * prevent the next I915_WRITE from detecting it and printing an error
11037 * message. */
Chris Wilson907b28c2013-07-19 20:36:52 +010011038 intel_uncore_clear_errors(dev);
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011039
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011040 return error;
11041}
11042
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011043#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11044
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011045void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011046intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011047 struct drm_device *dev,
11048 struct intel_display_error_state *error)
11049{
11050 int i;
11051
Chris Wilson63b66e52013-08-08 15:12:06 +020011052 if (!error)
11053 return;
11054
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011055 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011056 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011057 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011058 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011059 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011060 err_printf(m, "Pipe [%d]:\n", i);
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011061 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011062
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011063 err_printf(m, "Plane [%d]:\n", i);
11064 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11065 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011066 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011067 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11068 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011069 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011070 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011071 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011072 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011073 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11074 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011075 }
11076
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011077 err_printf(m, "Cursor [%d]:\n", i);
11078 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11079 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11080 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011081 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011082
11083 for (i = 0; i < error->num_transcoders; i++) {
11084 err_printf(m, " CPU transcoder: %c\n",
11085 transcoder_name(error->transcoder[i].cpu_transcoder));
11086 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11087 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11088 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11089 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11090 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11091 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11092 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11093 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011094}