blob: 784ff70a95eb2c70974d7007159e3fb263da803d [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
Ville Syrjälädc730512013-09-24 21:26:30 +0300312static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300313 /*
314 * These are the data rate limits (measured in fast clocks)
315 * since those are the strictest limits we have. The fast
316 * clock and actual rate limits are more relaxed, so checking
317 * them would make no difference.
318 */
319 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200320 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700321 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .m1 = { .min = 2, .max = 3 },
323 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300324 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300325 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700326};
327
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300328static void vlv_clock(int refclk, intel_clock_t *clock)
329{
330 clock->m = clock->m1 * clock->m2;
331 clock->p = clock->p1 * clock->p2;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300332 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
333 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300334}
335
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300336/**
337 * Returns whether any output on the specified pipe is of the specified type
338 */
339static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
340{
341 struct drm_device *dev = crtc->dev;
342 struct intel_encoder *encoder;
343
344 for_each_encoder_on_crtc(dev, crtc, encoder)
345 if (encoder->type == type)
346 return true;
347
348 return false;
349}
350
Chris Wilson1b894b52010-12-14 20:04:54 +0000351static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
352 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800353{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800355 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800356
357 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100358 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000359 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360 limit = &intel_limits_ironlake_dual_lvds_100m;
361 else
362 limit = &intel_limits_ironlake_dual_lvds;
363 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000364 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800365 limit = &intel_limits_ironlake_single_lvds_100m;
366 else
367 limit = &intel_limits_ironlake_single_lvds;
368 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200369 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800370 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800371
372 return limit;
373}
374
Ma Ling044c7c42009-03-18 20:13:23 +0800375static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
376{
377 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800378 const intel_limit_t *limit;
379
380 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100381 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700382 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800383 else
Keith Packarde4b36692009-06-05 19:22:17 -0700384 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800385 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
386 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700387 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800388 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700389 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800390 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700391 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800392
393 return limit;
394}
395
Chris Wilson1b894b52010-12-14 20:04:54 +0000396static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800397{
398 struct drm_device *dev = crtc->dev;
399 const intel_limit_t *limit;
400
Eric Anholtbad720f2009-10-22 16:11:14 -0700401 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000402 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800403 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800404 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500405 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500407 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800408 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500409 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700410 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300411 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100412 } else if (!IS_GEN2(dev)) {
413 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
414 limit = &intel_limits_i9xx_lvds;
415 else
416 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800417 } else {
418 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700419 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200420 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700421 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200422 else
423 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800424 }
425 return limit;
426}
427
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500428/* m1 is reserved as 0 in Pineview, n is a ring counter */
429static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800430{
Shaohua Li21778322009-02-23 15:19:16 +0800431 clock->m = clock->m2 + 2;
432 clock->p = clock->p1 * clock->p2;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300433 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
434 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800435}
436
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200437static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
438{
439 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
440}
441
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200442static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800443{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200444 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800445 clock->p = clock->p1 * clock->p2;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300446 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
447 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800448}
449
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800450#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800451/**
452 * Returns whether the given set of divisors are valid for a given refclk with
453 * the given connectors.
454 */
455
Chris Wilson1b894b52010-12-14 20:04:54 +0000456static bool intel_PLL_is_valid(struct drm_device *dev,
457 const intel_limit_t *limit,
458 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800459{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300460 if (clock->n < limit->n.min || limit->n.max < clock->n)
461 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800462 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400463 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800464 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800466 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300468
469 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
470 if (clock->m1 <= clock->m2)
471 INTELPllInvalid("m1 <= m2\n");
472
473 if (!IS_VALLEYVIEW(dev)) {
474 if (clock->p < limit->p.min || limit->p.max < clock->p)
475 INTELPllInvalid("p out of range\n");
476 if (clock->m < limit->m.min || limit->m.max < clock->m)
477 INTELPllInvalid("m out of range\n");
478 }
479
Jesse Barnes79e53942008-11-07 14:24:08 -0800480 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400481 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800482 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
483 * connector, etc., rather than just a single range.
484 */
485 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487
488 return true;
489}
490
Ma Lingd4906092009-03-18 20:13:27 +0800491static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200492i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800493 int target, int refclk, intel_clock_t *match_clock,
494 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800495{
496 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800498 int err = target;
499
Daniel Vettera210b022012-11-26 17:22:08 +0100500 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800501 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100502 * For LVDS just rely on its current settings for dual-channel.
503 * We haven't figured out how to reliably set up different
504 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100506 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 clock.p2 = limit->p2.p2_fast;
508 else
509 clock.p2 = limit->p2.p2_slow;
510 } else {
511 if (target < limit->p2.dot_limit)
512 clock.p2 = limit->p2.p2_slow;
513 else
514 clock.p2 = limit->p2.p2_fast;
515 }
516
Akshay Joshi0206e352011-08-16 15:34:10 -0400517 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800518
Zhao Yakui42158662009-11-20 11:24:18 +0800519 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
520 clock.m1++) {
521 for (clock.m2 = limit->m2.min;
522 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200523 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800524 break;
525 for (clock.n = limit->n.min;
526 clock.n <= limit->n.max; clock.n++) {
527 for (clock.p1 = limit->p1.min;
528 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800529 int this_err;
530
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200531 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000532 if (!intel_PLL_is_valid(dev, limit,
533 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800534 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800535 if (match_clock &&
536 clock.p != match_clock->p)
537 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800538
539 this_err = abs(clock.dot - target);
540 if (this_err < err) {
541 *best_clock = clock;
542 err = this_err;
543 }
544 }
545 }
546 }
547 }
548
549 return (err != target);
550}
551
Ma Lingd4906092009-03-18 20:13:27 +0800552static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200553pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
554 int target, int refclk, intel_clock_t *match_clock,
555 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200556{
557 struct drm_device *dev = crtc->dev;
558 intel_clock_t clock;
559 int err = target;
560
561 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
562 /*
563 * For LVDS just rely on its current settings for dual-channel.
564 * We haven't figured out how to reliably set up different
565 * single/dual channel state, if we even can.
566 */
567 if (intel_is_dual_link_lvds(dev))
568 clock.p2 = limit->p2.p2_fast;
569 else
570 clock.p2 = limit->p2.p2_slow;
571 } else {
572 if (target < limit->p2.dot_limit)
573 clock.p2 = limit->p2.p2_slow;
574 else
575 clock.p2 = limit->p2.p2_fast;
576 }
577
578 memset(best_clock, 0, sizeof(*best_clock));
579
580 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
581 clock.m1++) {
582 for (clock.m2 = limit->m2.min;
583 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200584 for (clock.n = limit->n.min;
585 clock.n <= limit->n.max; clock.n++) {
586 for (clock.p1 = limit->p1.min;
587 clock.p1 <= limit->p1.max; clock.p1++) {
588 int this_err;
589
590 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800591 if (!intel_PLL_is_valid(dev, limit,
592 &clock))
593 continue;
594 if (match_clock &&
595 clock.p != match_clock->p)
596 continue;
597
598 this_err = abs(clock.dot - target);
599 if (this_err < err) {
600 *best_clock = clock;
601 err = this_err;
602 }
603 }
604 }
605 }
606 }
607
608 return (err != target);
609}
610
Ma Lingd4906092009-03-18 20:13:27 +0800611static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200612g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
613 int target, int refclk, intel_clock_t *match_clock,
614 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800615{
616 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800617 intel_clock_t clock;
618 int max_n;
619 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400620 /* approximately equals target * 0.00585 */
621 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800622 found = false;
623
624 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100625 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800626 clock.p2 = limit->p2.p2_fast;
627 else
628 clock.p2 = limit->p2.p2_slow;
629 } else {
630 if (target < limit->p2.dot_limit)
631 clock.p2 = limit->p2.p2_slow;
632 else
633 clock.p2 = limit->p2.p2_fast;
634 }
635
636 memset(best_clock, 0, sizeof(*best_clock));
637 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200638 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800639 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200640 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800641 for (clock.m1 = limit->m1.max;
642 clock.m1 >= limit->m1.min; clock.m1--) {
643 for (clock.m2 = limit->m2.max;
644 clock.m2 >= limit->m2.min; clock.m2--) {
645 for (clock.p1 = limit->p1.max;
646 clock.p1 >= limit->p1.min; clock.p1--) {
647 int this_err;
648
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200649 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000650 if (!intel_PLL_is_valid(dev, limit,
651 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800652 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000653
654 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800655 if (this_err < err_most) {
656 *best_clock = clock;
657 err_most = this_err;
658 max_n = clock.n;
659 found = true;
660 }
661 }
662 }
663 }
664 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800665 return found;
666}
Ma Lingd4906092009-03-18 20:13:27 +0800667
Zhenyu Wang2c072452009-06-05 15:38:42 +0800668static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200669vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
670 int target, int refclk, intel_clock_t *match_clock,
671 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700672{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300673 struct drm_device *dev = crtc->dev;
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);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300678 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700679
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300680 target *= 5; /* fast clock */
681
682 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700683
684 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300685 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300686 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300687 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300688 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300689 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700690 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300691 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300692 unsigned int ppm, diff;
693
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300694 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
695 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300696
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300697 vlv_clock(refclk, &clock);
698
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300699 if (!intel_PLL_is_valid(dev, limit,
700 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300701 continue;
702
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300703 diff = abs(clock.dot - target);
704 ppm = div_u64(1000000ULL * diff, target);
705
706 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300707 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300708 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300709 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300710 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300711
Ville Syrjäläc6861222013-09-24 21:26:21 +0300712 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300713 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300714 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300715 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700716 }
717 }
718 }
719 }
720 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700721
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300722 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700723}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700724
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300725bool intel_crtc_active(struct drm_crtc *crtc)
726{
727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
728
729 /* Be paranoid as we can arrive here with only partial
730 * state retrieved from the hardware during setup.
731 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100732 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300733 * as Haswell has gained clock readout/fastboot support.
734 *
735 * We can ditch the crtc->fb check as soon as we can
736 * properly reconstruct framebuffers.
737 */
738 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100739 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300740}
741
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200742enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
743 enum pipe pipe)
744{
745 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
747
Daniel Vetter3b117c82013-04-17 20:15:07 +0200748 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200749}
750
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200751static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300752{
753 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200754 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300755
756 frame = I915_READ(frame_reg);
757
758 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
759 DRM_DEBUG_KMS("vblank wait timed out\n");
760}
761
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700762/**
763 * intel_wait_for_vblank - wait for vblank on a given pipe
764 * @dev: drm device
765 * @pipe: pipe to wait for
766 *
767 * Wait for vblank to occur on a given pipe. Needed for various bits of
768 * mode setting code.
769 */
770void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800771{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700772 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800773 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700774
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200775 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
776 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300777 return;
778 }
779
Chris Wilson300387c2010-09-05 20:25:43 +0100780 /* Clear existing vblank status. Note this will clear any other
781 * sticky status fields as well.
782 *
783 * This races with i915_driver_irq_handler() with the result
784 * that either function could miss a vblank event. Here it is not
785 * fatal, as we will either wait upon the next vblank interrupt or
786 * timeout. Generally speaking intel_wait_for_vblank() is only
787 * called during modeset at which time the GPU should be idle and
788 * should *not* be performing page flips and thus not waiting on
789 * vblanks...
790 * Currently, the result of us stealing a vblank from the irq
791 * handler is that a single frame will be skipped during swapbuffers.
792 */
793 I915_WRITE(pipestat_reg,
794 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
795
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700796 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100797 if (wait_for(I915_READ(pipestat_reg) &
798 PIPE_VBLANK_INTERRUPT_STATUS,
799 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700800 DRM_DEBUG_KMS("vblank wait timed out\n");
801}
802
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300803static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
804{
805 struct drm_i915_private *dev_priv = dev->dev_private;
806 u32 reg = PIPEDSL(pipe);
807 u32 line1, line2;
808 u32 line_mask;
809
810 if (IS_GEN2(dev))
811 line_mask = DSL_LINEMASK_GEN2;
812 else
813 line_mask = DSL_LINEMASK_GEN3;
814
815 line1 = I915_READ(reg) & line_mask;
816 mdelay(5);
817 line2 = I915_READ(reg) & line_mask;
818
819 return line1 == line2;
820}
821
Keith Packardab7ad7f2010-10-03 00:33:06 -0700822/*
823 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700824 * @dev: drm device
825 * @pipe: pipe to wait for
826 *
827 * After disabling a pipe, we can't wait for vblank in the usual way,
828 * spinning on the vblank interrupt status bit, since we won't actually
829 * see an interrupt when the pipe is disabled.
830 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700831 * On Gen4 and above:
832 * wait for the pipe register state bit to turn off
833 *
834 * Otherwise:
835 * wait for the display line value to settle (it usually
836 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100837 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700838 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100839void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700840{
841 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200842 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
843 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700844
Keith Packardab7ad7f2010-10-03 00:33:06 -0700845 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200846 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700847
Keith Packardab7ad7f2010-10-03 00:33:06 -0700848 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100849 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
850 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200851 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700852 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700853 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300854 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200855 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700856 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800857}
858
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000859/*
860 * ibx_digital_port_connected - is the specified port connected?
861 * @dev_priv: i915 private structure
862 * @port: the port to test
863 *
864 * Returns true if @port is connected, false otherwise.
865 */
866bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
867 struct intel_digital_port *port)
868{
869 u32 bit;
870
Damien Lespiauc36346e2012-12-13 16:09:03 +0000871 if (HAS_PCH_IBX(dev_priv->dev)) {
872 switch(port->port) {
873 case PORT_B:
874 bit = SDE_PORTB_HOTPLUG;
875 break;
876 case PORT_C:
877 bit = SDE_PORTC_HOTPLUG;
878 break;
879 case PORT_D:
880 bit = SDE_PORTD_HOTPLUG;
881 break;
882 default:
883 return true;
884 }
885 } else {
886 switch(port->port) {
887 case PORT_B:
888 bit = SDE_PORTB_HOTPLUG_CPT;
889 break;
890 case PORT_C:
891 bit = SDE_PORTC_HOTPLUG_CPT;
892 break;
893 case PORT_D:
894 bit = SDE_PORTD_HOTPLUG_CPT;
895 break;
896 default:
897 return true;
898 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000899 }
900
901 return I915_READ(SDEISR) & bit;
902}
903
Jesse Barnesb24e7172011-01-04 15:09:30 -0800904static const char *state_string(bool enabled)
905{
906 return enabled ? "on" : "off";
907}
908
909/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200910void assert_pll(struct drm_i915_private *dev_priv,
911 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800912{
913 int reg;
914 u32 val;
915 bool cur_state;
916
917 reg = DPLL(pipe);
918 val = I915_READ(reg);
919 cur_state = !!(val & DPLL_VCO_ENABLE);
920 WARN(cur_state != state,
921 "PLL state assertion failure (expected %s, current %s)\n",
922 state_string(state), state_string(cur_state));
923}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800924
Jani Nikula23538ef2013-08-27 15:12:22 +0300925/* XXX: the dsi pll is shared between MIPI DSI ports */
926static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
927{
928 u32 val;
929 bool cur_state;
930
931 mutex_lock(&dev_priv->dpio_lock);
932 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
933 mutex_unlock(&dev_priv->dpio_lock);
934
935 cur_state = val & DSI_PLL_VCO_EN;
936 WARN(cur_state != state,
937 "DSI PLL state assertion failure (expected %s, current %s)\n",
938 state_string(state), state_string(cur_state));
939}
940#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
941#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
942
Daniel Vetter55607e82013-06-16 21:42:39 +0200943struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200944intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800945{
Daniel Vettere2b78262013-06-07 23:10:03 +0200946 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
947
Daniel Vettera43f6e02013-06-07 23:10:32 +0200948 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200949 return NULL;
950
Daniel Vettera43f6e02013-06-07 23:10:32 +0200951 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200952}
953
Jesse Barnesb24e7172011-01-04 15:09:30 -0800954/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200955void assert_shared_dpll(struct drm_i915_private *dev_priv,
956 struct intel_shared_dpll *pll,
957 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800958{
Jesse Barnes040484a2011-01-03 12:14:26 -0800959 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200960 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800961
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300962 if (HAS_PCH_LPT(dev_priv->dev)) {
963 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
964 return;
965 }
966
Chris Wilson92b27b02012-05-20 18:10:50 +0100967 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200968 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100969 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100970
Daniel Vetter53589012013-06-05 13:34:16 +0200971 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100972 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200973 "%s assertion failure (expected %s, current %s)\n",
974 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800975}
Jesse Barnes040484a2011-01-03 12:14:26 -0800976
977static void assert_fdi_tx(struct drm_i915_private *dev_priv,
978 enum pipe pipe, bool state)
979{
980 int reg;
981 u32 val;
982 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200983 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
984 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800985
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200986 if (HAS_DDI(dev_priv->dev)) {
987 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200988 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300989 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200990 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300991 } else {
992 reg = FDI_TX_CTL(pipe);
993 val = I915_READ(reg);
994 cur_state = !!(val & FDI_TX_ENABLE);
995 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800996 WARN(cur_state != state,
997 "FDI TX state assertion failure (expected %s, current %s)\n",
998 state_string(state), state_string(cur_state));
999}
1000#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1001#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1002
1003static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1004 enum pipe pipe, bool state)
1005{
1006 int reg;
1007 u32 val;
1008 bool cur_state;
1009
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001010 reg = FDI_RX_CTL(pipe);
1011 val = I915_READ(reg);
1012 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001013 WARN(cur_state != state,
1014 "FDI RX state assertion failure (expected %s, current %s)\n",
1015 state_string(state), state_string(cur_state));
1016}
1017#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1018#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1019
1020static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1021 enum pipe pipe)
1022{
1023 int reg;
1024 u32 val;
1025
1026 /* ILK FDI PLL is always enabled */
1027 if (dev_priv->info->gen == 5)
1028 return;
1029
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001030 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001031 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001032 return;
1033
Jesse Barnes040484a2011-01-03 12:14:26 -08001034 reg = FDI_TX_CTL(pipe);
1035 val = I915_READ(reg);
1036 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1037}
1038
Daniel Vetter55607e82013-06-16 21:42:39 +02001039void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001041{
1042 int reg;
1043 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001044 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001045
1046 reg = FDI_RX_CTL(pipe);
1047 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001048 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1049 WARN(cur_state != state,
1050 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001052}
1053
Jesse Barnesea0760c2011-01-04 15:09:32 -08001054static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1055 enum pipe pipe)
1056{
1057 int pp_reg, lvds_reg;
1058 u32 val;
1059 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001060 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001061
1062 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1063 pp_reg = PCH_PP_CONTROL;
1064 lvds_reg = PCH_LVDS;
1065 } else {
1066 pp_reg = PP_CONTROL;
1067 lvds_reg = LVDS;
1068 }
1069
1070 val = I915_READ(pp_reg);
1071 if (!(val & PANEL_POWER_ON) ||
1072 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1073 locked = false;
1074
1075 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1076 panel_pipe = PIPE_B;
1077
1078 WARN(panel_pipe == pipe && locked,
1079 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001080 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001081}
1082
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001083static void assert_cursor(struct drm_i915_private *dev_priv,
1084 enum pipe pipe, bool state)
1085{
1086 struct drm_device *dev = dev_priv->dev;
1087 bool cur_state;
1088
1089 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1090 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1091 else if (IS_845G(dev) || IS_I865G(dev))
1092 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1093 else
1094 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1095
1096 WARN(cur_state != state,
1097 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1098 pipe_name(pipe), state_string(state), state_string(cur_state));
1099}
1100#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1101#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1102
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001103void assert_pipe(struct drm_i915_private *dev_priv,
1104 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105{
1106 int reg;
1107 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001108 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001109 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1110 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001111
Daniel Vetter8e636782012-01-22 01:36:48 +01001112 /* if we need the pipe A quirk it must be always on */
1113 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1114 state = true;
1115
Paulo Zanonib97186f2013-05-03 12:15:36 -03001116 if (!intel_display_power_enabled(dev_priv->dev,
1117 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001118 cur_state = false;
1119 } else {
1120 reg = PIPECONF(cpu_transcoder);
1121 val = I915_READ(reg);
1122 cur_state = !!(val & PIPECONF_ENABLE);
1123 }
1124
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001125 WARN(cur_state != state,
1126 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001127 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001128}
1129
Chris Wilson931872f2012-01-16 23:01:13 +00001130static void assert_plane(struct drm_i915_private *dev_priv,
1131 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001132{
1133 int reg;
1134 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001135 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136
1137 reg = DSPCNTR(plane);
1138 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001139 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1140 WARN(cur_state != state,
1141 "plane %c assertion failure (expected %s, current %s)\n",
1142 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001143}
1144
Chris Wilson931872f2012-01-16 23:01:13 +00001145#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1146#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1147
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1149 enum pipe pipe)
1150{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001151 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001152 int reg, i;
1153 u32 val;
1154 int cur_pipe;
1155
Ville Syrjälä653e1022013-06-04 13:49:05 +03001156 /* Primary planes are fixed to pipes on gen4+ */
1157 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001158 reg = DSPCNTR(pipe);
1159 val = I915_READ(reg);
1160 WARN((val & DISPLAY_PLANE_ENABLE),
1161 "plane %c assertion failure, should be disabled but not\n",
1162 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001163 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001164 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001165
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001167 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001168 reg = DSPCNTR(i);
1169 val = I915_READ(reg);
1170 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1171 DISPPLANE_SEL_PIPE_SHIFT;
1172 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001173 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1174 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001175 }
1176}
1177
Jesse Barnes19332d72013-03-28 09:55:38 -07001178static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1179 enum pipe pipe)
1180{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001181 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001182 int reg, i;
1183 u32 val;
1184
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001185 if (IS_VALLEYVIEW(dev)) {
1186 for (i = 0; i < dev_priv->num_plane; i++) {
1187 reg = SPCNTR(pipe, i);
1188 val = I915_READ(reg);
1189 WARN((val & SP_ENABLE),
1190 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1191 sprite_name(pipe, i), pipe_name(pipe));
1192 }
1193 } else if (INTEL_INFO(dev)->gen >= 7) {
1194 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001195 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001196 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001197 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001198 plane_name(pipe), pipe_name(pipe));
1199 } else if (INTEL_INFO(dev)->gen >= 5) {
1200 reg = DVSCNTR(pipe);
1201 val = I915_READ(reg);
1202 WARN((val & DVS_ENABLE),
1203 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1204 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001205 }
1206}
1207
Jesse Barnes92f25842011-01-04 15:09:34 -08001208static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1209{
1210 u32 val;
1211 bool enabled;
1212
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001213 if (HAS_PCH_LPT(dev_priv->dev)) {
1214 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1215 return;
1216 }
1217
Jesse Barnes92f25842011-01-04 15:09:34 -08001218 val = I915_READ(PCH_DREF_CONTROL);
1219 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1220 DREF_SUPERSPREAD_SOURCE_MASK));
1221 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1222}
1223
Daniel Vetterab9412b2013-05-03 11:49:46 +02001224static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1225 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001226{
1227 int reg;
1228 u32 val;
1229 bool enabled;
1230
Daniel Vetterab9412b2013-05-03 11:49:46 +02001231 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001232 val = I915_READ(reg);
1233 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001234 WARN(enabled,
1235 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1236 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001237}
1238
Keith Packard4e634382011-08-06 10:39:45 -07001239static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001241{
1242 if ((val & DP_PORT_EN) == 0)
1243 return false;
1244
1245 if (HAS_PCH_CPT(dev_priv->dev)) {
1246 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1247 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1248 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1249 return false;
1250 } else {
1251 if ((val & DP_PIPE_MASK) != (pipe << 30))
1252 return false;
1253 }
1254 return true;
1255}
1256
Keith Packard1519b992011-08-06 10:35:34 -07001257static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1258 enum pipe pipe, u32 val)
1259{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001260 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001261 return false;
1262
1263 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001264 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001265 return false;
1266 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001267 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001268 return false;
1269 }
1270 return true;
1271}
1272
1273static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1274 enum pipe pipe, u32 val)
1275{
1276 if ((val & LVDS_PORT_EN) == 0)
1277 return false;
1278
1279 if (HAS_PCH_CPT(dev_priv->dev)) {
1280 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1281 return false;
1282 } else {
1283 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1284 return false;
1285 }
1286 return true;
1287}
1288
1289static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1290 enum pipe pipe, u32 val)
1291{
1292 if ((val & ADPA_DAC_ENABLE) == 0)
1293 return false;
1294 if (HAS_PCH_CPT(dev_priv->dev)) {
1295 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1296 return false;
1297 } else {
1298 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1299 return false;
1300 }
1301 return true;
1302}
1303
Jesse Barnes291906f2011-02-02 12:28:03 -08001304static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001305 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001306{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001307 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001308 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001309 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001310 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001311
Daniel Vetter75c5da22012-09-10 21:58:29 +02001312 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1313 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001314 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001315}
1316
1317static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe, int reg)
1319{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001320 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001321 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001322 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001323 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001324
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001325 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001326 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001327 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001328}
1329
1330static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1331 enum pipe pipe)
1332{
1333 int reg;
1334 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001335
Keith Packardf0575e92011-07-25 22:12:43 -07001336 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1337 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1338 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001339
1340 reg = PCH_ADPA;
1341 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001342 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001343 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001344 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001345
1346 reg = PCH_LVDS;
1347 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001348 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001349 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001350 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001351
Paulo Zanonie2debe92013-02-18 19:00:27 -03001352 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1353 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1354 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001355}
1356
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001357static void intel_init_dpio(struct drm_device *dev)
1358{
1359 struct drm_i915_private *dev_priv = dev->dev_private;
1360
1361 if (!IS_VALLEYVIEW(dev))
1362 return;
1363
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001364 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001365 /*
1366 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1367 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1368 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1369 * b. The other bits such as sfr settings / modesel may all be set
1370 * to 0.
1371 *
1372 * This should only be done on init and resume from S3 with both
1373 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1374 */
1375 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1376}
1377
Daniel Vetter426115c2013-07-11 22:13:42 +02001378static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001379{
Daniel Vetter426115c2013-07-11 22:13:42 +02001380 struct drm_device *dev = crtc->base.dev;
1381 struct drm_i915_private *dev_priv = dev->dev_private;
1382 int reg = DPLL(crtc->pipe);
1383 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001384
Daniel Vetter426115c2013-07-11 22:13:42 +02001385 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001386
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001387 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001388 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1389
1390 /* PLL is protected by panel, make sure we can write it */
1391 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001392 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001393
Daniel Vetter426115c2013-07-11 22:13:42 +02001394 I915_WRITE(reg, dpll);
1395 POSTING_READ(reg);
1396 udelay(150);
1397
1398 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1399 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1400
1401 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1402 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001403
1404 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001405 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001406 POSTING_READ(reg);
1407 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001408 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001409 POSTING_READ(reg);
1410 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001411 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001412 POSTING_READ(reg);
1413 udelay(150); /* wait for warmup */
1414}
1415
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001416static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001417{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001418 struct drm_device *dev = crtc->base.dev;
1419 struct drm_i915_private *dev_priv = dev->dev_private;
1420 int reg = DPLL(crtc->pipe);
1421 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001422
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001423 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001424
1425 /* No really, not for ILK+ */
1426 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001427
1428 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001429 if (IS_MOBILE(dev) && !IS_I830(dev))
1430 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001431
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001432 I915_WRITE(reg, dpll);
1433
1434 /* Wait for the clocks to stabilize. */
1435 POSTING_READ(reg);
1436 udelay(150);
1437
1438 if (INTEL_INFO(dev)->gen >= 4) {
1439 I915_WRITE(DPLL_MD(crtc->pipe),
1440 crtc->config.dpll_hw_state.dpll_md);
1441 } else {
1442 /* The pixel multiplier can only be updated once the
1443 * DPLL is enabled and the clocks are stable.
1444 *
1445 * So write it again.
1446 */
1447 I915_WRITE(reg, dpll);
1448 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001449
1450 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001451 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001452 POSTING_READ(reg);
1453 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001454 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001455 POSTING_READ(reg);
1456 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001457 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001458 POSTING_READ(reg);
1459 udelay(150); /* wait for warmup */
1460}
1461
1462/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001463 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001464 * @dev_priv: i915 private structure
1465 * @pipe: pipe PLL to disable
1466 *
1467 * Disable the PLL for @pipe, making sure the pipe is off first.
1468 *
1469 * Note! This is for pre-ILK only.
1470 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001471static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001472{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001473 /* Don't disable pipe A or pipe A PLLs if needed */
1474 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1475 return;
1476
1477 /* Make sure the pipe isn't still relying on us */
1478 assert_pipe_disabled(dev_priv, pipe);
1479
Daniel Vetter50b44a42013-06-05 13:34:33 +02001480 I915_WRITE(DPLL(pipe), 0);
1481 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001482}
1483
Jesse Barnesf6071162013-10-01 10:41:38 -07001484static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1485{
1486 u32 val = 0;
1487
1488 /* Make sure the pipe isn't still relying on us */
1489 assert_pipe_disabled(dev_priv, pipe);
1490
1491 /* Leave integrated clock source enabled */
1492 if (pipe == PIPE_B)
1493 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1494 I915_WRITE(DPLL(pipe), val);
1495 POSTING_READ(DPLL(pipe));
1496}
1497
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001498void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1499 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001500{
1501 u32 port_mask;
1502
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001503 switch (dport->port) {
1504 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001505 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001506 break;
1507 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001508 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001509 break;
1510 default:
1511 BUG();
1512 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001513
1514 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1515 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001516 'B' + dport->port, I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001517}
1518
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001519/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001520 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001521 * @dev_priv: i915 private structure
1522 * @pipe: pipe PLL to enable
1523 *
1524 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1525 * drives the transcoder clock.
1526 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001527static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001528{
Daniel Vettere2b78262013-06-07 23:10:03 +02001529 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1530 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001531
Chris Wilson48da64a2012-05-13 20:16:12 +01001532 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001533 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001534 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001535 return;
1536
1537 if (WARN_ON(pll->refcount == 0))
1538 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001539
Daniel Vetter46edb022013-06-05 13:34:12 +02001540 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1541 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001542 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001543
Daniel Vettercdbd2312013-06-05 13:34:03 +02001544 if (pll->active++) {
1545 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001546 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001547 return;
1548 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001549 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001550
Daniel Vetter46edb022013-06-05 13:34:12 +02001551 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001552 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001553 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001554}
1555
Daniel Vettere2b78262013-06-07 23:10:03 +02001556static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001557{
Daniel Vettere2b78262013-06-07 23:10:03 +02001558 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1559 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001560
Jesse Barnes92f25842011-01-04 15:09:34 -08001561 /* PCH only available on ILK+ */
1562 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001563 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001564 return;
1565
Chris Wilson48da64a2012-05-13 20:16:12 +01001566 if (WARN_ON(pll->refcount == 0))
1567 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001568
Daniel Vetter46edb022013-06-05 13:34:12 +02001569 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1570 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001571 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001572
Chris Wilson48da64a2012-05-13 20:16:12 +01001573 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001574 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001575 return;
1576 }
1577
Daniel Vettere9d69442013-06-05 13:34:15 +02001578 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001579 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001580 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001581 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001582
Daniel Vetter46edb022013-06-05 13:34:12 +02001583 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001584 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001585 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001586}
1587
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001588static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1589 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001590{
Daniel Vetter23670b322012-11-01 09:15:30 +01001591 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001592 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001594 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001595
1596 /* PCH only available on ILK+ */
1597 BUG_ON(dev_priv->info->gen < 5);
1598
1599 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001600 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001601 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001602
1603 /* FDI must be feeding us bits for PCH ports */
1604 assert_fdi_tx_enabled(dev_priv, pipe);
1605 assert_fdi_rx_enabled(dev_priv, pipe);
1606
Daniel Vetter23670b322012-11-01 09:15:30 +01001607 if (HAS_PCH_CPT(dev)) {
1608 /* Workaround: Set the timing override bit before enabling the
1609 * pch transcoder. */
1610 reg = TRANS_CHICKEN2(pipe);
1611 val = I915_READ(reg);
1612 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1613 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001614 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001615
Daniel Vetterab9412b2013-05-03 11:49:46 +02001616 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001617 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001618 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001619
1620 if (HAS_PCH_IBX(dev_priv->dev)) {
1621 /*
1622 * make the BPC in transcoder be consistent with
1623 * that in pipeconf reg.
1624 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001625 val &= ~PIPECONF_BPC_MASK;
1626 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001627 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001628
1629 val &= ~TRANS_INTERLACE_MASK;
1630 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001631 if (HAS_PCH_IBX(dev_priv->dev) &&
1632 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1633 val |= TRANS_LEGACY_INTERLACED_ILK;
1634 else
1635 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001636 else
1637 val |= TRANS_PROGRESSIVE;
1638
Jesse Barnes040484a2011-01-03 12:14:26 -08001639 I915_WRITE(reg, val | TRANS_ENABLE);
1640 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001641 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001642}
1643
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001644static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001645 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001646{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001647 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001648
1649 /* PCH only available on ILK+ */
1650 BUG_ON(dev_priv->info->gen < 5);
1651
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001652 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001653 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001654 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001655
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001656 /* Workaround: set timing override bit. */
1657 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001658 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001659 I915_WRITE(_TRANSA_CHICKEN2, val);
1660
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001661 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001662 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001663
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001664 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1665 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001666 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001667 else
1668 val |= TRANS_PROGRESSIVE;
1669
Daniel Vetterab9412b2013-05-03 11:49:46 +02001670 I915_WRITE(LPT_TRANSCONF, val);
1671 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001672 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001673}
1674
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001675static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1676 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001677{
Daniel Vetter23670b322012-11-01 09:15:30 +01001678 struct drm_device *dev = dev_priv->dev;
1679 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001680
1681 /* FDI relies on the transcoder */
1682 assert_fdi_tx_disabled(dev_priv, pipe);
1683 assert_fdi_rx_disabled(dev_priv, pipe);
1684
Jesse Barnes291906f2011-02-02 12:28:03 -08001685 /* Ports must be off as well */
1686 assert_pch_ports_disabled(dev_priv, pipe);
1687
Daniel Vetterab9412b2013-05-03 11:49:46 +02001688 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001689 val = I915_READ(reg);
1690 val &= ~TRANS_ENABLE;
1691 I915_WRITE(reg, val);
1692 /* wait for PCH transcoder off, transcoder state */
1693 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001694 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001695
1696 if (!HAS_PCH_IBX(dev)) {
1697 /* Workaround: Clear the timing override chicken bit again. */
1698 reg = TRANS_CHICKEN2(pipe);
1699 val = I915_READ(reg);
1700 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1701 I915_WRITE(reg, val);
1702 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001703}
1704
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001705static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001706{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001707 u32 val;
1708
Daniel Vetterab9412b2013-05-03 11:49:46 +02001709 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001710 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001711 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001712 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001713 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001714 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001715
1716 /* Workaround: clear timing override bit. */
1717 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001718 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001719 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001720}
1721
1722/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001723 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001724 * @dev_priv: i915 private structure
1725 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001726 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001727 *
1728 * Enable @pipe, making sure that various hardware specific requirements
1729 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1730 *
1731 * @pipe should be %PIPE_A or %PIPE_B.
1732 *
1733 * Will wait until the pipe is actually running (i.e. first vblank) before
1734 * returning.
1735 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001736static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001737 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001738{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001739 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1740 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001741 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001742 int reg;
1743 u32 val;
1744
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001745 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001746 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001747 assert_sprites_disabled(dev_priv, pipe);
1748
Paulo Zanoni681e5812012-12-06 11:12:38 -02001749 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001750 pch_transcoder = TRANSCODER_A;
1751 else
1752 pch_transcoder = pipe;
1753
Jesse Barnesb24e7172011-01-04 15:09:30 -08001754 /*
1755 * A pipe without a PLL won't actually be able to drive bits from
1756 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1757 * need the check.
1758 */
1759 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001760 if (dsi)
1761 assert_dsi_pll_enabled(dev_priv);
1762 else
1763 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001764 else {
1765 if (pch_port) {
1766 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001767 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001768 assert_fdi_tx_pll_enabled(dev_priv,
1769 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001770 }
1771 /* FIXME: assert CPU port conditions for SNB+ */
1772 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001773
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001774 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001775 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001776 if (val & PIPECONF_ENABLE)
1777 return;
1778
1779 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001780 intel_wait_for_vblank(dev_priv->dev, pipe);
1781}
1782
1783/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001784 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785 * @dev_priv: i915 private structure
1786 * @pipe: pipe to disable
1787 *
1788 * Disable @pipe, making sure that various hardware specific requirements
1789 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1790 *
1791 * @pipe should be %PIPE_A or %PIPE_B.
1792 *
1793 * Will wait until the pipe has shut down before returning.
1794 */
1795static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1796 enum pipe pipe)
1797{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001798 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1799 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001800 int reg;
1801 u32 val;
1802
1803 /*
1804 * Make sure planes won't keep trying to pump pixels to us,
1805 * or we might hang the display.
1806 */
1807 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001808 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001809 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810
1811 /* Don't disable pipe A or pipe A PLLs if needed */
1812 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1813 return;
1814
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001815 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001816 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001817 if ((val & PIPECONF_ENABLE) == 0)
1818 return;
1819
1820 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001821 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1822}
1823
Keith Packardd74362c2011-07-28 14:47:14 -07001824/*
1825 * Plane regs are double buffered, going from enabled->disabled needs a
1826 * trigger in order to latch. The display address reg provides this.
1827 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001828void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1829 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001830{
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001831 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1832
1833 I915_WRITE(reg, I915_READ(reg));
1834 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001835}
1836
Jesse Barnesb24e7172011-01-04 15:09:30 -08001837/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001838 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001839 * @dev_priv: i915 private structure
1840 * @plane: plane to enable
1841 * @pipe: pipe being fed
1842 *
1843 * Enable @plane on @pipe, making sure that @pipe is running first.
1844 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001845static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1846 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001847{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001848 struct intel_crtc *intel_crtc =
1849 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001850 int reg;
1851 u32 val;
1852
1853 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1854 assert_pipe_enabled(dev_priv, pipe);
1855
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001856 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001857
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001858 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001859
Jesse Barnesb24e7172011-01-04 15:09:30 -08001860 reg = DSPCNTR(plane);
1861 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001862 if (val & DISPLAY_PLANE_ENABLE)
1863 return;
1864
1865 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001866 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001867 intel_wait_for_vblank(dev_priv->dev, pipe);
1868}
1869
Jesse Barnesb24e7172011-01-04 15:09:30 -08001870/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001871 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001872 * @dev_priv: i915 private structure
1873 * @plane: plane to disable
1874 * @pipe: pipe consuming the data
1875 *
1876 * Disable @plane; should be an independent operation.
1877 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001878static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1879 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001880{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001881 struct intel_crtc *intel_crtc =
1882 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001883 int reg;
1884 u32 val;
1885
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001886 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001887
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001888 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001889
Jesse Barnesb24e7172011-01-04 15:09:30 -08001890 reg = DSPCNTR(plane);
1891 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001892 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1893 return;
1894
1895 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001896 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001897 intel_wait_for_vblank(dev_priv->dev, pipe);
1898}
1899
Chris Wilson693db182013-03-05 14:52:39 +00001900static bool need_vtd_wa(struct drm_device *dev)
1901{
1902#ifdef CONFIG_INTEL_IOMMU
1903 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1904 return true;
1905#endif
1906 return false;
1907}
1908
Chris Wilson127bd2a2010-07-23 23:32:05 +01001909int
Chris Wilson48b956c2010-09-14 12:50:34 +01001910intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001911 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001912 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001913{
Chris Wilsonce453d82011-02-21 14:43:56 +00001914 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001915 u32 alignment;
1916 int ret;
1917
Chris Wilson05394f32010-11-08 19:18:58 +00001918 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001919 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001920 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1921 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001922 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001923 alignment = 4 * 1024;
1924 else
1925 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001926 break;
1927 case I915_TILING_X:
1928 /* pin() will align the object as required by fence */
1929 alignment = 0;
1930 break;
1931 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001932 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001933 return -EINVAL;
1934 default:
1935 BUG();
1936 }
1937
Chris Wilson693db182013-03-05 14:52:39 +00001938 /* Note that the w/a also requires 64 PTE of padding following the
1939 * bo. We currently fill all unused PTE with the shadow page and so
1940 * we should always have valid PTE following the scanout preventing
1941 * the VT-d warning.
1942 */
1943 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1944 alignment = 256 * 1024;
1945
Chris Wilsonce453d82011-02-21 14:43:56 +00001946 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001947 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001948 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001949 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001950
1951 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1952 * fence, whereas 965+ only requires a fence if using
1953 * framebuffer compression. For simplicity, we always install
1954 * a fence as the cost is not that onerous.
1955 */
Chris Wilson06d98132012-04-17 15:31:24 +01001956 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001957 if (ret)
1958 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001959
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001960 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001961
Chris Wilsonce453d82011-02-21 14:43:56 +00001962 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001963 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001964
1965err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001966 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001967err_interruptible:
1968 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001969 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001970}
1971
Chris Wilson1690e1e2011-12-14 13:57:08 +01001972void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1973{
1974 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001975 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001976}
1977
Daniel Vetterc2c75132012-07-05 12:17:30 +02001978/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1979 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001980unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1981 unsigned int tiling_mode,
1982 unsigned int cpp,
1983 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001984{
Chris Wilsonbc752862013-02-21 20:04:31 +00001985 if (tiling_mode != I915_TILING_NONE) {
1986 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001987
Chris Wilsonbc752862013-02-21 20:04:31 +00001988 tile_rows = *y / 8;
1989 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001990
Chris Wilsonbc752862013-02-21 20:04:31 +00001991 tiles = *x / (512/cpp);
1992 *x %= 512/cpp;
1993
1994 return tile_rows * pitch * 8 + tiles * 4096;
1995 } else {
1996 unsigned int offset;
1997
1998 offset = *y * pitch + *x * cpp;
1999 *y = 0;
2000 *x = (offset & 4095) / cpp;
2001 return offset & -4096;
2002 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002003}
2004
Jesse Barnes17638cd2011-06-24 12:19:23 -07002005static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2006 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002007{
2008 struct drm_device *dev = crtc->dev;
2009 struct drm_i915_private *dev_priv = dev->dev_private;
2010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2011 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002012 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002013 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002014 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002015 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002016 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002017
2018 switch (plane) {
2019 case 0:
2020 case 1:
2021 break;
2022 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002023 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002024 return -EINVAL;
2025 }
2026
2027 intel_fb = to_intel_framebuffer(fb);
2028 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002029
Chris Wilson5eddb702010-09-11 13:48:45 +01002030 reg = DSPCNTR(plane);
2031 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002032 /* Mask out pixel format bits in case we change it */
2033 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002034 switch (fb->pixel_format) {
2035 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002036 dspcntr |= DISPPLANE_8BPP;
2037 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002038 case DRM_FORMAT_XRGB1555:
2039 case DRM_FORMAT_ARGB1555:
2040 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002041 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002042 case DRM_FORMAT_RGB565:
2043 dspcntr |= DISPPLANE_BGRX565;
2044 break;
2045 case DRM_FORMAT_XRGB8888:
2046 case DRM_FORMAT_ARGB8888:
2047 dspcntr |= DISPPLANE_BGRX888;
2048 break;
2049 case DRM_FORMAT_XBGR8888:
2050 case DRM_FORMAT_ABGR8888:
2051 dspcntr |= DISPPLANE_RGBX888;
2052 break;
2053 case DRM_FORMAT_XRGB2101010:
2054 case DRM_FORMAT_ARGB2101010:
2055 dspcntr |= DISPPLANE_BGRX101010;
2056 break;
2057 case DRM_FORMAT_XBGR2101010:
2058 case DRM_FORMAT_ABGR2101010:
2059 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002060 break;
2061 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002062 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002063 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002064
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002065 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002066 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002067 dspcntr |= DISPPLANE_TILED;
2068 else
2069 dspcntr &= ~DISPPLANE_TILED;
2070 }
2071
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002072 if (IS_G4X(dev))
2073 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2074
Chris Wilson5eddb702010-09-11 13:48:45 +01002075 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002076
Daniel Vettere506a0c2012-07-05 12:17:29 +02002077 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002078
Daniel Vetterc2c75132012-07-05 12:17:30 +02002079 if (INTEL_INFO(dev)->gen >= 4) {
2080 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002081 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2082 fb->bits_per_pixel / 8,
2083 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002084 linear_offset -= intel_crtc->dspaddr_offset;
2085 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002086 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002087 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002088
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002089 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2090 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2091 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002092 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002093 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002094 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002095 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002096 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002097 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002098 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002099 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002100 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002101
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 return 0;
2103}
2104
2105static int ironlake_update_plane(struct drm_crtc *crtc,
2106 struct drm_framebuffer *fb, int x, int y)
2107{
2108 struct drm_device *dev = crtc->dev;
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2111 struct intel_framebuffer *intel_fb;
2112 struct drm_i915_gem_object *obj;
2113 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002114 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002115 u32 dspcntr;
2116 u32 reg;
2117
2118 switch (plane) {
2119 case 0:
2120 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002121 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002122 break;
2123 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002124 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002125 return -EINVAL;
2126 }
2127
2128 intel_fb = to_intel_framebuffer(fb);
2129 obj = intel_fb->obj;
2130
2131 reg = DSPCNTR(plane);
2132 dspcntr = I915_READ(reg);
2133 /* Mask out pixel format bits in case we change it */
2134 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002135 switch (fb->pixel_format) {
2136 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002137 dspcntr |= DISPPLANE_8BPP;
2138 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002139 case DRM_FORMAT_RGB565:
2140 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002142 case DRM_FORMAT_XRGB8888:
2143 case DRM_FORMAT_ARGB8888:
2144 dspcntr |= DISPPLANE_BGRX888;
2145 break;
2146 case DRM_FORMAT_XBGR8888:
2147 case DRM_FORMAT_ABGR8888:
2148 dspcntr |= DISPPLANE_RGBX888;
2149 break;
2150 case DRM_FORMAT_XRGB2101010:
2151 case DRM_FORMAT_ARGB2101010:
2152 dspcntr |= DISPPLANE_BGRX101010;
2153 break;
2154 case DRM_FORMAT_XBGR2101010:
2155 case DRM_FORMAT_ABGR2101010:
2156 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002157 break;
2158 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002159 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002160 }
2161
2162 if (obj->tiling_mode != I915_TILING_NONE)
2163 dspcntr |= DISPPLANE_TILED;
2164 else
2165 dspcntr &= ~DISPPLANE_TILED;
2166
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002167 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002168 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2169 else
2170 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002171
2172 I915_WRITE(reg, dspcntr);
2173
Daniel Vettere506a0c2012-07-05 12:17:29 +02002174 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002175 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002176 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2177 fb->bits_per_pixel / 8,
2178 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002179 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002180
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002181 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2182 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2183 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002184 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002185 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002186 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002187 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002188 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2189 } else {
2190 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2191 I915_WRITE(DSPLINOFF(plane), linear_offset);
2192 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002193 POSTING_READ(reg);
2194
2195 return 0;
2196}
2197
2198/* Assume fb object is pinned & idle & fenced and just update base pointers */
2199static int
2200intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2201 int x, int y, enum mode_set_atomic state)
2202{
2203 struct drm_device *dev = crtc->dev;
2204 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002205
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002206 if (dev_priv->display.disable_fbc)
2207 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002208 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002209
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002210 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002211}
2212
Ville Syrjälä96a02912013-02-18 19:08:49 +02002213void intel_display_handle_reset(struct drm_device *dev)
2214{
2215 struct drm_i915_private *dev_priv = dev->dev_private;
2216 struct drm_crtc *crtc;
2217
2218 /*
2219 * Flips in the rings have been nuked by the reset,
2220 * so complete all pending flips so that user space
2221 * will get its events and not get stuck.
2222 *
2223 * Also update the base address of all primary
2224 * planes to the the last fb to make sure we're
2225 * showing the correct fb after a reset.
2226 *
2227 * Need to make two loops over the crtcs so that we
2228 * don't try to grab a crtc mutex before the
2229 * pending_flip_queue really got woken up.
2230 */
2231
2232 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2234 enum plane plane = intel_crtc->plane;
2235
2236 intel_prepare_page_flip(dev, plane);
2237 intel_finish_page_flip_plane(dev, plane);
2238 }
2239
2240 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2242
2243 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002244 /*
2245 * FIXME: Once we have proper support for primary planes (and
2246 * disabling them without disabling the entire crtc) allow again
2247 * a NULL crtc->fb.
2248 */
2249 if (intel_crtc->active && crtc->fb)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002250 dev_priv->display.update_plane(crtc, crtc->fb,
2251 crtc->x, crtc->y);
2252 mutex_unlock(&crtc->mutex);
2253 }
2254}
2255
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002256static int
Chris Wilson14667a42012-04-03 17:58:35 +01002257intel_finish_fb(struct drm_framebuffer *old_fb)
2258{
2259 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2260 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2261 bool was_interruptible = dev_priv->mm.interruptible;
2262 int ret;
2263
Chris Wilson14667a42012-04-03 17:58:35 +01002264 /* Big Hammer, we also need to ensure that any pending
2265 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2266 * current scanout is retired before unpinning the old
2267 * framebuffer.
2268 *
2269 * This should only fail upon a hung GPU, in which case we
2270 * can safely continue.
2271 */
2272 dev_priv->mm.interruptible = false;
2273 ret = i915_gem_object_finish_gpu(obj);
2274 dev_priv->mm.interruptible = was_interruptible;
2275
2276 return ret;
2277}
2278
Ville Syrjälä198598d2012-10-31 17:50:24 +02002279static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2280{
2281 struct drm_device *dev = crtc->dev;
2282 struct drm_i915_master_private *master_priv;
2283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2284
2285 if (!dev->primary->master)
2286 return;
2287
2288 master_priv = dev->primary->master->driver_priv;
2289 if (!master_priv->sarea_priv)
2290 return;
2291
2292 switch (intel_crtc->pipe) {
2293 case 0:
2294 master_priv->sarea_priv->pipeA_x = x;
2295 master_priv->sarea_priv->pipeA_y = y;
2296 break;
2297 case 1:
2298 master_priv->sarea_priv->pipeB_x = x;
2299 master_priv->sarea_priv->pipeB_y = y;
2300 break;
2301 default:
2302 break;
2303 }
2304}
2305
Chris Wilson14667a42012-04-03 17:58:35 +01002306static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002307intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002308 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002309{
2310 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002311 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002313 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002314 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002315
2316 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002317 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002318 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002319 return 0;
2320 }
2321
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002322 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002323 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2324 plane_name(intel_crtc->plane),
2325 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002326 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002327 }
2328
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002329 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002330 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002331 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002332 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002333 if (ret != 0) {
2334 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002335 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002336 return ret;
2337 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002338
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002339 /*
2340 * Update pipe size and adjust fitter if needed: the reason for this is
2341 * that in compute_mode_changes we check the native mode (not the pfit
2342 * mode) to see if we can flip rather than do a full mode set. In the
2343 * fastboot case, we'll flip, but if we don't update the pipesrc and
2344 * pfit state, we'll end up with a big fb scanned out into the wrong
2345 * sized surface.
2346 *
2347 * To fix this properly, we need to hoist the checks up into
2348 * compute_mode_changes (or above), check the actual pfit state and
2349 * whether the platform allows pfit disable with pipe active, and only
2350 * then update the pipesrc and pfit state, even on the flip path.
2351 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002352 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002353 const struct drm_display_mode *adjusted_mode =
2354 &intel_crtc->config.adjusted_mode;
2355
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002356 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002357 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2358 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002359 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002360 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2361 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2362 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2363 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2364 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2365 }
2366 }
2367
Daniel Vetter94352cf2012-07-05 22:51:56 +02002368 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002369 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002370 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002371 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002372 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002373 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002374 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002375
Daniel Vetter94352cf2012-07-05 22:51:56 +02002376 old_fb = crtc->fb;
2377 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002378 crtc->x = x;
2379 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002380
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002381 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002382 if (intel_crtc->active && old_fb != fb)
2383 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002384 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002385 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002386
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002387 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002388 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002389 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002390
Ville Syrjälä198598d2012-10-31 17:50:24 +02002391 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002392
2393 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002394}
2395
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002396static void intel_fdi_normal_train(struct drm_crtc *crtc)
2397{
2398 struct drm_device *dev = crtc->dev;
2399 struct drm_i915_private *dev_priv = dev->dev_private;
2400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2401 int pipe = intel_crtc->pipe;
2402 u32 reg, temp;
2403
2404 /* enable normal train */
2405 reg = FDI_TX_CTL(pipe);
2406 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002407 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002408 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2409 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002410 } else {
2411 temp &= ~FDI_LINK_TRAIN_NONE;
2412 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002413 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002414 I915_WRITE(reg, temp);
2415
2416 reg = FDI_RX_CTL(pipe);
2417 temp = I915_READ(reg);
2418 if (HAS_PCH_CPT(dev)) {
2419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2420 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2421 } else {
2422 temp &= ~FDI_LINK_TRAIN_NONE;
2423 temp |= FDI_LINK_TRAIN_NONE;
2424 }
2425 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2426
2427 /* wait one idle pattern time */
2428 POSTING_READ(reg);
2429 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002430
2431 /* IVB wants error correction enabled */
2432 if (IS_IVYBRIDGE(dev))
2433 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2434 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002435}
2436
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002437static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002438{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002439 return crtc->base.enabled && crtc->active &&
2440 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002441}
2442
Daniel Vetter01a415f2012-10-27 15:58:40 +02002443static void ivb_modeset_global_resources(struct drm_device *dev)
2444{
2445 struct drm_i915_private *dev_priv = dev->dev_private;
2446 struct intel_crtc *pipe_B_crtc =
2447 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2448 struct intel_crtc *pipe_C_crtc =
2449 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2450 uint32_t temp;
2451
Daniel Vetter1e833f42013-02-19 22:31:57 +01002452 /*
2453 * When everything is off disable fdi C so that we could enable fdi B
2454 * with all lanes. Note that we don't care about enabled pipes without
2455 * an enabled pch encoder.
2456 */
2457 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2458 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002459 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2460 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2461
2462 temp = I915_READ(SOUTH_CHICKEN1);
2463 temp &= ~FDI_BC_BIFURCATION_SELECT;
2464 DRM_DEBUG_KMS("disabling fdi C rx\n");
2465 I915_WRITE(SOUTH_CHICKEN1, temp);
2466 }
2467}
2468
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002469/* The FDI link training functions for ILK/Ibexpeak. */
2470static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2471{
2472 struct drm_device *dev = crtc->dev;
2473 struct drm_i915_private *dev_priv = dev->dev_private;
2474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2475 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002476 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002477 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002479 /* FDI needs bits from pipe & plane first */
2480 assert_pipe_enabled(dev_priv, pipe);
2481 assert_plane_enabled(dev_priv, plane);
2482
Adam Jacksone1a44742010-06-25 15:32:14 -04002483 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2484 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 reg = FDI_RX_IMR(pipe);
2486 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002487 temp &= ~FDI_RX_SYMBOL_LOCK;
2488 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002489 I915_WRITE(reg, temp);
2490 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002491 udelay(150);
2492
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 reg = FDI_TX_CTL(pipe);
2495 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002496 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2497 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498 temp &= ~FDI_LINK_TRAIN_NONE;
2499 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002501
Chris Wilson5eddb702010-09-11 13:48:45 +01002502 reg = FDI_RX_CTL(pipe);
2503 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504 temp &= ~FDI_LINK_TRAIN_NONE;
2505 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002506 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2507
2508 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002509 udelay(150);
2510
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002511 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002512 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2513 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2514 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002515
Chris Wilson5eddb702010-09-11 13:48:45 +01002516 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002517 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002518 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2520
2521 if ((temp & FDI_RX_BIT_LOCK)) {
2522 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002523 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002524 break;
2525 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002526 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002527 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002528 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002529
2530 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002531 reg = FDI_TX_CTL(pipe);
2532 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533 temp &= ~FDI_LINK_TRAIN_NONE;
2534 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002535 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536
Chris Wilson5eddb702010-09-11 13:48:45 +01002537 reg = FDI_RX_CTL(pipe);
2538 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539 temp &= ~FDI_LINK_TRAIN_NONE;
2540 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 I915_WRITE(reg, temp);
2542
2543 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544 udelay(150);
2545
Chris Wilson5eddb702010-09-11 13:48:45 +01002546 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002547 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002548 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002549 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2550
2551 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002552 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002553 DRM_DEBUG_KMS("FDI train 2 done.\n");
2554 break;
2555 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002556 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002557 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002558 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559
2560 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002561
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562}
2563
Akshay Joshi0206e352011-08-16 15:34:10 -04002564static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002565 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2566 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2567 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2568 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2569};
2570
2571/* The FDI link training functions for SNB/Cougarpoint. */
2572static void gen6_fdi_link_train(struct drm_crtc *crtc)
2573{
2574 struct drm_device *dev = crtc->dev;
2575 struct drm_i915_private *dev_priv = dev->dev_private;
2576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2577 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002578 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002579
Adam Jacksone1a44742010-06-25 15:32:14 -04002580 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2581 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002582 reg = FDI_RX_IMR(pipe);
2583 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002584 temp &= ~FDI_RX_SYMBOL_LOCK;
2585 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 I915_WRITE(reg, temp);
2587
2588 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002589 udelay(150);
2590
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002591 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 reg = FDI_TX_CTL(pipe);
2593 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002594 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2595 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002596 temp &= ~FDI_LINK_TRAIN_NONE;
2597 temp |= FDI_LINK_TRAIN_PATTERN_1;
2598 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2599 /* SNB-B */
2600 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002601 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002602
Daniel Vetterd74cf322012-10-26 10:58:13 +02002603 I915_WRITE(FDI_RX_MISC(pipe),
2604 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2605
Chris Wilson5eddb702010-09-11 13:48:45 +01002606 reg = FDI_RX_CTL(pipe);
2607 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002608 if (HAS_PCH_CPT(dev)) {
2609 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2610 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2611 } else {
2612 temp &= ~FDI_LINK_TRAIN_NONE;
2613 temp |= FDI_LINK_TRAIN_PATTERN_1;
2614 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002615 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2616
2617 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002618 udelay(150);
2619
Akshay Joshi0206e352011-08-16 15:34:10 -04002620 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002621 reg = FDI_TX_CTL(pipe);
2622 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002623 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2624 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002625 I915_WRITE(reg, temp);
2626
2627 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002628 udelay(500);
2629
Sean Paulfa37d392012-03-02 12:53:39 -05002630 for (retry = 0; retry < 5; retry++) {
2631 reg = FDI_RX_IIR(pipe);
2632 temp = I915_READ(reg);
2633 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2634 if (temp & FDI_RX_BIT_LOCK) {
2635 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2636 DRM_DEBUG_KMS("FDI train 1 done.\n");
2637 break;
2638 }
2639 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002640 }
Sean Paulfa37d392012-03-02 12:53:39 -05002641 if (retry < 5)
2642 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002643 }
2644 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002645 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002646
2647 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002648 reg = FDI_TX_CTL(pipe);
2649 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002650 temp &= ~FDI_LINK_TRAIN_NONE;
2651 temp |= FDI_LINK_TRAIN_PATTERN_2;
2652 if (IS_GEN6(dev)) {
2653 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2654 /* SNB-B */
2655 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2656 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002657 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002658
Chris Wilson5eddb702010-09-11 13:48:45 +01002659 reg = FDI_RX_CTL(pipe);
2660 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002661 if (HAS_PCH_CPT(dev)) {
2662 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2663 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2664 } else {
2665 temp &= ~FDI_LINK_TRAIN_NONE;
2666 temp |= FDI_LINK_TRAIN_PATTERN_2;
2667 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002668 I915_WRITE(reg, temp);
2669
2670 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002671 udelay(150);
2672
Akshay Joshi0206e352011-08-16 15:34:10 -04002673 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002674 reg = FDI_TX_CTL(pipe);
2675 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002676 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2677 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002678 I915_WRITE(reg, temp);
2679
2680 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002681 udelay(500);
2682
Sean Paulfa37d392012-03-02 12:53:39 -05002683 for (retry = 0; retry < 5; retry++) {
2684 reg = FDI_RX_IIR(pipe);
2685 temp = I915_READ(reg);
2686 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2687 if (temp & FDI_RX_SYMBOL_LOCK) {
2688 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2689 DRM_DEBUG_KMS("FDI train 2 done.\n");
2690 break;
2691 }
2692 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002693 }
Sean Paulfa37d392012-03-02 12:53:39 -05002694 if (retry < 5)
2695 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002696 }
2697 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002698 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002699
2700 DRM_DEBUG_KMS("FDI train done.\n");
2701}
2702
Jesse Barnes357555c2011-04-28 15:09:55 -07002703/* Manual link training for Ivy Bridge A0 parts */
2704static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2705{
2706 struct drm_device *dev = crtc->dev;
2707 struct drm_i915_private *dev_priv = dev->dev_private;
2708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2709 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002710 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002711
2712 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2713 for train result */
2714 reg = FDI_RX_IMR(pipe);
2715 temp = I915_READ(reg);
2716 temp &= ~FDI_RX_SYMBOL_LOCK;
2717 temp &= ~FDI_RX_BIT_LOCK;
2718 I915_WRITE(reg, temp);
2719
2720 POSTING_READ(reg);
2721 udelay(150);
2722
Daniel Vetter01a415f2012-10-27 15:58:40 +02002723 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2724 I915_READ(FDI_RX_IIR(pipe)));
2725
Jesse Barnes139ccd32013-08-19 11:04:55 -07002726 /* Try each vswing and preemphasis setting twice before moving on */
2727 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2728 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002729 reg = FDI_TX_CTL(pipe);
2730 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002731 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2732 temp &= ~FDI_TX_ENABLE;
2733 I915_WRITE(reg, temp);
2734
2735 reg = FDI_RX_CTL(pipe);
2736 temp = I915_READ(reg);
2737 temp &= ~FDI_LINK_TRAIN_AUTO;
2738 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2739 temp &= ~FDI_RX_ENABLE;
2740 I915_WRITE(reg, temp);
2741
2742 /* enable CPU FDI TX and PCH FDI RX */
2743 reg = FDI_TX_CTL(pipe);
2744 temp = I915_READ(reg);
2745 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2746 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2747 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002748 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002749 temp |= snb_b_fdi_train_param[j/2];
2750 temp |= FDI_COMPOSITE_SYNC;
2751 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2752
2753 I915_WRITE(FDI_RX_MISC(pipe),
2754 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2755
2756 reg = FDI_RX_CTL(pipe);
2757 temp = I915_READ(reg);
2758 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2759 temp |= FDI_COMPOSITE_SYNC;
2760 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2761
2762 POSTING_READ(reg);
2763 udelay(1); /* should be 0.5us */
2764
2765 for (i = 0; i < 4; i++) {
2766 reg = FDI_RX_IIR(pipe);
2767 temp = I915_READ(reg);
2768 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2769
2770 if (temp & FDI_RX_BIT_LOCK ||
2771 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2772 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2773 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2774 i);
2775 break;
2776 }
2777 udelay(1); /* should be 0.5us */
2778 }
2779 if (i == 4) {
2780 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2781 continue;
2782 }
2783
2784 /* Train 2 */
2785 reg = FDI_TX_CTL(pipe);
2786 temp = I915_READ(reg);
2787 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2788 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2789 I915_WRITE(reg, temp);
2790
2791 reg = FDI_RX_CTL(pipe);
2792 temp = I915_READ(reg);
2793 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2794 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002795 I915_WRITE(reg, temp);
2796
2797 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002798 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002799
Jesse Barnes139ccd32013-08-19 11:04:55 -07002800 for (i = 0; i < 4; i++) {
2801 reg = FDI_RX_IIR(pipe);
2802 temp = I915_READ(reg);
2803 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002804
Jesse Barnes139ccd32013-08-19 11:04:55 -07002805 if (temp & FDI_RX_SYMBOL_LOCK ||
2806 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2807 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2808 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2809 i);
2810 goto train_done;
2811 }
2812 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002813 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002814 if (i == 4)
2815 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002816 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002817
Jesse Barnes139ccd32013-08-19 11:04:55 -07002818train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002819 DRM_DEBUG_KMS("FDI train done.\n");
2820}
2821
Daniel Vetter88cefb62012-08-12 19:27:14 +02002822static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002823{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002824 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002825 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002826 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002827 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002828
Jesse Barnesc64e3112010-09-10 11:27:03 -07002829
Jesse Barnes0e23b992010-09-10 11:10:00 -07002830 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002831 reg = FDI_RX_CTL(pipe);
2832 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002833 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2834 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002835 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002836 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2837
2838 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002839 udelay(200);
2840
2841 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002842 temp = I915_READ(reg);
2843 I915_WRITE(reg, temp | FDI_PCDCLK);
2844
2845 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002846 udelay(200);
2847
Paulo Zanoni20749732012-11-23 15:30:38 -02002848 /* Enable CPU FDI TX PLL, always on for Ironlake */
2849 reg = FDI_TX_CTL(pipe);
2850 temp = I915_READ(reg);
2851 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2852 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002853
Paulo Zanoni20749732012-11-23 15:30:38 -02002854 POSTING_READ(reg);
2855 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002856 }
2857}
2858
Daniel Vetter88cefb62012-08-12 19:27:14 +02002859static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2860{
2861 struct drm_device *dev = intel_crtc->base.dev;
2862 struct drm_i915_private *dev_priv = dev->dev_private;
2863 int pipe = intel_crtc->pipe;
2864 u32 reg, temp;
2865
2866 /* Switch from PCDclk to Rawclk */
2867 reg = FDI_RX_CTL(pipe);
2868 temp = I915_READ(reg);
2869 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2870
2871 /* Disable CPU FDI TX PLL */
2872 reg = FDI_TX_CTL(pipe);
2873 temp = I915_READ(reg);
2874 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2875
2876 POSTING_READ(reg);
2877 udelay(100);
2878
2879 reg = FDI_RX_CTL(pipe);
2880 temp = I915_READ(reg);
2881 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2882
2883 /* Wait for the clocks to turn off. */
2884 POSTING_READ(reg);
2885 udelay(100);
2886}
2887
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002888static void ironlake_fdi_disable(struct drm_crtc *crtc)
2889{
2890 struct drm_device *dev = crtc->dev;
2891 struct drm_i915_private *dev_priv = dev->dev_private;
2892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2893 int pipe = intel_crtc->pipe;
2894 u32 reg, temp;
2895
2896 /* disable CPU FDI tx and PCH FDI rx */
2897 reg = FDI_TX_CTL(pipe);
2898 temp = I915_READ(reg);
2899 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2900 POSTING_READ(reg);
2901
2902 reg = FDI_RX_CTL(pipe);
2903 temp = I915_READ(reg);
2904 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002905 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002906 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2907
2908 POSTING_READ(reg);
2909 udelay(100);
2910
2911 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002912 if (HAS_PCH_IBX(dev)) {
2913 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002914 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002915
2916 /* still set train pattern 1 */
2917 reg = FDI_TX_CTL(pipe);
2918 temp = I915_READ(reg);
2919 temp &= ~FDI_LINK_TRAIN_NONE;
2920 temp |= FDI_LINK_TRAIN_PATTERN_1;
2921 I915_WRITE(reg, temp);
2922
2923 reg = FDI_RX_CTL(pipe);
2924 temp = I915_READ(reg);
2925 if (HAS_PCH_CPT(dev)) {
2926 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2927 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2928 } else {
2929 temp &= ~FDI_LINK_TRAIN_NONE;
2930 temp |= FDI_LINK_TRAIN_PATTERN_1;
2931 }
2932 /* BPC in FDI rx is consistent with that in PIPECONF */
2933 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002934 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002935 I915_WRITE(reg, temp);
2936
2937 POSTING_READ(reg);
2938 udelay(100);
2939}
2940
Chris Wilson5bb61642012-09-27 21:25:58 +01002941static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002946 unsigned long flags;
2947 bool pending;
2948
Ville Syrjälä10d83732013-01-29 18:13:34 +02002949 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2950 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002951 return false;
2952
2953 spin_lock_irqsave(&dev->event_lock, flags);
2954 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2955 spin_unlock_irqrestore(&dev->event_lock, flags);
2956
2957 return pending;
2958}
2959
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002960static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2961{
Chris Wilson0f911282012-04-17 10:05:38 +01002962 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002963 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002964
2965 if (crtc->fb == NULL)
2966 return;
2967
Daniel Vetter2c10d572012-12-20 21:24:07 +01002968 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2969
Chris Wilson5bb61642012-09-27 21:25:58 +01002970 wait_event(dev_priv->pending_flip_queue,
2971 !intel_crtc_has_pending_flip(crtc));
2972
Chris Wilson0f911282012-04-17 10:05:38 +01002973 mutex_lock(&dev->struct_mutex);
2974 intel_finish_fb(crtc->fb);
2975 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002976}
2977
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002978/* Program iCLKIP clock to the desired frequency */
2979static void lpt_program_iclkip(struct drm_crtc *crtc)
2980{
2981 struct drm_device *dev = crtc->dev;
2982 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002983 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002984 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2985 u32 temp;
2986
Daniel Vetter09153002012-12-12 14:06:44 +01002987 mutex_lock(&dev_priv->dpio_lock);
2988
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002989 /* It is necessary to ungate the pixclk gate prior to programming
2990 * the divisors, and gate it back when it is done.
2991 */
2992 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2993
2994 /* Disable SSCCTL */
2995 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002996 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2997 SBI_SSCCTL_DISABLE,
2998 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002999
3000 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003001 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003002 auxdiv = 1;
3003 divsel = 0x41;
3004 phaseinc = 0x20;
3005 } else {
3006 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003007 * but the adjusted_mode->crtc_clock in in KHz. To get the
3008 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003009 * convert the virtual clock precision to KHz here for higher
3010 * precision.
3011 */
3012 u32 iclk_virtual_root_freq = 172800 * 1000;
3013 u32 iclk_pi_range = 64;
3014 u32 desired_divisor, msb_divisor_value, pi_value;
3015
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003016 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003017 msb_divisor_value = desired_divisor / iclk_pi_range;
3018 pi_value = desired_divisor % iclk_pi_range;
3019
3020 auxdiv = 0;
3021 divsel = msb_divisor_value - 2;
3022 phaseinc = pi_value;
3023 }
3024
3025 /* This should not happen with any sane values */
3026 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3027 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3028 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3029 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3030
3031 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 +03003032 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003033 auxdiv,
3034 divsel,
3035 phasedir,
3036 phaseinc);
3037
3038 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003039 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003040 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3041 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3042 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3043 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3044 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3045 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003046 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003047
3048 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003049 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003050 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3051 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003052 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003053
3054 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003055 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003056 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003057 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003058
3059 /* Wait for initialization time */
3060 udelay(24);
3061
3062 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003063
3064 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003065}
3066
Daniel Vetter275f01b22013-05-03 11:49:47 +02003067static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3068 enum pipe pch_transcoder)
3069{
3070 struct drm_device *dev = crtc->base.dev;
3071 struct drm_i915_private *dev_priv = dev->dev_private;
3072 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3073
3074 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3075 I915_READ(HTOTAL(cpu_transcoder)));
3076 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3077 I915_READ(HBLANK(cpu_transcoder)));
3078 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3079 I915_READ(HSYNC(cpu_transcoder)));
3080
3081 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3082 I915_READ(VTOTAL(cpu_transcoder)));
3083 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3084 I915_READ(VBLANK(cpu_transcoder)));
3085 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3086 I915_READ(VSYNC(cpu_transcoder)));
3087 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3088 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3089}
3090
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003091static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3092{
3093 struct drm_i915_private *dev_priv = dev->dev_private;
3094 uint32_t temp;
3095
3096 temp = I915_READ(SOUTH_CHICKEN1);
3097 if (temp & FDI_BC_BIFURCATION_SELECT)
3098 return;
3099
3100 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3101 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3102
3103 temp |= FDI_BC_BIFURCATION_SELECT;
3104 DRM_DEBUG_KMS("enabling fdi C rx\n");
3105 I915_WRITE(SOUTH_CHICKEN1, temp);
3106 POSTING_READ(SOUTH_CHICKEN1);
3107}
3108
3109static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3110{
3111 struct drm_device *dev = intel_crtc->base.dev;
3112 struct drm_i915_private *dev_priv = dev->dev_private;
3113
3114 switch (intel_crtc->pipe) {
3115 case PIPE_A:
3116 break;
3117 case PIPE_B:
3118 if (intel_crtc->config.fdi_lanes > 2)
3119 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3120 else
3121 cpt_enable_fdi_bc_bifurcation(dev);
3122
3123 break;
3124 case PIPE_C:
3125 cpt_enable_fdi_bc_bifurcation(dev);
3126
3127 break;
3128 default:
3129 BUG();
3130 }
3131}
3132
Jesse Barnesf67a5592011-01-05 10:31:48 -08003133/*
3134 * Enable PCH resources required for PCH ports:
3135 * - PCH PLLs
3136 * - FDI training & RX/TX
3137 * - update transcoder timings
3138 * - DP transcoding bits
3139 * - transcoder
3140 */
3141static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003142{
3143 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3146 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003147 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003148
Daniel Vetterab9412b2013-05-03 11:49:46 +02003149 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003150
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003151 if (IS_IVYBRIDGE(dev))
3152 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3153
Daniel Vettercd986ab2012-10-26 10:58:12 +02003154 /* Write the TU size bits before fdi link training, so that error
3155 * detection works. */
3156 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3157 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3158
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003159 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003160 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003161
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003162 /* We need to program the right clock selection before writing the pixel
3163 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003164 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003165 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003166
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003167 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003168 temp |= TRANS_DPLL_ENABLE(pipe);
3169 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003170 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003171 temp |= sel;
3172 else
3173 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003174 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003175 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003176
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003177 /* XXX: pch pll's can be enabled any time before we enable the PCH
3178 * transcoder, and we actually should do this to not upset any PCH
3179 * transcoder that already use the clock when we share it.
3180 *
3181 * Note that enable_shared_dpll tries to do the right thing, but
3182 * get_shared_dpll unconditionally resets the pll - we need that to have
3183 * the right LVDS enable sequence. */
3184 ironlake_enable_shared_dpll(intel_crtc);
3185
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003186 /* set transcoder timing, panel must allow it */
3187 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003188 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003189
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003190 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003191
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003192 /* For PCH DP, enable TRANS_DP_CTL */
3193 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003194 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3195 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003196 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003197 reg = TRANS_DP_CTL(pipe);
3198 temp = I915_READ(reg);
3199 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003200 TRANS_DP_SYNC_MASK |
3201 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003202 temp |= (TRANS_DP_OUTPUT_ENABLE |
3203 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003204 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003205
3206 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003207 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003208 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003209 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003210
3211 switch (intel_trans_dp_port_sel(crtc)) {
3212 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003213 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003214 break;
3215 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003216 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003217 break;
3218 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003219 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003220 break;
3221 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003222 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003223 }
3224
Chris Wilson5eddb702010-09-11 13:48:45 +01003225 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003226 }
3227
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003228 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003229}
3230
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003231static void lpt_pch_enable(struct drm_crtc *crtc)
3232{
3233 struct drm_device *dev = crtc->dev;
3234 struct drm_i915_private *dev_priv = dev->dev_private;
3235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003236 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003237
Daniel Vetterab9412b2013-05-03 11:49:46 +02003238 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003239
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003240 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003241
Paulo Zanoni0540e482012-10-31 18:12:40 -02003242 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003243 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003244
Paulo Zanoni937bb612012-10-31 18:12:47 -02003245 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003246}
3247
Daniel Vettere2b78262013-06-07 23:10:03 +02003248static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003249{
Daniel Vettere2b78262013-06-07 23:10:03 +02003250 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003251
3252 if (pll == NULL)
3253 return;
3254
3255 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003256 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003257 return;
3258 }
3259
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003260 if (--pll->refcount == 0) {
3261 WARN_ON(pll->on);
3262 WARN_ON(pll->active);
3263 }
3264
Daniel Vettera43f6e02013-06-07 23:10:32 +02003265 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003266}
3267
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003268static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003269{
Daniel Vettere2b78262013-06-07 23:10:03 +02003270 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3271 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3272 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003273
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003274 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003275 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3276 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003277 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003278 }
3279
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003280 if (HAS_PCH_IBX(dev_priv->dev)) {
3281 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003282 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003283 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003284
Daniel Vetter46edb022013-06-05 13:34:12 +02003285 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3286 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003287
3288 goto found;
3289 }
3290
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003291 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3292 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003293
3294 /* Only want to check enabled timings first */
3295 if (pll->refcount == 0)
3296 continue;
3297
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003298 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3299 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003300 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003301 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003302 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003303
3304 goto found;
3305 }
3306 }
3307
3308 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003309 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3310 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003311 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003312 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3313 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003314 goto found;
3315 }
3316 }
3317
3318 return NULL;
3319
3320found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003321 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003322 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3323 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003324
Daniel Vettercdbd2312013-06-05 13:34:03 +02003325 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003326 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3327 sizeof(pll->hw_state));
3328
Daniel Vetter46edb022013-06-05 13:34:12 +02003329 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003330 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003331 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003332
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003333 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003334 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003335 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003336
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003337 return pll;
3338}
3339
Daniel Vettera1520312013-05-03 11:49:50 +02003340static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003341{
3342 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003343 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003344 u32 temp;
3345
3346 temp = I915_READ(dslreg);
3347 udelay(500);
3348 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003349 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003350 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003351 }
3352}
3353
Jesse Barnesb074cec2013-04-25 12:55:02 -07003354static void ironlake_pfit_enable(struct intel_crtc *crtc)
3355{
3356 struct drm_device *dev = crtc->base.dev;
3357 struct drm_i915_private *dev_priv = dev->dev_private;
3358 int pipe = crtc->pipe;
3359
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003360 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003361 /* Force use of hard-coded filter coefficients
3362 * as some pre-programmed values are broken,
3363 * e.g. x201.
3364 */
3365 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3366 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3367 PF_PIPE_SEL_IVB(pipe));
3368 else
3369 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3370 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3371 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003372 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003373}
3374
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003375static void intel_enable_planes(struct drm_crtc *crtc)
3376{
3377 struct drm_device *dev = crtc->dev;
3378 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3379 struct intel_plane *intel_plane;
3380
3381 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3382 if (intel_plane->pipe == pipe)
3383 intel_plane_restore(&intel_plane->base);
3384}
3385
3386static void intel_disable_planes(struct drm_crtc *crtc)
3387{
3388 struct drm_device *dev = crtc->dev;
3389 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3390 struct intel_plane *intel_plane;
3391
3392 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3393 if (intel_plane->pipe == pipe)
3394 intel_plane_disable(&intel_plane->base);
3395}
3396
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003397void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003398{
3399 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3400
3401 if (!crtc->config.ips_enabled)
3402 return;
3403
3404 /* We can only enable IPS after we enable a plane and wait for a vblank.
3405 * We guarantee that the plane is enabled by calling intel_enable_ips
3406 * only after intel_enable_plane. And intel_enable_plane already waits
3407 * for a vblank, so all we need to do here is to enable the IPS bit. */
3408 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003409 if (IS_BROADWELL(crtc->base.dev)) {
3410 mutex_lock(&dev_priv->rps.hw_lock);
3411 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3412 mutex_unlock(&dev_priv->rps.hw_lock);
3413 /* Quoting Art Runyan: "its not safe to expect any particular
3414 * value in IPS_CTL bit 31 after enabling IPS through the
3415 * mailbox." Therefore we need to defer waiting on the state
3416 * change.
3417 * TODO: need to fix this for state checker
3418 */
3419 } else {
3420 I915_WRITE(IPS_CTL, IPS_ENABLE);
3421 /* The bit only becomes 1 in the next vblank, so this wait here
3422 * is essentially intel_wait_for_vblank. If we don't have this
3423 * and don't wait for vblanks until the end of crtc_enable, then
3424 * the HW state readout code will complain that the expected
3425 * IPS_CTL value is not the one we read. */
3426 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3427 DRM_ERROR("Timed out waiting for IPS enable\n");
3428 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003429}
3430
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003431void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003432{
3433 struct drm_device *dev = crtc->base.dev;
3434 struct drm_i915_private *dev_priv = dev->dev_private;
3435
3436 if (!crtc->config.ips_enabled)
3437 return;
3438
3439 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003440 if (IS_BROADWELL(crtc->base.dev)) {
3441 mutex_lock(&dev_priv->rps.hw_lock);
3442 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3443 mutex_unlock(&dev_priv->rps.hw_lock);
3444 } else
3445 I915_WRITE(IPS_CTL, 0);
Paulo Zanonid77e4532013-09-24 13:52:55 -03003446 POSTING_READ(IPS_CTL);
3447
3448 /* We need to wait for a vblank before we can disable the plane. */
3449 intel_wait_for_vblank(dev, crtc->pipe);
3450}
3451
3452/** Loads the palette/gamma unit for the CRTC with the prepared values */
3453static void intel_crtc_load_lut(struct drm_crtc *crtc)
3454{
3455 struct drm_device *dev = crtc->dev;
3456 struct drm_i915_private *dev_priv = dev->dev_private;
3457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3458 enum pipe pipe = intel_crtc->pipe;
3459 int palreg = PALETTE(pipe);
3460 int i;
3461 bool reenable_ips = false;
3462
3463 /* The clocks have to be on to load the palette. */
3464 if (!crtc->enabled || !intel_crtc->active)
3465 return;
3466
3467 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3468 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3469 assert_dsi_pll_enabled(dev_priv);
3470 else
3471 assert_pll_enabled(dev_priv, pipe);
3472 }
3473
3474 /* use legacy palette for Ironlake */
3475 if (HAS_PCH_SPLIT(dev))
3476 palreg = LGC_PALETTE(pipe);
3477
3478 /* Workaround : Do not read or write the pipe palette/gamma data while
3479 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3480 */
3481 if (intel_crtc->config.ips_enabled &&
3482 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3483 GAMMA_MODE_MODE_SPLIT)) {
3484 hsw_disable_ips(intel_crtc);
3485 reenable_ips = true;
3486 }
3487
3488 for (i = 0; i < 256; i++) {
3489 I915_WRITE(palreg + 4 * i,
3490 (intel_crtc->lut_r[i] << 16) |
3491 (intel_crtc->lut_g[i] << 8) |
3492 intel_crtc->lut_b[i]);
3493 }
3494
3495 if (reenable_ips)
3496 hsw_enable_ips(intel_crtc);
3497}
3498
Jesse Barnesf67a5592011-01-05 10:31:48 -08003499static void ironlake_crtc_enable(struct drm_crtc *crtc)
3500{
3501 struct drm_device *dev = crtc->dev;
3502 struct drm_i915_private *dev_priv = dev->dev_private;
3503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003504 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003505 int pipe = intel_crtc->pipe;
3506 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003507
Daniel Vetter08a48462012-07-02 11:43:47 +02003508 WARN_ON(!crtc->enabled);
3509
Jesse Barnesf67a5592011-01-05 10:31:48 -08003510 if (intel_crtc->active)
3511 return;
3512
3513 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003514
3515 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3516 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3517
Daniel Vetterf6736a12013-06-05 13:34:30 +02003518 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003519 if (encoder->pre_enable)
3520 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003521
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003522 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003523 /* Note: FDI PLL enabling _must_ be done before we enable the
3524 * cpu pipes, hence this is separate from all the other fdi/pch
3525 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003526 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003527 } else {
3528 assert_fdi_tx_disabled(dev_priv, pipe);
3529 assert_fdi_rx_disabled(dev_priv, pipe);
3530 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003531
Jesse Barnesb074cec2013-04-25 12:55:02 -07003532 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003533
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003534 /*
3535 * On ILK+ LUT must be loaded before the pipe is running but with
3536 * clocks enabled
3537 */
3538 intel_crtc_load_lut(crtc);
3539
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003540 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003541 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003542 intel_crtc->config.has_pch_encoder, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003543 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003544 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003545 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003546
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003547 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003548 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003549
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003550 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003551 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003552 mutex_unlock(&dev->struct_mutex);
3553
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003554 for_each_encoder_on_crtc(dev, crtc, encoder)
3555 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003556
3557 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003558 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003559
3560 /*
3561 * There seems to be a race in PCH platform hw (at least on some
3562 * outputs) where an enabled pipe still completes any pageflip right
3563 * away (as if the pipe is off) instead of waiting for vblank. As soon
3564 * as the first vblank happend, everything works as expected. Hence just
3565 * wait for one vblank before returning to avoid strange things
3566 * happening.
3567 */
3568 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003569}
3570
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003571/* IPS only exists on ULT machines and is tied to pipe A. */
3572static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3573{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003574 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003575}
3576
Ville Syrjälädda9a662013-09-19 17:00:37 -03003577static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3578{
3579 struct drm_device *dev = crtc->dev;
3580 struct drm_i915_private *dev_priv = dev->dev_private;
3581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3582 int pipe = intel_crtc->pipe;
3583 int plane = intel_crtc->plane;
3584
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003585 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003586 intel_enable_planes(crtc);
3587 intel_crtc_update_cursor(crtc, true);
3588
3589 hsw_enable_ips(intel_crtc);
3590
3591 mutex_lock(&dev->struct_mutex);
3592 intel_update_fbc(dev);
3593 mutex_unlock(&dev->struct_mutex);
3594}
3595
3596static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3597{
3598 struct drm_device *dev = crtc->dev;
3599 struct drm_i915_private *dev_priv = dev->dev_private;
3600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3601 int pipe = intel_crtc->pipe;
3602 int plane = intel_crtc->plane;
3603
3604 intel_crtc_wait_for_pending_flips(crtc);
3605 drm_vblank_off(dev, pipe);
3606
3607 /* FBC must be disabled before disabling the plane on HSW. */
3608 if (dev_priv->fbc.plane == plane)
3609 intel_disable_fbc(dev);
3610
3611 hsw_disable_ips(intel_crtc);
3612
3613 intel_crtc_update_cursor(crtc, false);
3614 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003615 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003616}
3617
Paulo Zanonie4916942013-09-20 16:21:19 -03003618/*
3619 * This implements the workaround described in the "notes" section of the mode
3620 * set sequence documentation. When going from no pipes or single pipe to
3621 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3622 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3623 */
3624static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3625{
3626 struct drm_device *dev = crtc->base.dev;
3627 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3628
3629 /* We want to get the other_active_crtc only if there's only 1 other
3630 * active crtc. */
3631 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3632 if (!crtc_it->active || crtc_it == crtc)
3633 continue;
3634
3635 if (other_active_crtc)
3636 return;
3637
3638 other_active_crtc = crtc_it;
3639 }
3640 if (!other_active_crtc)
3641 return;
3642
3643 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3644 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3645}
3646
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003647static void haswell_crtc_enable(struct drm_crtc *crtc)
3648{
3649 struct drm_device *dev = crtc->dev;
3650 struct drm_i915_private *dev_priv = dev->dev_private;
3651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3652 struct intel_encoder *encoder;
3653 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003654
3655 WARN_ON(!crtc->enabled);
3656
3657 if (intel_crtc->active)
3658 return;
3659
3660 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003661
3662 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3663 if (intel_crtc->config.has_pch_encoder)
3664 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3665
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003666 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003667 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003668
3669 for_each_encoder_on_crtc(dev, crtc, encoder)
3670 if (encoder->pre_enable)
3671 encoder->pre_enable(encoder);
3672
Paulo Zanoni1f544382012-10-24 11:32:00 -02003673 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003674
Jesse Barnesb074cec2013-04-25 12:55:02 -07003675 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003676
3677 /*
3678 * On ILK+ LUT must be loaded before the pipe is running but with
3679 * clocks enabled
3680 */
3681 intel_crtc_load_lut(crtc);
3682
Paulo Zanoni1f544382012-10-24 11:32:00 -02003683 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003684 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003685
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003686 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003687 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003688 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003689
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003690 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003691 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003692
Jani Nikula8807e552013-08-30 19:40:32 +03003693 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003694 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003695 intel_opregion_notify_encoder(encoder, true);
3696 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003697
Paulo Zanonie4916942013-09-20 16:21:19 -03003698 /* If we change the relative order between pipe/planes enabling, we need
3699 * to change the workaround. */
3700 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003701 haswell_crtc_enable_planes(crtc);
3702
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003703 /*
3704 * There seems to be a race in PCH platform hw (at least on some
3705 * outputs) where an enabled pipe still completes any pageflip right
3706 * away (as if the pipe is off) instead of waiting for vblank. As soon
3707 * as the first vblank happend, everything works as expected. Hence just
3708 * wait for one vblank before returning to avoid strange things
3709 * happening.
3710 */
3711 intel_wait_for_vblank(dev, intel_crtc->pipe);
3712}
3713
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003714static void ironlake_pfit_disable(struct intel_crtc *crtc)
3715{
3716 struct drm_device *dev = crtc->base.dev;
3717 struct drm_i915_private *dev_priv = dev->dev_private;
3718 int pipe = crtc->pipe;
3719
3720 /* To avoid upsetting the power well on haswell only disable the pfit if
3721 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003722 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003723 I915_WRITE(PF_CTL(pipe), 0);
3724 I915_WRITE(PF_WIN_POS(pipe), 0);
3725 I915_WRITE(PF_WIN_SZ(pipe), 0);
3726 }
3727}
3728
Jesse Barnes6be4a602010-09-10 10:26:01 -07003729static void ironlake_crtc_disable(struct drm_crtc *crtc)
3730{
3731 struct drm_device *dev = crtc->dev;
3732 struct drm_i915_private *dev_priv = dev->dev_private;
3733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003734 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003735 int pipe = intel_crtc->pipe;
3736 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003737 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003738
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003739
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003740 if (!intel_crtc->active)
3741 return;
3742
Daniel Vetterea9d7582012-07-10 10:42:52 +02003743 for_each_encoder_on_crtc(dev, crtc, encoder)
3744 encoder->disable(encoder);
3745
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003746 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003747 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003748
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003749 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003750 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003751
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003752 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003753 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003754 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003755
Daniel Vetterd925c592013-06-05 13:34:04 +02003756 if (intel_crtc->config.has_pch_encoder)
3757 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3758
Jesse Barnesb24e7172011-01-04 15:09:30 -08003759 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003760
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003761 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003762
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003763 for_each_encoder_on_crtc(dev, crtc, encoder)
3764 if (encoder->post_disable)
3765 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003766
Daniel Vetterd925c592013-06-05 13:34:04 +02003767 if (intel_crtc->config.has_pch_encoder) {
3768 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003769
Daniel Vetterd925c592013-06-05 13:34:04 +02003770 ironlake_disable_pch_transcoder(dev_priv, pipe);
3771 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003772
Daniel Vetterd925c592013-06-05 13:34:04 +02003773 if (HAS_PCH_CPT(dev)) {
3774 /* disable TRANS_DP_CTL */
3775 reg = TRANS_DP_CTL(pipe);
3776 temp = I915_READ(reg);
3777 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3778 TRANS_DP_PORT_SEL_MASK);
3779 temp |= TRANS_DP_PORT_SEL_NONE;
3780 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003781
Daniel Vetterd925c592013-06-05 13:34:04 +02003782 /* disable DPLL_SEL */
3783 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003784 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003785 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003786 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003787
3788 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003789 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003790
3791 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003792 }
3793
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003794 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003795 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003796
3797 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003798 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003799 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003800}
3801
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003802static void haswell_crtc_disable(struct drm_crtc *crtc)
3803{
3804 struct drm_device *dev = crtc->dev;
3805 struct drm_i915_private *dev_priv = dev->dev_private;
3806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3807 struct intel_encoder *encoder;
3808 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003809 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003810
3811 if (!intel_crtc->active)
3812 return;
3813
Ville Syrjälädda9a662013-09-19 17:00:37 -03003814 haswell_crtc_disable_planes(crtc);
3815
Jani Nikula8807e552013-08-30 19:40:32 +03003816 for_each_encoder_on_crtc(dev, crtc, encoder) {
3817 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003818 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003819 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003820
Paulo Zanoni86642812013-04-12 17:57:57 -03003821 if (intel_crtc->config.has_pch_encoder)
3822 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003823 intel_disable_pipe(dev_priv, pipe);
3824
Paulo Zanoniad80a812012-10-24 16:06:19 -02003825 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003826
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003827 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003828
Paulo Zanoni1f544382012-10-24 11:32:00 -02003829 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003830
3831 for_each_encoder_on_crtc(dev, crtc, encoder)
3832 if (encoder->post_disable)
3833 encoder->post_disable(encoder);
3834
Daniel Vetter88adfff2013-03-28 10:42:01 +01003835 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003836 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003837 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003838 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003839 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003840
3841 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003842 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003843
3844 mutex_lock(&dev->struct_mutex);
3845 intel_update_fbc(dev);
3846 mutex_unlock(&dev->struct_mutex);
3847}
3848
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003849static void ironlake_crtc_off(struct drm_crtc *crtc)
3850{
3851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003852 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003853}
3854
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003855static void haswell_crtc_off(struct drm_crtc *crtc)
3856{
3857 intel_ddi_put_crtc_pll(crtc);
3858}
3859
Daniel Vetter02e792f2009-09-15 22:57:34 +02003860static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3861{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003862 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003863 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003864 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003865
Chris Wilson23f09ce2010-08-12 13:53:37 +01003866 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003867 dev_priv->mm.interruptible = false;
3868 (void) intel_overlay_switch_off(intel_crtc->overlay);
3869 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003870 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003871 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003872
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003873 /* Let userspace switch the overlay on again. In most cases userspace
3874 * has to recompute where to put it anyway.
3875 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003876}
3877
Egbert Eich61bc95c2013-03-04 09:24:38 -05003878/**
3879 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3880 * cursor plane briefly if not already running after enabling the display
3881 * plane.
3882 * This workaround avoids occasional blank screens when self refresh is
3883 * enabled.
3884 */
3885static void
3886g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3887{
3888 u32 cntl = I915_READ(CURCNTR(pipe));
3889
3890 if ((cntl & CURSOR_MODE) == 0) {
3891 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3892
3893 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3894 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3895 intel_wait_for_vblank(dev_priv->dev, pipe);
3896 I915_WRITE(CURCNTR(pipe), cntl);
3897 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3898 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3899 }
3900}
3901
Jesse Barnes2dd24552013-04-25 12:55:01 -07003902static void i9xx_pfit_enable(struct intel_crtc *crtc)
3903{
3904 struct drm_device *dev = crtc->base.dev;
3905 struct drm_i915_private *dev_priv = dev->dev_private;
3906 struct intel_crtc_config *pipe_config = &crtc->config;
3907
Daniel Vetter328d8e82013-05-08 10:36:31 +02003908 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003909 return;
3910
Daniel Vetterc0b03412013-05-28 12:05:54 +02003911 /*
3912 * The panel fitter should only be adjusted whilst the pipe is disabled,
3913 * according to register description and PRM.
3914 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003915 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3916 assert_pipe_disabled(dev_priv, crtc->pipe);
3917
Jesse Barnesb074cec2013-04-25 12:55:02 -07003918 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3919 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003920
3921 /* Border color in case we don't scale up to the full screen. Black by
3922 * default, change to something else for debugging. */
3923 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003924}
3925
Jesse Barnes586f49d2013-11-04 16:06:59 -08003926int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003927{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003928 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003929
Jesse Barnes586f49d2013-11-04 16:06:59 -08003930 /* Obtain SKU information */
3931 mutex_lock(&dev_priv->dpio_lock);
3932 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3933 CCK_FUSE_HPLL_FREQ_MASK;
3934 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003935
Jesse Barnes586f49d2013-11-04 16:06:59 -08003936 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003937}
3938
3939/* Adjust CDclk dividers to allow high res or save power if possible */
3940static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3941{
3942 struct drm_i915_private *dev_priv = dev->dev_private;
3943 u32 val, cmd;
3944
3945 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3946 cmd = 2;
3947 else if (cdclk == 266)
3948 cmd = 1;
3949 else
3950 cmd = 0;
3951
3952 mutex_lock(&dev_priv->rps.hw_lock);
3953 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3954 val &= ~DSPFREQGUAR_MASK;
3955 val |= (cmd << DSPFREQGUAR_SHIFT);
3956 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3957 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3958 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3959 50)) {
3960 DRM_ERROR("timed out waiting for CDclk change\n");
3961 }
3962 mutex_unlock(&dev_priv->rps.hw_lock);
3963
3964 if (cdclk == 400) {
3965 u32 divider, vco;
3966
3967 vco = valleyview_get_vco(dev_priv);
3968 divider = ((vco << 1) / cdclk) - 1;
3969
3970 mutex_lock(&dev_priv->dpio_lock);
3971 /* adjust cdclk divider */
3972 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3973 val &= ~0xf;
3974 val |= divider;
3975 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3976 mutex_unlock(&dev_priv->dpio_lock);
3977 }
3978
3979 mutex_lock(&dev_priv->dpio_lock);
3980 /* adjust self-refresh exit latency value */
3981 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
3982 val &= ~0x7f;
3983
3984 /*
3985 * For high bandwidth configs, we set a higher latency in the bunit
3986 * so that the core display fetch happens in time to avoid underruns.
3987 */
3988 if (cdclk == 400)
3989 val |= 4500 / 250; /* 4.5 usec */
3990 else
3991 val |= 3000 / 250; /* 3.0 usec */
3992 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
3993 mutex_unlock(&dev_priv->dpio_lock);
3994
3995 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
3996 intel_i2c_reset(dev);
3997}
3998
3999static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4000{
4001 int cur_cdclk, vco;
4002 int divider;
4003
4004 vco = valleyview_get_vco(dev_priv);
4005
4006 mutex_lock(&dev_priv->dpio_lock);
4007 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4008 mutex_unlock(&dev_priv->dpio_lock);
4009
4010 divider &= 0xf;
4011
4012 cur_cdclk = (vco << 1) / (divider + 1);
4013
4014 return cur_cdclk;
4015}
4016
4017static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4018 int max_pixclk)
4019{
4020 int cur_cdclk;
4021
4022 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4023
4024 /*
4025 * Really only a few cases to deal with, as only 4 CDclks are supported:
4026 * 200MHz
4027 * 267MHz
4028 * 320MHz
4029 * 400MHz
4030 * So we check to see whether we're above 90% of the lower bin and
4031 * adjust if needed.
4032 */
4033 if (max_pixclk > 288000) {
4034 return 400;
4035 } else if (max_pixclk > 240000) {
4036 return 320;
4037 } else
4038 return 266;
4039 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4040}
4041
4042static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4043 unsigned modeset_pipes,
4044 struct intel_crtc_config *pipe_config)
4045{
4046 struct drm_device *dev = dev_priv->dev;
4047 struct intel_crtc *intel_crtc;
4048 int max_pixclk = 0;
4049
4050 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4051 base.head) {
4052 if (modeset_pipes & (1 << intel_crtc->pipe))
4053 max_pixclk = max(max_pixclk,
4054 pipe_config->adjusted_mode.crtc_clock);
4055 else if (intel_crtc->base.enabled)
4056 max_pixclk = max(max_pixclk,
4057 intel_crtc->config.adjusted_mode.crtc_clock);
4058 }
4059
4060 return max_pixclk;
4061}
4062
4063static void valleyview_modeset_global_pipes(struct drm_device *dev,
4064 unsigned *prepare_pipes,
4065 unsigned modeset_pipes,
4066 struct intel_crtc_config *pipe_config)
4067{
4068 struct drm_i915_private *dev_priv = dev->dev_private;
4069 struct intel_crtc *intel_crtc;
4070 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4071 pipe_config);
4072 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4073
4074 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4075 return;
4076
4077 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4078 base.head)
4079 if (intel_crtc->base.enabled)
4080 *prepare_pipes |= (1 << intel_crtc->pipe);
4081}
4082
4083static void valleyview_modeset_global_resources(struct drm_device *dev)
4084{
4085 struct drm_i915_private *dev_priv = dev->dev_private;
4086 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4087 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4088 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4089
4090 if (req_cdclk != cur_cdclk)
4091 valleyview_set_cdclk(dev, req_cdclk);
4092}
4093
Jesse Barnes89b667f2013-04-18 14:51:36 -07004094static void valleyview_crtc_enable(struct drm_crtc *crtc)
4095{
4096 struct drm_device *dev = crtc->dev;
4097 struct drm_i915_private *dev_priv = dev->dev_private;
4098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4099 struct intel_encoder *encoder;
4100 int pipe = intel_crtc->pipe;
4101 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004102 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004103
4104 WARN_ON(!crtc->enabled);
4105
4106 if (intel_crtc->active)
4107 return;
4108
4109 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004110
Jesse Barnes89b667f2013-04-18 14:51:36 -07004111 for_each_encoder_on_crtc(dev, crtc, encoder)
4112 if (encoder->pre_pll_enable)
4113 encoder->pre_pll_enable(encoder);
4114
Jani Nikula23538ef2013-08-27 15:12:22 +03004115 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4116
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004117 if (!is_dsi)
4118 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004119
4120 for_each_encoder_on_crtc(dev, crtc, encoder)
4121 if (encoder->pre_enable)
4122 encoder->pre_enable(encoder);
4123
Jesse Barnes2dd24552013-04-25 12:55:01 -07004124 i9xx_pfit_enable(intel_crtc);
4125
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004126 intel_crtc_load_lut(crtc);
4127
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004128 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004129 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004130 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004131 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004132 intel_crtc_update_cursor(crtc, true);
4133
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004134 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004135
4136 for_each_encoder_on_crtc(dev, crtc, encoder)
4137 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004138}
4139
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004140static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004141{
4142 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004143 struct drm_i915_private *dev_priv = dev->dev_private;
4144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004145 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004146 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004147 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004148
Daniel Vetter08a48462012-07-02 11:43:47 +02004149 WARN_ON(!crtc->enabled);
4150
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004151 if (intel_crtc->active)
4152 return;
4153
4154 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004155
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004156 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004157 if (encoder->pre_enable)
4158 encoder->pre_enable(encoder);
4159
Daniel Vetterf6736a12013-06-05 13:34:30 +02004160 i9xx_enable_pll(intel_crtc);
4161
Jesse Barnes2dd24552013-04-25 12:55:01 -07004162 i9xx_pfit_enable(intel_crtc);
4163
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004164 intel_crtc_load_lut(crtc);
4165
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004166 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004167 intel_enable_pipe(dev_priv, pipe, false, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004168 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004169 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004170 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004171 if (IS_G4X(dev))
4172 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004173 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004174
4175 /* Give the overlay scaler a chance to enable if it's on this pipe */
4176 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004177
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004178 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004179
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004180 for_each_encoder_on_crtc(dev, crtc, encoder)
4181 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004182}
4183
Daniel Vetter87476d62013-04-11 16:29:06 +02004184static void i9xx_pfit_disable(struct intel_crtc *crtc)
4185{
4186 struct drm_device *dev = crtc->base.dev;
4187 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004188
4189 if (!crtc->config.gmch_pfit.control)
4190 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004191
4192 assert_pipe_disabled(dev_priv, crtc->pipe);
4193
Daniel Vetter328d8e82013-05-08 10:36:31 +02004194 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4195 I915_READ(PFIT_CONTROL));
4196 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004197}
4198
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004199static void i9xx_crtc_disable(struct drm_crtc *crtc)
4200{
4201 struct drm_device *dev = crtc->dev;
4202 struct drm_i915_private *dev_priv = dev->dev_private;
4203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004204 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004205 int pipe = intel_crtc->pipe;
4206 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004207
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004208 if (!intel_crtc->active)
4209 return;
4210
Daniel Vetterea9d7582012-07-10 10:42:52 +02004211 for_each_encoder_on_crtc(dev, crtc, encoder)
4212 encoder->disable(encoder);
4213
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004214 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004215 intel_crtc_wait_for_pending_flips(crtc);
4216 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004217
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004218 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004219 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004220
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004221 intel_crtc_dpms_overlay(intel_crtc, false);
4222 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004223 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004224 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004225
Jesse Barnesb24e7172011-01-04 15:09:30 -08004226 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004227
Daniel Vetter87476d62013-04-11 16:29:06 +02004228 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004229
Jesse Barnes89b667f2013-04-18 14:51:36 -07004230 for_each_encoder_on_crtc(dev, crtc, encoder)
4231 if (encoder->post_disable)
4232 encoder->post_disable(encoder);
4233
Jesse Barnesf6071162013-10-01 10:41:38 -07004234 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4235 vlv_disable_pll(dev_priv, pipe);
4236 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004237 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004238
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004239 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004240 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004241
Chris Wilson6b383a72010-09-13 13:54:26 +01004242 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004243}
4244
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004245static void i9xx_crtc_off(struct drm_crtc *crtc)
4246{
4247}
4248
Daniel Vetter976f8a22012-07-08 22:34:21 +02004249static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4250 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004251{
4252 struct drm_device *dev = crtc->dev;
4253 struct drm_i915_master_private *master_priv;
4254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4255 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004256
4257 if (!dev->primary->master)
4258 return;
4259
4260 master_priv = dev->primary->master->driver_priv;
4261 if (!master_priv->sarea_priv)
4262 return;
4263
Jesse Barnes79e53942008-11-07 14:24:08 -08004264 switch (pipe) {
4265 case 0:
4266 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4267 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4268 break;
4269 case 1:
4270 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4271 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4272 break;
4273 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004274 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004275 break;
4276 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004277}
4278
Daniel Vetter976f8a22012-07-08 22:34:21 +02004279/**
4280 * Sets the power management mode of the pipe and plane.
4281 */
4282void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004283{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004284 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004285 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004286 struct intel_encoder *intel_encoder;
4287 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004288
Daniel Vetter976f8a22012-07-08 22:34:21 +02004289 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4290 enable |= intel_encoder->connectors_active;
4291
4292 if (enable)
4293 dev_priv->display.crtc_enable(crtc);
4294 else
4295 dev_priv->display.crtc_disable(crtc);
4296
4297 intel_crtc_update_sarea(crtc, enable);
4298}
4299
Daniel Vetter976f8a22012-07-08 22:34:21 +02004300static void intel_crtc_disable(struct drm_crtc *crtc)
4301{
4302 struct drm_device *dev = crtc->dev;
4303 struct drm_connector *connector;
4304 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004306
4307 /* crtc should still be enabled when we disable it. */
4308 WARN_ON(!crtc->enabled);
4309
4310 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004311 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004312 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004313 dev_priv->display.off(crtc);
4314
Chris Wilson931872f2012-01-16 23:01:13 +00004315 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004316 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004317 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004318
4319 if (crtc->fb) {
4320 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004321 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004322 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004323 crtc->fb = NULL;
4324 }
4325
4326 /* Update computed state. */
4327 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4328 if (!connector->encoder || !connector->encoder->crtc)
4329 continue;
4330
4331 if (connector->encoder->crtc != crtc)
4332 continue;
4333
4334 connector->dpms = DRM_MODE_DPMS_OFF;
4335 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004336 }
4337}
4338
Chris Wilsonea5b2132010-08-04 13:50:23 +01004339void intel_encoder_destroy(struct drm_encoder *encoder)
4340{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004341 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004342
Chris Wilsonea5b2132010-08-04 13:50:23 +01004343 drm_encoder_cleanup(encoder);
4344 kfree(intel_encoder);
4345}
4346
Damien Lespiau92373292013-08-08 22:28:57 +01004347/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004348 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4349 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004350static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004351{
4352 if (mode == DRM_MODE_DPMS_ON) {
4353 encoder->connectors_active = true;
4354
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004355 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004356 } else {
4357 encoder->connectors_active = false;
4358
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004359 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004360 }
4361}
4362
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004363/* Cross check the actual hw state with our own modeset state tracking (and it's
4364 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004365static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004366{
4367 if (connector->get_hw_state(connector)) {
4368 struct intel_encoder *encoder = connector->encoder;
4369 struct drm_crtc *crtc;
4370 bool encoder_enabled;
4371 enum pipe pipe;
4372
4373 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4374 connector->base.base.id,
4375 drm_get_connector_name(&connector->base));
4376
4377 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4378 "wrong connector dpms state\n");
4379 WARN(connector->base.encoder != &encoder->base,
4380 "active connector not linked to encoder\n");
4381 WARN(!encoder->connectors_active,
4382 "encoder->connectors_active not set\n");
4383
4384 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4385 WARN(!encoder_enabled, "encoder not enabled\n");
4386 if (WARN_ON(!encoder->base.crtc))
4387 return;
4388
4389 crtc = encoder->base.crtc;
4390
4391 WARN(!crtc->enabled, "crtc not enabled\n");
4392 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4393 WARN(pipe != to_intel_crtc(crtc)->pipe,
4394 "encoder active on the wrong pipe\n");
4395 }
4396}
4397
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004398/* Even simpler default implementation, if there's really no special case to
4399 * consider. */
4400void intel_connector_dpms(struct drm_connector *connector, int mode)
4401{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004402 /* All the simple cases only support two dpms states. */
4403 if (mode != DRM_MODE_DPMS_ON)
4404 mode = DRM_MODE_DPMS_OFF;
4405
4406 if (mode == connector->dpms)
4407 return;
4408
4409 connector->dpms = mode;
4410
4411 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004412 if (connector->encoder)
4413 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004414
Daniel Vetterb9805142012-08-31 17:37:33 +02004415 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004416}
4417
Daniel Vetterf0947c32012-07-02 13:10:34 +02004418/* Simple connector->get_hw_state implementation for encoders that support only
4419 * one connector and no cloning and hence the encoder state determines the state
4420 * of the connector. */
4421bool intel_connector_get_hw_state(struct intel_connector *connector)
4422{
Daniel Vetter24929352012-07-02 20:28:59 +02004423 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004424 struct intel_encoder *encoder = connector->encoder;
4425
4426 return encoder->get_hw_state(encoder, &pipe);
4427}
4428
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004429static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4430 struct intel_crtc_config *pipe_config)
4431{
4432 struct drm_i915_private *dev_priv = dev->dev_private;
4433 struct intel_crtc *pipe_B_crtc =
4434 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4435
4436 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4437 pipe_name(pipe), pipe_config->fdi_lanes);
4438 if (pipe_config->fdi_lanes > 4) {
4439 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4440 pipe_name(pipe), pipe_config->fdi_lanes);
4441 return false;
4442 }
4443
Paulo Zanonibafb6552013-11-02 21:07:44 -07004444 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004445 if (pipe_config->fdi_lanes > 2) {
4446 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4447 pipe_config->fdi_lanes);
4448 return false;
4449 } else {
4450 return true;
4451 }
4452 }
4453
4454 if (INTEL_INFO(dev)->num_pipes == 2)
4455 return true;
4456
4457 /* Ivybridge 3 pipe is really complicated */
4458 switch (pipe) {
4459 case PIPE_A:
4460 return true;
4461 case PIPE_B:
4462 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4463 pipe_config->fdi_lanes > 2) {
4464 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4465 pipe_name(pipe), pipe_config->fdi_lanes);
4466 return false;
4467 }
4468 return true;
4469 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004470 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004471 pipe_B_crtc->config.fdi_lanes <= 2) {
4472 if (pipe_config->fdi_lanes > 2) {
4473 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4474 pipe_name(pipe), pipe_config->fdi_lanes);
4475 return false;
4476 }
4477 } else {
4478 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4479 return false;
4480 }
4481 return true;
4482 default:
4483 BUG();
4484 }
4485}
4486
Daniel Vettere29c22c2013-02-21 00:00:16 +01004487#define RETRY 1
4488static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4489 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004490{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004491 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004492 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004493 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004494 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004495
Daniel Vettere29c22c2013-02-21 00:00:16 +01004496retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004497 /* FDI is a binary signal running at ~2.7GHz, encoding
4498 * each output octet as 10 bits. The actual frequency
4499 * is stored as a divider into a 100MHz clock, and the
4500 * mode pixel clock is stored in units of 1KHz.
4501 * Hence the bw of each lane in terms of the mode signal
4502 * is:
4503 */
4504 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4505
Damien Lespiau241bfc32013-09-25 16:45:37 +01004506 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004507
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004508 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004509 pipe_config->pipe_bpp);
4510
4511 pipe_config->fdi_lanes = lane;
4512
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004513 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004514 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004515
Daniel Vettere29c22c2013-02-21 00:00:16 +01004516 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4517 intel_crtc->pipe, pipe_config);
4518 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4519 pipe_config->pipe_bpp -= 2*3;
4520 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4521 pipe_config->pipe_bpp);
4522 needs_recompute = true;
4523 pipe_config->bw_constrained = true;
4524
4525 goto retry;
4526 }
4527
4528 if (needs_recompute)
4529 return RETRY;
4530
4531 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004532}
4533
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004534static void hsw_compute_ips_config(struct intel_crtc *crtc,
4535 struct intel_crtc_config *pipe_config)
4536{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004537 pipe_config->ips_enabled = i915_enable_ips &&
4538 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004539 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004540}
4541
Daniel Vettera43f6e02013-06-07 23:10:32 +02004542static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004543 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004544{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004545 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004546 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004547
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004548 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004549 if (INTEL_INFO(dev)->gen < 4) {
4550 struct drm_i915_private *dev_priv = dev->dev_private;
4551 int clock_limit =
4552 dev_priv->display.get_display_clock_speed(dev);
4553
4554 /*
4555 * Enable pixel doubling when the dot clock
4556 * is > 90% of the (display) core speed.
4557 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004558 * GDG double wide on either pipe,
4559 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004560 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004561 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004562 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004563 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004564 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004565 }
4566
Damien Lespiau241bfc32013-09-25 16:45:37 +01004567 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004568 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004569 }
Chris Wilson89749352010-09-12 18:25:19 +01004570
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004571 /*
4572 * Pipe horizontal size must be even in:
4573 * - DVO ganged mode
4574 * - LVDS dual channel mode
4575 * - Double wide pipe
4576 */
4577 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4578 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4579 pipe_config->pipe_src_w &= ~1;
4580
Damien Lespiau8693a822013-05-03 18:48:11 +01004581 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4582 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004583 */
4584 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4585 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004586 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004587
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004588 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004589 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004590 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004591 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4592 * for lvds. */
4593 pipe_config->pipe_bpp = 8*3;
4594 }
4595
Damien Lespiauf5adf942013-06-24 18:29:34 +01004596 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004597 hsw_compute_ips_config(crtc, pipe_config);
4598
4599 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4600 * clock survives for now. */
4601 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4602 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004603
Daniel Vetter877d48d2013-04-19 11:24:43 +02004604 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004605 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004606
Daniel Vettere29c22c2013-02-21 00:00:16 +01004607 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004608}
4609
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004610static int valleyview_get_display_clock_speed(struct drm_device *dev)
4611{
4612 return 400000; /* FIXME */
4613}
4614
Jesse Barnese70236a2009-09-21 10:42:27 -07004615static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004616{
Jesse Barnese70236a2009-09-21 10:42:27 -07004617 return 400000;
4618}
Jesse Barnes79e53942008-11-07 14:24:08 -08004619
Jesse Barnese70236a2009-09-21 10:42:27 -07004620static int i915_get_display_clock_speed(struct drm_device *dev)
4621{
4622 return 333000;
4623}
Jesse Barnes79e53942008-11-07 14:24:08 -08004624
Jesse Barnese70236a2009-09-21 10:42:27 -07004625static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4626{
4627 return 200000;
4628}
Jesse Barnes79e53942008-11-07 14:24:08 -08004629
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004630static int pnv_get_display_clock_speed(struct drm_device *dev)
4631{
4632 u16 gcfgc = 0;
4633
4634 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4635
4636 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4637 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4638 return 267000;
4639 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4640 return 333000;
4641 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4642 return 444000;
4643 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4644 return 200000;
4645 default:
4646 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4647 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4648 return 133000;
4649 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4650 return 167000;
4651 }
4652}
4653
Jesse Barnese70236a2009-09-21 10:42:27 -07004654static int i915gm_get_display_clock_speed(struct drm_device *dev)
4655{
4656 u16 gcfgc = 0;
4657
4658 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4659
4660 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004661 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004662 else {
4663 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4664 case GC_DISPLAY_CLOCK_333_MHZ:
4665 return 333000;
4666 default:
4667 case GC_DISPLAY_CLOCK_190_200_MHZ:
4668 return 190000;
4669 }
4670 }
4671}
Jesse Barnes79e53942008-11-07 14:24:08 -08004672
Jesse Barnese70236a2009-09-21 10:42:27 -07004673static int i865_get_display_clock_speed(struct drm_device *dev)
4674{
4675 return 266000;
4676}
4677
4678static int i855_get_display_clock_speed(struct drm_device *dev)
4679{
4680 u16 hpllcc = 0;
4681 /* Assume that the hardware is in the high speed state. This
4682 * should be the default.
4683 */
4684 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4685 case GC_CLOCK_133_200:
4686 case GC_CLOCK_100_200:
4687 return 200000;
4688 case GC_CLOCK_166_250:
4689 return 250000;
4690 case GC_CLOCK_100_133:
4691 return 133000;
4692 }
4693
4694 /* Shouldn't happen */
4695 return 0;
4696}
4697
4698static int i830_get_display_clock_speed(struct drm_device *dev)
4699{
4700 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004701}
4702
Zhenyu Wang2c072452009-06-05 15:38:42 +08004703static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004704intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004705{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004706 while (*num > DATA_LINK_M_N_MASK ||
4707 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004708 *num >>= 1;
4709 *den >>= 1;
4710 }
4711}
4712
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004713static void compute_m_n(unsigned int m, unsigned int n,
4714 uint32_t *ret_m, uint32_t *ret_n)
4715{
4716 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4717 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4718 intel_reduce_m_n_ratio(ret_m, ret_n);
4719}
4720
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004721void
4722intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4723 int pixel_clock, int link_clock,
4724 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004725{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004726 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004727
4728 compute_m_n(bits_per_pixel * pixel_clock,
4729 link_clock * nlanes * 8,
4730 &m_n->gmch_m, &m_n->gmch_n);
4731
4732 compute_m_n(pixel_clock, link_clock,
4733 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004734}
4735
Chris Wilsona7615032011-01-12 17:04:08 +00004736static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4737{
Keith Packard72bbe582011-09-26 16:09:45 -07004738 if (i915_panel_use_ssc >= 0)
4739 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004740 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004741 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004742}
4743
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004744static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4745{
4746 struct drm_device *dev = crtc->dev;
4747 struct drm_i915_private *dev_priv = dev->dev_private;
4748 int refclk;
4749
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004750 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004751 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004752 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004753 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004754 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004755 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4756 refclk / 1000);
4757 } else if (!IS_GEN2(dev)) {
4758 refclk = 96000;
4759 } else {
4760 refclk = 48000;
4761 }
4762
4763 return refclk;
4764}
4765
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004766static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004767{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004768 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004769}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004770
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004771static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4772{
4773 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004774}
4775
Daniel Vetterf47709a2013-03-28 10:42:02 +01004776static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004777 intel_clock_t *reduced_clock)
4778{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004779 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004780 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004781 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004782 u32 fp, fp2 = 0;
4783
4784 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004785 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004786 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004787 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004788 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004789 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004790 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004791 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004792 }
4793
4794 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004795 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004796
Daniel Vetterf47709a2013-03-28 10:42:02 +01004797 crtc->lowfreq_avail = false;
4798 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004799 reduced_clock && i915_powersave) {
4800 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004801 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004802 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004803 } else {
4804 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004805 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004806 }
4807}
4808
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004809static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4810 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004811{
4812 u32 reg_val;
4813
4814 /*
4815 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4816 * and set it to a reasonable value instead.
4817 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004818 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004819 reg_val &= 0xffffff00;
4820 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004821 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004822
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004823 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004824 reg_val &= 0x8cffffff;
4825 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004826 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004827
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004828 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004829 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004830 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004831
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004832 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004833 reg_val &= 0x00ffffff;
4834 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004835 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004836}
4837
Daniel Vetterb5518422013-05-03 11:49:48 +02004838static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4839 struct intel_link_m_n *m_n)
4840{
4841 struct drm_device *dev = crtc->base.dev;
4842 struct drm_i915_private *dev_priv = dev->dev_private;
4843 int pipe = crtc->pipe;
4844
Daniel Vettere3b95f12013-05-03 11:49:49 +02004845 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4846 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4847 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4848 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004849}
4850
4851static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4852 struct intel_link_m_n *m_n)
4853{
4854 struct drm_device *dev = crtc->base.dev;
4855 struct drm_i915_private *dev_priv = dev->dev_private;
4856 int pipe = crtc->pipe;
4857 enum transcoder transcoder = crtc->config.cpu_transcoder;
4858
4859 if (INTEL_INFO(dev)->gen >= 5) {
4860 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4861 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4862 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4863 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4864 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004865 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4866 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4867 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4868 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004869 }
4870}
4871
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004872static void intel_dp_set_m_n(struct intel_crtc *crtc)
4873{
4874 if (crtc->config.has_pch_encoder)
4875 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4876 else
4877 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4878}
4879
Daniel Vetterf47709a2013-03-28 10:42:02 +01004880static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004881{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004882 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004883 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004884 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004885 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004886 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004887 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004888
Daniel Vetter09153002012-12-12 14:06:44 +01004889 mutex_lock(&dev_priv->dpio_lock);
4890
Daniel Vetterf47709a2013-03-28 10:42:02 +01004891 bestn = crtc->config.dpll.n;
4892 bestm1 = crtc->config.dpll.m1;
4893 bestm2 = crtc->config.dpll.m2;
4894 bestp1 = crtc->config.dpll.p1;
4895 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004896
Jesse Barnes89b667f2013-04-18 14:51:36 -07004897 /* See eDP HDMI DPIO driver vbios notes doc */
4898
4899 /* PLL B needs special handling */
4900 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004901 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004902
4903 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004904 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004905
4906 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004907 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004908 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004909 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004910
4911 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004912 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004913
4914 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004915 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4916 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4917 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004918 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004919
4920 /*
4921 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4922 * but we don't support that).
4923 * Note: don't use the DAC post divider as it seems unstable.
4924 */
4925 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004926 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004927
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004928 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004929 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004930
Jesse Barnes89b667f2013-04-18 14:51:36 -07004931 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004932 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004933 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004934 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004936 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004937 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004938 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004939 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004940
Jesse Barnes89b667f2013-04-18 14:51:36 -07004941 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4942 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4943 /* Use SSC source */
4944 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004945 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004946 0x0df40000);
4947 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004948 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004949 0x0df70000);
4950 } else { /* HDMI or VGA */
4951 /* Use bend source */
4952 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004954 0x0df70000);
4955 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004956 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004957 0x0df40000);
4958 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004959
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004960 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004961 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4962 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4963 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4964 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004965 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004966
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004967 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004968
Jesse Barnes89b667f2013-04-18 14:51:36 -07004969 /* Enable DPIO clock input */
4970 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4971 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004972 /* We should never disable this, set it here for state tracking */
4973 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004974 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004975 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004976 crtc->config.dpll_hw_state.dpll = dpll;
4977
Daniel Vetteref1b4602013-06-01 17:17:04 +02004978 dpll_md = (crtc->config.pixel_multiplier - 1)
4979 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004980 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4981
Daniel Vetterf47709a2013-03-28 10:42:02 +01004982 if (crtc->config.has_dp_encoder)
4983 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304984
Daniel Vetter09153002012-12-12 14:06:44 +01004985 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004986}
4987
Daniel Vetterf47709a2013-03-28 10:42:02 +01004988static void i9xx_update_pll(struct intel_crtc *crtc,
4989 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004990 int num_connectors)
4991{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004992 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004993 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004994 u32 dpll;
4995 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004996 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004997
Daniel Vetterf47709a2013-03-28 10:42:02 +01004998 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304999
Daniel Vetterf47709a2013-03-28 10:42:02 +01005000 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5001 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005002
5003 dpll = DPLL_VGA_MODE_DIS;
5004
Daniel Vetterf47709a2013-03-28 10:42:02 +01005005 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005006 dpll |= DPLLB_MODE_LVDS;
5007 else
5008 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005009
Daniel Vetteref1b4602013-06-01 17:17:04 +02005010 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005011 dpll |= (crtc->config.pixel_multiplier - 1)
5012 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005013 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005014
5015 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005016 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005017
Daniel Vetterf47709a2013-03-28 10:42:02 +01005018 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005019 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005020
5021 /* compute bitmask from p1 value */
5022 if (IS_PINEVIEW(dev))
5023 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5024 else {
5025 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5026 if (IS_G4X(dev) && reduced_clock)
5027 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5028 }
5029 switch (clock->p2) {
5030 case 5:
5031 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5032 break;
5033 case 7:
5034 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5035 break;
5036 case 10:
5037 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5038 break;
5039 case 14:
5040 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5041 break;
5042 }
5043 if (INTEL_INFO(dev)->gen >= 4)
5044 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5045
Daniel Vetter09ede542013-04-30 14:01:45 +02005046 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005047 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005048 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005049 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5050 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5051 else
5052 dpll |= PLL_REF_INPUT_DREFCLK;
5053
5054 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005055 crtc->config.dpll_hw_state.dpll = dpll;
5056
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005057 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005058 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5059 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005060 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005061 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005062
5063 if (crtc->config.has_dp_encoder)
5064 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005065}
5066
Daniel Vetterf47709a2013-03-28 10:42:02 +01005067static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005068 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005069 int num_connectors)
5070{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005071 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005072 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005073 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005074 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005075
Daniel Vetterf47709a2013-03-28 10:42:02 +01005076 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305077
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005078 dpll = DPLL_VGA_MODE_DIS;
5079
Daniel Vetterf47709a2013-03-28 10:42:02 +01005080 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005081 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5082 } else {
5083 if (clock->p1 == 2)
5084 dpll |= PLL_P1_DIVIDE_BY_TWO;
5085 else
5086 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5087 if (clock->p2 == 4)
5088 dpll |= PLL_P2_DIVIDE_BY_4;
5089 }
5090
Daniel Vetter4a33e482013-07-06 12:52:05 +02005091 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5092 dpll |= DPLL_DVO_2X_MODE;
5093
Daniel Vetterf47709a2013-03-28 10:42:02 +01005094 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005095 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5096 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5097 else
5098 dpll |= PLL_REF_INPUT_DREFCLK;
5099
5100 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005101 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005102}
5103
Daniel Vetter8a654f32013-06-01 17:16:22 +02005104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005105{
5106 struct drm_device *dev = intel_crtc->base.dev;
5107 struct drm_i915_private *dev_priv = dev->dev_private;
5108 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005109 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005110 struct drm_display_mode *adjusted_mode =
5111 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005112 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5113
5114 /* We need to be careful not to changed the adjusted mode, for otherwise
5115 * the hw state checker will get angry at the mismatch. */
5116 crtc_vtotal = adjusted_mode->crtc_vtotal;
5117 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005118
5119 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5120 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005121 crtc_vtotal -= 1;
5122 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005123 vsyncshift = adjusted_mode->crtc_hsync_start
5124 - adjusted_mode->crtc_htotal / 2;
5125 } else {
5126 vsyncshift = 0;
5127 }
5128
5129 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005130 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005131
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005132 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005133 (adjusted_mode->crtc_hdisplay - 1) |
5134 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005135 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005136 (adjusted_mode->crtc_hblank_start - 1) |
5137 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005138 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005139 (adjusted_mode->crtc_hsync_start - 1) |
5140 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5141
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005142 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005143 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005144 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005145 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005146 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005147 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005148 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005149 (adjusted_mode->crtc_vsync_start - 1) |
5150 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5151
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005152 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5153 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5154 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5155 * bits. */
5156 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5157 (pipe == PIPE_B || pipe == PIPE_C))
5158 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5159
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005160 /* pipesrc controls the size that is scaled from, which should
5161 * always be the user's requested size.
5162 */
5163 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005164 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5165 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005166}
5167
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005168static void intel_get_pipe_timings(struct intel_crtc *crtc,
5169 struct intel_crtc_config *pipe_config)
5170{
5171 struct drm_device *dev = crtc->base.dev;
5172 struct drm_i915_private *dev_priv = dev->dev_private;
5173 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5174 uint32_t tmp;
5175
5176 tmp = I915_READ(HTOTAL(cpu_transcoder));
5177 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5178 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5179 tmp = I915_READ(HBLANK(cpu_transcoder));
5180 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5181 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5182 tmp = I915_READ(HSYNC(cpu_transcoder));
5183 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5184 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5185
5186 tmp = I915_READ(VTOTAL(cpu_transcoder));
5187 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5188 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5189 tmp = I915_READ(VBLANK(cpu_transcoder));
5190 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5191 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5192 tmp = I915_READ(VSYNC(cpu_transcoder));
5193 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5194 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5195
5196 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5197 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5198 pipe_config->adjusted_mode.crtc_vtotal += 1;
5199 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5200 }
5201
5202 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005203 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5204 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5205
5206 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5207 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005208}
5209
Jesse Barnesbabea612013-06-26 18:57:38 +03005210static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5211 struct intel_crtc_config *pipe_config)
5212{
5213 struct drm_crtc *crtc = &intel_crtc->base;
5214
5215 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5216 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5217 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5218 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5219
5220 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5221 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5222 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5223 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5224
5225 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5226
Damien Lespiau241bfc32013-09-25 16:45:37 +01005227 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005228 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5229}
5230
Daniel Vetter84b046f2013-02-19 18:48:54 +01005231static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5232{
5233 struct drm_device *dev = intel_crtc->base.dev;
5234 struct drm_i915_private *dev_priv = dev->dev_private;
5235 uint32_t pipeconf;
5236
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005237 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005238
Daniel Vetter67c72a12013-09-24 11:46:14 +02005239 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5240 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5241 pipeconf |= PIPECONF_ENABLE;
5242
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005243 if (intel_crtc->config.double_wide)
5244 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005245
Daniel Vetterff9ce462013-04-24 14:57:17 +02005246 /* only g4x and later have fancy bpc/dither controls */
5247 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005248 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5249 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5250 pipeconf |= PIPECONF_DITHER_EN |
5251 PIPECONF_DITHER_TYPE_SP;
5252
5253 switch (intel_crtc->config.pipe_bpp) {
5254 case 18:
5255 pipeconf |= PIPECONF_6BPC;
5256 break;
5257 case 24:
5258 pipeconf |= PIPECONF_8BPC;
5259 break;
5260 case 30:
5261 pipeconf |= PIPECONF_10BPC;
5262 break;
5263 default:
5264 /* Case prevented by intel_choose_pipe_bpp_dither. */
5265 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005266 }
5267 }
5268
5269 if (HAS_PIPE_CXSR(dev)) {
5270 if (intel_crtc->lowfreq_avail) {
5271 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5272 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5273 } else {
5274 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005275 }
5276 }
5277
Daniel Vetter84b046f2013-02-19 18:48:54 +01005278 if (!IS_GEN2(dev) &&
5279 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5280 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5281 else
5282 pipeconf |= PIPECONF_PROGRESSIVE;
5283
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005284 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5285 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005286
Daniel Vetter84b046f2013-02-19 18:48:54 +01005287 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5288 POSTING_READ(PIPECONF(intel_crtc->pipe));
5289}
5290
Eric Anholtf564048e2011-03-30 13:01:02 -07005291static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005292 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005293 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005294{
5295 struct drm_device *dev = crtc->dev;
5296 struct drm_i915_private *dev_priv = dev->dev_private;
5297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5298 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005299 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005300 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005301 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005302 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005303 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005304 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005305 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005306 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005307 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005308
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005309 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005310 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005311 case INTEL_OUTPUT_LVDS:
5312 is_lvds = true;
5313 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005314 case INTEL_OUTPUT_DSI:
5315 is_dsi = true;
5316 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005317 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005318
Eric Anholtc751ce42010-03-25 11:48:48 -07005319 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005320 }
5321
Jani Nikulaf2335332013-09-13 11:03:09 +03005322 if (is_dsi)
5323 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005324
Jani Nikulaf2335332013-09-13 11:03:09 +03005325 if (!intel_crtc->config.clock_set) {
5326 refclk = i9xx_get_refclk(crtc, num_connectors);
5327
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005328 /*
5329 * Returns a set of divisors for the desired target clock with
5330 * the given refclk, or FALSE. The returned values represent
5331 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5332 * 2) / p1 / p2.
5333 */
5334 limit = intel_limit(crtc, refclk);
5335 ok = dev_priv->display.find_dpll(limit, crtc,
5336 intel_crtc->config.port_clock,
5337 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005338 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005339 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5340 return -EINVAL;
5341 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005342
Jani Nikulaf2335332013-09-13 11:03:09 +03005343 if (is_lvds && dev_priv->lvds_downclock_avail) {
5344 /*
5345 * Ensure we match the reduced clock's P to the target
5346 * clock. If the clocks don't match, we can't switch
5347 * the display clock by using the FP0/FP1. In such case
5348 * we will disable the LVDS downclock feature.
5349 */
5350 has_reduced_clock =
5351 dev_priv->display.find_dpll(limit, crtc,
5352 dev_priv->lvds_downclock,
5353 refclk, &clock,
5354 &reduced_clock);
5355 }
5356 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005357 intel_crtc->config.dpll.n = clock.n;
5358 intel_crtc->config.dpll.m1 = clock.m1;
5359 intel_crtc->config.dpll.m2 = clock.m2;
5360 intel_crtc->config.dpll.p1 = clock.p1;
5361 intel_crtc->config.dpll.p2 = clock.p2;
5362 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005363
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005364 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005365 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305366 has_reduced_clock ? &reduced_clock : NULL,
5367 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005368 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005369 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005370 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005371 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005372 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005373 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005374 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005375
Jani Nikulaf2335332013-09-13 11:03:09 +03005376skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005377 /* Set up the display plane register */
5378 dspcntr = DISPPLANE_GAMMA_ENABLE;
5379
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005380 if (!IS_VALLEYVIEW(dev)) {
5381 if (pipe == 0)
5382 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5383 else
5384 dspcntr |= DISPPLANE_SEL_PIPE_B;
5385 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005386
Daniel Vetter8a654f32013-06-01 17:16:22 +02005387 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005388
5389 /* pipesrc and dspsize control the size that is scaled from,
5390 * which should always be the user's requested size.
5391 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005392 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005393 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5394 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005395 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005396
Daniel Vetter84b046f2013-02-19 18:48:54 +01005397 i9xx_set_pipeconf(intel_crtc);
5398
Eric Anholtf564048e2011-03-30 13:01:02 -07005399 I915_WRITE(DSPCNTR(plane), dspcntr);
5400 POSTING_READ(DSPCNTR(plane));
5401
Daniel Vetter94352cf2012-07-05 22:51:56 +02005402 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005403
Eric Anholtf564048e2011-03-30 13:01:02 -07005404 return ret;
5405}
5406
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005407static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5408 struct intel_crtc_config *pipe_config)
5409{
5410 struct drm_device *dev = crtc->base.dev;
5411 struct drm_i915_private *dev_priv = dev->dev_private;
5412 uint32_t tmp;
5413
5414 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005415 if (!(tmp & PFIT_ENABLE))
5416 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005417
Daniel Vetter06922822013-07-11 13:35:40 +02005418 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005419 if (INTEL_INFO(dev)->gen < 4) {
5420 if (crtc->pipe != PIPE_B)
5421 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005422 } else {
5423 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5424 return;
5425 }
5426
Daniel Vetter06922822013-07-11 13:35:40 +02005427 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005428 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5429 if (INTEL_INFO(dev)->gen < 5)
5430 pipe_config->gmch_pfit.lvds_border_bits =
5431 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5432}
5433
Jesse Barnesacbec812013-09-20 11:29:32 -07005434static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5435 struct intel_crtc_config *pipe_config)
5436{
5437 struct drm_device *dev = crtc->base.dev;
5438 struct drm_i915_private *dev_priv = dev->dev_private;
5439 int pipe = pipe_config->cpu_transcoder;
5440 intel_clock_t clock;
5441 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005442 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005443
5444 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005445 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005446 mutex_unlock(&dev_priv->dpio_lock);
5447
5448 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5449 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5450 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5451 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5452 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5453
Ville Syrjäläf6466282013-10-14 14:50:31 +03005454 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005455
Ville Syrjäläf6466282013-10-14 14:50:31 +03005456 /* clock.dot is the fast clock */
5457 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005458}
5459
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005460static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5461 struct intel_crtc_config *pipe_config)
5462{
5463 struct drm_device *dev = crtc->base.dev;
5464 struct drm_i915_private *dev_priv = dev->dev_private;
5465 uint32_t tmp;
5466
Daniel Vettere143a212013-07-04 12:01:15 +02005467 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005468 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005469
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005470 tmp = I915_READ(PIPECONF(crtc->pipe));
5471 if (!(tmp & PIPECONF_ENABLE))
5472 return false;
5473
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005474 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5475 switch (tmp & PIPECONF_BPC_MASK) {
5476 case PIPECONF_6BPC:
5477 pipe_config->pipe_bpp = 18;
5478 break;
5479 case PIPECONF_8BPC:
5480 pipe_config->pipe_bpp = 24;
5481 break;
5482 case PIPECONF_10BPC:
5483 pipe_config->pipe_bpp = 30;
5484 break;
5485 default:
5486 break;
5487 }
5488 }
5489
Ville Syrjälä282740f2013-09-04 18:30:03 +03005490 if (INTEL_INFO(dev)->gen < 4)
5491 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5492
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005493 intel_get_pipe_timings(crtc, pipe_config);
5494
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005495 i9xx_get_pfit_config(crtc, pipe_config);
5496
Daniel Vetter6c49f242013-06-06 12:45:25 +02005497 if (INTEL_INFO(dev)->gen >= 4) {
5498 tmp = I915_READ(DPLL_MD(crtc->pipe));
5499 pipe_config->pixel_multiplier =
5500 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5501 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005502 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005503 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5504 tmp = I915_READ(DPLL(crtc->pipe));
5505 pipe_config->pixel_multiplier =
5506 ((tmp & SDVO_MULTIPLIER_MASK)
5507 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5508 } else {
5509 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5510 * port and will be fixed up in the encoder->get_config
5511 * function. */
5512 pipe_config->pixel_multiplier = 1;
5513 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005514 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5515 if (!IS_VALLEYVIEW(dev)) {
5516 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5517 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005518 } else {
5519 /* Mask out read-only status bits. */
5520 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5521 DPLL_PORTC_READY_MASK |
5522 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005523 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005524
Jesse Barnesacbec812013-09-20 11:29:32 -07005525 if (IS_VALLEYVIEW(dev))
5526 vlv_crtc_clock_get(crtc, pipe_config);
5527 else
5528 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005529
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005530 return true;
5531}
5532
Paulo Zanonidde86e22012-12-01 12:04:25 -02005533static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005534{
5535 struct drm_i915_private *dev_priv = dev->dev_private;
5536 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005537 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005538 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005539 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005540 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005541 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005542 bool has_ck505 = false;
5543 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005544
5545 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005546 list_for_each_entry(encoder, &mode_config->encoder_list,
5547 base.head) {
5548 switch (encoder->type) {
5549 case INTEL_OUTPUT_LVDS:
5550 has_panel = true;
5551 has_lvds = true;
5552 break;
5553 case INTEL_OUTPUT_EDP:
5554 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005555 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005556 has_cpu_edp = true;
5557 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005558 }
5559 }
5560
Keith Packard99eb6a02011-09-26 14:29:12 -07005561 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005562 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005563 can_ssc = has_ck505;
5564 } else {
5565 has_ck505 = false;
5566 can_ssc = true;
5567 }
5568
Imre Deak2de69052013-05-08 13:14:04 +03005569 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5570 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005571
5572 /* Ironlake: try to setup display ref clock before DPLL
5573 * enabling. This is only under driver's control after
5574 * PCH B stepping, previous chipset stepping should be
5575 * ignoring this setting.
5576 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005577 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005578
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005579 /* As we must carefully and slowly disable/enable each source in turn,
5580 * compute the final state we want first and check if we need to
5581 * make any changes at all.
5582 */
5583 final = val;
5584 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005585 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005586 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005587 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005588 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5589
5590 final &= ~DREF_SSC_SOURCE_MASK;
5591 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5592 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005593
Keith Packard199e5d72011-09-22 12:01:57 -07005594 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005595 final |= DREF_SSC_SOURCE_ENABLE;
5596
5597 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5598 final |= DREF_SSC1_ENABLE;
5599
5600 if (has_cpu_edp) {
5601 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5602 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5603 else
5604 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5605 } else
5606 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5607 } else {
5608 final |= DREF_SSC_SOURCE_DISABLE;
5609 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5610 }
5611
5612 if (final == val)
5613 return;
5614
5615 /* Always enable nonspread source */
5616 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5617
5618 if (has_ck505)
5619 val |= DREF_NONSPREAD_CK505_ENABLE;
5620 else
5621 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5622
5623 if (has_panel) {
5624 val &= ~DREF_SSC_SOURCE_MASK;
5625 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005626
Keith Packard199e5d72011-09-22 12:01:57 -07005627 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005628 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005629 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005630 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005631 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005632 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005633
5634 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005635 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005636 POSTING_READ(PCH_DREF_CONTROL);
5637 udelay(200);
5638
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005639 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005640
5641 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005642 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005643 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005644 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005645 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005646 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005647 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005648 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005649 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005650 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005651
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005652 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005653 POSTING_READ(PCH_DREF_CONTROL);
5654 udelay(200);
5655 } else {
5656 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5657
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005658 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005659
5660 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005661 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005662
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005663 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005664 POSTING_READ(PCH_DREF_CONTROL);
5665 udelay(200);
5666
5667 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005668 val &= ~DREF_SSC_SOURCE_MASK;
5669 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005670
5671 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005672 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005673
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005674 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005675 POSTING_READ(PCH_DREF_CONTROL);
5676 udelay(200);
5677 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005678
5679 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005680}
5681
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005682static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005683{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005684 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005685
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005686 tmp = I915_READ(SOUTH_CHICKEN2);
5687 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5688 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005689
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005690 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5691 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5692 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005693
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005694 tmp = I915_READ(SOUTH_CHICKEN2);
5695 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5696 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005697
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005698 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5699 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5700 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005701}
5702
5703/* WaMPhyProgramming:hsw */
5704static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5705{
5706 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005707
5708 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5709 tmp &= ~(0xFF << 24);
5710 tmp |= (0x12 << 24);
5711 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5712
Paulo Zanonidde86e22012-12-01 12:04:25 -02005713 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5714 tmp |= (1 << 11);
5715 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5716
5717 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5718 tmp |= (1 << 11);
5719 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5720
Paulo Zanonidde86e22012-12-01 12:04:25 -02005721 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5722 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5723 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5724
5725 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5726 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5727 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5728
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005729 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5730 tmp &= ~(7 << 13);
5731 tmp |= (5 << 13);
5732 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005733
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005734 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5735 tmp &= ~(7 << 13);
5736 tmp |= (5 << 13);
5737 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005738
5739 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5740 tmp &= ~0xFF;
5741 tmp |= 0x1C;
5742 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5743
5744 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5745 tmp &= ~0xFF;
5746 tmp |= 0x1C;
5747 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5748
5749 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5750 tmp &= ~(0xFF << 16);
5751 tmp |= (0x1C << 16);
5752 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5753
5754 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5755 tmp &= ~(0xFF << 16);
5756 tmp |= (0x1C << 16);
5757 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5758
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005759 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5760 tmp |= (1 << 27);
5761 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005762
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005763 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5764 tmp |= (1 << 27);
5765 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005766
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005767 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5768 tmp &= ~(0xF << 28);
5769 tmp |= (4 << 28);
5770 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005771
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005772 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5773 tmp &= ~(0xF << 28);
5774 tmp |= (4 << 28);
5775 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005776}
5777
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005778/* Implements 3 different sequences from BSpec chapter "Display iCLK
5779 * Programming" based on the parameters passed:
5780 * - Sequence to enable CLKOUT_DP
5781 * - Sequence to enable CLKOUT_DP without spread
5782 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5783 */
5784static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5785 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005786{
5787 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005788 uint32_t reg, tmp;
5789
5790 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5791 with_spread = true;
5792 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5793 with_fdi, "LP PCH doesn't have FDI\n"))
5794 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005795
5796 mutex_lock(&dev_priv->dpio_lock);
5797
5798 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5799 tmp &= ~SBI_SSCCTL_DISABLE;
5800 tmp |= SBI_SSCCTL_PATHALT;
5801 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5802
5803 udelay(24);
5804
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005805 if (with_spread) {
5806 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5807 tmp &= ~SBI_SSCCTL_PATHALT;
5808 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005809
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005810 if (with_fdi) {
5811 lpt_reset_fdi_mphy(dev_priv);
5812 lpt_program_fdi_mphy(dev_priv);
5813 }
5814 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005815
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005816 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5817 SBI_GEN0 : SBI_DBUFF0;
5818 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5819 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5820 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005821
5822 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005823}
5824
Paulo Zanoni47701c32013-07-23 11:19:25 -03005825/* Sequence to disable CLKOUT_DP */
5826static void lpt_disable_clkout_dp(struct drm_device *dev)
5827{
5828 struct drm_i915_private *dev_priv = dev->dev_private;
5829 uint32_t reg, tmp;
5830
5831 mutex_lock(&dev_priv->dpio_lock);
5832
5833 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5834 SBI_GEN0 : SBI_DBUFF0;
5835 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5836 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5837 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5838
5839 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5840 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5841 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5842 tmp |= SBI_SSCCTL_PATHALT;
5843 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5844 udelay(32);
5845 }
5846 tmp |= SBI_SSCCTL_DISABLE;
5847 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5848 }
5849
5850 mutex_unlock(&dev_priv->dpio_lock);
5851}
5852
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005853static void lpt_init_pch_refclk(struct drm_device *dev)
5854{
5855 struct drm_mode_config *mode_config = &dev->mode_config;
5856 struct intel_encoder *encoder;
5857 bool has_vga = false;
5858
5859 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5860 switch (encoder->type) {
5861 case INTEL_OUTPUT_ANALOG:
5862 has_vga = true;
5863 break;
5864 }
5865 }
5866
Paulo Zanoni47701c32013-07-23 11:19:25 -03005867 if (has_vga)
5868 lpt_enable_clkout_dp(dev, true, true);
5869 else
5870 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005871}
5872
Paulo Zanonidde86e22012-12-01 12:04:25 -02005873/*
5874 * Initialize reference clocks when the driver loads
5875 */
5876void intel_init_pch_refclk(struct drm_device *dev)
5877{
5878 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5879 ironlake_init_pch_refclk(dev);
5880 else if (HAS_PCH_LPT(dev))
5881 lpt_init_pch_refclk(dev);
5882}
5883
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005884static int ironlake_get_refclk(struct drm_crtc *crtc)
5885{
5886 struct drm_device *dev = crtc->dev;
5887 struct drm_i915_private *dev_priv = dev->dev_private;
5888 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005889 int num_connectors = 0;
5890 bool is_lvds = false;
5891
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005892 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005893 switch (encoder->type) {
5894 case INTEL_OUTPUT_LVDS:
5895 is_lvds = true;
5896 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005897 }
5898 num_connectors++;
5899 }
5900
5901 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5902 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005903 dev_priv->vbt.lvds_ssc_freq);
5904 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005905 }
5906
5907 return 120000;
5908}
5909
Daniel Vetter6ff93602013-04-19 11:24:36 +02005910static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005911{
5912 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5914 int pipe = intel_crtc->pipe;
5915 uint32_t val;
5916
Daniel Vetter78114072013-06-13 00:54:57 +02005917 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005918
Daniel Vetter965e0c42013-03-27 00:44:57 +01005919 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005920 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005921 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005922 break;
5923 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005924 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005925 break;
5926 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005927 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005928 break;
5929 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005930 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005931 break;
5932 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005933 /* Case prevented by intel_choose_pipe_bpp_dither. */
5934 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005935 }
5936
Daniel Vetterd8b32242013-04-25 17:54:44 +02005937 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005938 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5939
Daniel Vetter6ff93602013-04-19 11:24:36 +02005940 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005941 val |= PIPECONF_INTERLACED_ILK;
5942 else
5943 val |= PIPECONF_PROGRESSIVE;
5944
Daniel Vetter50f3b012013-03-27 00:44:56 +01005945 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005946 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005947
Paulo Zanonic8203562012-09-12 10:06:29 -03005948 I915_WRITE(PIPECONF(pipe), val);
5949 POSTING_READ(PIPECONF(pipe));
5950}
5951
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005952/*
5953 * Set up the pipe CSC unit.
5954 *
5955 * Currently only full range RGB to limited range RGB conversion
5956 * is supported, but eventually this should handle various
5957 * RGB<->YCbCr scenarios as well.
5958 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005959static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005960{
5961 struct drm_device *dev = crtc->dev;
5962 struct drm_i915_private *dev_priv = dev->dev_private;
5963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5964 int pipe = intel_crtc->pipe;
5965 uint16_t coeff = 0x7800; /* 1.0 */
5966
5967 /*
5968 * TODO: Check what kind of values actually come out of the pipe
5969 * with these coeff/postoff values and adjust to get the best
5970 * accuracy. Perhaps we even need to take the bpc value into
5971 * consideration.
5972 */
5973
Daniel Vetter50f3b012013-03-27 00:44:56 +01005974 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005975 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5976
5977 /*
5978 * GY/GU and RY/RU should be the other way around according
5979 * to BSpec, but reality doesn't agree. Just set them up in
5980 * a way that results in the correct picture.
5981 */
5982 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5983 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5984
5985 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5986 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5987
5988 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5989 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5990
5991 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5992 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5993 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5994
5995 if (INTEL_INFO(dev)->gen > 6) {
5996 uint16_t postoff = 0;
5997
Daniel Vetter50f3b012013-03-27 00:44:56 +01005998 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005999 postoff = (16 * (1 << 13) / 255) & 0x1fff;
6000
6001 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6002 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6003 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6004
6005 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6006 } else {
6007 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6008
Daniel Vetter50f3b012013-03-27 00:44:56 +01006009 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006010 mode |= CSC_BLACK_SCREEN_OFFSET;
6011
6012 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6013 }
6014}
6015
Daniel Vetter6ff93602013-04-19 11:24:36 +02006016static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006017{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006018 struct drm_device *dev = crtc->dev;
6019 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006021 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006022 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006023 uint32_t val;
6024
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006025 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006026
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006027 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006028 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6029
Daniel Vetter6ff93602013-04-19 11:24:36 +02006030 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006031 val |= PIPECONF_INTERLACED_ILK;
6032 else
6033 val |= PIPECONF_PROGRESSIVE;
6034
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006035 I915_WRITE(PIPECONF(cpu_transcoder), val);
6036 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006037
6038 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6039 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006040
6041 if (IS_BROADWELL(dev)) {
6042 val = 0;
6043
6044 switch (intel_crtc->config.pipe_bpp) {
6045 case 18:
6046 val |= PIPEMISC_DITHER_6_BPC;
6047 break;
6048 case 24:
6049 val |= PIPEMISC_DITHER_8_BPC;
6050 break;
6051 case 30:
6052 val |= PIPEMISC_DITHER_10_BPC;
6053 break;
6054 case 36:
6055 val |= PIPEMISC_DITHER_12_BPC;
6056 break;
6057 default:
6058 /* Case prevented by pipe_config_set_bpp. */
6059 BUG();
6060 }
6061
6062 if (intel_crtc->config.dither)
6063 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6064
6065 I915_WRITE(PIPEMISC(pipe), val);
6066 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006067}
6068
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006069static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006070 intel_clock_t *clock,
6071 bool *has_reduced_clock,
6072 intel_clock_t *reduced_clock)
6073{
6074 struct drm_device *dev = crtc->dev;
6075 struct drm_i915_private *dev_priv = dev->dev_private;
6076 struct intel_encoder *intel_encoder;
6077 int refclk;
6078 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006079 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006080
6081 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6082 switch (intel_encoder->type) {
6083 case INTEL_OUTPUT_LVDS:
6084 is_lvds = true;
6085 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006086 }
6087 }
6088
6089 refclk = ironlake_get_refclk(crtc);
6090
6091 /*
6092 * Returns a set of divisors for the desired target clock with the given
6093 * refclk, or FALSE. The returned values represent the clock equation:
6094 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6095 */
6096 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006097 ret = dev_priv->display.find_dpll(limit, crtc,
6098 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006099 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006100 if (!ret)
6101 return false;
6102
6103 if (is_lvds && dev_priv->lvds_downclock_avail) {
6104 /*
6105 * Ensure we match the reduced clock's P to the target clock.
6106 * If the clocks don't match, we can't switch the display clock
6107 * by using the FP0/FP1. In such case we will disable the LVDS
6108 * downclock feature.
6109 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006110 *has_reduced_clock =
6111 dev_priv->display.find_dpll(limit, crtc,
6112 dev_priv->lvds_downclock,
6113 refclk, clock,
6114 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006115 }
6116
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006117 return true;
6118}
6119
Paulo Zanonid4b19312012-11-29 11:29:32 -02006120int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6121{
6122 /*
6123 * Account for spread spectrum to avoid
6124 * oversubscribing the link. Max center spread
6125 * is 2.5%; use 5% for safety's sake.
6126 */
6127 u32 bps = target_clock * bpp * 21 / 20;
6128 return bps / (link_bw * 8) + 1;
6129}
6130
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006131static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006132{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006133 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006134}
6135
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006136static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006137 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006138 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006139{
6140 struct drm_crtc *crtc = &intel_crtc->base;
6141 struct drm_device *dev = crtc->dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
6143 struct intel_encoder *intel_encoder;
6144 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006145 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006146 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006147
6148 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6149 switch (intel_encoder->type) {
6150 case INTEL_OUTPUT_LVDS:
6151 is_lvds = true;
6152 break;
6153 case INTEL_OUTPUT_SDVO:
6154 case INTEL_OUTPUT_HDMI:
6155 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006156 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006157 }
6158
6159 num_connectors++;
6160 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006161
Chris Wilsonc1858122010-12-03 21:35:48 +00006162 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006163 factor = 21;
6164 if (is_lvds) {
6165 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006166 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006167 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006168 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006169 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006170 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006171
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006172 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006173 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006174
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006175 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6176 *fp2 |= FP_CB_TUNE;
6177
Chris Wilson5eddb702010-09-11 13:48:45 +01006178 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006179
Eric Anholta07d6782011-03-30 13:01:08 -07006180 if (is_lvds)
6181 dpll |= DPLLB_MODE_LVDS;
6182 else
6183 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006184
Daniel Vetteref1b4602013-06-01 17:17:04 +02006185 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6186 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006187
6188 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006189 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006190 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006191 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006192
Eric Anholta07d6782011-03-30 13:01:08 -07006193 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006194 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006195 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006196 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006197
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006198 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006199 case 5:
6200 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6201 break;
6202 case 7:
6203 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6204 break;
6205 case 10:
6206 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6207 break;
6208 case 14:
6209 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6210 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006211 }
6212
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006213 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006214 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006215 else
6216 dpll |= PLL_REF_INPUT_DREFCLK;
6217
Daniel Vetter959e16d2013-06-05 13:34:21 +02006218 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006219}
6220
Jesse Barnes79e53942008-11-07 14:24:08 -08006221static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006222 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006223 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006224{
6225 struct drm_device *dev = crtc->dev;
6226 struct drm_i915_private *dev_priv = dev->dev_private;
6227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6228 int pipe = intel_crtc->pipe;
6229 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006230 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006231 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006232 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006233 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006234 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006235 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006236 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006237 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006238
6239 for_each_encoder_on_crtc(dev, crtc, encoder) {
6240 switch (encoder->type) {
6241 case INTEL_OUTPUT_LVDS:
6242 is_lvds = true;
6243 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006244 }
6245
6246 num_connectors++;
6247 }
6248
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006249 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6250 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6251
Daniel Vetterff9a6752013-06-01 17:16:21 +02006252 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006253 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006254 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006255 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6256 return -EINVAL;
6257 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006258 /* Compat-code for transition, will disappear. */
6259 if (!intel_crtc->config.clock_set) {
6260 intel_crtc->config.dpll.n = clock.n;
6261 intel_crtc->config.dpll.m1 = clock.m1;
6262 intel_crtc->config.dpll.m2 = clock.m2;
6263 intel_crtc->config.dpll.p1 = clock.p1;
6264 intel_crtc->config.dpll.p2 = clock.p2;
6265 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006266
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006267 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006268 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006269 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006270 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006271 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006272
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006273 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006274 &fp, &reduced_clock,
6275 has_reduced_clock ? &fp2 : NULL);
6276
Daniel Vetter959e16d2013-06-05 13:34:21 +02006277 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006278 intel_crtc->config.dpll_hw_state.fp0 = fp;
6279 if (has_reduced_clock)
6280 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6281 else
6282 intel_crtc->config.dpll_hw_state.fp1 = fp;
6283
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006284 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006285 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006286 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6287 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006288 return -EINVAL;
6289 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006290 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006291 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006292
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006293 if (intel_crtc->config.has_dp_encoder)
6294 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006295
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006296 if (is_lvds && has_reduced_clock && i915_powersave)
6297 intel_crtc->lowfreq_avail = true;
6298 else
6299 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006300
Daniel Vetter8a654f32013-06-01 17:16:22 +02006301 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006302
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006303 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006304 intel_cpu_transcoder_set_m_n(intel_crtc,
6305 &intel_crtc->config.fdi_m_n);
6306 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006307
Daniel Vetter6ff93602013-04-19 11:24:36 +02006308 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006309
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006310 /* Set up the display plane register */
6311 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006312 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006313
Daniel Vetter94352cf2012-07-05 22:51:56 +02006314 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006315
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006316 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006317}
6318
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006319static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6320 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006321{
6322 struct drm_device *dev = crtc->base.dev;
6323 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006324 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006325
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006326 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6327 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6328 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6329 & ~TU_SIZE_MASK;
6330 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6331 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6332 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6333}
6334
6335static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6336 enum transcoder transcoder,
6337 struct intel_link_m_n *m_n)
6338{
6339 struct drm_device *dev = crtc->base.dev;
6340 struct drm_i915_private *dev_priv = dev->dev_private;
6341 enum pipe pipe = crtc->pipe;
6342
6343 if (INTEL_INFO(dev)->gen >= 5) {
6344 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6345 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6346 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6347 & ~TU_SIZE_MASK;
6348 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6349 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6350 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6351 } else {
6352 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6353 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6354 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6355 & ~TU_SIZE_MASK;
6356 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6357 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6358 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6359 }
6360}
6361
6362void intel_dp_get_m_n(struct intel_crtc *crtc,
6363 struct intel_crtc_config *pipe_config)
6364{
6365 if (crtc->config.has_pch_encoder)
6366 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6367 else
6368 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6369 &pipe_config->dp_m_n);
6370}
6371
Daniel Vetter72419202013-04-04 13:28:53 +02006372static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6373 struct intel_crtc_config *pipe_config)
6374{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006375 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6376 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006377}
6378
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006379static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6380 struct intel_crtc_config *pipe_config)
6381{
6382 struct drm_device *dev = crtc->base.dev;
6383 struct drm_i915_private *dev_priv = dev->dev_private;
6384 uint32_t tmp;
6385
6386 tmp = I915_READ(PF_CTL(crtc->pipe));
6387
6388 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006389 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006390 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6391 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006392
6393 /* We currently do not free assignements of panel fitters on
6394 * ivb/hsw (since we don't use the higher upscaling modes which
6395 * differentiates them) so just WARN about this case for now. */
6396 if (IS_GEN7(dev)) {
6397 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6398 PF_PIPE_SEL_IVB(crtc->pipe));
6399 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006400 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006401}
6402
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006403static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6404 struct intel_crtc_config *pipe_config)
6405{
6406 struct drm_device *dev = crtc->base.dev;
6407 struct drm_i915_private *dev_priv = dev->dev_private;
6408 uint32_t tmp;
6409
Daniel Vettere143a212013-07-04 12:01:15 +02006410 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006411 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006412
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006413 tmp = I915_READ(PIPECONF(crtc->pipe));
6414 if (!(tmp & PIPECONF_ENABLE))
6415 return false;
6416
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006417 switch (tmp & PIPECONF_BPC_MASK) {
6418 case PIPECONF_6BPC:
6419 pipe_config->pipe_bpp = 18;
6420 break;
6421 case PIPECONF_8BPC:
6422 pipe_config->pipe_bpp = 24;
6423 break;
6424 case PIPECONF_10BPC:
6425 pipe_config->pipe_bpp = 30;
6426 break;
6427 case PIPECONF_12BPC:
6428 pipe_config->pipe_bpp = 36;
6429 break;
6430 default:
6431 break;
6432 }
6433
Daniel Vetterab9412b2013-05-03 11:49:46 +02006434 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006435 struct intel_shared_dpll *pll;
6436
Daniel Vetter88adfff2013-03-28 10:42:01 +01006437 pipe_config->has_pch_encoder = true;
6438
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006439 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6440 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6441 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006442
6443 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006444
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006445 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006446 pipe_config->shared_dpll =
6447 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006448 } else {
6449 tmp = I915_READ(PCH_DPLL_SEL);
6450 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6451 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6452 else
6453 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6454 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006455
6456 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6457
6458 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6459 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006460
6461 tmp = pipe_config->dpll_hw_state.dpll;
6462 pipe_config->pixel_multiplier =
6463 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6464 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006465
6466 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006467 } else {
6468 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006469 }
6470
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006471 intel_get_pipe_timings(crtc, pipe_config);
6472
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006473 ironlake_get_pfit_config(crtc, pipe_config);
6474
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006475 return true;
6476}
6477
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006478static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6479{
6480 struct drm_device *dev = dev_priv->dev;
6481 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6482 struct intel_crtc *crtc;
6483 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006484 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006485
6486 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6487 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6488 pipe_name(crtc->pipe));
6489
6490 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6491 WARN(plls->spll_refcount, "SPLL enabled\n");
6492 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6493 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6494 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6495 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6496 "CPU PWM1 enabled\n");
6497 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6498 "CPU PWM2 enabled\n");
6499 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6500 "PCH PWM1 enabled\n");
6501 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6502 "Utility pin enabled\n");
6503 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6504
6505 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6506 val = I915_READ(DEIMR);
6507 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6508 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6509 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006510 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006511 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6512 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6513}
6514
6515/*
6516 * This function implements pieces of two sequences from BSpec:
6517 * - Sequence for display software to disable LCPLL
6518 * - Sequence for display software to allow package C8+
6519 * The steps implemented here are just the steps that actually touch the LCPLL
6520 * register. Callers should take care of disabling all the display engine
6521 * functions, doing the mode unset, fixing interrupts, etc.
6522 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006523static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6524 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006525{
6526 uint32_t val;
6527
6528 assert_can_disable_lcpll(dev_priv);
6529
6530 val = I915_READ(LCPLL_CTL);
6531
6532 if (switch_to_fclk) {
6533 val |= LCPLL_CD_SOURCE_FCLK;
6534 I915_WRITE(LCPLL_CTL, val);
6535
6536 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6537 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6538 DRM_ERROR("Switching to FCLK failed\n");
6539
6540 val = I915_READ(LCPLL_CTL);
6541 }
6542
6543 val |= LCPLL_PLL_DISABLE;
6544 I915_WRITE(LCPLL_CTL, val);
6545 POSTING_READ(LCPLL_CTL);
6546
6547 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6548 DRM_ERROR("LCPLL still locked\n");
6549
6550 val = I915_READ(D_COMP);
6551 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006552 mutex_lock(&dev_priv->rps.hw_lock);
6553 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6554 DRM_ERROR("Failed to disable D_COMP\n");
6555 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006556 POSTING_READ(D_COMP);
6557 ndelay(100);
6558
6559 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6560 DRM_ERROR("D_COMP RCOMP still in progress\n");
6561
6562 if (allow_power_down) {
6563 val = I915_READ(LCPLL_CTL);
6564 val |= LCPLL_POWER_DOWN_ALLOW;
6565 I915_WRITE(LCPLL_CTL, val);
6566 POSTING_READ(LCPLL_CTL);
6567 }
6568}
6569
6570/*
6571 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6572 * source.
6573 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006574static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006575{
6576 uint32_t val;
6577
6578 val = I915_READ(LCPLL_CTL);
6579
6580 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6581 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6582 return;
6583
Paulo Zanoni215733f2013-08-19 13:18:07 -03006584 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6585 * we'll hang the machine! */
6586 dev_priv->uncore.funcs.force_wake_get(dev_priv);
6587
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006588 if (val & LCPLL_POWER_DOWN_ALLOW) {
6589 val &= ~LCPLL_POWER_DOWN_ALLOW;
6590 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006591 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006592 }
6593
6594 val = I915_READ(D_COMP);
6595 val |= D_COMP_COMP_FORCE;
6596 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006597 mutex_lock(&dev_priv->rps.hw_lock);
6598 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6599 DRM_ERROR("Failed to enable D_COMP\n");
6600 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006601 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006602
6603 val = I915_READ(LCPLL_CTL);
6604 val &= ~LCPLL_PLL_DISABLE;
6605 I915_WRITE(LCPLL_CTL, val);
6606
6607 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6608 DRM_ERROR("LCPLL not locked yet\n");
6609
6610 if (val & LCPLL_CD_SOURCE_FCLK) {
6611 val = I915_READ(LCPLL_CTL);
6612 val &= ~LCPLL_CD_SOURCE_FCLK;
6613 I915_WRITE(LCPLL_CTL, val);
6614
6615 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6616 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6617 DRM_ERROR("Switching back to LCPLL failed\n");
6618 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006619
6620 dev_priv->uncore.funcs.force_wake_put(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006621}
6622
Paulo Zanonic67a4702013-08-19 13:18:09 -03006623void hsw_enable_pc8_work(struct work_struct *__work)
6624{
6625 struct drm_i915_private *dev_priv =
6626 container_of(to_delayed_work(__work), struct drm_i915_private,
6627 pc8.enable_work);
6628 struct drm_device *dev = dev_priv->dev;
6629 uint32_t val;
6630
6631 if (dev_priv->pc8.enabled)
6632 return;
6633
6634 DRM_DEBUG_KMS("Enabling package C8+\n");
6635
6636 dev_priv->pc8.enabled = true;
6637
6638 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6639 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6640 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6641 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6642 }
6643
6644 lpt_disable_clkout_dp(dev);
6645 hsw_pc8_disable_interrupts(dev);
6646 hsw_disable_lcpll(dev_priv, true, true);
6647}
6648
6649static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6650{
6651 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6652 WARN(dev_priv->pc8.disable_count < 1,
6653 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6654
6655 dev_priv->pc8.disable_count--;
6656 if (dev_priv->pc8.disable_count != 0)
6657 return;
6658
6659 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006660 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006661}
6662
6663static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6664{
6665 struct drm_device *dev = dev_priv->dev;
6666 uint32_t val;
6667
6668 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6669 WARN(dev_priv->pc8.disable_count < 0,
6670 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6671
6672 dev_priv->pc8.disable_count++;
6673 if (dev_priv->pc8.disable_count != 1)
6674 return;
6675
6676 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6677 if (!dev_priv->pc8.enabled)
6678 return;
6679
6680 DRM_DEBUG_KMS("Disabling package C8+\n");
6681
6682 hsw_restore_lcpll(dev_priv);
6683 hsw_pc8_restore_interrupts(dev);
6684 lpt_init_pch_refclk(dev);
6685
6686 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6687 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6688 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6689 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6690 }
6691
6692 intel_prepare_ddi(dev);
6693 i915_gem_init_swizzling(dev);
6694 mutex_lock(&dev_priv->rps.hw_lock);
6695 gen6_update_ring_freq(dev);
6696 mutex_unlock(&dev_priv->rps.hw_lock);
6697 dev_priv->pc8.enabled = false;
6698}
6699
6700void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6701{
6702 mutex_lock(&dev_priv->pc8.lock);
6703 __hsw_enable_package_c8(dev_priv);
6704 mutex_unlock(&dev_priv->pc8.lock);
6705}
6706
6707void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6708{
6709 mutex_lock(&dev_priv->pc8.lock);
6710 __hsw_disable_package_c8(dev_priv);
6711 mutex_unlock(&dev_priv->pc8.lock);
6712}
6713
6714static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6715{
6716 struct drm_device *dev = dev_priv->dev;
6717 struct intel_crtc *crtc;
6718 uint32_t val;
6719
6720 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6721 if (crtc->base.enabled)
6722 return false;
6723
6724 /* This case is still possible since we have the i915.disable_power_well
6725 * parameter and also the KVMr or something else might be requesting the
6726 * power well. */
6727 val = I915_READ(HSW_PWR_WELL_DRIVER);
6728 if (val != 0) {
6729 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6730 return false;
6731 }
6732
6733 return true;
6734}
6735
6736/* Since we're called from modeset_global_resources there's no way to
6737 * symmetrically increase and decrease the refcount, so we use
6738 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6739 * or not.
6740 */
6741static void hsw_update_package_c8(struct drm_device *dev)
6742{
6743 struct drm_i915_private *dev_priv = dev->dev_private;
6744 bool allow;
6745
6746 if (!i915_enable_pc8)
6747 return;
6748
6749 mutex_lock(&dev_priv->pc8.lock);
6750
6751 allow = hsw_can_enable_package_c8(dev_priv);
6752
6753 if (allow == dev_priv->pc8.requirements_met)
6754 goto done;
6755
6756 dev_priv->pc8.requirements_met = allow;
6757
6758 if (allow)
6759 __hsw_enable_package_c8(dev_priv);
6760 else
6761 __hsw_disable_package_c8(dev_priv);
6762
6763done:
6764 mutex_unlock(&dev_priv->pc8.lock);
6765}
6766
6767static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6768{
Chris Wilson34581222013-11-18 18:32:36 -08006769 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006770 if (!dev_priv->pc8.gpu_idle) {
6771 dev_priv->pc8.gpu_idle = true;
Chris Wilson34581222013-11-18 18:32:36 -08006772 __hsw_enable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006773 }
Chris Wilson34581222013-11-18 18:32:36 -08006774 mutex_unlock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006775}
6776
6777static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6778{
Chris Wilson34581222013-11-18 18:32:36 -08006779 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006780 if (dev_priv->pc8.gpu_idle) {
6781 dev_priv->pc8.gpu_idle = false;
Chris Wilson34581222013-11-18 18:32:36 -08006782 __hsw_disable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006783 }
Chris Wilson34581222013-11-18 18:32:36 -08006784 mutex_unlock(&dev_priv->pc8.lock);
Daniel Vetter94352cf2012-07-05 22:51:56 +02006785}
Eric Anholtf564048e2011-03-30 13:01:02 -07006786
Imre Deak6efdf352013-10-16 17:25:52 +03006787#define for_each_power_domain(domain, mask) \
6788 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6789 if ((1 << (domain)) & (mask))
6790
6791static unsigned long get_pipe_power_domains(struct drm_device *dev,
6792 enum pipe pipe, bool pfit_enabled)
6793{
6794 unsigned long mask;
6795 enum transcoder transcoder;
6796
6797 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6798
6799 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6800 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6801 if (pfit_enabled)
6802 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6803
6804 return mask;
6805}
6806
Imre Deakbaa70702013-10-25 17:36:48 +03006807void intel_display_set_init_power(struct drm_device *dev, bool enable)
6808{
6809 struct drm_i915_private *dev_priv = dev->dev_private;
6810
6811 if (dev_priv->power_domains.init_power_on == enable)
6812 return;
6813
6814 if (enable)
6815 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6816 else
6817 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6818
6819 dev_priv->power_domains.init_power_on = enable;
6820}
6821
Imre Deak4f074122013-10-16 17:25:51 +03006822static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006823{
Imre Deak6efdf352013-10-16 17:25:52 +03006824 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006825 struct intel_crtc *crtc;
6826
Imre Deak6efdf352013-10-16 17:25:52 +03006827 /*
6828 * First get all needed power domains, then put all unneeded, to avoid
6829 * any unnecessary toggling of the power wells.
6830 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006831 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006832 enum intel_display_power_domain domain;
6833
Jesse Barnes79e53942008-11-07 14:24:08 -08006834 if (!crtc->base.enabled)
6835 continue;
6836
Imre Deak6efdf352013-10-16 17:25:52 +03006837 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6838 crtc->pipe,
6839 crtc->config.pch_pfit.enabled);
6840
6841 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6842 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006843 }
6844
Imre Deak6efdf352013-10-16 17:25:52 +03006845 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6846 enum intel_display_power_domain domain;
6847
6848 for_each_power_domain(domain, crtc->enabled_power_domains)
6849 intel_display_power_put(dev, domain);
6850
6851 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6852 }
Imre Deakbaa70702013-10-25 17:36:48 +03006853
6854 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006855}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006856
Imre Deak4f074122013-10-16 17:25:51 +03006857static void haswell_modeset_global_resources(struct drm_device *dev)
6858{
6859 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006860 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006861}
6862
6863static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6864 int x, int y,
6865 struct drm_framebuffer *fb)
6866{
6867 struct drm_device *dev = crtc->dev;
6868 struct drm_i915_private *dev_priv = dev->dev_private;
6869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6870 int plane = intel_crtc->plane;
6871 int ret;
6872
6873 if (!intel_ddi_pll_mode_set(crtc))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006874 return -EINVAL;
Eric Anholtbad720f2009-10-22 16:11:14 -07006875
Chris Wilson560b85b2010-08-07 11:01:38 +01006876 if (intel_crtc->config.has_dp_encoder)
6877 intel_dp_set_m_n(intel_crtc);
6878
6879 intel_crtc->lowfreq_avail = false;
6880
6881 intel_set_pipe_timings(intel_crtc);
6882
6883 if (intel_crtc->config.has_pch_encoder) {
6884 intel_cpu_transcoder_set_m_n(intel_crtc,
6885 &intel_crtc->config.fdi_m_n);
6886 }
6887
6888 haswell_set_pipeconf(crtc);
6889
6890 intel_set_pipe_csc(crtc);
6891
6892 /* Set up the display plane register */
6893 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6894 POSTING_READ(DSPCNTR(plane));
6895
6896 ret = intel_pipe_set_base(crtc, x, y, fb);
6897
Chris Wilson560b85b2010-08-07 11:01:38 +01006898 return ret;
6899}
6900
6901static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6902 struct intel_crtc_config *pipe_config)
6903{
6904 struct drm_device *dev = crtc->base.dev;
6905 struct drm_i915_private *dev_priv = dev->dev_private;
6906 enum intel_display_power_domain pfit_domain;
6907 uint32_t tmp;
6908
6909 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6910 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6911
6912 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6913 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6914 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006915 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01006916 default:
6917 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006918 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6919 case TRANS_DDI_EDP_INPUT_A_ON:
6920 trans_edp_pipe = PIPE_A;
6921 break;
6922 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6923 trans_edp_pipe = PIPE_B;
6924 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01006925 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006926 trans_edp_pipe = PIPE_C;
6927 break;
6928 }
6929
Chris Wilson6b383a72010-09-13 13:54:26 +01006930 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006931 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6932 }
6933
6934 if (!intel_display_power_enabled(dev,
6935 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6936 return false;
6937
6938 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6939 if (!(tmp & PIPECONF_ENABLE))
6940 return false;
6941
6942 /*
6943 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6944 * DDI E. So just check whether this pipe is wired to DDI E and whether
6945 * the PCH transcoder is on.
6946 */
6947 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6948 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6949 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6950 pipe_config->has_pch_encoder = true;
6951
6952 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6953 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6954 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6955
6956 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6957 }
6958
Chris Wilson560b85b2010-08-07 11:01:38 +01006959 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006960
6961 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6962 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01006963 ironlake_get_pfit_config(crtc, pipe_config);
6964
6965 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6966 (I915_READ(IPS_CTL) & IPS_ENABLE);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006967
6968 pipe_config->pixel_multiplier = 1;
6969
6970 return true;
6971}
Jesse Barnes79e53942008-11-07 14:24:08 -08006972
Chris Wilson05394f32010-11-08 19:18:58 +00006973static int intel_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006974 int x, int y,
6975 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006976{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006977 struct drm_device *dev = crtc->dev;
6978 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006979 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006981 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006982 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006983 int ret;
6984
Eric Anholt0b701d22011-03-30 13:01:03 -07006985 drm_vblank_pre_modeset(dev, pipe);
6986
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006987 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6988
Jesse Barnes79e53942008-11-07 14:24:08 -08006989 drm_vblank_post_modeset(dev, pipe);
6990
Daniel Vetter9256aa12012-10-31 19:26:13 +01006991 if (ret != 0)
6992 return ret;
6993
6994 for_each_encoder_on_crtc(dev, crtc, encoder) {
6995 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6996 encoder->base.base.id,
6997 drm_get_encoder_name(&encoder->base),
6998 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02006999 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007000 }
7001
7002 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007003}
7004
Jani Nikula1a915102013-10-16 12:34:48 +03007005static struct {
7006 int clock;
7007 u32 config;
7008} hdmi_audio_clock[] = {
7009 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7010 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7011 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7012 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7013 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7014 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7015 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7016 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7017 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7018 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7019};
7020
7021/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7022static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7023{
7024 int i;
7025
7026 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7027 if (mode->clock == hdmi_audio_clock[i].clock)
7028 break;
7029 }
7030
7031 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7032 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7033 i = 1;
7034 }
7035
7036 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7037 hdmi_audio_clock[i].clock,
7038 hdmi_audio_clock[i].config);
7039
7040 return hdmi_audio_clock[i].config;
7041}
7042
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007043static bool intel_eld_uptodate(struct drm_connector *connector,
7044 int reg_eldv, uint32_t bits_eldv,
7045 int reg_elda, uint32_t bits_elda,
7046 int reg_edid)
7047{
7048 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7049 uint8_t *eld = connector->eld;
7050 uint32_t i;
7051
7052 i = I915_READ(reg_eldv);
7053 i &= bits_eldv;
7054
7055 if (!eld[0])
7056 return !i;
7057
7058 if (!i)
7059 return false;
7060
7061 i = I915_READ(reg_elda);
7062 i &= ~bits_elda;
7063 I915_WRITE(reg_elda, i);
7064
7065 for (i = 0; i < eld[2]; i++)
7066 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7067 return false;
7068
7069 return true;
7070}
7071
Wu Fengguange0dac652011-09-05 14:25:34 +08007072static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007073 struct drm_crtc *crtc,
7074 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007075{
7076 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7077 uint8_t *eld = connector->eld;
7078 uint32_t eldv;
7079 uint32_t len;
7080 uint32_t i;
7081
7082 i = I915_READ(G4X_AUD_VID_DID);
7083
7084 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7085 eldv = G4X_ELDV_DEVCL_DEVBLC;
7086 else
7087 eldv = G4X_ELDV_DEVCTG;
7088
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007089 if (intel_eld_uptodate(connector,
7090 G4X_AUD_CNTL_ST, eldv,
7091 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7092 G4X_HDMIW_HDMIEDID))
7093 return;
7094
Wu Fengguange0dac652011-09-05 14:25:34 +08007095 i = I915_READ(G4X_AUD_CNTL_ST);
7096 i &= ~(eldv | G4X_ELD_ADDR);
7097 len = (i >> 9) & 0x1f; /* ELD buffer size */
7098 I915_WRITE(G4X_AUD_CNTL_ST, i);
7099
7100 if (!eld[0])
7101 return;
7102
7103 len = min_t(uint8_t, eld[2], len);
7104 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7105 for (i = 0; i < len; i++)
7106 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7107
7108 i = I915_READ(G4X_AUD_CNTL_ST);
7109 i |= eldv;
7110 I915_WRITE(G4X_AUD_CNTL_ST, i);
7111}
7112
Wang Xingchao83358c852012-08-16 22:43:37 +08007113static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007114 struct drm_crtc *crtc,
7115 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007116{
7117 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7118 uint8_t *eld = connector->eld;
7119 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007121 uint32_t eldv;
7122 uint32_t i;
7123 int len;
7124 int pipe = to_intel_crtc(crtc)->pipe;
7125 int tmp;
7126
7127 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7128 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7129 int aud_config = HSW_AUD_CFG(pipe);
7130 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7131
7132
7133 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7134
7135 /* Audio output enable */
7136 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7137 tmp = I915_READ(aud_cntrl_st2);
7138 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7139 I915_WRITE(aud_cntrl_st2, tmp);
7140
7141 /* Wait for 1 vertical blank */
7142 intel_wait_for_vblank(dev, pipe);
7143
7144 /* Set ELD valid state */
7145 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007146 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007147 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7148 I915_WRITE(aud_cntrl_st2, tmp);
7149 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007150 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007151
7152 /* Enable HDMI mode */
7153 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007154 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007155 /* clear N_programing_enable and N_value_index */
7156 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7157 I915_WRITE(aud_config, tmp);
7158
7159 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7160
7161 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007162 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007163
7164 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7165 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7166 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7167 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007168 } else {
7169 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7170 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007171
7172 if (intel_eld_uptodate(connector,
7173 aud_cntrl_st2, eldv,
7174 aud_cntl_st, IBX_ELD_ADDRESS,
7175 hdmiw_hdmiedid))
7176 return;
7177
7178 i = I915_READ(aud_cntrl_st2);
7179 i &= ~eldv;
7180 I915_WRITE(aud_cntrl_st2, i);
7181
7182 if (!eld[0])
7183 return;
7184
7185 i = I915_READ(aud_cntl_st);
7186 i &= ~IBX_ELD_ADDRESS;
7187 I915_WRITE(aud_cntl_st, i);
7188 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7189 DRM_DEBUG_DRIVER("port num:%d\n", i);
7190
7191 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7192 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7193 for (i = 0; i < len; i++)
7194 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7195
7196 i = I915_READ(aud_cntrl_st2);
7197 i |= eldv;
7198 I915_WRITE(aud_cntrl_st2, i);
7199
7200}
7201
Wu Fengguange0dac652011-09-05 14:25:34 +08007202static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007203 struct drm_crtc *crtc,
7204 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007205{
7206 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7207 uint8_t *eld = connector->eld;
7208 uint32_t eldv;
7209 uint32_t i;
7210 int len;
7211 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007212 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007213 int aud_cntl_st;
7214 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007215 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007216
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007217 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007218 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7219 aud_config = IBX_AUD_CFG(pipe);
7220 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007221 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007222 } else if (IS_VALLEYVIEW(connector->dev)) {
7223 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7224 aud_config = VLV_AUD_CFG(pipe);
7225 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7226 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007227 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007228 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7229 aud_config = CPT_AUD_CFG(pipe);
7230 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007231 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007232 }
7233
Wang Xingchao9b138a82012-08-09 16:52:18 +08007234 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007235
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007236 if (IS_VALLEYVIEW(connector->dev)) {
7237 struct intel_encoder *intel_encoder;
7238 struct intel_digital_port *intel_dig_port;
7239
7240 intel_encoder = intel_attached_encoder(connector);
7241 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7242 i = intel_dig_port->port;
7243 } else {
7244 i = I915_READ(aud_cntl_st);
7245 i = (i >> 29) & DIP_PORT_SEL_MASK;
7246 /* DIP_Port_Select, 0x1 = PortB */
7247 }
7248
Wu Fengguange0dac652011-09-05 14:25:34 +08007249 if (!i) {
7250 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7251 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007252 eldv = IBX_ELD_VALIDB;
7253 eldv |= IBX_ELD_VALIDB << 4;
7254 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007255 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007256 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007257 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007258 }
7259
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007260 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7261 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7262 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007263 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007264 } else {
7265 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7266 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007267
7268 if (intel_eld_uptodate(connector,
7269 aud_cntrl_st2, eldv,
7270 aud_cntl_st, IBX_ELD_ADDRESS,
7271 hdmiw_hdmiedid))
7272 return;
7273
Wu Fengguange0dac652011-09-05 14:25:34 +08007274 i = I915_READ(aud_cntrl_st2);
7275 i &= ~eldv;
7276 I915_WRITE(aud_cntrl_st2, i);
7277
7278 if (!eld[0])
7279 return;
7280
Wu Fengguange0dac652011-09-05 14:25:34 +08007281 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007282 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007283 I915_WRITE(aud_cntl_st, i);
7284
7285 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7286 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7287 for (i = 0; i < len; i++)
7288 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7289
7290 i = I915_READ(aud_cntrl_st2);
7291 i |= eldv;
7292 I915_WRITE(aud_cntrl_st2, i);
7293}
7294
7295void intel_write_eld(struct drm_encoder *encoder,
7296 struct drm_display_mode *mode)
7297{
7298 struct drm_crtc *crtc = encoder->crtc;
7299 struct drm_connector *connector;
7300 struct drm_device *dev = encoder->dev;
7301 struct drm_i915_private *dev_priv = dev->dev_private;
7302
7303 connector = drm_select_eld(encoder, mode);
7304 if (!connector)
7305 return;
7306
7307 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7308 connector->base.id,
7309 drm_get_connector_name(connector),
7310 connector->encoder->base.id,
7311 drm_get_encoder_name(connector->encoder));
7312
7313 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7314
7315 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007316 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007317}
7318
Jesse Barnes79e53942008-11-07 14:24:08 -08007319static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7320{
7321 struct drm_device *dev = crtc->dev;
7322 struct drm_i915_private *dev_priv = dev->dev_private;
7323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7324 bool visible = base != 0;
7325 u32 cntl;
7326
7327 if (intel_crtc->cursor_visible == visible)
7328 return;
7329
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007330 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007331 if (visible) {
7332 /* On these chipsets we can only modify the base whilst
7333 * the cursor is disabled.
7334 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007335 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007336
7337 cntl &= ~(CURSOR_FORMAT_MASK);
7338 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7339 cntl |= CURSOR_ENABLE |
7340 CURSOR_GAMMA_ENABLE |
7341 CURSOR_FORMAT_ARGB;
7342 } else
7343 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007344 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007345
7346 intel_crtc->cursor_visible = visible;
7347}
7348
7349static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7350{
7351 struct drm_device *dev = crtc->dev;
7352 struct drm_i915_private *dev_priv = dev->dev_private;
7353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7354 int pipe = intel_crtc->pipe;
7355 bool visible = base != 0;
7356
7357 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007358 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007359 if (base) {
7360 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7361 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7362 cntl |= pipe << 28; /* Connect to correct pipe */
7363 } else {
7364 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7365 cntl |= CURSOR_MODE_DISABLE;
7366 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007367 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007368
7369 intel_crtc->cursor_visible = visible;
7370 }
7371 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007372 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007373}
7374
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007375static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7376{
7377 struct drm_device *dev = crtc->dev;
7378 struct drm_i915_private *dev_priv = dev->dev_private;
7379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7380 int pipe = intel_crtc->pipe;
7381 bool visible = base != 0;
7382
7383 if (intel_crtc->cursor_visible != visible) {
7384 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7385 if (base) {
7386 cntl &= ~CURSOR_MODE;
7387 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7388 } else {
7389 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7390 cntl |= CURSOR_MODE_DISABLE;
7391 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007392 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007393 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007394 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7395 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007396 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7397
7398 intel_crtc->cursor_visible = visible;
7399 }
7400 /* and commit changes on next vblank */
7401 I915_WRITE(CURBASE_IVB(pipe), base);
7402}
7403
Jesse Barnes79e53942008-11-07 14:24:08 -08007404/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7405static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7406 bool on)
7407{
7408 struct drm_device *dev = crtc->dev;
7409 struct drm_i915_private *dev_priv = dev->dev_private;
7410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7411 int pipe = intel_crtc->pipe;
7412 int x = intel_crtc->cursor_x;
7413 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007414 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007415 bool visible;
7416
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007417 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007418 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007419
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007420 if (x >= intel_crtc->config.pipe_src_w)
7421 base = 0;
7422
7423 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007424 base = 0;
7425
7426 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007427 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007428 base = 0;
7429
7430 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7431 x = -x;
7432 }
7433 pos |= x << CURSOR_X_SHIFT;
7434
7435 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007436 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007437 base = 0;
7438
7439 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7440 y = -y;
7441 }
7442 pos |= y << CURSOR_Y_SHIFT;
7443
7444 visible = base != 0;
7445 if (!visible && !intel_crtc->cursor_visible)
7446 return;
7447
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007448 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007449 I915_WRITE(CURPOS_IVB(pipe), pos);
7450 ivb_update_cursor(crtc, base);
7451 } else {
7452 I915_WRITE(CURPOS(pipe), pos);
7453 if (IS_845G(dev) || IS_I865G(dev))
7454 i845_update_cursor(crtc, base);
7455 else
7456 i9xx_update_cursor(crtc, base);
7457 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007458}
7459
7460static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7461 struct drm_file *file,
7462 uint32_t handle,
7463 uint32_t width, uint32_t height)
7464{
7465 struct drm_device *dev = crtc->dev;
7466 struct drm_i915_private *dev_priv = dev->dev_private;
7467 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007468 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007469 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007470 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007471
Jesse Barnes79e53942008-11-07 14:24:08 -08007472 /* if we want to turn off the cursor ignore width and height */
7473 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007474 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007475 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007476 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007477 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007478 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007479 }
7480
7481 /* Currently we only support 64x64 cursors */
7482 if (width != 64 || height != 64) {
7483 DRM_ERROR("we currently only support 64x64 cursors\n");
7484 return -EINVAL;
7485 }
7486
Chris Wilson05394f32010-11-08 19:18:58 +00007487 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007488 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007489 return -ENOENT;
7490
Chris Wilson05394f32010-11-08 19:18:58 +00007491 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007492 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007493 ret = -ENOMEM;
7494 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007495 }
7496
Dave Airlie71acb5e2008-12-30 20:31:46 +10007497 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007498 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007499 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007500 unsigned alignment;
7501
Chris Wilsond9e86c02010-11-10 16:40:20 +00007502 if (obj->tiling_mode) {
7503 DRM_ERROR("cursor cannot be tiled\n");
7504 ret = -EINVAL;
7505 goto fail_locked;
7506 }
7507
Chris Wilson693db182013-03-05 14:52:39 +00007508 /* Note that the w/a also requires 2 PTE of padding following
7509 * the bo. We currently fill all unused PTE with the shadow
7510 * page and so we should always have valid PTE following the
7511 * cursor preventing the VT-d warning.
7512 */
7513 alignment = 0;
7514 if (need_vtd_wa(dev))
7515 alignment = 64*1024;
7516
7517 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007518 if (ret) {
7519 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007520 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007521 }
7522
Chris Wilsond9e86c02010-11-10 16:40:20 +00007523 ret = i915_gem_object_put_fence(obj);
7524 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007525 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007526 goto fail_unpin;
7527 }
7528
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007529 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007530 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007531 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007532 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007533 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7534 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007535 if (ret) {
7536 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007537 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007538 }
Chris Wilson05394f32010-11-08 19:18:58 +00007539 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007540 }
7541
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007542 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007543 I915_WRITE(CURSIZE, (height << 12) | width);
7544
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007545 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007546 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007547 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007548 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007549 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7550 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007551 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007552 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007553 }
Jesse Barnes80824002009-09-10 15:28:06 -07007554
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007555 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007556
7557 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007558 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007559 intel_crtc->cursor_width = width;
7560 intel_crtc->cursor_height = height;
7561
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007562 if (intel_crtc->active)
7563 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007564
Jesse Barnes79e53942008-11-07 14:24:08 -08007565 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007566fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007567 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007568fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007569 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007570fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007571 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007572 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007573}
7574
7575static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7576{
Jesse Barnes79e53942008-11-07 14:24:08 -08007577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007578
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007579 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7580 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007581
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007582 if (intel_crtc->active)
7583 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007584
7585 return 0;
7586}
7587
Jesse Barnes79e53942008-11-07 14:24:08 -08007588static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007589 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007590{
James Simmons72034252010-08-03 01:33:19 +01007591 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007593
James Simmons72034252010-08-03 01:33:19 +01007594 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007595 intel_crtc->lut_r[i] = red[i] >> 8;
7596 intel_crtc->lut_g[i] = green[i] >> 8;
7597 intel_crtc->lut_b[i] = blue[i] >> 8;
7598 }
7599
7600 intel_crtc_load_lut(crtc);
7601}
7602
Jesse Barnes79e53942008-11-07 14:24:08 -08007603/* VESA 640x480x72Hz mode to set on the pipe */
7604static struct drm_display_mode load_detect_mode = {
7605 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7606 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7607};
7608
Chris Wilsond2dff872011-04-19 08:36:26 +01007609static struct drm_framebuffer *
7610intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007611 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007612 struct drm_i915_gem_object *obj)
7613{
7614 struct intel_framebuffer *intel_fb;
7615 int ret;
7616
7617 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7618 if (!intel_fb) {
7619 drm_gem_object_unreference_unlocked(&obj->base);
7620 return ERR_PTR(-ENOMEM);
7621 }
7622
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007623 ret = i915_mutex_lock_interruptible(dev);
7624 if (ret)
7625 goto err;
7626
Chris Wilsond2dff872011-04-19 08:36:26 +01007627 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007628 mutex_unlock(&dev->struct_mutex);
7629 if (ret)
7630 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007631
7632 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007633err:
7634 drm_gem_object_unreference_unlocked(&obj->base);
7635 kfree(intel_fb);
7636
7637 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007638}
7639
7640static u32
7641intel_framebuffer_pitch_for_width(int width, int bpp)
7642{
7643 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7644 return ALIGN(pitch, 64);
7645}
7646
7647static u32
7648intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7649{
7650 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7651 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7652}
7653
7654static struct drm_framebuffer *
7655intel_framebuffer_create_for_mode(struct drm_device *dev,
7656 struct drm_display_mode *mode,
7657 int depth, int bpp)
7658{
7659 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007660 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007661
7662 obj = i915_gem_alloc_object(dev,
7663 intel_framebuffer_size_for_mode(mode, bpp));
7664 if (obj == NULL)
7665 return ERR_PTR(-ENOMEM);
7666
7667 mode_cmd.width = mode->hdisplay;
7668 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007669 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7670 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007671 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007672
7673 return intel_framebuffer_create(dev, &mode_cmd, obj);
7674}
7675
7676static struct drm_framebuffer *
7677mode_fits_in_fbdev(struct drm_device *dev,
7678 struct drm_display_mode *mode)
7679{
Daniel Vetter4520f532013-10-09 09:18:51 +02007680#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007681 struct drm_i915_private *dev_priv = dev->dev_private;
7682 struct drm_i915_gem_object *obj;
7683 struct drm_framebuffer *fb;
7684
7685 if (dev_priv->fbdev == NULL)
7686 return NULL;
7687
7688 obj = dev_priv->fbdev->ifb.obj;
7689 if (obj == NULL)
7690 return NULL;
7691
7692 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007693 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7694 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007695 return NULL;
7696
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007697 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007698 return NULL;
7699
7700 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007701#else
7702 return NULL;
7703#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007704}
7705
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007706bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007707 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007708 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007709{
7710 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007711 struct intel_encoder *intel_encoder =
7712 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007713 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007714 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007715 struct drm_crtc *crtc = NULL;
7716 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007717 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007718 int i = -1;
7719
Chris Wilsond2dff872011-04-19 08:36:26 +01007720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7721 connector->base.id, drm_get_connector_name(connector),
7722 encoder->base.id, drm_get_encoder_name(encoder));
7723
Jesse Barnes79e53942008-11-07 14:24:08 -08007724 /*
7725 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007726 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007727 * - if the connector already has an assigned crtc, use it (but make
7728 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007729 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007730 * - try to find the first unused crtc that can drive this connector,
7731 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007732 */
7733
7734 /* See if we already have a CRTC for this connector */
7735 if (encoder->crtc) {
7736 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007737
Daniel Vetter7b240562012-12-12 00:35:33 +01007738 mutex_lock(&crtc->mutex);
7739
Daniel Vetter24218aa2012-08-12 19:27:11 +02007740 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007741 old->load_detect_temp = false;
7742
7743 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007744 if (connector->dpms != DRM_MODE_DPMS_ON)
7745 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007746
Chris Wilson71731882011-04-19 23:10:58 +01007747 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007748 }
7749
7750 /* Find an unused one (if possible) */
7751 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7752 i++;
7753 if (!(encoder->possible_crtcs & (1 << i)))
7754 continue;
7755 if (!possible_crtc->enabled) {
7756 crtc = possible_crtc;
7757 break;
7758 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007759 }
7760
7761 /*
7762 * If we didn't find an unused CRTC, don't use any.
7763 */
7764 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007765 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7766 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007767 }
7768
Daniel Vetter7b240562012-12-12 00:35:33 +01007769 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007770 intel_encoder->new_crtc = to_intel_crtc(crtc);
7771 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007772
7773 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007774 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007775 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007776 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007777
Chris Wilson64927112011-04-20 07:25:26 +01007778 if (!mode)
7779 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007780
Chris Wilsond2dff872011-04-19 08:36:26 +01007781 /* We need a framebuffer large enough to accommodate all accesses
7782 * that the plane may generate whilst we perform load detection.
7783 * We can not rely on the fbcon either being present (we get called
7784 * during its initialisation to detect all boot displays, or it may
7785 * not even exist) or that it is large enough to satisfy the
7786 * requested mode.
7787 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007788 fb = mode_fits_in_fbdev(dev, mode);
7789 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007790 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007791 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7792 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007793 } else
7794 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007795 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007796 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007797 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007798 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007799 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007800
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007801 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007802 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007803 if (old->release_fb)
7804 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007805 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007806 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007807 }
Chris Wilson71731882011-04-19 23:10:58 +01007808
Jesse Barnes79e53942008-11-07 14:24:08 -08007809 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007810 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007811 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007812}
7813
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007814void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007815 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007816{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007817 struct intel_encoder *intel_encoder =
7818 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007819 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007820 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007821
Chris Wilsond2dff872011-04-19 08:36:26 +01007822 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7823 connector->base.id, drm_get_connector_name(connector),
7824 encoder->base.id, drm_get_encoder_name(encoder));
7825
Chris Wilson8261b192011-04-19 23:18:09 +01007826 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007827 to_intel_connector(connector)->new_encoder = NULL;
7828 intel_encoder->new_crtc = NULL;
7829 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007830
Daniel Vetter36206362012-12-10 20:42:17 +01007831 if (old->release_fb) {
7832 drm_framebuffer_unregister_private(old->release_fb);
7833 drm_framebuffer_unreference(old->release_fb);
7834 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007835
Daniel Vetter67c96402013-01-23 16:25:09 +00007836 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007837 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007838 }
7839
Eric Anholtc751ce42010-03-25 11:48:48 -07007840 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007841 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7842 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007843
7844 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007845}
7846
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007847static int i9xx_pll_refclk(struct drm_device *dev,
7848 const struct intel_crtc_config *pipe_config)
7849{
7850 struct drm_i915_private *dev_priv = dev->dev_private;
7851 u32 dpll = pipe_config->dpll_hw_state.dpll;
7852
7853 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7854 return dev_priv->vbt.lvds_ssc_freq * 1000;
7855 else if (HAS_PCH_SPLIT(dev))
7856 return 120000;
7857 else if (!IS_GEN2(dev))
7858 return 96000;
7859 else
7860 return 48000;
7861}
7862
Jesse Barnes79e53942008-11-07 14:24:08 -08007863/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007864static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7865 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007866{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007867 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007868 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007869 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007870 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007871 u32 fp;
7872 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007873 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007874
7875 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007876 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007877 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007878 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007879
7880 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007881 if (IS_PINEVIEW(dev)) {
7882 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7883 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007884 } else {
7885 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7886 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7887 }
7888
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007889 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007890 if (IS_PINEVIEW(dev))
7891 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7892 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007893 else
7894 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007895 DPLL_FPA01_P1_POST_DIV_SHIFT);
7896
7897 switch (dpll & DPLL_MODE_MASK) {
7898 case DPLLB_MODE_DAC_SERIAL:
7899 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7900 5 : 10;
7901 break;
7902 case DPLLB_MODE_LVDS:
7903 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7904 7 : 14;
7905 break;
7906 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007907 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007908 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007909 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007910 }
7911
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007912 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007913 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007914 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007915 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007916 } else {
7917 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7918
7919 if (is_lvds) {
7920 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7921 DPLL_FPA01_P1_POST_DIV_SHIFT);
7922 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007923 } else {
7924 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7925 clock.p1 = 2;
7926 else {
7927 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7928 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7929 }
7930 if (dpll & PLL_P2_DIVIDE_BY_4)
7931 clock.p2 = 4;
7932 else
7933 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007934 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007935
7936 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007937 }
7938
Ville Syrjälä18442d02013-09-13 16:00:08 +03007939 /*
7940 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007941 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007942 * encoder's get_config() function.
7943 */
7944 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007945}
7946
Ville Syrjälä6878da02013-09-13 15:59:11 +03007947int intel_dotclock_calculate(int link_freq,
7948 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007949{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007950 /*
7951 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007952 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007953 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007954 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007955 *
7956 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007957 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007958 */
7959
Ville Syrjälä6878da02013-09-13 15:59:11 +03007960 if (!m_n->link_n)
7961 return 0;
7962
7963 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7964}
7965
Ville Syrjälä18442d02013-09-13 16:00:08 +03007966static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7967 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007968{
7969 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007970
7971 /* read out port_clock from the DPLL */
7972 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007973
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007974 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007975 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007976 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007977 * agree once we know their relationship in the encoder's
7978 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007979 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007980 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007981 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7982 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007983}
7984
7985/** Returns the currently programmed mode of the given pipe. */
7986struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7987 struct drm_crtc *crtc)
7988{
Jesse Barnes548f2452011-02-17 10:40:53 -08007989 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007991 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007992 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007993 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007994 int htot = I915_READ(HTOTAL(cpu_transcoder));
7995 int hsync = I915_READ(HSYNC(cpu_transcoder));
7996 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7997 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03007998 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08007999
8000 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8001 if (!mode)
8002 return NULL;
8003
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008004 /*
8005 * Construct a pipe_config sufficient for getting the clock info
8006 * back out of crtc_clock_get.
8007 *
8008 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8009 * to use a real value here instead.
8010 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008011 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008012 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008013 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8014 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8015 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008016 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8017
Ville Syrjälä773ae032013-09-23 17:48:20 +03008018 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008019 mode->hdisplay = (htot & 0xffff) + 1;
8020 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8021 mode->hsync_start = (hsync & 0xffff) + 1;
8022 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8023 mode->vdisplay = (vtot & 0xffff) + 1;
8024 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8025 mode->vsync_start = (vsync & 0xffff) + 1;
8026 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8027
8028 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008029
8030 return mode;
8031}
8032
Daniel Vetter3dec0092010-08-20 21:40:52 +02008033static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008034{
8035 struct drm_device *dev = crtc->dev;
8036 drm_i915_private_t *dev_priv = dev->dev_private;
8037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8038 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008039 int dpll_reg = DPLL(pipe);
8040 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008041
Eric Anholtbad720f2009-10-22 16:11:14 -07008042 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008043 return;
8044
8045 if (!dev_priv->lvds_downclock_avail)
8046 return;
8047
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008048 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008049 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008050 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008051
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008052 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008053
8054 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8055 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008056 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008057
Jesse Barnes652c3932009-08-17 13:31:43 -07008058 dpll = I915_READ(dpll_reg);
8059 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008060 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008061 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008062}
8063
8064static void intel_decrease_pllclock(struct drm_crtc *crtc)
8065{
8066 struct drm_device *dev = crtc->dev;
8067 drm_i915_private_t *dev_priv = dev->dev_private;
8068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008069
Eric Anholtbad720f2009-10-22 16:11:14 -07008070 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008071 return;
8072
8073 if (!dev_priv->lvds_downclock_avail)
8074 return;
8075
8076 /*
8077 * Since this is called by a timer, we should never get here in
8078 * the manual case.
8079 */
8080 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008081 int pipe = intel_crtc->pipe;
8082 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008083 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008084
Zhao Yakui44d98a62009-10-09 11:39:40 +08008085 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008086
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008087 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008088
Chris Wilson074b5e12012-05-02 12:07:06 +01008089 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008090 dpll |= DISPLAY_RATE_SELECT_FPA1;
8091 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008092 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008093 dpll = I915_READ(dpll_reg);
8094 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008095 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008096 }
8097
8098}
8099
Chris Wilsonf047e392012-07-21 12:31:41 +01008100void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008101{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008102 struct drm_i915_private *dev_priv = dev->dev_private;
8103
8104 hsw_package_c8_gpu_busy(dev_priv);
8105 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008106}
8107
8108void intel_mark_idle(struct drm_device *dev)
8109{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008110 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008111 struct drm_crtc *crtc;
8112
Paulo Zanonic67a4702013-08-19 13:18:09 -03008113 hsw_package_c8_gpu_idle(dev_priv);
8114
Chris Wilson725a5b52013-01-08 11:02:57 +00008115 if (!i915_powersave)
8116 return;
8117
8118 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8119 if (!crtc->fb)
8120 continue;
8121
8122 intel_decrease_pllclock(crtc);
8123 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008124
8125 if (dev_priv->info->gen >= 6)
8126 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008127}
8128
Chris Wilsonc65355b2013-06-06 16:53:41 -03008129void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8130 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008131{
8132 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008133 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008134
8135 if (!i915_powersave)
8136 return;
8137
Jesse Barnes652c3932009-08-17 13:31:43 -07008138 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008139 if (!crtc->fb)
8140 continue;
8141
Chris Wilsonc65355b2013-06-06 16:53:41 -03008142 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8143 continue;
8144
8145 intel_increase_pllclock(crtc);
8146 if (ring && intel_fbc_enabled(dev))
8147 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008148 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008149}
8150
Jesse Barnes79e53942008-11-07 14:24:08 -08008151static void intel_crtc_destroy(struct drm_crtc *crtc)
8152{
8153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008154 struct drm_device *dev = crtc->dev;
8155 struct intel_unpin_work *work;
8156 unsigned long flags;
8157
8158 spin_lock_irqsave(&dev->event_lock, flags);
8159 work = intel_crtc->unpin_work;
8160 intel_crtc->unpin_work = NULL;
8161 spin_unlock_irqrestore(&dev->event_lock, flags);
8162
8163 if (work) {
8164 cancel_work_sync(&work->work);
8165 kfree(work);
8166 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008167
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008168 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8169
Jesse Barnes79e53942008-11-07 14:24:08 -08008170 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008171
Jesse Barnes79e53942008-11-07 14:24:08 -08008172 kfree(intel_crtc);
8173}
8174
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008175static void intel_unpin_work_fn(struct work_struct *__work)
8176{
8177 struct intel_unpin_work *work =
8178 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008179 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008180
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008181 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008182 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008183 drm_gem_object_unreference(&work->pending_flip_obj->base);
8184 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008185
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008186 intel_update_fbc(dev);
8187 mutex_unlock(&dev->struct_mutex);
8188
8189 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8190 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8191
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008192 kfree(work);
8193}
8194
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008195static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008196 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008197{
8198 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008199 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8200 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008201 unsigned long flags;
8202
8203 /* Ignore early vblank irqs */
8204 if (intel_crtc == NULL)
8205 return;
8206
8207 spin_lock_irqsave(&dev->event_lock, flags);
8208 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008209
8210 /* Ensure we don't miss a work->pending update ... */
8211 smp_rmb();
8212
8213 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008214 spin_unlock_irqrestore(&dev->event_lock, flags);
8215 return;
8216 }
8217
Chris Wilsone7d841c2012-12-03 11:36:30 +00008218 /* and that the unpin work is consistent wrt ->pending. */
8219 smp_rmb();
8220
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008221 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008222
Rob Clark45a066e2012-10-08 14:50:40 -05008223 if (work->event)
8224 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008225
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008226 drm_vblank_put(dev, intel_crtc->pipe);
8227
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008228 spin_unlock_irqrestore(&dev->event_lock, flags);
8229
Daniel Vetter2c10d572012-12-20 21:24:07 +01008230 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008231
8232 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008233
8234 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008235}
8236
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008237void intel_finish_page_flip(struct drm_device *dev, int pipe)
8238{
8239 drm_i915_private_t *dev_priv = dev->dev_private;
8240 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8241
Mario Kleiner49b14a52010-12-09 07:00:07 +01008242 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008243}
8244
8245void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8246{
8247 drm_i915_private_t *dev_priv = dev->dev_private;
8248 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8249
Mario Kleiner49b14a52010-12-09 07:00:07 +01008250 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008251}
8252
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008253void intel_prepare_page_flip(struct drm_device *dev, int plane)
8254{
8255 drm_i915_private_t *dev_priv = dev->dev_private;
8256 struct intel_crtc *intel_crtc =
8257 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8258 unsigned long flags;
8259
Chris Wilsone7d841c2012-12-03 11:36:30 +00008260 /* NB: An MMIO update of the plane base pointer will also
8261 * generate a page-flip completion irq, i.e. every modeset
8262 * is also accompanied by a spurious intel_prepare_page_flip().
8263 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008264 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008265 if (intel_crtc->unpin_work)
8266 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008267 spin_unlock_irqrestore(&dev->event_lock, flags);
8268}
8269
Chris Wilsone7d841c2012-12-03 11:36:30 +00008270inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8271{
8272 /* Ensure that the work item is consistent when activating it ... */
8273 smp_wmb();
8274 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8275 /* and that it is marked active as soon as the irq could fire. */
8276 smp_wmb();
8277}
8278
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008279static int intel_gen2_queue_flip(struct drm_device *dev,
8280 struct drm_crtc *crtc,
8281 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008282 struct drm_i915_gem_object *obj,
8283 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008284{
8285 struct drm_i915_private *dev_priv = dev->dev_private;
8286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008287 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008288 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008289 int ret;
8290
Daniel Vetter6d90c952012-04-26 23:28:05 +02008291 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008292 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008293 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008294
Daniel Vetter6d90c952012-04-26 23:28:05 +02008295 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008296 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008297 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008298
8299 /* Can't queue multiple flips, so wait for the previous
8300 * one to finish before executing the next.
8301 */
8302 if (intel_crtc->plane)
8303 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8304 else
8305 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008306 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8307 intel_ring_emit(ring, MI_NOOP);
8308 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8309 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8310 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008311 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008312 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008313
8314 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008315 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008316 return 0;
8317
8318err_unpin:
8319 intel_unpin_fb_obj(obj);
8320err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008321 return ret;
8322}
8323
8324static int intel_gen3_queue_flip(struct drm_device *dev,
8325 struct drm_crtc *crtc,
8326 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008327 struct drm_i915_gem_object *obj,
8328 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008329{
8330 struct drm_i915_private *dev_priv = dev->dev_private;
8331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008332 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008333 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008334 int ret;
8335
Daniel Vetter6d90c952012-04-26 23:28:05 +02008336 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008337 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008338 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008339
Daniel Vetter6d90c952012-04-26 23:28:05 +02008340 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008341 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008342 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008343
8344 if (intel_crtc->plane)
8345 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8346 else
8347 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008348 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8349 intel_ring_emit(ring, MI_NOOP);
8350 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8351 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8352 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008353 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008354 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008355
Chris Wilsone7d841c2012-12-03 11:36:30 +00008356 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008357 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008358 return 0;
8359
8360err_unpin:
8361 intel_unpin_fb_obj(obj);
8362err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008363 return ret;
8364}
8365
8366static int intel_gen4_queue_flip(struct drm_device *dev,
8367 struct drm_crtc *crtc,
8368 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008369 struct drm_i915_gem_object *obj,
8370 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008371{
8372 struct drm_i915_private *dev_priv = dev->dev_private;
8373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8374 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008375 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008376 int ret;
8377
Daniel Vetter6d90c952012-04-26 23:28:05 +02008378 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008379 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008380 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008381
Daniel Vetter6d90c952012-04-26 23:28:05 +02008382 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008383 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008384 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008385
8386 /* i965+ uses the linear or tiled offsets from the
8387 * Display Registers (which do not change across a page-flip)
8388 * so we need only reprogram the base address.
8389 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008390 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8391 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8392 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008393 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008394 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008395 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008396
8397 /* XXX Enabling the panel-fitter across page-flip is so far
8398 * untested on non-native modes, so ignore it for now.
8399 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8400 */
8401 pf = 0;
8402 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008403 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008404
8405 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008406 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008407 return 0;
8408
8409err_unpin:
8410 intel_unpin_fb_obj(obj);
8411err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008412 return ret;
8413}
8414
8415static int intel_gen6_queue_flip(struct drm_device *dev,
8416 struct drm_crtc *crtc,
8417 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008418 struct drm_i915_gem_object *obj,
8419 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008420{
8421 struct drm_i915_private *dev_priv = dev->dev_private;
8422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008423 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008424 uint32_t pf, pipesrc;
8425 int ret;
8426
Daniel Vetter6d90c952012-04-26 23:28:05 +02008427 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008428 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008429 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008430
Daniel Vetter6d90c952012-04-26 23:28:05 +02008431 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008432 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008433 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008434
Daniel Vetter6d90c952012-04-26 23:28:05 +02008435 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8436 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8437 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008438 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008439
Chris Wilson99d9acd2012-04-17 20:37:00 +01008440 /* Contrary to the suggestions in the documentation,
8441 * "Enable Panel Fitter" does not seem to be required when page
8442 * flipping with a non-native mode, and worse causes a normal
8443 * modeset to fail.
8444 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8445 */
8446 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008447 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008448 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008449
8450 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008451 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008452 return 0;
8453
8454err_unpin:
8455 intel_unpin_fb_obj(obj);
8456err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008457 return ret;
8458}
8459
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008460static int intel_gen7_queue_flip(struct drm_device *dev,
8461 struct drm_crtc *crtc,
8462 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008463 struct drm_i915_gem_object *obj,
8464 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008465{
8466 struct drm_i915_private *dev_priv = dev->dev_private;
8467 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008468 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008469 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008470 int len, ret;
8471
8472 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008473 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008474 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008475
8476 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8477 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008478 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008479
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008480 switch(intel_crtc->plane) {
8481 case PLANE_A:
8482 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8483 break;
8484 case PLANE_B:
8485 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8486 break;
8487 case PLANE_C:
8488 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8489 break;
8490 default:
8491 WARN_ONCE(1, "unknown plane in flip command\n");
8492 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008493 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008494 }
8495
Chris Wilsonffe74d72013-08-26 20:58:12 +01008496 len = 4;
8497 if (ring->id == RCS)
8498 len += 6;
8499
8500 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008501 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008502 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008503
Chris Wilsonffe74d72013-08-26 20:58:12 +01008504 /* Unmask the flip-done completion message. Note that the bspec says that
8505 * we should do this for both the BCS and RCS, and that we must not unmask
8506 * more than one flip event at any time (or ensure that one flip message
8507 * can be sent by waiting for flip-done prior to queueing new flips).
8508 * Experimentation says that BCS works despite DERRMR masking all
8509 * flip-done completion events and that unmasking all planes at once
8510 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8511 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8512 */
8513 if (ring->id == RCS) {
8514 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8515 intel_ring_emit(ring, DERRMR);
8516 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8517 DERRMR_PIPEB_PRI_FLIP_DONE |
8518 DERRMR_PIPEC_PRI_FLIP_DONE));
8519 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8520 intel_ring_emit(ring, DERRMR);
8521 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8522 }
8523
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008524 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008525 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008526 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008527 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008528
8529 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008530 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008531 return 0;
8532
8533err_unpin:
8534 intel_unpin_fb_obj(obj);
8535err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008536 return ret;
8537}
8538
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008539static int intel_default_queue_flip(struct drm_device *dev,
8540 struct drm_crtc *crtc,
8541 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008542 struct drm_i915_gem_object *obj,
8543 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008544{
8545 return -ENODEV;
8546}
8547
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008548static int intel_crtc_page_flip(struct drm_crtc *crtc,
8549 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008550 struct drm_pending_vblank_event *event,
8551 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008552{
8553 struct drm_device *dev = crtc->dev;
8554 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008555 struct drm_framebuffer *old_fb = crtc->fb;
8556 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8558 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008559 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008560 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008561
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008562 /* Can't change pixel format via MI display flips. */
8563 if (fb->pixel_format != crtc->fb->pixel_format)
8564 return -EINVAL;
8565
8566 /*
8567 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8568 * Note that pitch changes could also affect these register.
8569 */
8570 if (INTEL_INFO(dev)->gen > 3 &&
8571 (fb->offsets[0] != crtc->fb->offsets[0] ||
8572 fb->pitches[0] != crtc->fb->pitches[0]))
8573 return -EINVAL;
8574
Daniel Vetterb14c5672013-09-19 12:18:32 +02008575 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008576 if (work == NULL)
8577 return -ENOMEM;
8578
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008579 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008580 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008581 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008582 INIT_WORK(&work->work, intel_unpin_work_fn);
8583
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008584 ret = drm_vblank_get(dev, intel_crtc->pipe);
8585 if (ret)
8586 goto free_work;
8587
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008588 /* We borrow the event spin lock for protecting unpin_work */
8589 spin_lock_irqsave(&dev->event_lock, flags);
8590 if (intel_crtc->unpin_work) {
8591 spin_unlock_irqrestore(&dev->event_lock, flags);
8592 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008593 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008594
8595 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008596 return -EBUSY;
8597 }
8598 intel_crtc->unpin_work = work;
8599 spin_unlock_irqrestore(&dev->event_lock, flags);
8600
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008601 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8602 flush_workqueue(dev_priv->wq);
8603
Chris Wilson79158102012-05-23 11:13:58 +01008604 ret = i915_mutex_lock_interruptible(dev);
8605 if (ret)
8606 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008607
Jesse Barnes75dfca82010-02-10 15:09:44 -08008608 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008609 drm_gem_object_reference(&work->old_fb_obj->base);
8610 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008611
8612 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008613
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008614 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008615
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008616 work->enable_stall_check = true;
8617
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008618 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008619 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008620
Keith Packarded8d1972013-07-22 18:49:58 -07008621 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008622 if (ret)
8623 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008624
Chris Wilson7782de32011-07-08 12:22:41 +01008625 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008626 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008627 mutex_unlock(&dev->struct_mutex);
8628
Jesse Barnese5510fa2010-07-01 16:48:37 -07008629 trace_i915_flip_request(intel_crtc->plane, obj);
8630
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008631 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008632
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008633cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008634 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008635 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008636 drm_gem_object_unreference(&work->old_fb_obj->base);
8637 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008638 mutex_unlock(&dev->struct_mutex);
8639
Chris Wilson79158102012-05-23 11:13:58 +01008640cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008641 spin_lock_irqsave(&dev->event_lock, flags);
8642 intel_crtc->unpin_work = NULL;
8643 spin_unlock_irqrestore(&dev->event_lock, flags);
8644
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008645 drm_vblank_put(dev, intel_crtc->pipe);
8646free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008647 kfree(work);
8648
8649 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008650}
8651
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008652static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008653 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8654 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008655};
8656
Daniel Vetter50f56112012-07-02 09:35:43 +02008657static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8658 struct drm_crtc *crtc)
8659{
8660 struct drm_device *dev;
8661 struct drm_crtc *tmp;
8662 int crtc_mask = 1;
8663
8664 WARN(!crtc, "checking null crtc?\n");
8665
8666 dev = crtc->dev;
8667
8668 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8669 if (tmp == crtc)
8670 break;
8671 crtc_mask <<= 1;
8672 }
8673
8674 if (encoder->possible_crtcs & crtc_mask)
8675 return true;
8676 return false;
8677}
8678
Daniel Vetter9a935852012-07-05 22:34:27 +02008679/**
8680 * intel_modeset_update_staged_output_state
8681 *
8682 * Updates the staged output configuration state, e.g. after we've read out the
8683 * current hw state.
8684 */
8685static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8686{
8687 struct intel_encoder *encoder;
8688 struct intel_connector *connector;
8689
8690 list_for_each_entry(connector, &dev->mode_config.connector_list,
8691 base.head) {
8692 connector->new_encoder =
8693 to_intel_encoder(connector->base.encoder);
8694 }
8695
8696 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8697 base.head) {
8698 encoder->new_crtc =
8699 to_intel_crtc(encoder->base.crtc);
8700 }
8701}
8702
8703/**
8704 * intel_modeset_commit_output_state
8705 *
8706 * This function copies the stage display pipe configuration to the real one.
8707 */
8708static void intel_modeset_commit_output_state(struct drm_device *dev)
8709{
8710 struct intel_encoder *encoder;
8711 struct intel_connector *connector;
8712
8713 list_for_each_entry(connector, &dev->mode_config.connector_list,
8714 base.head) {
8715 connector->base.encoder = &connector->new_encoder->base;
8716 }
8717
8718 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8719 base.head) {
8720 encoder->base.crtc = &encoder->new_crtc->base;
8721 }
8722}
8723
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008724static void
8725connected_sink_compute_bpp(struct intel_connector * connector,
8726 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008727{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008728 int bpp = pipe_config->pipe_bpp;
8729
8730 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8731 connector->base.base.id,
8732 drm_get_connector_name(&connector->base));
8733
8734 /* Don't use an invalid EDID bpc value */
8735 if (connector->base.display_info.bpc &&
8736 connector->base.display_info.bpc * 3 < bpp) {
8737 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8738 bpp, connector->base.display_info.bpc*3);
8739 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8740 }
8741
8742 /* Clamp bpp to 8 on screens without EDID 1.4 */
8743 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8744 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8745 bpp);
8746 pipe_config->pipe_bpp = 24;
8747 }
8748}
8749
8750static int
8751compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8752 struct drm_framebuffer *fb,
8753 struct intel_crtc_config *pipe_config)
8754{
8755 struct drm_device *dev = crtc->base.dev;
8756 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008757 int bpp;
8758
Daniel Vetterd42264b2013-03-28 16:38:08 +01008759 switch (fb->pixel_format) {
8760 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008761 bpp = 8*3; /* since we go through a colormap */
8762 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008763 case DRM_FORMAT_XRGB1555:
8764 case DRM_FORMAT_ARGB1555:
8765 /* checked in intel_framebuffer_init already */
8766 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8767 return -EINVAL;
8768 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008769 bpp = 6*3; /* min is 18bpp */
8770 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008771 case DRM_FORMAT_XBGR8888:
8772 case DRM_FORMAT_ABGR8888:
8773 /* checked in intel_framebuffer_init already */
8774 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8775 return -EINVAL;
8776 case DRM_FORMAT_XRGB8888:
8777 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008778 bpp = 8*3;
8779 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008780 case DRM_FORMAT_XRGB2101010:
8781 case DRM_FORMAT_ARGB2101010:
8782 case DRM_FORMAT_XBGR2101010:
8783 case DRM_FORMAT_ABGR2101010:
8784 /* checked in intel_framebuffer_init already */
8785 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008786 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008787 bpp = 10*3;
8788 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008789 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008790 default:
8791 DRM_DEBUG_KMS("unsupported depth\n");
8792 return -EINVAL;
8793 }
8794
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008795 pipe_config->pipe_bpp = bpp;
8796
8797 /* Clamp display bpp to EDID value */
8798 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008799 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008800 if (!connector->new_encoder ||
8801 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008802 continue;
8803
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008804 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008805 }
8806
8807 return bpp;
8808}
8809
Daniel Vetter644db712013-09-19 14:53:58 +02008810static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8811{
8812 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8813 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008814 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008815 mode->crtc_hdisplay, mode->crtc_hsync_start,
8816 mode->crtc_hsync_end, mode->crtc_htotal,
8817 mode->crtc_vdisplay, mode->crtc_vsync_start,
8818 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8819}
8820
Daniel Vetterc0b03412013-05-28 12:05:54 +02008821static void intel_dump_pipe_config(struct intel_crtc *crtc,
8822 struct intel_crtc_config *pipe_config,
8823 const char *context)
8824{
8825 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8826 context, pipe_name(crtc->pipe));
8827
8828 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8829 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8830 pipe_config->pipe_bpp, pipe_config->dither);
8831 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8832 pipe_config->has_pch_encoder,
8833 pipe_config->fdi_lanes,
8834 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8835 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8836 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008837 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8838 pipe_config->has_dp_encoder,
8839 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8840 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8841 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008842 DRM_DEBUG_KMS("requested mode:\n");
8843 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8844 DRM_DEBUG_KMS("adjusted mode:\n");
8845 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008846 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008847 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008848 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8849 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008850 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8851 pipe_config->gmch_pfit.control,
8852 pipe_config->gmch_pfit.pgm_ratios,
8853 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008854 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008855 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008856 pipe_config->pch_pfit.size,
8857 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008858 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008859 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008860}
8861
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008862static bool check_encoder_cloning(struct drm_crtc *crtc)
8863{
8864 int num_encoders = 0;
8865 bool uncloneable_encoders = false;
8866 struct intel_encoder *encoder;
8867
8868 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8869 base.head) {
8870 if (&encoder->new_crtc->base != crtc)
8871 continue;
8872
8873 num_encoders++;
8874 if (!encoder->cloneable)
8875 uncloneable_encoders = true;
8876 }
8877
8878 return !(num_encoders > 1 && uncloneable_encoders);
8879}
8880
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008881static struct intel_crtc_config *
8882intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008883 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008884 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008885{
8886 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008887 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008888 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008889 int plane_bpp, ret = -EINVAL;
8890 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008891
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008892 if (!check_encoder_cloning(crtc)) {
8893 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8894 return ERR_PTR(-EINVAL);
8895 }
8896
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008897 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8898 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008899 return ERR_PTR(-ENOMEM);
8900
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008901 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8902 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008903
Daniel Vettere143a212013-07-04 12:01:15 +02008904 pipe_config->cpu_transcoder =
8905 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008906 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008907
Imre Deak2960bc92013-07-30 13:36:32 +03008908 /*
8909 * Sanitize sync polarity flags based on requested ones. If neither
8910 * positive or negative polarity is requested, treat this as meaning
8911 * negative polarity.
8912 */
8913 if (!(pipe_config->adjusted_mode.flags &
8914 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8915 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8916
8917 if (!(pipe_config->adjusted_mode.flags &
8918 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8919 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8920
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008921 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8922 * plane pixel format and any sink constraints into account. Returns the
8923 * source plane bpp so that dithering can be selected on mismatches
8924 * after encoders and crtc also have had their say. */
8925 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8926 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008927 if (plane_bpp < 0)
8928 goto fail;
8929
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008930 /*
8931 * Determine the real pipe dimensions. Note that stereo modes can
8932 * increase the actual pipe size due to the frame doubling and
8933 * insertion of additional space for blanks between the frame. This
8934 * is stored in the crtc timings. We use the requested mode to do this
8935 * computation to clearly distinguish it from the adjusted mode, which
8936 * can be changed by the connectors in the below retry loop.
8937 */
8938 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8939 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8940 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8941
Daniel Vettere29c22c2013-02-21 00:00:16 +01008942encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008943 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008944 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008945 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008946
Daniel Vetter135c81b2013-07-21 21:37:09 +02008947 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008948 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008949
Daniel Vetter7758a112012-07-08 19:40:39 +02008950 /* Pass our mode to the connectors and the CRTC to give them a chance to
8951 * adjust it according to limitations or connector properties, and also
8952 * a chance to reject the mode entirely.
8953 */
8954 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8955 base.head) {
8956
8957 if (&encoder->new_crtc->base != crtc)
8958 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008959
Daniel Vetterefea6e82013-07-21 21:36:59 +02008960 if (!(encoder->compute_config(encoder, pipe_config))) {
8961 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008962 goto fail;
8963 }
8964 }
8965
Daniel Vetterff9a6752013-06-01 17:16:21 +02008966 /* Set default port clock if not overwritten by the encoder. Needs to be
8967 * done afterwards in case the encoder adjusts the mode. */
8968 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008969 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8970 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008971
Daniel Vettera43f6e02013-06-07 23:10:32 +02008972 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008973 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008974 DRM_DEBUG_KMS("CRTC fixup failed\n");
8975 goto fail;
8976 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008977
8978 if (ret == RETRY) {
8979 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8980 ret = -EINVAL;
8981 goto fail;
8982 }
8983
8984 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8985 retry = false;
8986 goto encoder_retry;
8987 }
8988
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008989 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8990 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8991 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8992
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008993 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008994fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008995 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008996 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008997}
8998
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008999/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9000 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9001static void
9002intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9003 unsigned *prepare_pipes, unsigned *disable_pipes)
9004{
9005 struct intel_crtc *intel_crtc;
9006 struct drm_device *dev = crtc->dev;
9007 struct intel_encoder *encoder;
9008 struct intel_connector *connector;
9009 struct drm_crtc *tmp_crtc;
9010
9011 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9012
9013 /* Check which crtcs have changed outputs connected to them, these need
9014 * to be part of the prepare_pipes mask. We don't (yet) support global
9015 * modeset across multiple crtcs, so modeset_pipes will only have one
9016 * bit set at most. */
9017 list_for_each_entry(connector, &dev->mode_config.connector_list,
9018 base.head) {
9019 if (connector->base.encoder == &connector->new_encoder->base)
9020 continue;
9021
9022 if (connector->base.encoder) {
9023 tmp_crtc = connector->base.encoder->crtc;
9024
9025 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9026 }
9027
9028 if (connector->new_encoder)
9029 *prepare_pipes |=
9030 1 << connector->new_encoder->new_crtc->pipe;
9031 }
9032
9033 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9034 base.head) {
9035 if (encoder->base.crtc == &encoder->new_crtc->base)
9036 continue;
9037
9038 if (encoder->base.crtc) {
9039 tmp_crtc = encoder->base.crtc;
9040
9041 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9042 }
9043
9044 if (encoder->new_crtc)
9045 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9046 }
9047
9048 /* Check for any pipes that will be fully disabled ... */
9049 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9050 base.head) {
9051 bool used = false;
9052
9053 /* Don't try to disable disabled crtcs. */
9054 if (!intel_crtc->base.enabled)
9055 continue;
9056
9057 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9058 base.head) {
9059 if (encoder->new_crtc == intel_crtc)
9060 used = true;
9061 }
9062
9063 if (!used)
9064 *disable_pipes |= 1 << intel_crtc->pipe;
9065 }
9066
9067
9068 /* set_mode is also used to update properties on life display pipes. */
9069 intel_crtc = to_intel_crtc(crtc);
9070 if (crtc->enabled)
9071 *prepare_pipes |= 1 << intel_crtc->pipe;
9072
Daniel Vetterb6c51642013-04-12 18:48:43 +02009073 /*
9074 * For simplicity do a full modeset on any pipe where the output routing
9075 * changed. We could be more clever, but that would require us to be
9076 * more careful with calling the relevant encoder->mode_set functions.
9077 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009078 if (*prepare_pipes)
9079 *modeset_pipes = *prepare_pipes;
9080
9081 /* ... and mask these out. */
9082 *modeset_pipes &= ~(*disable_pipes);
9083 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009084
9085 /*
9086 * HACK: We don't (yet) fully support global modesets. intel_set_config
9087 * obies this rule, but the modeset restore mode of
9088 * intel_modeset_setup_hw_state does not.
9089 */
9090 *modeset_pipes &= 1 << intel_crtc->pipe;
9091 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009092
9093 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9094 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009095}
9096
Daniel Vetterea9d7582012-07-10 10:42:52 +02009097static bool intel_crtc_in_use(struct drm_crtc *crtc)
9098{
9099 struct drm_encoder *encoder;
9100 struct drm_device *dev = crtc->dev;
9101
9102 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9103 if (encoder->crtc == crtc)
9104 return true;
9105
9106 return false;
9107}
9108
9109static void
9110intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9111{
9112 struct intel_encoder *intel_encoder;
9113 struct intel_crtc *intel_crtc;
9114 struct drm_connector *connector;
9115
9116 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9117 base.head) {
9118 if (!intel_encoder->base.crtc)
9119 continue;
9120
9121 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9122
9123 if (prepare_pipes & (1 << intel_crtc->pipe))
9124 intel_encoder->connectors_active = false;
9125 }
9126
9127 intel_modeset_commit_output_state(dev);
9128
9129 /* Update computed state. */
9130 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9131 base.head) {
9132 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9133 }
9134
9135 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9136 if (!connector->encoder || !connector->encoder->crtc)
9137 continue;
9138
9139 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9140
9141 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009142 struct drm_property *dpms_property =
9143 dev->mode_config.dpms_property;
9144
Daniel Vetterea9d7582012-07-10 10:42:52 +02009145 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009146 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009147 dpms_property,
9148 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009149
9150 intel_encoder = to_intel_encoder(connector->encoder);
9151 intel_encoder->connectors_active = true;
9152 }
9153 }
9154
9155}
9156
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009157static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009158{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009159 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009160
9161 if (clock1 == clock2)
9162 return true;
9163
9164 if (!clock1 || !clock2)
9165 return false;
9166
9167 diff = abs(clock1 - clock2);
9168
9169 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9170 return true;
9171
9172 return false;
9173}
9174
Daniel Vetter25c5b262012-07-08 22:08:04 +02009175#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9176 list_for_each_entry((intel_crtc), \
9177 &(dev)->mode_config.crtc_list, \
9178 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009179 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009180
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009181static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009182intel_pipe_config_compare(struct drm_device *dev,
9183 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009184 struct intel_crtc_config *pipe_config)
9185{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009186#define PIPE_CONF_CHECK_X(name) \
9187 if (current_config->name != pipe_config->name) { \
9188 DRM_ERROR("mismatch in " #name " " \
9189 "(expected 0x%08x, found 0x%08x)\n", \
9190 current_config->name, \
9191 pipe_config->name); \
9192 return false; \
9193 }
9194
Daniel Vetter08a24032013-04-19 11:25:34 +02009195#define PIPE_CONF_CHECK_I(name) \
9196 if (current_config->name != pipe_config->name) { \
9197 DRM_ERROR("mismatch in " #name " " \
9198 "(expected %i, found %i)\n", \
9199 current_config->name, \
9200 pipe_config->name); \
9201 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009202 }
9203
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009204#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9205 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009206 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009207 "(expected %i, found %i)\n", \
9208 current_config->name & (mask), \
9209 pipe_config->name & (mask)); \
9210 return false; \
9211 }
9212
Ville Syrjälä5e550652013-09-06 23:29:07 +03009213#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9214 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9215 DRM_ERROR("mismatch in " #name " " \
9216 "(expected %i, found %i)\n", \
9217 current_config->name, \
9218 pipe_config->name); \
9219 return false; \
9220 }
9221
Daniel Vetterbb760062013-06-06 14:55:52 +02009222#define PIPE_CONF_QUIRK(quirk) \
9223 ((current_config->quirks | pipe_config->quirks) & (quirk))
9224
Daniel Vettereccb1402013-05-22 00:50:22 +02009225 PIPE_CONF_CHECK_I(cpu_transcoder);
9226
Daniel Vetter08a24032013-04-19 11:25:34 +02009227 PIPE_CONF_CHECK_I(has_pch_encoder);
9228 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009229 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9230 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9231 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9232 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9233 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009234
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009235 PIPE_CONF_CHECK_I(has_dp_encoder);
9236 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9237 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9238 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9239 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9240 PIPE_CONF_CHECK_I(dp_m_n.tu);
9241
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009242 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9243 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9244 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9245 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9246 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9247 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9248
9249 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9250 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9251 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9252 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9253 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9254 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9255
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009256 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009257
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009258 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9259 DRM_MODE_FLAG_INTERLACE);
9260
Daniel Vetterbb760062013-06-06 14:55:52 +02009261 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9262 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9263 DRM_MODE_FLAG_PHSYNC);
9264 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9265 DRM_MODE_FLAG_NHSYNC);
9266 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9267 DRM_MODE_FLAG_PVSYNC);
9268 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9269 DRM_MODE_FLAG_NVSYNC);
9270 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009271
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009272 PIPE_CONF_CHECK_I(pipe_src_w);
9273 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009274
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009275 PIPE_CONF_CHECK_I(gmch_pfit.control);
9276 /* pfit ratios are autocomputed by the hw on gen4+ */
9277 if (INTEL_INFO(dev)->gen < 4)
9278 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9279 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009280 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9281 if (current_config->pch_pfit.enabled) {
9282 PIPE_CONF_CHECK_I(pch_pfit.pos);
9283 PIPE_CONF_CHECK_I(pch_pfit.size);
9284 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009285
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009286 PIPE_CONF_CHECK_I(ips_enabled);
9287
Ville Syrjälä282740f2013-09-04 18:30:03 +03009288 PIPE_CONF_CHECK_I(double_wide);
9289
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009290 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009291 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009292 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009293 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9294 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009295
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009296 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9297 PIPE_CONF_CHECK_I(pipe_bpp);
9298
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009299 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01009300 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009301 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9302 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03009303
Daniel Vetter66e985c2013-06-05 13:34:20 +02009304#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009305#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009306#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009307#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009308#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009309
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009310 return true;
9311}
9312
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009313static void
9314check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009315{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009316 struct intel_connector *connector;
9317
9318 list_for_each_entry(connector, &dev->mode_config.connector_list,
9319 base.head) {
9320 /* This also checks the encoder/connector hw state with the
9321 * ->get_hw_state callbacks. */
9322 intel_connector_check_state(connector);
9323
9324 WARN(&connector->new_encoder->base != connector->base.encoder,
9325 "connector's staged encoder doesn't match current encoder\n");
9326 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009327}
9328
9329static void
9330check_encoder_state(struct drm_device *dev)
9331{
9332 struct intel_encoder *encoder;
9333 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009334
9335 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9336 base.head) {
9337 bool enabled = false;
9338 bool active = false;
9339 enum pipe pipe, tracked_pipe;
9340
9341 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9342 encoder->base.base.id,
9343 drm_get_encoder_name(&encoder->base));
9344
9345 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9346 "encoder's stage crtc doesn't match current crtc\n");
9347 WARN(encoder->connectors_active && !encoder->base.crtc,
9348 "encoder's active_connectors set, but no crtc\n");
9349
9350 list_for_each_entry(connector, &dev->mode_config.connector_list,
9351 base.head) {
9352 if (connector->base.encoder != &encoder->base)
9353 continue;
9354 enabled = true;
9355 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9356 active = true;
9357 }
9358 WARN(!!encoder->base.crtc != enabled,
9359 "encoder's enabled state mismatch "
9360 "(expected %i, found %i)\n",
9361 !!encoder->base.crtc, enabled);
9362 WARN(active && !encoder->base.crtc,
9363 "active encoder with no crtc\n");
9364
9365 WARN(encoder->connectors_active != active,
9366 "encoder's computed active state doesn't match tracked active state "
9367 "(expected %i, found %i)\n", active, encoder->connectors_active);
9368
9369 active = encoder->get_hw_state(encoder, &pipe);
9370 WARN(active != encoder->connectors_active,
9371 "encoder's hw state doesn't match sw tracking "
9372 "(expected %i, found %i)\n",
9373 encoder->connectors_active, active);
9374
9375 if (!encoder->base.crtc)
9376 continue;
9377
9378 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9379 WARN(active && pipe != tracked_pipe,
9380 "active encoder's pipe doesn't match"
9381 "(expected %i, found %i)\n",
9382 tracked_pipe, pipe);
9383
9384 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009385}
9386
9387static void
9388check_crtc_state(struct drm_device *dev)
9389{
9390 drm_i915_private_t *dev_priv = dev->dev_private;
9391 struct intel_crtc *crtc;
9392 struct intel_encoder *encoder;
9393 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009394
9395 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9396 base.head) {
9397 bool enabled = false;
9398 bool active = false;
9399
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009400 memset(&pipe_config, 0, sizeof(pipe_config));
9401
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009402 DRM_DEBUG_KMS("[CRTC:%d]\n",
9403 crtc->base.base.id);
9404
9405 WARN(crtc->active && !crtc->base.enabled,
9406 "active crtc, but not enabled in sw tracking\n");
9407
9408 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9409 base.head) {
9410 if (encoder->base.crtc != &crtc->base)
9411 continue;
9412 enabled = true;
9413 if (encoder->connectors_active)
9414 active = true;
9415 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009416
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009417 WARN(active != crtc->active,
9418 "crtc's computed active state doesn't match tracked active state "
9419 "(expected %i, found %i)\n", active, crtc->active);
9420 WARN(enabled != crtc->base.enabled,
9421 "crtc's computed enabled state doesn't match tracked enabled state "
9422 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9423
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009424 active = dev_priv->display.get_pipe_config(crtc,
9425 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009426
9427 /* hw state is inconsistent with the pipe A quirk */
9428 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9429 active = crtc->active;
9430
Daniel Vetter6c49f242013-06-06 12:45:25 +02009431 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9432 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009433 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009434 if (encoder->base.crtc != &crtc->base)
9435 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009436 if (encoder->get_config &&
9437 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009438 encoder->get_config(encoder, &pipe_config);
9439 }
9440
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009441 WARN(crtc->active != active,
9442 "crtc active state doesn't match with hw state "
9443 "(expected %i, found %i)\n", crtc->active, active);
9444
Daniel Vetterc0b03412013-05-28 12:05:54 +02009445 if (active &&
9446 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9447 WARN(1, "pipe state doesn't match!\n");
9448 intel_dump_pipe_config(crtc, &pipe_config,
9449 "[hw state]");
9450 intel_dump_pipe_config(crtc, &crtc->config,
9451 "[sw state]");
9452 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009453 }
9454}
9455
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009456static void
9457check_shared_dpll_state(struct drm_device *dev)
9458{
9459 drm_i915_private_t *dev_priv = dev->dev_private;
9460 struct intel_crtc *crtc;
9461 struct intel_dpll_hw_state dpll_hw_state;
9462 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009463
9464 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9465 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9466 int enabled_crtcs = 0, active_crtcs = 0;
9467 bool active;
9468
9469 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9470
9471 DRM_DEBUG_KMS("%s\n", pll->name);
9472
9473 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9474
9475 WARN(pll->active > pll->refcount,
9476 "more active pll users than references: %i vs %i\n",
9477 pll->active, pll->refcount);
9478 WARN(pll->active && !pll->on,
9479 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009480 WARN(pll->on && !pll->active,
9481 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009482 WARN(pll->on != active,
9483 "pll on state mismatch (expected %i, found %i)\n",
9484 pll->on, active);
9485
9486 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9487 base.head) {
9488 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9489 enabled_crtcs++;
9490 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9491 active_crtcs++;
9492 }
9493 WARN(pll->active != active_crtcs,
9494 "pll active crtcs mismatch (expected %i, found %i)\n",
9495 pll->active, active_crtcs);
9496 WARN(pll->refcount != enabled_crtcs,
9497 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9498 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009499
9500 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9501 sizeof(dpll_hw_state)),
9502 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009503 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009504}
9505
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009506void
9507intel_modeset_check_state(struct drm_device *dev)
9508{
9509 check_connector_state(dev);
9510 check_encoder_state(dev);
9511 check_crtc_state(dev);
9512 check_shared_dpll_state(dev);
9513}
9514
Ville Syrjälä18442d02013-09-13 16:00:08 +03009515void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9516 int dotclock)
9517{
9518 /*
9519 * FDI already provided one idea for the dotclock.
9520 * Yell if the encoder disagrees.
9521 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009522 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009523 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009524 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009525}
9526
Daniel Vetterf30da182013-04-11 20:22:50 +02009527static int __intel_set_mode(struct drm_crtc *crtc,
9528 struct drm_display_mode *mode,
9529 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009530{
9531 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009532 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009533 struct drm_display_mode *saved_mode, *saved_hwmode;
9534 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009535 struct intel_crtc *intel_crtc;
9536 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009537 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009538
Daniel Vettera1e22652013-09-21 00:35:38 +02009539 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009540 if (!saved_mode)
9541 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009542 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009543
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009544 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009545 &prepare_pipes, &disable_pipes);
9546
Tim Gardner3ac18232012-12-07 07:54:26 -07009547 *saved_hwmode = crtc->hwmode;
9548 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009549
Daniel Vetter25c5b262012-07-08 22:08:04 +02009550 /* Hack: Because we don't (yet) support global modeset on multiple
9551 * crtcs, we don't keep track of the new mode for more than one crtc.
9552 * Hence simply check whether any bit is set in modeset_pipes in all the
9553 * pieces of code that are not yet converted to deal with mutliple crtcs
9554 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009555 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009556 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009557 if (IS_ERR(pipe_config)) {
9558 ret = PTR_ERR(pipe_config);
9559 pipe_config = NULL;
9560
Tim Gardner3ac18232012-12-07 07:54:26 -07009561 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009562 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009563 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9564 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009565 }
9566
Jesse Barnes30a970c2013-11-04 13:48:12 -08009567 /*
9568 * See if the config requires any additional preparation, e.g.
9569 * to adjust global state with pipes off. We need to do this
9570 * here so we can get the modeset_pipe updated config for the new
9571 * mode set on this crtc. For other crtcs we need to use the
9572 * adjusted_mode bits in the crtc directly.
9573 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009574 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08009575 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9576 modeset_pipes, pipe_config);
9577
Ville Syrjäläc164f832013-11-05 22:34:12 +02009578 /* may have added more to prepare_pipes than we should */
9579 prepare_pipes &= ~disable_pipes;
9580 }
9581
Daniel Vetter460da9162013-03-27 00:44:51 +01009582 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9583 intel_crtc_disable(&intel_crtc->base);
9584
Daniel Vetterea9d7582012-07-10 10:42:52 +02009585 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9586 if (intel_crtc->base.enabled)
9587 dev_priv->display.crtc_disable(&intel_crtc->base);
9588 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009589
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009590 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9591 * to set it here already despite that we pass it down the callchain.
9592 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009593 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009594 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009595 /* mode_set/enable/disable functions rely on a correct pipe
9596 * config. */
9597 to_intel_crtc(crtc)->config = *pipe_config;
9598 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009599
Daniel Vetterea9d7582012-07-10 10:42:52 +02009600 /* Only after disabling all output pipelines that will be changed can we
9601 * update the the output configuration. */
9602 intel_modeset_update_state(dev, prepare_pipes);
9603
Daniel Vetter47fab732012-10-26 10:58:18 +02009604 if (dev_priv->display.modeset_global_resources)
9605 dev_priv->display.modeset_global_resources(dev);
9606
Daniel Vettera6778b32012-07-02 09:56:42 +02009607 /* Set up the DPLL and any encoders state that needs to adjust or depend
9608 * on the DPLL.
9609 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009610 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009611 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009612 x, y, fb);
9613 if (ret)
9614 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009615 }
9616
9617 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009618 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9619 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009620
Daniel Vetter25c5b262012-07-08 22:08:04 +02009621 if (modeset_pipes) {
9622 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009623 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009624
Daniel Vetter25c5b262012-07-08 22:08:04 +02009625 /* Calculate and store various constants which
9626 * are later needed by vblank and swap-completion
9627 * timestamping. They are derived from true hwmode.
9628 */
9629 drm_calc_timestamping_constants(crtc);
9630 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009631
9632 /* FIXME: add subpixel order */
9633done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009634 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009635 crtc->hwmode = *saved_hwmode;
9636 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009637 }
9638
Tim Gardner3ac18232012-12-07 07:54:26 -07009639out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009640 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009641 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009642 return ret;
9643}
9644
Damien Lespiaue7457a92013-08-08 22:28:59 +01009645static int intel_set_mode(struct drm_crtc *crtc,
9646 struct drm_display_mode *mode,
9647 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009648{
9649 int ret;
9650
9651 ret = __intel_set_mode(crtc, mode, x, y, fb);
9652
9653 if (ret == 0)
9654 intel_modeset_check_state(crtc->dev);
9655
9656 return ret;
9657}
9658
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009659void intel_crtc_restore_mode(struct drm_crtc *crtc)
9660{
9661 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9662}
9663
Daniel Vetter25c5b262012-07-08 22:08:04 +02009664#undef for_each_intel_crtc_masked
9665
Daniel Vetterd9e55602012-07-04 22:16:09 +02009666static void intel_set_config_free(struct intel_set_config *config)
9667{
9668 if (!config)
9669 return;
9670
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009671 kfree(config->save_connector_encoders);
9672 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009673 kfree(config);
9674}
9675
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009676static int intel_set_config_save_state(struct drm_device *dev,
9677 struct intel_set_config *config)
9678{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009679 struct drm_encoder *encoder;
9680 struct drm_connector *connector;
9681 int count;
9682
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009683 config->save_encoder_crtcs =
9684 kcalloc(dev->mode_config.num_encoder,
9685 sizeof(struct drm_crtc *), GFP_KERNEL);
9686 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009687 return -ENOMEM;
9688
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009689 config->save_connector_encoders =
9690 kcalloc(dev->mode_config.num_connector,
9691 sizeof(struct drm_encoder *), GFP_KERNEL);
9692 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009693 return -ENOMEM;
9694
9695 /* Copy data. Note that driver private data is not affected.
9696 * Should anything bad happen only the expected state is
9697 * restored, not the drivers personal bookkeeping.
9698 */
9699 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009700 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009701 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009702 }
9703
9704 count = 0;
9705 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009706 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009707 }
9708
9709 return 0;
9710}
9711
9712static void intel_set_config_restore_state(struct drm_device *dev,
9713 struct intel_set_config *config)
9714{
Daniel Vetter9a935852012-07-05 22:34:27 +02009715 struct intel_encoder *encoder;
9716 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009717 int count;
9718
9719 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009720 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9721 encoder->new_crtc =
9722 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009723 }
9724
9725 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009726 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9727 connector->new_encoder =
9728 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009729 }
9730}
9731
Imre Deake3de42b2013-05-03 19:44:07 +02009732static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009733is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009734{
9735 int i;
9736
Chris Wilson2e57f472013-07-17 12:14:40 +01009737 if (set->num_connectors == 0)
9738 return false;
9739
9740 if (WARN_ON(set->connectors == NULL))
9741 return false;
9742
9743 for (i = 0; i < set->num_connectors; i++)
9744 if (set->connectors[i]->encoder &&
9745 set->connectors[i]->encoder->crtc == set->crtc &&
9746 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009747 return true;
9748
9749 return false;
9750}
9751
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009752static void
9753intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9754 struct intel_set_config *config)
9755{
9756
9757 /* We should be able to check here if the fb has the same properties
9758 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009759 if (is_crtc_connector_off(set)) {
9760 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009761 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009762 /* If we have no fb then treat it as a full mode set */
9763 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009764 struct intel_crtc *intel_crtc =
9765 to_intel_crtc(set->crtc);
9766
9767 if (intel_crtc->active && i915_fastboot) {
9768 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9769 config->fb_changed = true;
9770 } else {
9771 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9772 config->mode_changed = true;
9773 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009774 } else if (set->fb == NULL) {
9775 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009776 } else if (set->fb->pixel_format !=
9777 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009778 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009779 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009780 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009781 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009782 }
9783
Daniel Vetter835c5872012-07-10 18:11:08 +02009784 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009785 config->fb_changed = true;
9786
9787 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9788 DRM_DEBUG_KMS("modes are different, full mode set\n");
9789 drm_mode_debug_printmodeline(&set->crtc->mode);
9790 drm_mode_debug_printmodeline(set->mode);
9791 config->mode_changed = true;
9792 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009793
9794 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9795 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009796}
9797
Daniel Vetter2e431052012-07-04 22:42:15 +02009798static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009799intel_modeset_stage_output_state(struct drm_device *dev,
9800 struct drm_mode_set *set,
9801 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009802{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009803 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009804 struct intel_connector *connector;
9805 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009806 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009807
Damien Lespiau9abdda72013-02-13 13:29:23 +00009808 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009809 * of connectors. For paranoia, double-check this. */
9810 WARN_ON(!set->fb && (set->num_connectors != 0));
9811 WARN_ON(set->fb && (set->num_connectors == 0));
9812
Daniel Vetter9a935852012-07-05 22:34:27 +02009813 list_for_each_entry(connector, &dev->mode_config.connector_list,
9814 base.head) {
9815 /* Otherwise traverse passed in connector list and get encoders
9816 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009817 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009818 if (set->connectors[ro] == &connector->base) {
9819 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009820 break;
9821 }
9822 }
9823
Daniel Vetter9a935852012-07-05 22:34:27 +02009824 /* If we disable the crtc, disable all its connectors. Also, if
9825 * the connector is on the changing crtc but not on the new
9826 * connector list, disable it. */
9827 if ((!set->fb || ro == set->num_connectors) &&
9828 connector->base.encoder &&
9829 connector->base.encoder->crtc == set->crtc) {
9830 connector->new_encoder = NULL;
9831
9832 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9833 connector->base.base.id,
9834 drm_get_connector_name(&connector->base));
9835 }
9836
9837
9838 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009839 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009840 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009841 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009842 }
9843 /* connector->new_encoder is now updated for all connectors. */
9844
9845 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009846 list_for_each_entry(connector, &dev->mode_config.connector_list,
9847 base.head) {
9848 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009849 continue;
9850
Daniel Vetter9a935852012-07-05 22:34:27 +02009851 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009852
9853 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009854 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009855 new_crtc = set->crtc;
9856 }
9857
9858 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009859 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9860 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009861 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009862 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009863 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9864
9865 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9866 connector->base.base.id,
9867 drm_get_connector_name(&connector->base),
9868 new_crtc->base.id);
9869 }
9870
9871 /* Check for any encoders that needs to be disabled. */
9872 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9873 base.head) {
9874 list_for_each_entry(connector,
9875 &dev->mode_config.connector_list,
9876 base.head) {
9877 if (connector->new_encoder == encoder) {
9878 WARN_ON(!connector->new_encoder->new_crtc);
9879
9880 goto next_encoder;
9881 }
9882 }
9883 encoder->new_crtc = NULL;
9884next_encoder:
9885 /* Only now check for crtc changes so we don't miss encoders
9886 * that will be disabled. */
9887 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009888 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009889 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009890 }
9891 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009892 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009893
Daniel Vetter2e431052012-07-04 22:42:15 +02009894 return 0;
9895}
9896
9897static int intel_crtc_set_config(struct drm_mode_set *set)
9898{
9899 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009900 struct drm_mode_set save_set;
9901 struct intel_set_config *config;
9902 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009903
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009904 BUG_ON(!set);
9905 BUG_ON(!set->crtc);
9906 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009907
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009908 /* Enforce sane interface api - has been abused by the fb helper. */
9909 BUG_ON(!set->mode && set->fb);
9910 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009911
Daniel Vetter2e431052012-07-04 22:42:15 +02009912 if (set->fb) {
9913 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9914 set->crtc->base.id, set->fb->base.id,
9915 (int)set->num_connectors, set->x, set->y);
9916 } else {
9917 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009918 }
9919
9920 dev = set->crtc->dev;
9921
9922 ret = -ENOMEM;
9923 config = kzalloc(sizeof(*config), GFP_KERNEL);
9924 if (!config)
9925 goto out_config;
9926
9927 ret = intel_set_config_save_state(dev, config);
9928 if (ret)
9929 goto out_config;
9930
9931 save_set.crtc = set->crtc;
9932 save_set.mode = &set->crtc->mode;
9933 save_set.x = set->crtc->x;
9934 save_set.y = set->crtc->y;
9935 save_set.fb = set->crtc->fb;
9936
9937 /* Compute whether we need a full modeset, only an fb base update or no
9938 * change at all. In the future we might also check whether only the
9939 * mode changed, e.g. for LVDS where we only change the panel fitter in
9940 * such cases. */
9941 intel_set_config_compute_mode_changes(set, config);
9942
Daniel Vetter9a935852012-07-05 22:34:27 +02009943 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009944 if (ret)
9945 goto fail;
9946
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009947 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009948 ret = intel_set_mode(set->crtc, set->mode,
9949 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009950 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009951 intel_crtc_wait_for_pending_flips(set->crtc);
9952
Daniel Vetter4f660f42012-07-02 09:47:37 +02009953 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009954 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009955 }
9956
Chris Wilson2d05eae2013-05-03 17:36:25 +01009957 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009958 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9959 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009960fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009961 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009962
Chris Wilson2d05eae2013-05-03 17:36:25 +01009963 /* Try to restore the config */
9964 if (config->mode_changed &&
9965 intel_set_mode(save_set.crtc, save_set.mode,
9966 save_set.x, save_set.y, save_set.fb))
9967 DRM_ERROR("failed to restore config after modeset failure\n");
9968 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009969
Daniel Vetterd9e55602012-07-04 22:16:09 +02009970out_config:
9971 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009972 return ret;
9973}
9974
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009975static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009976 .cursor_set = intel_crtc_cursor_set,
9977 .cursor_move = intel_crtc_cursor_move,
9978 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009979 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009980 .destroy = intel_crtc_destroy,
9981 .page_flip = intel_crtc_page_flip,
9982};
9983
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009984static void intel_cpu_pll_init(struct drm_device *dev)
9985{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009986 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009987 intel_ddi_pll_init(dev);
9988}
9989
Daniel Vetter53589012013-06-05 13:34:16 +02009990static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9991 struct intel_shared_dpll *pll,
9992 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009993{
Daniel Vetter53589012013-06-05 13:34:16 +02009994 uint32_t val;
9995
9996 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009997 hw_state->dpll = val;
9998 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9999 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010000
10001 return val & DPLL_VCO_ENABLE;
10002}
10003
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010004static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10005 struct intel_shared_dpll *pll)
10006{
10007 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10008 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10009}
10010
Daniel Vettere7b903d2013-06-05 13:34:14 +020010011static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10012 struct intel_shared_dpll *pll)
10013{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010014 /* PCH refclock must be enabled first */
10015 assert_pch_refclk_enabled(dev_priv);
10016
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010017 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10018
10019 /* Wait for the clocks to stabilize. */
10020 POSTING_READ(PCH_DPLL(pll->id));
10021 udelay(150);
10022
10023 /* The pixel multiplier can only be updated once the
10024 * DPLL is enabled and the clocks are stable.
10025 *
10026 * So write it again.
10027 */
10028 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10029 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010030 udelay(200);
10031}
10032
10033static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10034 struct intel_shared_dpll *pll)
10035{
10036 struct drm_device *dev = dev_priv->dev;
10037 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010038
10039 /* Make sure no transcoder isn't still depending on us. */
10040 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10041 if (intel_crtc_to_shared_dpll(crtc) == pll)
10042 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10043 }
10044
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010045 I915_WRITE(PCH_DPLL(pll->id), 0);
10046 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010047 udelay(200);
10048}
10049
Daniel Vetter46edb022013-06-05 13:34:12 +020010050static char *ibx_pch_dpll_names[] = {
10051 "PCH DPLL A",
10052 "PCH DPLL B",
10053};
10054
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010055static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010056{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010057 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010058 int i;
10059
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010060 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010061
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010062 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010063 dev_priv->shared_dplls[i].id = i;
10064 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010065 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010066 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10067 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010068 dev_priv->shared_dplls[i].get_hw_state =
10069 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010070 }
10071}
10072
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010073static void intel_shared_dpll_init(struct drm_device *dev)
10074{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010075 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010076
10077 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10078 ibx_pch_dpll_init(dev);
10079 else
10080 dev_priv->num_shared_dpll = 0;
10081
10082 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10083 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
10084 dev_priv->num_shared_dpll);
10085}
10086
Hannes Ederb358d0a2008-12-18 21:18:47 +010010087static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010088{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010089 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010090 struct intel_crtc *intel_crtc;
10091 int i;
10092
Daniel Vetter955382f2013-09-19 14:05:45 +020010093 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010094 if (intel_crtc == NULL)
10095 return;
10096
10097 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10098
10099 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010100 for (i = 0; i < 256; i++) {
10101 intel_crtc->lut_r[i] = i;
10102 intel_crtc->lut_g[i] = i;
10103 intel_crtc->lut_b[i] = i;
10104 }
10105
Jesse Barnes80824002009-09-10 15:28:06 -070010106 /* Swap pipes & planes for FBC on pre-965 */
10107 intel_crtc->pipe = pipe;
10108 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +010010109 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010110 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010111 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010112 }
10113
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010114 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10115 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10116 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10117 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10118
Jesse Barnes79e53942008-11-07 14:24:08 -080010119 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010120}
10121
Jesse Barnes752aa882013-10-31 18:55:49 +020010122enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10123{
10124 struct drm_encoder *encoder = connector->base.encoder;
10125
10126 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10127
10128 if (!encoder)
10129 return INVALID_PIPE;
10130
10131 return to_intel_crtc(encoder->crtc)->pipe;
10132}
10133
Carl Worth08d7b3d2009-04-29 14:43:54 -070010134int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010135 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010136{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010137 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010138 struct drm_mode_object *drmmode_obj;
10139 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010140
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010141 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10142 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010143
Daniel Vetterc05422d2009-08-11 16:05:30 +020010144 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10145 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010146
Daniel Vetterc05422d2009-08-11 16:05:30 +020010147 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010148 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010149 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010150 }
10151
Daniel Vetterc05422d2009-08-11 16:05:30 +020010152 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10153 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010154
Daniel Vetterc05422d2009-08-11 16:05:30 +020010155 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010156}
10157
Daniel Vetter66a92782012-07-12 20:08:18 +020010158static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010159{
Daniel Vetter66a92782012-07-12 20:08:18 +020010160 struct drm_device *dev = encoder->base.dev;
10161 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010162 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010163 int entry = 0;
10164
Daniel Vetter66a92782012-07-12 20:08:18 +020010165 list_for_each_entry(source_encoder,
10166 &dev->mode_config.encoder_list, base.head) {
10167
10168 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010169 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010170
10171 /* Intel hw has only one MUX where enocoders could be cloned. */
10172 if (encoder->cloneable && source_encoder->cloneable)
10173 index_mask |= (1 << entry);
10174
Jesse Barnes79e53942008-11-07 14:24:08 -080010175 entry++;
10176 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010177
Jesse Barnes79e53942008-11-07 14:24:08 -080010178 return index_mask;
10179}
10180
Chris Wilson4d302442010-12-14 19:21:29 +000010181static bool has_edp_a(struct drm_device *dev)
10182{
10183 struct drm_i915_private *dev_priv = dev->dev_private;
10184
10185 if (!IS_MOBILE(dev))
10186 return false;
10187
10188 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10189 return false;
10190
10191 if (IS_GEN5(dev) &&
10192 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10193 return false;
10194
10195 return true;
10196}
10197
Jesse Barnes79e53942008-11-07 14:24:08 -080010198static void intel_setup_outputs(struct drm_device *dev)
10199{
Eric Anholt725e30a2009-01-22 13:01:02 -080010200 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010201 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010202 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010203
Daniel Vetterc9093352013-06-06 22:22:47 +020010204 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010205
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010206 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010207 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010208
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010209 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010210 int found;
10211
10212 /* Haswell uses DDI functions to detect digital outputs */
10213 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10214 /* DDI A only supports eDP */
10215 if (found)
10216 intel_ddi_init(dev, PORT_A);
10217
10218 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10219 * register */
10220 found = I915_READ(SFUSE_STRAP);
10221
10222 if (found & SFUSE_STRAP_DDIB_DETECTED)
10223 intel_ddi_init(dev, PORT_B);
10224 if (found & SFUSE_STRAP_DDIC_DETECTED)
10225 intel_ddi_init(dev, PORT_C);
10226 if (found & SFUSE_STRAP_DDID_DETECTED)
10227 intel_ddi_init(dev, PORT_D);
10228 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010229 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +020010230 dpd_is_edp = intel_dpd_is_edp(dev);
10231
10232 if (has_edp_a(dev))
10233 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010234
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010235 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010236 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010237 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010238 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010239 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010240 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010241 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010242 }
10243
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010244 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010245 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010246
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010247 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010248 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010249
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010250 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010251 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010252
Daniel Vetter270b3042012-10-27 15:52:05 +020010253 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010254 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010255 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010256 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10257 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10258 PORT_B);
10259 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10260 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10261 }
10262
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010263 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10264 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10265 PORT_C);
10266 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10267 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10268 PORT_C);
10269 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010270
Jani Nikula3cfca972013-08-27 15:12:26 +030010271 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010272 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010273 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010274
Paulo Zanonie2debe92013-02-18 19:00:27 -030010275 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010276 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010277 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010278 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10279 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010280 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010281 }
Ma Ling27185ae2009-08-24 13:50:23 +080010282
Imre Deake7281ea2013-05-08 13:14:08 +030010283 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010284 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010285 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010286
10287 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010288
Paulo Zanonie2debe92013-02-18 19:00:27 -030010289 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010290 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010291 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010292 }
Ma Ling27185ae2009-08-24 13:50:23 +080010293
Paulo Zanonie2debe92013-02-18 19:00:27 -030010294 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010295
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010296 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10297 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010298 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010299 }
Imre Deake7281ea2013-05-08 13:14:08 +030010300 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010301 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010302 }
Ma Ling27185ae2009-08-24 13:50:23 +080010303
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010304 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010305 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010306 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010307 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010308 intel_dvo_init(dev);
10309
Zhenyu Wang103a1962009-11-27 11:44:36 +080010310 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010311 intel_tv_init(dev);
10312
Chris Wilson4ef69c72010-09-09 15:14:28 +010010313 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10314 encoder->base.possible_crtcs = encoder->crtc_mask;
10315 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010316 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010317 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010318
Paulo Zanonidde86e22012-12-01 12:04:25 -020010319 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010320
10321 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010322}
10323
Chris Wilsonddfe1562013-08-06 17:43:07 +010010324void intel_framebuffer_fini(struct intel_framebuffer *fb)
10325{
10326 drm_framebuffer_cleanup(&fb->base);
Daniel Vetter80075d42013-10-09 21:23:52 +020010327 WARN_ON(!fb->obj->framebuffer_references--);
Chris Wilsonddfe1562013-08-06 17:43:07 +010010328 drm_gem_object_unreference_unlocked(&fb->obj->base);
10329}
10330
Jesse Barnes79e53942008-11-07 14:24:08 -080010331static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10332{
10333 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010334
Chris Wilsonddfe1562013-08-06 17:43:07 +010010335 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010336 kfree(intel_fb);
10337}
10338
10339static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010340 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010341 unsigned int *handle)
10342{
10343 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010344 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010345
Chris Wilson05394f32010-11-08 19:18:58 +000010346 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010347}
10348
10349static const struct drm_framebuffer_funcs intel_fb_funcs = {
10350 .destroy = intel_user_framebuffer_destroy,
10351 .create_handle = intel_user_framebuffer_create_handle,
10352};
10353
Dave Airlie38651672010-03-30 05:34:13 +000010354int intel_framebuffer_init(struct drm_device *dev,
10355 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010356 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010357 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010358{
Daniel Vetter53155c02013-10-09 21:55:33 +020010359 int aligned_height, tile_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010360 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010361 int ret;
10362
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010363 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10364
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010365 if (obj->tiling_mode == I915_TILING_Y) {
10366 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010367 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010368 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010369
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010370 if (mode_cmd->pitches[0] & 63) {
10371 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10372 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010373 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010374 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010375
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010376 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10377 pitch_limit = 32*1024;
10378 } else if (INTEL_INFO(dev)->gen >= 4) {
10379 if (obj->tiling_mode)
10380 pitch_limit = 16*1024;
10381 else
10382 pitch_limit = 32*1024;
10383 } else if (INTEL_INFO(dev)->gen >= 3) {
10384 if (obj->tiling_mode)
10385 pitch_limit = 8*1024;
10386 else
10387 pitch_limit = 16*1024;
10388 } else
10389 /* XXX DSPC is limited to 4k tiled */
10390 pitch_limit = 8*1024;
10391
10392 if (mode_cmd->pitches[0] > pitch_limit) {
10393 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10394 obj->tiling_mode ? "tiled" : "linear",
10395 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010396 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010397 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010398
10399 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010400 mode_cmd->pitches[0] != obj->stride) {
10401 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10402 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010403 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010404 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010405
Ville Syrjälä57779d02012-10-31 17:50:14 +020010406 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010407 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010408 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010409 case DRM_FORMAT_RGB565:
10410 case DRM_FORMAT_XRGB8888:
10411 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010412 break;
10413 case DRM_FORMAT_XRGB1555:
10414 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010415 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010416 DRM_DEBUG("unsupported pixel format: %s\n",
10417 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010418 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010419 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010420 break;
10421 case DRM_FORMAT_XBGR8888:
10422 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010423 case DRM_FORMAT_XRGB2101010:
10424 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010425 case DRM_FORMAT_XBGR2101010:
10426 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010427 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010428 DRM_DEBUG("unsupported pixel format: %s\n",
10429 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010430 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010431 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010432 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010433 case DRM_FORMAT_YUYV:
10434 case DRM_FORMAT_UYVY:
10435 case DRM_FORMAT_YVYU:
10436 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010437 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010438 DRM_DEBUG("unsupported pixel format: %s\n",
10439 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010440 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010441 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010442 break;
10443 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010444 DRM_DEBUG("unsupported pixel format: %s\n",
10445 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010446 return -EINVAL;
10447 }
10448
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010449 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10450 if (mode_cmd->offsets[0] != 0)
10451 return -EINVAL;
10452
Daniel Vetter53155c02013-10-09 21:55:33 +020010453 tile_height = IS_GEN2(dev) ? 16 : 8;
10454 aligned_height = ALIGN(mode_cmd->height,
10455 obj->tiling_mode ? tile_height : 1);
10456 /* FIXME drm helper for size checks (especially planar formats)? */
10457 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10458 return -EINVAL;
10459
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010460 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10461 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010462 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010463
Jesse Barnes79e53942008-11-07 14:24:08 -080010464 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10465 if (ret) {
10466 DRM_ERROR("framebuffer init failed %d\n", ret);
10467 return ret;
10468 }
10469
Jesse Barnes79e53942008-11-07 14:24:08 -080010470 return 0;
10471}
10472
Jesse Barnes79e53942008-11-07 14:24:08 -080010473static struct drm_framebuffer *
10474intel_user_framebuffer_create(struct drm_device *dev,
10475 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010476 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010477{
Chris Wilson05394f32010-11-08 19:18:58 +000010478 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010479
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010480 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10481 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010482 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010483 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010484
Chris Wilsond2dff872011-04-19 08:36:26 +010010485 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010486}
10487
Daniel Vetter4520f532013-10-09 09:18:51 +020010488#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010489static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010490{
10491}
10492#endif
10493
Jesse Barnes79e53942008-11-07 14:24:08 -080010494static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010495 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010496 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010497};
10498
Jesse Barnese70236a2009-09-21 10:42:27 -070010499/* Set up chip specific display functions */
10500static void intel_init_display(struct drm_device *dev)
10501{
10502 struct drm_i915_private *dev_priv = dev->dev_private;
10503
Daniel Vetteree9300b2013-06-03 22:40:22 +020010504 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10505 dev_priv->display.find_dpll = g4x_find_best_dpll;
10506 else if (IS_VALLEYVIEW(dev))
10507 dev_priv->display.find_dpll = vlv_find_best_dpll;
10508 else if (IS_PINEVIEW(dev))
10509 dev_priv->display.find_dpll = pnv_find_best_dpll;
10510 else
10511 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10512
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010513 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010514 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010515 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010516 dev_priv->display.crtc_enable = haswell_crtc_enable;
10517 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010518 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010519 dev_priv->display.update_plane = ironlake_update_plane;
10520 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010521 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010522 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010523 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10524 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010525 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010526 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010527 } else if (IS_VALLEYVIEW(dev)) {
10528 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10529 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10530 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10531 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10532 dev_priv->display.off = i9xx_crtc_off;
10533 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010534 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010535 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010536 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010537 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10538 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010539 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010540 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010541 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010542
Jesse Barnese70236a2009-09-21 10:42:27 -070010543 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010544 if (IS_VALLEYVIEW(dev))
10545 dev_priv->display.get_display_clock_speed =
10546 valleyview_get_display_clock_speed;
10547 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010548 dev_priv->display.get_display_clock_speed =
10549 i945_get_display_clock_speed;
10550 else if (IS_I915G(dev))
10551 dev_priv->display.get_display_clock_speed =
10552 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010553 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010554 dev_priv->display.get_display_clock_speed =
10555 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010556 else if (IS_PINEVIEW(dev))
10557 dev_priv->display.get_display_clock_speed =
10558 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010559 else if (IS_I915GM(dev))
10560 dev_priv->display.get_display_clock_speed =
10561 i915gm_get_display_clock_speed;
10562 else if (IS_I865G(dev))
10563 dev_priv->display.get_display_clock_speed =
10564 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010565 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010566 dev_priv->display.get_display_clock_speed =
10567 i855_get_display_clock_speed;
10568 else /* 852, 830 */
10569 dev_priv->display.get_display_clock_speed =
10570 i830_get_display_clock_speed;
10571
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010572 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010573 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010574 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010575 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010576 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010577 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010578 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010579 } else if (IS_IVYBRIDGE(dev)) {
10580 /* FIXME: detect B0+ stepping and use auto training */
10581 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010582 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010583 dev_priv->display.modeset_global_resources =
10584 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010585 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010586 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010587 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010588 dev_priv->display.modeset_global_resources =
10589 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010590 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010591 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010592 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010593 } else if (IS_VALLEYVIEW(dev)) {
10594 dev_priv->display.modeset_global_resources =
10595 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040010596 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010597 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010598
10599 /* Default just returns -ENODEV to indicate unsupported */
10600 dev_priv->display.queue_flip = intel_default_queue_flip;
10601
10602 switch (INTEL_INFO(dev)->gen) {
10603 case 2:
10604 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10605 break;
10606
10607 case 3:
10608 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10609 break;
10610
10611 case 4:
10612 case 5:
10613 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10614 break;
10615
10616 case 6:
10617 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10618 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010619 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010620 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010621 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10622 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010623 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020010624
10625 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010626}
10627
Jesse Barnesb690e962010-07-19 13:53:12 -070010628/*
10629 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10630 * resume, or other times. This quirk makes sure that's the case for
10631 * affected systems.
10632 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010633static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010634{
10635 struct drm_i915_private *dev_priv = dev->dev_private;
10636
10637 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010638 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010639}
10640
Keith Packard435793d2011-07-12 14:56:22 -070010641/*
10642 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10643 */
10644static void quirk_ssc_force_disable(struct drm_device *dev)
10645{
10646 struct drm_i915_private *dev_priv = dev->dev_private;
10647 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010648 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010649}
10650
Carsten Emde4dca20e2012-03-15 15:56:26 +010010651/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010652 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10653 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010654 */
10655static void quirk_invert_brightness(struct drm_device *dev)
10656{
10657 struct drm_i915_private *dev_priv = dev->dev_private;
10658 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010659 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010660}
10661
10662struct intel_quirk {
10663 int device;
10664 int subsystem_vendor;
10665 int subsystem_device;
10666 void (*hook)(struct drm_device *dev);
10667};
10668
Egbert Eich5f85f1762012-10-14 15:46:38 +020010669/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10670struct intel_dmi_quirk {
10671 void (*hook)(struct drm_device *dev);
10672 const struct dmi_system_id (*dmi_id_list)[];
10673};
10674
10675static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10676{
10677 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10678 return 1;
10679}
10680
10681static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10682 {
10683 .dmi_id_list = &(const struct dmi_system_id[]) {
10684 {
10685 .callback = intel_dmi_reverse_brightness,
10686 .ident = "NCR Corporation",
10687 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10688 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10689 },
10690 },
10691 { } /* terminating entry */
10692 },
10693 .hook = quirk_invert_brightness,
10694 },
10695};
10696
Ben Widawskyc43b5632012-04-16 14:07:40 -070010697static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010698 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010699 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010700
Jesse Barnesb690e962010-07-19 13:53:12 -070010701 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10702 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10703
Jesse Barnesb690e962010-07-19 13:53:12 -070010704 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10705 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10706
Chris Wilsona4945f92013-10-08 11:16:59 +010010707 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010708 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010709
10710 /* Lenovo U160 cannot use SSC on LVDS */
10711 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010712
10713 /* Sony Vaio Y cannot use SSC on LVDS */
10714 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010715
Jani Nikulaee1452d2013-09-20 15:05:30 +030010716 /*
10717 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10718 * seem to use inverted backlight PWM.
10719 */
10720 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070010721};
10722
10723static void intel_init_quirks(struct drm_device *dev)
10724{
10725 struct pci_dev *d = dev->pdev;
10726 int i;
10727
10728 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10729 struct intel_quirk *q = &intel_quirks[i];
10730
10731 if (d->device == q->device &&
10732 (d->subsystem_vendor == q->subsystem_vendor ||
10733 q->subsystem_vendor == PCI_ANY_ID) &&
10734 (d->subsystem_device == q->subsystem_device ||
10735 q->subsystem_device == PCI_ANY_ID))
10736 q->hook(dev);
10737 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010738 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10739 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10740 intel_dmi_quirks[i].hook(dev);
10741 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010742}
10743
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010744/* Disable the VGA plane that we never use */
10745static void i915_disable_vga(struct drm_device *dev)
10746{
10747 struct drm_i915_private *dev_priv = dev->dev_private;
10748 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010749 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010750
10751 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010752 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010753 sr1 = inb(VGA_SR_DATA);
10754 outb(sr1 | 1<<5, VGA_SR_DATA);
10755 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10756 udelay(300);
10757
10758 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10759 POSTING_READ(vga_reg);
10760}
10761
Daniel Vetterf8175862012-04-10 15:50:11 +020010762void intel_modeset_init_hw(struct drm_device *dev)
10763{
Jesse Barnesf6071162013-10-01 10:41:38 -070010764 struct drm_i915_private *dev_priv = dev->dev_private;
10765
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010766 intel_prepare_ddi(dev);
10767
Daniel Vetterf8175862012-04-10 15:50:11 +020010768 intel_init_clock_gating(dev);
10769
Jesse Barnesf6071162013-10-01 10:41:38 -070010770 /* Enable the CRI clock source so we can get at the display */
10771 if (IS_VALLEYVIEW(dev))
10772 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10773 DPLL_INTEGRATED_CRI_CLK_VLV);
10774
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010775 intel_init_dpio(dev);
10776
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010777 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010778 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010779 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010780}
10781
Imre Deak7d708ee2013-04-17 14:04:50 +030010782void intel_modeset_suspend_hw(struct drm_device *dev)
10783{
10784 intel_suspend_hw(dev);
10785}
10786
Jesse Barnes79e53942008-11-07 14:24:08 -080010787void intel_modeset_init(struct drm_device *dev)
10788{
Jesse Barnes652c3932009-08-17 13:31:43 -070010789 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010790 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010791
10792 drm_mode_config_init(dev);
10793
10794 dev->mode_config.min_width = 0;
10795 dev->mode_config.min_height = 0;
10796
Dave Airlie019d96c2011-09-29 16:20:42 +010010797 dev->mode_config.preferred_depth = 24;
10798 dev->mode_config.prefer_shadow = 1;
10799
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010800 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010801
Jesse Barnesb690e962010-07-19 13:53:12 -070010802 intel_init_quirks(dev);
10803
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010804 intel_init_pm(dev);
10805
Ben Widawskye3c74752013-04-05 13:12:39 -070010806 if (INTEL_INFO(dev)->num_pipes == 0)
10807 return;
10808
Jesse Barnese70236a2009-09-21 10:42:27 -070010809 intel_init_display(dev);
10810
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010811 if (IS_GEN2(dev)) {
10812 dev->mode_config.max_width = 2048;
10813 dev->mode_config.max_height = 2048;
10814 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010815 dev->mode_config.max_width = 4096;
10816 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010817 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010818 dev->mode_config.max_width = 8192;
10819 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010820 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010821 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010822
Zhao Yakui28c97732009-10-09 11:39:41 +080010823 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010824 INTEL_INFO(dev)->num_pipes,
10825 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010826
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010827 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010828 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010829 for (j = 0; j < dev_priv->num_plane; j++) {
10830 ret = intel_plane_init(dev, i, j);
10831 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010832 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10833 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010834 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010835 }
10836
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010837 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010838 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010839
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010840 /* Just disable it once at startup */
10841 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010842 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010843
10844 /* Just in case the BIOS is doing something questionable. */
10845 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010846}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010847
Daniel Vetter24929352012-07-02 20:28:59 +020010848static void
10849intel_connector_break_all_links(struct intel_connector *connector)
10850{
10851 connector->base.dpms = DRM_MODE_DPMS_OFF;
10852 connector->base.encoder = NULL;
10853 connector->encoder->connectors_active = false;
10854 connector->encoder->base.crtc = NULL;
10855}
10856
Daniel Vetter7fad7982012-07-04 17:51:47 +020010857static void intel_enable_pipe_a(struct drm_device *dev)
10858{
10859 struct intel_connector *connector;
10860 struct drm_connector *crt = NULL;
10861 struct intel_load_detect_pipe load_detect_temp;
10862
10863 /* We can't just switch on the pipe A, we need to set things up with a
10864 * proper mode and output configuration. As a gross hack, enable pipe A
10865 * by enabling the load detect pipe once. */
10866 list_for_each_entry(connector,
10867 &dev->mode_config.connector_list,
10868 base.head) {
10869 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10870 crt = &connector->base;
10871 break;
10872 }
10873 }
10874
10875 if (!crt)
10876 return;
10877
10878 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10879 intel_release_load_detect_pipe(crt, &load_detect_temp);
10880
10881
10882}
10883
Daniel Vetterfa555832012-10-10 23:14:00 +020010884static bool
10885intel_check_plane_mapping(struct intel_crtc *crtc)
10886{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010887 struct drm_device *dev = crtc->base.dev;
10888 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010889 u32 reg, val;
10890
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010891 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010892 return true;
10893
10894 reg = DSPCNTR(!crtc->plane);
10895 val = I915_READ(reg);
10896
10897 if ((val & DISPLAY_PLANE_ENABLE) &&
10898 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10899 return false;
10900
10901 return true;
10902}
10903
Daniel Vetter24929352012-07-02 20:28:59 +020010904static void intel_sanitize_crtc(struct intel_crtc *crtc)
10905{
10906 struct drm_device *dev = crtc->base.dev;
10907 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010908 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010909
Daniel Vetter24929352012-07-02 20:28:59 +020010910 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010911 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010912 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10913
10914 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010915 * disable the crtc (and hence change the state) if it is wrong. Note
10916 * that gen4+ has a fixed plane -> pipe mapping. */
10917 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010918 struct intel_connector *connector;
10919 bool plane;
10920
Daniel Vetter24929352012-07-02 20:28:59 +020010921 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10922 crtc->base.base.id);
10923
10924 /* Pipe has the wrong plane attached and the plane is active.
10925 * Temporarily change the plane mapping and disable everything
10926 * ... */
10927 plane = crtc->plane;
10928 crtc->plane = !plane;
10929 dev_priv->display.crtc_disable(&crtc->base);
10930 crtc->plane = plane;
10931
10932 /* ... and break all links. */
10933 list_for_each_entry(connector, &dev->mode_config.connector_list,
10934 base.head) {
10935 if (connector->encoder->base.crtc != &crtc->base)
10936 continue;
10937
10938 intel_connector_break_all_links(connector);
10939 }
10940
10941 WARN_ON(crtc->active);
10942 crtc->base.enabled = false;
10943 }
Daniel Vetter24929352012-07-02 20:28:59 +020010944
Daniel Vetter7fad7982012-07-04 17:51:47 +020010945 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10946 crtc->pipe == PIPE_A && !crtc->active) {
10947 /* BIOS forgot to enable pipe A, this mostly happens after
10948 * resume. Force-enable the pipe to fix this, the update_dpms
10949 * call below we restore the pipe to the right state, but leave
10950 * the required bits on. */
10951 intel_enable_pipe_a(dev);
10952 }
10953
Daniel Vetter24929352012-07-02 20:28:59 +020010954 /* Adjust the state of the output pipe according to whether we
10955 * have active connectors/encoders. */
10956 intel_crtc_update_dpms(&crtc->base);
10957
10958 if (crtc->active != crtc->base.enabled) {
10959 struct intel_encoder *encoder;
10960
10961 /* This can happen either due to bugs in the get_hw_state
10962 * functions or because the pipe is force-enabled due to the
10963 * pipe A quirk. */
10964 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10965 crtc->base.base.id,
10966 crtc->base.enabled ? "enabled" : "disabled",
10967 crtc->active ? "enabled" : "disabled");
10968
10969 crtc->base.enabled = crtc->active;
10970
10971 /* Because we only establish the connector -> encoder ->
10972 * crtc links if something is active, this means the
10973 * crtc is now deactivated. Break the links. connector
10974 * -> encoder links are only establish when things are
10975 * actually up, hence no need to break them. */
10976 WARN_ON(crtc->active);
10977
10978 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10979 WARN_ON(encoder->connectors_active);
10980 encoder->base.crtc = NULL;
10981 }
10982 }
10983}
10984
10985static void intel_sanitize_encoder(struct intel_encoder *encoder)
10986{
10987 struct intel_connector *connector;
10988 struct drm_device *dev = encoder->base.dev;
10989
10990 /* We need to check both for a crtc link (meaning that the
10991 * encoder is active and trying to read from a pipe) and the
10992 * pipe itself being active. */
10993 bool has_active_crtc = encoder->base.crtc &&
10994 to_intel_crtc(encoder->base.crtc)->active;
10995
10996 if (encoder->connectors_active && !has_active_crtc) {
10997 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10998 encoder->base.base.id,
10999 drm_get_encoder_name(&encoder->base));
11000
11001 /* Connector is active, but has no active pipe. This is
11002 * fallout from our resume register restoring. Disable
11003 * the encoder manually again. */
11004 if (encoder->base.crtc) {
11005 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11006 encoder->base.base.id,
11007 drm_get_encoder_name(&encoder->base));
11008 encoder->disable(encoder);
11009 }
11010
11011 /* Inconsistent output/port/pipe state happens presumably due to
11012 * a bug in one of the get_hw_state functions. Or someplace else
11013 * in our code, like the register restore mess on resume. Clamp
11014 * things to off as a safer default. */
11015 list_for_each_entry(connector,
11016 &dev->mode_config.connector_list,
11017 base.head) {
11018 if (connector->encoder != encoder)
11019 continue;
11020
11021 intel_connector_break_all_links(connector);
11022 }
11023 }
11024 /* Enabled encoders without active connectors will be fixed in
11025 * the crtc fixup. */
11026}
11027
Daniel Vetter44cec742013-01-25 17:53:21 +010011028void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011029{
11030 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011031 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011032
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011033 /* This function can be called both from intel_modeset_setup_hw_state or
11034 * at a very early point in our resume sequence, where the power well
11035 * structures are not yet restored. Since this function is at a very
11036 * paranoid "someone might have enabled VGA while we were not looking"
11037 * level, just check if the power well is enabled instead of trying to
11038 * follow the "don't touch the power well if we don't need it" policy
11039 * the rest of the driver uses. */
Jesse Barnesf9e711e2013-11-25 17:15:32 +020011040 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030011041 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011042 return;
11043
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030011044 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011045 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020011046 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011047 }
11048}
11049
Daniel Vetter30e984d2013-06-05 13:34:17 +020011050static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011051{
11052 struct drm_i915_private *dev_priv = dev->dev_private;
11053 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011054 struct intel_crtc *crtc;
11055 struct intel_encoder *encoder;
11056 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011057 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011058
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011059 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11060 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011061 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011062
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011063 crtc->active = dev_priv->display.get_pipe_config(crtc,
11064 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011065
11066 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030011067 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020011068
11069 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11070 crtc->base.base.id,
11071 crtc->active ? "enabled" : "disabled");
11072 }
11073
Daniel Vetter53589012013-06-05 13:34:16 +020011074 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011075 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011076 intel_ddi_setup_hw_pll_state(dev);
11077
Daniel Vetter53589012013-06-05 13:34:16 +020011078 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11079 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11080
11081 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11082 pll->active = 0;
11083 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11084 base.head) {
11085 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11086 pll->active++;
11087 }
11088 pll->refcount = pll->active;
11089
Daniel Vetter35c95372013-07-17 06:55:04 +020011090 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11091 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011092 }
11093
Daniel Vetter24929352012-07-02 20:28:59 +020011094 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11095 base.head) {
11096 pipe = 0;
11097
11098 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011099 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11100 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070011101 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011102 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011103 } else {
11104 encoder->base.crtc = NULL;
11105 }
11106
11107 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011108 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011109 encoder->base.base.id,
11110 drm_get_encoder_name(&encoder->base),
11111 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011112 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011113 }
11114
11115 list_for_each_entry(connector, &dev->mode_config.connector_list,
11116 base.head) {
11117 if (connector->get_hw_state(connector)) {
11118 connector->base.dpms = DRM_MODE_DPMS_ON;
11119 connector->encoder->connectors_active = true;
11120 connector->base.encoder = &connector->encoder->base;
11121 } else {
11122 connector->base.dpms = DRM_MODE_DPMS_OFF;
11123 connector->base.encoder = NULL;
11124 }
11125 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11126 connector->base.base.id,
11127 drm_get_connector_name(&connector->base),
11128 connector->base.encoder ? "enabled" : "disabled");
11129 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011130}
11131
11132/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11133 * and i915 state tracking structures. */
11134void intel_modeset_setup_hw_state(struct drm_device *dev,
11135 bool force_restore)
11136{
11137 struct drm_i915_private *dev_priv = dev->dev_private;
11138 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011139 struct intel_crtc *crtc;
11140 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011141 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011142
11143 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011144
Jesse Barnesbabea612013-06-26 18:57:38 +030011145 /*
11146 * Now that we have the config, copy it to each CRTC struct
11147 * Note that this could go away if we move to using crtc_config
11148 * checking everywhere.
11149 */
11150 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11151 base.head) {
11152 if (crtc->active && i915_fastboot) {
11153 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11154
11155 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11156 crtc->base.base.id);
11157 drm_mode_debug_printmodeline(&crtc->base.mode);
11158 }
11159 }
11160
Daniel Vetter24929352012-07-02 20:28:59 +020011161 /* HW state is read out, now we need to sanitize this mess. */
11162 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11163 base.head) {
11164 intel_sanitize_encoder(encoder);
11165 }
11166
11167 for_each_pipe(pipe) {
11168 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11169 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011170 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011171 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011172
Daniel Vetter35c95372013-07-17 06:55:04 +020011173 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11174 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11175
11176 if (!pll->on || pll->active)
11177 continue;
11178
11179 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11180
11181 pll->disable(dev_priv, pll);
11182 pll->on = false;
11183 }
11184
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011185 if (IS_HASWELL(dev))
11186 ilk_wm_get_hw_state(dev);
11187
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011188 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011189 i915_redisable_vga(dev);
11190
Daniel Vetterf30da182013-04-11 20:22:50 +020011191 /*
11192 * We need to use raw interfaces for restoring state to avoid
11193 * checking (bogus) intermediate states.
11194 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011195 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011196 struct drm_crtc *crtc =
11197 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011198
11199 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11200 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011201 }
11202 } else {
11203 intel_modeset_update_staged_output_state(dev);
11204 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011205
11206 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020011207
11208 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011209}
11210
11211void intel_modeset_gem_init(struct drm_device *dev)
11212{
Chris Wilson1833b132012-05-09 11:56:28 +010011213 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011214
11215 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011216
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011217 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080011218}
11219
11220void intel_modeset_cleanup(struct drm_device *dev)
11221{
Jesse Barnes652c3932009-08-17 13:31:43 -070011222 struct drm_i915_private *dev_priv = dev->dev_private;
11223 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011224 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011225
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011226 /*
11227 * Interrupts and polling as the first thing to avoid creating havoc.
11228 * Too much stuff here (turning of rps, connectors, ...) would
11229 * experience fancy races otherwise.
11230 */
11231 drm_irq_uninstall(dev);
11232 cancel_work_sync(&dev_priv->hotplug_work);
11233 /*
11234 * Due to the hpd irq storm handling the hotplug work can re-arm the
11235 * poll handlers. Hence disable polling after hpd handling is shut down.
11236 */
Keith Packardf87ea762010-10-03 19:36:26 -070011237 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011238
Jesse Barnes652c3932009-08-17 13:31:43 -070011239 mutex_lock(&dev->struct_mutex);
11240
Jesse Barnes723bfd72010-10-07 16:01:13 -070011241 intel_unregister_dsm_handler();
11242
Jesse Barnes652c3932009-08-17 13:31:43 -070011243 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11244 /* Skip inactive CRTCs */
11245 if (!crtc->fb)
11246 continue;
11247
Daniel Vetter3dec0092010-08-20 21:40:52 +020011248 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011249 }
11250
Chris Wilson973d04f2011-07-08 12:22:37 +010011251 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011252
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011253 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011254
Daniel Vetter930ebb42012-06-29 23:32:16 +020011255 ironlake_teardown_rc6(dev);
11256
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011257 mutex_unlock(&dev->struct_mutex);
11258
Chris Wilson1630fe72011-07-08 12:22:42 +010011259 /* flush any delayed tasks or pending work */
11260 flush_scheduled_work();
11261
Jani Nikuladb31af12013-11-08 16:48:53 +020011262 /* destroy the backlight and sysfs files before encoders/connectors */
11263 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11264 intel_panel_destroy_backlight(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030011265 drm_sysfs_connector_remove(connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011266 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011267
Jesse Barnes79e53942008-11-07 14:24:08 -080011268 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011269
11270 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011271}
11272
Dave Airlie28d52042009-09-21 14:33:58 +100011273/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011274 * Return which encoder is currently attached for connector.
11275 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011276struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011277{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011278 return &intel_attached_encoder(connector)->base;
11279}
Jesse Barnes79e53942008-11-07 14:24:08 -080011280
Chris Wilsondf0e9242010-09-09 16:20:55 +010011281void intel_connector_attach_encoder(struct intel_connector *connector,
11282 struct intel_encoder *encoder)
11283{
11284 connector->encoder = encoder;
11285 drm_mode_connector_attach_encoder(&connector->base,
11286 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011287}
Dave Airlie28d52042009-09-21 14:33:58 +100011288
11289/*
11290 * set vga decode state - true == enable VGA decode
11291 */
11292int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11293{
11294 struct drm_i915_private *dev_priv = dev->dev_private;
11295 u16 gmch_ctrl;
11296
11297 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11298 if (state)
11299 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11300 else
11301 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11302 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11303 return 0;
11304}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011305
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011306struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011307
11308 u32 power_well_driver;
11309
Chris Wilson63b66e52013-08-08 15:12:06 +020011310 int num_transcoders;
11311
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011312 struct intel_cursor_error_state {
11313 u32 control;
11314 u32 position;
11315 u32 base;
11316 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011317 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011318
11319 struct intel_pipe_error_state {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011320 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011321 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011322
11323 struct intel_plane_error_state {
11324 u32 control;
11325 u32 stride;
11326 u32 size;
11327 u32 pos;
11328 u32 addr;
11329 u32 surface;
11330 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011331 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011332
11333 struct intel_transcoder_error_state {
11334 enum transcoder cpu_transcoder;
11335
11336 u32 conf;
11337
11338 u32 htotal;
11339 u32 hblank;
11340 u32 hsync;
11341 u32 vtotal;
11342 u32 vblank;
11343 u32 vsync;
11344 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011345};
11346
11347struct intel_display_error_state *
11348intel_display_capture_error_state(struct drm_device *dev)
11349{
Akshay Joshi0206e352011-08-16 15:34:10 -040011350 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011351 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011352 int transcoders[] = {
11353 TRANSCODER_A,
11354 TRANSCODER_B,
11355 TRANSCODER_C,
11356 TRANSCODER_EDP,
11357 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011358 int i;
11359
Chris Wilson63b66e52013-08-08 15:12:06 +020011360 if (INTEL_INFO(dev)->num_pipes == 0)
11361 return NULL;
11362
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011363 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011364 if (error == NULL)
11365 return NULL;
11366
Imre Deak190be112013-11-25 17:15:31 +020011367 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011368 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11369
Damien Lespiau52331302012-08-15 19:23:25 +010011370 for_each_pipe(i) {
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011371 if (!intel_display_power_enabled(dev, POWER_DOMAIN_PIPE(i)))
11372 continue;
11373
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011374 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11375 error->cursor[i].control = I915_READ(CURCNTR(i));
11376 error->cursor[i].position = I915_READ(CURPOS(i));
11377 error->cursor[i].base = I915_READ(CURBASE(i));
11378 } else {
11379 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11380 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11381 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11382 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011383
11384 error->plane[i].control = I915_READ(DSPCNTR(i));
11385 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011386 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011387 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011388 error->plane[i].pos = I915_READ(DSPPOS(i));
11389 }
Paulo Zanonica291362013-03-06 20:03:14 -030011390 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11391 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011392 if (INTEL_INFO(dev)->gen >= 4) {
11393 error->plane[i].surface = I915_READ(DSPSURF(i));
11394 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11395 }
11396
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011397 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011398 }
11399
11400 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11401 if (HAS_DDI(dev_priv->dev))
11402 error->num_transcoders++; /* Account for eDP. */
11403
11404 for (i = 0; i < error->num_transcoders; i++) {
11405 enum transcoder cpu_transcoder = transcoders[i];
11406
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011407 if (!intel_display_power_enabled(dev,
11408 POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
11409 continue;
11410
Chris Wilson63b66e52013-08-08 15:12:06 +020011411 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11412
11413 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11414 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11415 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11416 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11417 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11418 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11419 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011420 }
11421
11422 return error;
11423}
11424
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011425#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11426
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011427void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011428intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011429 struct drm_device *dev,
11430 struct intel_display_error_state *error)
11431{
11432 int i;
11433
Chris Wilson63b66e52013-08-08 15:12:06 +020011434 if (!error)
11435 return;
11436
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011437 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020011438 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011439 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011440 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011441 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011442 err_printf(m, "Pipe [%d]:\n", i);
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011443 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011444
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011445 err_printf(m, "Plane [%d]:\n", i);
11446 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11447 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011448 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011449 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11450 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011451 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011452 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011453 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011454 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011455 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11456 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011457 }
11458
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011459 err_printf(m, "Cursor [%d]:\n", i);
11460 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11461 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11462 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011463 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011464
11465 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011466 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011467 transcoder_name(error->transcoder[i].cpu_transcoder));
11468 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11469 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11470 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11471 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11472 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11473 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11474 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11475 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011476}