blob: 0e87970a03f6b99c34d849e4a7dc486470d8120f [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 = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200313 .dot = { .min = 25000, .max = 270000 },
314 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700315 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700316 .m1 = { .min = 2, .max = 3 },
317 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300318 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300319 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700320};
321
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300322static void vlv_clock(int refclk, intel_clock_t *clock)
323{
324 clock->m = clock->m1 * clock->m2;
325 clock->p = clock->p1 * clock->p2;
326 clock->vco = refclk * clock->m / clock->n;
327 clock->dot = clock->vco / clock->p;
328}
329
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300330/**
331 * Returns whether any output on the specified pipe is of the specified type
332 */
333static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
334{
335 struct drm_device *dev = crtc->dev;
336 struct intel_encoder *encoder;
337
338 for_each_encoder_on_crtc(dev, crtc, encoder)
339 if (encoder->type == type)
340 return true;
341
342 return false;
343}
344
Chris Wilson1b894b52010-12-14 20:04:54 +0000345static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
346 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800347{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800349 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800350
351 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100352 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000353 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354 limit = &intel_limits_ironlake_dual_lvds_100m;
355 else
356 limit = &intel_limits_ironlake_dual_lvds;
357 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000358 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800359 limit = &intel_limits_ironlake_single_lvds_100m;
360 else
361 limit = &intel_limits_ironlake_single_lvds;
362 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200363 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800364 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800365
366 return limit;
367}
368
Ma Ling044c7c42009-03-18 20:13:23 +0800369static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
370{
371 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800372 const intel_limit_t *limit;
373
374 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100375 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700376 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800377 else
Keith Packarde4b36692009-06-05 19:22:17 -0700378 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800379 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
380 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700381 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800382 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700383 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800384 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700385 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800386
387 return limit;
388}
389
Chris Wilson1b894b52010-12-14 20:04:54 +0000390static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800391{
392 struct drm_device *dev = crtc->dev;
393 const intel_limit_t *limit;
394
Eric Anholtbad720f2009-10-22 16:11:14 -0700395 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000396 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800397 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800398 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500399 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800400 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500401 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800402 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500403 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700404 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300405 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100406 } else if (!IS_GEN2(dev)) {
407 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
408 limit = &intel_limits_i9xx_lvds;
409 else
410 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800411 } else {
412 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700413 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200414 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700415 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200416 else
417 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800418 }
419 return limit;
420}
421
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500422/* m1 is reserved as 0 in Pineview, n is a ring counter */
423static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800424{
Shaohua Li21778322009-02-23 15:19:16 +0800425 clock->m = clock->m2 + 2;
426 clock->p = clock->p1 * clock->p2;
427 clock->vco = refclk * clock->m / clock->n;
428 clock->dot = clock->vco / clock->p;
429}
430
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200431static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
432{
433 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
434}
435
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200436static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800437{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200438 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800439 clock->p = clock->p1 * clock->p2;
440 clock->vco = refclk * clock->m / (clock->n + 2);
441 clock->dot = clock->vco / clock->p;
442}
443
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800444#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800445/**
446 * Returns whether the given set of divisors are valid for a given refclk with
447 * the given connectors.
448 */
449
Chris Wilson1b894b52010-12-14 20:04:54 +0000450static bool intel_PLL_is_valid(struct drm_device *dev,
451 const intel_limit_t *limit,
452 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800453{
Jesse Barnes79e53942008-11-07 14:24:08 -0800454 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400455 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800456 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400457 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800458 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400459 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800460 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400461 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500462 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400463 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800464 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800466 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800468 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400469 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800470 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
471 * connector, etc., rather than just a single range.
472 */
473 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800475
476 return true;
477}
478
Ma Lingd4906092009-03-18 20:13:27 +0800479static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200480i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800481 int target, int refclk, intel_clock_t *match_clock,
482 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800483{
484 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800486 int err = target;
487
Daniel Vettera210b022012-11-26 17:22:08 +0100488 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800489 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100490 * For LVDS just rely on its current settings for dual-channel.
491 * We haven't figured out how to reliably set up different
492 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100494 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 clock.p2 = limit->p2.p2_fast;
496 else
497 clock.p2 = limit->p2.p2_slow;
498 } else {
499 if (target < limit->p2.dot_limit)
500 clock.p2 = limit->p2.p2_slow;
501 else
502 clock.p2 = limit->p2.p2_fast;
503 }
504
Akshay Joshi0206e352011-08-16 15:34:10 -0400505 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800506
Zhao Yakui42158662009-11-20 11:24:18 +0800507 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
508 clock.m1++) {
509 for (clock.m2 = limit->m2.min;
510 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200511 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800512 break;
513 for (clock.n = limit->n.min;
514 clock.n <= limit->n.max; clock.n++) {
515 for (clock.p1 = limit->p1.min;
516 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800517 int this_err;
518
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200519 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000520 if (!intel_PLL_is_valid(dev, limit,
521 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800522 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800523 if (match_clock &&
524 clock.p != match_clock->p)
525 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800526
527 this_err = abs(clock.dot - target);
528 if (this_err < err) {
529 *best_clock = clock;
530 err = this_err;
531 }
532 }
533 }
534 }
535 }
536
537 return (err != target);
538}
539
Ma Lingd4906092009-03-18 20:13:27 +0800540static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200541pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
542 int target, int refclk, intel_clock_t *match_clock,
543 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200544{
545 struct drm_device *dev = crtc->dev;
546 intel_clock_t clock;
547 int err = target;
548
549 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
550 /*
551 * For LVDS just rely on its current settings for dual-channel.
552 * We haven't figured out how to reliably set up different
553 * single/dual channel state, if we even can.
554 */
555 if (intel_is_dual_link_lvds(dev))
556 clock.p2 = limit->p2.p2_fast;
557 else
558 clock.p2 = limit->p2.p2_slow;
559 } else {
560 if (target < limit->p2.dot_limit)
561 clock.p2 = limit->p2.p2_slow;
562 else
563 clock.p2 = limit->p2.p2_fast;
564 }
565
566 memset(best_clock, 0, sizeof(*best_clock));
567
568 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
569 clock.m1++) {
570 for (clock.m2 = limit->m2.min;
571 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200572 for (clock.n = limit->n.min;
573 clock.n <= limit->n.max; clock.n++) {
574 for (clock.p1 = limit->p1.min;
575 clock.p1 <= limit->p1.max; clock.p1++) {
576 int this_err;
577
578 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800579 if (!intel_PLL_is_valid(dev, limit,
580 &clock))
581 continue;
582 if (match_clock &&
583 clock.p != match_clock->p)
584 continue;
585
586 this_err = abs(clock.dot - target);
587 if (this_err < err) {
588 *best_clock = clock;
589 err = this_err;
590 }
591 }
592 }
593 }
594 }
595
596 return (err != target);
597}
598
Ma Lingd4906092009-03-18 20:13:27 +0800599static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200600g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
601 int target, int refclk, intel_clock_t *match_clock,
602 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800603{
604 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800605 intel_clock_t clock;
606 int max_n;
607 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400608 /* approximately equals target * 0.00585 */
609 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800610 found = false;
611
612 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100613 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800614 clock.p2 = limit->p2.p2_fast;
615 else
616 clock.p2 = limit->p2.p2_slow;
617 } else {
618 if (target < limit->p2.dot_limit)
619 clock.p2 = limit->p2.p2_slow;
620 else
621 clock.p2 = limit->p2.p2_fast;
622 }
623
624 memset(best_clock, 0, sizeof(*best_clock));
625 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200626 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800627 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200628 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800629 for (clock.m1 = limit->m1.max;
630 clock.m1 >= limit->m1.min; clock.m1--) {
631 for (clock.m2 = limit->m2.max;
632 clock.m2 >= limit->m2.min; clock.m2--) {
633 for (clock.p1 = limit->p1.max;
634 clock.p1 >= limit->p1.min; clock.p1--) {
635 int this_err;
636
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200637 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000638 if (!intel_PLL_is_valid(dev, limit,
639 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800640 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000641
642 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800643 if (this_err < err_most) {
644 *best_clock = clock;
645 err_most = this_err;
646 max_n = clock.n;
647 found = true;
648 }
649 }
650 }
651 }
652 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800653 return found;
654}
Ma Lingd4906092009-03-18 20:13:27 +0800655
Zhenyu Wang2c072452009-06-05 15:38:42 +0800656static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200657vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
658 int target, int refclk, intel_clock_t *match_clock,
659 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700660{
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300661 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300662 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300663 /* min update 19.2 MHz */
664 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300665 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700666
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300667 target *= 5; /* fast clock */
668
669 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700670
671 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300672 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300673 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300674 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300675 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300676 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700677 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300678 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300679 unsigned int ppm, diff;
680
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300681 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
682 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300683
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300684 vlv_clock(refclk, &clock);
685
686 if (clock.vco < limit->vco.min ||
687 clock.vco >= limit->vco.max)
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300688 continue;
689
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300690 diff = abs(clock.dot - target);
691 ppm = div_u64(1000000ULL * diff, target);
692
693 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300694 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300695 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300696 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300697 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300698
Ville Syrjäläc6861222013-09-24 21:26:21 +0300699 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300700 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300701 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300702 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700703 }
704 }
705 }
706 }
707 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700708
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300709 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700710}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700711
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300712bool intel_crtc_active(struct drm_crtc *crtc)
713{
714 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
715
716 /* Be paranoid as we can arrive here with only partial
717 * state retrieved from the hardware during setup.
718 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100719 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300720 * as Haswell has gained clock readout/fastboot support.
721 *
722 * We can ditch the crtc->fb check as soon as we can
723 * properly reconstruct framebuffers.
724 */
725 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100726 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300727}
728
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200729enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
730 enum pipe pipe)
731{
732 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
734
Daniel Vetter3b117c82013-04-17 20:15:07 +0200735 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200736}
737
Paulo Zanonia928d532012-05-04 17:18:15 -0300738static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
739{
740 struct drm_i915_private *dev_priv = dev->dev_private;
741 u32 frame, frame_reg = PIPEFRAME(pipe);
742
743 frame = I915_READ(frame_reg);
744
745 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
746 DRM_DEBUG_KMS("vblank wait timed out\n");
747}
748
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700749/**
750 * intel_wait_for_vblank - wait for vblank on a given pipe
751 * @dev: drm device
752 * @pipe: pipe to wait for
753 *
754 * Wait for vblank to occur on a given pipe. Needed for various bits of
755 * mode setting code.
756 */
757void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800758{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700759 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800760 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700761
Paulo Zanonia928d532012-05-04 17:18:15 -0300762 if (INTEL_INFO(dev)->gen >= 5) {
763 ironlake_wait_for_vblank(dev, pipe);
764 return;
765 }
766
Chris Wilson300387c2010-09-05 20:25:43 +0100767 /* Clear existing vblank status. Note this will clear any other
768 * sticky status fields as well.
769 *
770 * This races with i915_driver_irq_handler() with the result
771 * that either function could miss a vblank event. Here it is not
772 * fatal, as we will either wait upon the next vblank interrupt or
773 * timeout. Generally speaking intel_wait_for_vblank() is only
774 * called during modeset at which time the GPU should be idle and
775 * should *not* be performing page flips and thus not waiting on
776 * vblanks...
777 * Currently, the result of us stealing a vblank from the irq
778 * handler is that a single frame will be skipped during swapbuffers.
779 */
780 I915_WRITE(pipestat_reg,
781 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
782
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700783 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100784 if (wait_for(I915_READ(pipestat_reg) &
785 PIPE_VBLANK_INTERRUPT_STATUS,
786 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700787 DRM_DEBUG_KMS("vblank wait timed out\n");
788}
789
Keith Packardab7ad7f2010-10-03 00:33:06 -0700790/*
791 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700792 * @dev: drm device
793 * @pipe: pipe to wait for
794 *
795 * After disabling a pipe, we can't wait for vblank in the usual way,
796 * spinning on the vblank interrupt status bit, since we won't actually
797 * see an interrupt when the pipe is disabled.
798 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700799 * On Gen4 and above:
800 * wait for the pipe register state bit to turn off
801 *
802 * Otherwise:
803 * wait for the display line value to settle (it usually
804 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100805 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700806 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100807void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700808{
809 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200810 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
811 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700812
Keith Packardab7ad7f2010-10-03 00:33:06 -0700813 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200814 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700815
Keith Packardab7ad7f2010-10-03 00:33:06 -0700816 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100817 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
818 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200819 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700820 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300821 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100822 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700823 unsigned long timeout = jiffies + msecs_to_jiffies(100);
824
Paulo Zanoni837ba002012-05-04 17:18:14 -0300825 if (IS_GEN2(dev))
826 line_mask = DSL_LINEMASK_GEN2;
827 else
828 line_mask = DSL_LINEMASK_GEN3;
829
Keith Packardab7ad7f2010-10-03 00:33:06 -0700830 /* Wait for the display line to settle */
831 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300832 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700833 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300834 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700835 time_after(timeout, jiffies));
836 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200837 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700838 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800839}
840
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000841/*
842 * ibx_digital_port_connected - is the specified port connected?
843 * @dev_priv: i915 private structure
844 * @port: the port to test
845 *
846 * Returns true if @port is connected, false otherwise.
847 */
848bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
849 struct intel_digital_port *port)
850{
851 u32 bit;
852
Damien Lespiauc36346e2012-12-13 16:09:03 +0000853 if (HAS_PCH_IBX(dev_priv->dev)) {
854 switch(port->port) {
855 case PORT_B:
856 bit = SDE_PORTB_HOTPLUG;
857 break;
858 case PORT_C:
859 bit = SDE_PORTC_HOTPLUG;
860 break;
861 case PORT_D:
862 bit = SDE_PORTD_HOTPLUG;
863 break;
864 default:
865 return true;
866 }
867 } else {
868 switch(port->port) {
869 case PORT_B:
870 bit = SDE_PORTB_HOTPLUG_CPT;
871 break;
872 case PORT_C:
873 bit = SDE_PORTC_HOTPLUG_CPT;
874 break;
875 case PORT_D:
876 bit = SDE_PORTD_HOTPLUG_CPT;
877 break;
878 default:
879 return true;
880 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000881 }
882
883 return I915_READ(SDEISR) & bit;
884}
885
Jesse Barnesb24e7172011-01-04 15:09:30 -0800886static const char *state_string(bool enabled)
887{
888 return enabled ? "on" : "off";
889}
890
891/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200892void assert_pll(struct drm_i915_private *dev_priv,
893 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800894{
895 int reg;
896 u32 val;
897 bool cur_state;
898
899 reg = DPLL(pipe);
900 val = I915_READ(reg);
901 cur_state = !!(val & DPLL_VCO_ENABLE);
902 WARN(cur_state != state,
903 "PLL state assertion failure (expected %s, current %s)\n",
904 state_string(state), state_string(cur_state));
905}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800906
Jani Nikula23538ef2013-08-27 15:12:22 +0300907/* XXX: the dsi pll is shared between MIPI DSI ports */
908static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
909{
910 u32 val;
911 bool cur_state;
912
913 mutex_lock(&dev_priv->dpio_lock);
914 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
915 mutex_unlock(&dev_priv->dpio_lock);
916
917 cur_state = val & DSI_PLL_VCO_EN;
918 WARN(cur_state != state,
919 "DSI PLL state assertion failure (expected %s, current %s)\n",
920 state_string(state), state_string(cur_state));
921}
922#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
923#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
924
Daniel Vetter55607e82013-06-16 21:42:39 +0200925struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200926intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800927{
Daniel Vettere2b78262013-06-07 23:10:03 +0200928 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
929
Daniel Vettera43f6e02013-06-07 23:10:32 +0200930 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200931 return NULL;
932
Daniel Vettera43f6e02013-06-07 23:10:32 +0200933 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200934}
935
Jesse Barnesb24e7172011-01-04 15:09:30 -0800936/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200937void assert_shared_dpll(struct drm_i915_private *dev_priv,
938 struct intel_shared_dpll *pll,
939 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800940{
Jesse Barnes040484a2011-01-03 12:14:26 -0800941 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200942 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800943
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300944 if (HAS_PCH_LPT(dev_priv->dev)) {
945 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
946 return;
947 }
948
Chris Wilson92b27b02012-05-20 18:10:50 +0100949 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200950 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100951 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100952
Daniel Vetter53589012013-06-05 13:34:16 +0200953 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100954 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200955 "%s assertion failure (expected %s, current %s)\n",
956 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800957}
Jesse Barnes040484a2011-01-03 12:14:26 -0800958
959static void assert_fdi_tx(struct drm_i915_private *dev_priv,
960 enum pipe pipe, bool state)
961{
962 int reg;
963 u32 val;
964 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200965 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
966 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800967
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200968 if (HAS_DDI(dev_priv->dev)) {
969 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200970 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300971 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200972 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300973 } else {
974 reg = FDI_TX_CTL(pipe);
975 val = I915_READ(reg);
976 cur_state = !!(val & FDI_TX_ENABLE);
977 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800978 WARN(cur_state != state,
979 "FDI TX state assertion failure (expected %s, current %s)\n",
980 state_string(state), state_string(cur_state));
981}
982#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
983#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
984
985static void assert_fdi_rx(struct drm_i915_private *dev_priv,
986 enum pipe pipe, bool state)
987{
988 int reg;
989 u32 val;
990 bool cur_state;
991
Paulo Zanonid63fa0d2012-11-20 13:27:35 -0200992 reg = FDI_RX_CTL(pipe);
993 val = I915_READ(reg);
994 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -0800995 WARN(cur_state != state,
996 "FDI RX state assertion failure (expected %s, current %s)\n",
997 state_string(state), state_string(cur_state));
998}
999#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1000#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1001
1002static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1003 enum pipe pipe)
1004{
1005 int reg;
1006 u32 val;
1007
1008 /* ILK FDI PLL is always enabled */
1009 if (dev_priv->info->gen == 5)
1010 return;
1011
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001012 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001013 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001014 return;
1015
Jesse Barnes040484a2011-01-03 12:14:26 -08001016 reg = FDI_TX_CTL(pipe);
1017 val = I915_READ(reg);
1018 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1019}
1020
Daniel Vetter55607e82013-06-16 21:42:39 +02001021void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1022 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001023{
1024 int reg;
1025 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001026 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001027
1028 reg = FDI_RX_CTL(pipe);
1029 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001030 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1031 WARN(cur_state != state,
1032 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1033 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001034}
1035
Jesse Barnesea0760c2011-01-04 15:09:32 -08001036static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1037 enum pipe pipe)
1038{
1039 int pp_reg, lvds_reg;
1040 u32 val;
1041 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001042 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001043
1044 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1045 pp_reg = PCH_PP_CONTROL;
1046 lvds_reg = PCH_LVDS;
1047 } else {
1048 pp_reg = PP_CONTROL;
1049 lvds_reg = LVDS;
1050 }
1051
1052 val = I915_READ(pp_reg);
1053 if (!(val & PANEL_POWER_ON) ||
1054 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1055 locked = false;
1056
1057 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1058 panel_pipe = PIPE_B;
1059
1060 WARN(panel_pipe == pipe && locked,
1061 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001062 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001063}
1064
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001065static void assert_cursor(struct drm_i915_private *dev_priv,
1066 enum pipe pipe, bool state)
1067{
1068 struct drm_device *dev = dev_priv->dev;
1069 bool cur_state;
1070
1071 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1072 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1073 else if (IS_845G(dev) || IS_I865G(dev))
1074 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1075 else
1076 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1077
1078 WARN(cur_state != state,
1079 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1080 pipe_name(pipe), state_string(state), state_string(cur_state));
1081}
1082#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1083#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1084
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001085void assert_pipe(struct drm_i915_private *dev_priv,
1086 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001087{
1088 int reg;
1089 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001090 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001091 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1092 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001093
Daniel Vetter8e636782012-01-22 01:36:48 +01001094 /* if we need the pipe A quirk it must be always on */
1095 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1096 state = true;
1097
Paulo Zanonib97186f2013-05-03 12:15:36 -03001098 if (!intel_display_power_enabled(dev_priv->dev,
1099 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001100 cur_state = false;
1101 } else {
1102 reg = PIPECONF(cpu_transcoder);
1103 val = I915_READ(reg);
1104 cur_state = !!(val & PIPECONF_ENABLE);
1105 }
1106
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001107 WARN(cur_state != state,
1108 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001109 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001110}
1111
Chris Wilson931872f2012-01-16 23:01:13 +00001112static void assert_plane(struct drm_i915_private *dev_priv,
1113 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001114{
1115 int reg;
1116 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001117 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001118
1119 reg = DSPCNTR(plane);
1120 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001121 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1122 WARN(cur_state != state,
1123 "plane %c assertion failure (expected %s, current %s)\n",
1124 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001125}
1126
Chris Wilson931872f2012-01-16 23:01:13 +00001127#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1128#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1129
Jesse Barnesb24e7172011-01-04 15:09:30 -08001130static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1131 enum pipe pipe)
1132{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001133 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134 int reg, i;
1135 u32 val;
1136 int cur_pipe;
1137
Ville Syrjälä653e1022013-06-04 13:49:05 +03001138 /* Primary planes are fixed to pipes on gen4+ */
1139 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001140 reg = DSPCNTR(pipe);
1141 val = I915_READ(reg);
1142 WARN((val & DISPLAY_PLANE_ENABLE),
1143 "plane %c assertion failure, should be disabled but not\n",
1144 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001145 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001146 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001147
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001149 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001150 reg = DSPCNTR(i);
1151 val = I915_READ(reg);
1152 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1153 DISPPLANE_SEL_PIPE_SHIFT;
1154 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001155 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1156 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001157 }
1158}
1159
Jesse Barnes19332d72013-03-28 09:55:38 -07001160static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001163 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001164 int reg, i;
1165 u32 val;
1166
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001167 if (IS_VALLEYVIEW(dev)) {
1168 for (i = 0; i < dev_priv->num_plane; i++) {
1169 reg = SPCNTR(pipe, i);
1170 val = I915_READ(reg);
1171 WARN((val & SP_ENABLE),
1172 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1173 sprite_name(pipe, i), pipe_name(pipe));
1174 }
1175 } else if (INTEL_INFO(dev)->gen >= 7) {
1176 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001177 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001178 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001179 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001180 plane_name(pipe), pipe_name(pipe));
1181 } else if (INTEL_INFO(dev)->gen >= 5) {
1182 reg = DVSCNTR(pipe);
1183 val = I915_READ(reg);
1184 WARN((val & DVS_ENABLE),
1185 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1186 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001187 }
1188}
1189
Jesse Barnes92f25842011-01-04 15:09:34 -08001190static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1191{
1192 u32 val;
1193 bool enabled;
1194
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001195 if (HAS_PCH_LPT(dev_priv->dev)) {
1196 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1197 return;
1198 }
1199
Jesse Barnes92f25842011-01-04 15:09:34 -08001200 val = I915_READ(PCH_DREF_CONTROL);
1201 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1202 DREF_SUPERSPREAD_SOURCE_MASK));
1203 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1204}
1205
Daniel Vetterab9412b2013-05-03 11:49:46 +02001206static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1207 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001208{
1209 int reg;
1210 u32 val;
1211 bool enabled;
1212
Daniel Vetterab9412b2013-05-03 11:49:46 +02001213 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001214 val = I915_READ(reg);
1215 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001216 WARN(enabled,
1217 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1218 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001219}
1220
Keith Packard4e634382011-08-06 10:39:45 -07001221static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1222 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001223{
1224 if ((val & DP_PORT_EN) == 0)
1225 return false;
1226
1227 if (HAS_PCH_CPT(dev_priv->dev)) {
1228 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1229 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1230 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1231 return false;
1232 } else {
1233 if ((val & DP_PIPE_MASK) != (pipe << 30))
1234 return false;
1235 }
1236 return true;
1237}
1238
Keith Packard1519b992011-08-06 10:35:34 -07001239static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, u32 val)
1241{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001242 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001243 return false;
1244
1245 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001246 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001247 return false;
1248 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001249 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001250 return false;
1251 }
1252 return true;
1253}
1254
1255static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1256 enum pipe pipe, u32 val)
1257{
1258 if ((val & LVDS_PORT_EN) == 0)
1259 return false;
1260
1261 if (HAS_PCH_CPT(dev_priv->dev)) {
1262 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1263 return false;
1264 } else {
1265 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1266 return false;
1267 }
1268 return true;
1269}
1270
1271static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1272 enum pipe pipe, u32 val)
1273{
1274 if ((val & ADPA_DAC_ENABLE) == 0)
1275 return false;
1276 if (HAS_PCH_CPT(dev_priv->dev)) {
1277 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1278 return false;
1279 } else {
1280 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1281 return false;
1282 }
1283 return true;
1284}
1285
Jesse Barnes291906f2011-02-02 12:28:03 -08001286static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001287 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001288{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001289 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001290 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001291 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001292 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001293
Daniel Vetter75c5da22012-09-10 21:58:29 +02001294 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1295 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001296 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001297}
1298
1299static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1300 enum pipe pipe, int reg)
1301{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001302 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001303 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001304 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001305 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001306
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001307 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001308 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001309 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001310}
1311
1312static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1313 enum pipe pipe)
1314{
1315 int reg;
1316 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001317
Keith Packardf0575e92011-07-25 22:12:43 -07001318 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1319 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1320 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001321
1322 reg = PCH_ADPA;
1323 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001324 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001325 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001326 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001327
1328 reg = PCH_LVDS;
1329 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001330 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001331 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001332 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001333
Paulo Zanonie2debe92013-02-18 19:00:27 -03001334 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1335 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1336 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001337}
1338
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001339static void intel_init_dpio(struct drm_device *dev)
1340{
1341 struct drm_i915_private *dev_priv = dev->dev_private;
1342
1343 if (!IS_VALLEYVIEW(dev))
1344 return;
1345
1346 /*
1347 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1348 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1349 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1350 * b. The other bits such as sfr settings / modesel may all be set
1351 * to 0.
1352 *
1353 * This should only be done on init and resume from S3 with both
1354 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1355 */
1356 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1357}
1358
Daniel Vetter426115c2013-07-11 22:13:42 +02001359static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001360{
Daniel Vetter426115c2013-07-11 22:13:42 +02001361 struct drm_device *dev = crtc->base.dev;
1362 struct drm_i915_private *dev_priv = dev->dev_private;
1363 int reg = DPLL(crtc->pipe);
1364 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001365
Daniel Vetter426115c2013-07-11 22:13:42 +02001366 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001367
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001368 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001369 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1370
1371 /* PLL is protected by panel, make sure we can write it */
1372 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001373 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001374
Daniel Vetter426115c2013-07-11 22:13:42 +02001375 I915_WRITE(reg, dpll);
1376 POSTING_READ(reg);
1377 udelay(150);
1378
1379 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1380 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1381
1382 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1383 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001384
1385 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001386 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001387 POSTING_READ(reg);
1388 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001389 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001390 POSTING_READ(reg);
1391 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001392 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001393 POSTING_READ(reg);
1394 udelay(150); /* wait for warmup */
1395}
1396
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001397static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001398{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001399 struct drm_device *dev = crtc->base.dev;
1400 struct drm_i915_private *dev_priv = dev->dev_private;
1401 int reg = DPLL(crtc->pipe);
1402 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001403
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001404 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001405
1406 /* No really, not for ILK+ */
1407 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001408
1409 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001410 if (IS_MOBILE(dev) && !IS_I830(dev))
1411 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001412
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001413 I915_WRITE(reg, dpll);
1414
1415 /* Wait for the clocks to stabilize. */
1416 POSTING_READ(reg);
1417 udelay(150);
1418
1419 if (INTEL_INFO(dev)->gen >= 4) {
1420 I915_WRITE(DPLL_MD(crtc->pipe),
1421 crtc->config.dpll_hw_state.dpll_md);
1422 } else {
1423 /* The pixel multiplier can only be updated once the
1424 * DPLL is enabled and the clocks are stable.
1425 *
1426 * So write it again.
1427 */
1428 I915_WRITE(reg, dpll);
1429 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001430
1431 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001432 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001433 POSTING_READ(reg);
1434 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001435 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001436 POSTING_READ(reg);
1437 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001438 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001439 POSTING_READ(reg);
1440 udelay(150); /* wait for warmup */
1441}
1442
1443/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001444 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001445 * @dev_priv: i915 private structure
1446 * @pipe: pipe PLL to disable
1447 *
1448 * Disable the PLL for @pipe, making sure the pipe is off first.
1449 *
1450 * Note! This is for pre-ILK only.
1451 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001452static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001453{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001454 /* Don't disable pipe A or pipe A PLLs if needed */
1455 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1456 return;
1457
1458 /* Make sure the pipe isn't still relying on us */
1459 assert_pipe_disabled(dev_priv, pipe);
1460
Daniel Vetter50b44a42013-06-05 13:34:33 +02001461 I915_WRITE(DPLL(pipe), 0);
1462 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001463}
1464
Jesse Barnesf6071162013-10-01 10:41:38 -07001465static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1466{
1467 u32 val = 0;
1468
1469 /* Make sure the pipe isn't still relying on us */
1470 assert_pipe_disabled(dev_priv, pipe);
1471
1472 /* Leave integrated clock source enabled */
1473 if (pipe == PIPE_B)
1474 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1475 I915_WRITE(DPLL(pipe), val);
1476 POSTING_READ(DPLL(pipe));
1477}
1478
Jesse Barnes89b667f2013-04-18 14:51:36 -07001479void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1480{
1481 u32 port_mask;
1482
1483 if (!port)
1484 port_mask = DPLL_PORTB_READY_MASK;
1485 else
1486 port_mask = DPLL_PORTC_READY_MASK;
1487
1488 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1489 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1490 'B' + port, I915_READ(DPLL(0)));
1491}
1492
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001493/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001494 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001495 * @dev_priv: i915 private structure
1496 * @pipe: pipe PLL to enable
1497 *
1498 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1499 * drives the transcoder clock.
1500 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001501static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001502{
Daniel Vettere2b78262013-06-07 23:10:03 +02001503 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1504 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001505
Chris Wilson48da64a2012-05-13 20:16:12 +01001506 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001507 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001508 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001509 return;
1510
1511 if (WARN_ON(pll->refcount == 0))
1512 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001513
Daniel Vetter46edb022013-06-05 13:34:12 +02001514 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1515 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001516 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001517
Daniel Vettercdbd2312013-06-05 13:34:03 +02001518 if (pll->active++) {
1519 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001520 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001521 return;
1522 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001523 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001524
Daniel Vetter46edb022013-06-05 13:34:12 +02001525 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001526 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001527 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001528}
1529
Daniel Vettere2b78262013-06-07 23:10:03 +02001530static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001531{
Daniel Vettere2b78262013-06-07 23:10:03 +02001532 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1533 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001534
Jesse Barnes92f25842011-01-04 15:09:34 -08001535 /* PCH only available on ILK+ */
1536 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001537 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001538 return;
1539
Chris Wilson48da64a2012-05-13 20:16:12 +01001540 if (WARN_ON(pll->refcount == 0))
1541 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001542
Daniel Vetter46edb022013-06-05 13:34:12 +02001543 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1544 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001545 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001546
Chris Wilson48da64a2012-05-13 20:16:12 +01001547 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001548 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001549 return;
1550 }
1551
Daniel Vettere9d69442013-06-05 13:34:15 +02001552 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001553 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001554 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001555 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001556
Daniel Vetter46edb022013-06-05 13:34:12 +02001557 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001558 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001559 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001560}
1561
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001562static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1563 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001564{
Daniel Vetter23670b322012-11-01 09:15:30 +01001565 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001566 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001568 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001569
1570 /* PCH only available on ILK+ */
1571 BUG_ON(dev_priv->info->gen < 5);
1572
1573 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001574 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001575 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001576
1577 /* FDI must be feeding us bits for PCH ports */
1578 assert_fdi_tx_enabled(dev_priv, pipe);
1579 assert_fdi_rx_enabled(dev_priv, pipe);
1580
Daniel Vetter23670b322012-11-01 09:15:30 +01001581 if (HAS_PCH_CPT(dev)) {
1582 /* Workaround: Set the timing override bit before enabling the
1583 * pch transcoder. */
1584 reg = TRANS_CHICKEN2(pipe);
1585 val = I915_READ(reg);
1586 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1587 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001588 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001589
Daniel Vetterab9412b2013-05-03 11:49:46 +02001590 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001591 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001592 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001593
1594 if (HAS_PCH_IBX(dev_priv->dev)) {
1595 /*
1596 * make the BPC in transcoder be consistent with
1597 * that in pipeconf reg.
1598 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001599 val &= ~PIPECONF_BPC_MASK;
1600 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001601 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001602
1603 val &= ~TRANS_INTERLACE_MASK;
1604 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001605 if (HAS_PCH_IBX(dev_priv->dev) &&
1606 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1607 val |= TRANS_LEGACY_INTERLACED_ILK;
1608 else
1609 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001610 else
1611 val |= TRANS_PROGRESSIVE;
1612
Jesse Barnes040484a2011-01-03 12:14:26 -08001613 I915_WRITE(reg, val | TRANS_ENABLE);
1614 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001615 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001616}
1617
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001618static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001619 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001620{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001621 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001622
1623 /* PCH only available on ILK+ */
1624 BUG_ON(dev_priv->info->gen < 5);
1625
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001626 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001627 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001628 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001629
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001630 /* Workaround: set timing override bit. */
1631 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001632 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001633 I915_WRITE(_TRANSA_CHICKEN2, val);
1634
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001635 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001636 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001637
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001638 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1639 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001640 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001641 else
1642 val |= TRANS_PROGRESSIVE;
1643
Daniel Vetterab9412b2013-05-03 11:49:46 +02001644 I915_WRITE(LPT_TRANSCONF, val);
1645 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001646 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001647}
1648
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001649static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1650 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001651{
Daniel Vetter23670b322012-11-01 09:15:30 +01001652 struct drm_device *dev = dev_priv->dev;
1653 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001654
1655 /* FDI relies on the transcoder */
1656 assert_fdi_tx_disabled(dev_priv, pipe);
1657 assert_fdi_rx_disabled(dev_priv, pipe);
1658
Jesse Barnes291906f2011-02-02 12:28:03 -08001659 /* Ports must be off as well */
1660 assert_pch_ports_disabled(dev_priv, pipe);
1661
Daniel Vetterab9412b2013-05-03 11:49:46 +02001662 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001663 val = I915_READ(reg);
1664 val &= ~TRANS_ENABLE;
1665 I915_WRITE(reg, val);
1666 /* wait for PCH transcoder off, transcoder state */
1667 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001668 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001669
1670 if (!HAS_PCH_IBX(dev)) {
1671 /* Workaround: Clear the timing override chicken bit again. */
1672 reg = TRANS_CHICKEN2(pipe);
1673 val = I915_READ(reg);
1674 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1675 I915_WRITE(reg, val);
1676 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001677}
1678
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001679static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001680{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001681 u32 val;
1682
Daniel Vetterab9412b2013-05-03 11:49:46 +02001683 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001684 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001685 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001686 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001687 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001688 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001689
1690 /* Workaround: clear timing override bit. */
1691 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001692 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001693 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001694}
1695
1696/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001697 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001698 * @dev_priv: i915 private structure
1699 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001700 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001701 *
1702 * Enable @pipe, making sure that various hardware specific requirements
1703 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1704 *
1705 * @pipe should be %PIPE_A or %PIPE_B.
1706 *
1707 * Will wait until the pipe is actually running (i.e. first vblank) before
1708 * returning.
1709 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001710static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001711 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001712{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001713 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1714 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001715 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001716 int reg;
1717 u32 val;
1718
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001719 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001720 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001721 assert_sprites_disabled(dev_priv, pipe);
1722
Paulo Zanoni681e5812012-12-06 11:12:38 -02001723 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001724 pch_transcoder = TRANSCODER_A;
1725 else
1726 pch_transcoder = pipe;
1727
Jesse Barnesb24e7172011-01-04 15:09:30 -08001728 /*
1729 * A pipe without a PLL won't actually be able to drive bits from
1730 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1731 * need the check.
1732 */
1733 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001734 if (dsi)
1735 assert_dsi_pll_enabled(dev_priv);
1736 else
1737 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001738 else {
1739 if (pch_port) {
1740 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001741 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001742 assert_fdi_tx_pll_enabled(dev_priv,
1743 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001744 }
1745 /* FIXME: assert CPU port conditions for SNB+ */
1746 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001747
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001748 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001749 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001750 if (val & PIPECONF_ENABLE)
1751 return;
1752
1753 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001754 intel_wait_for_vblank(dev_priv->dev, pipe);
1755}
1756
1757/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001758 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001759 * @dev_priv: i915 private structure
1760 * @pipe: pipe to disable
1761 *
1762 * Disable @pipe, making sure that various hardware specific requirements
1763 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1764 *
1765 * @pipe should be %PIPE_A or %PIPE_B.
1766 *
1767 * Will wait until the pipe has shut down before returning.
1768 */
1769static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1770 enum pipe pipe)
1771{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001772 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1773 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001774 int reg;
1775 u32 val;
1776
1777 /*
1778 * Make sure planes won't keep trying to pump pixels to us,
1779 * or we might hang the display.
1780 */
1781 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001782 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001783 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001784
1785 /* Don't disable pipe A or pipe A PLLs if needed */
1786 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1787 return;
1788
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001789 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001790 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001791 if ((val & PIPECONF_ENABLE) == 0)
1792 return;
1793
1794 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001795 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1796}
1797
Keith Packardd74362c2011-07-28 14:47:14 -07001798/*
1799 * Plane regs are double buffered, going from enabled->disabled needs a
1800 * trigger in order to latch. The display address reg provides this.
1801 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001802void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001803 enum plane plane)
1804{
Damien Lespiau14f86142012-10-29 15:24:49 +00001805 if (dev_priv->info->gen >= 4)
1806 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1807 else
1808 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001809}
1810
Jesse Barnesb24e7172011-01-04 15:09:30 -08001811/**
1812 * intel_enable_plane - enable a display plane on a given pipe
1813 * @dev_priv: i915 private structure
1814 * @plane: plane to enable
1815 * @pipe: pipe being fed
1816 *
1817 * Enable @plane on @pipe, making sure that @pipe is running first.
1818 */
1819static void intel_enable_plane(struct drm_i915_private *dev_priv,
1820 enum plane plane, enum pipe pipe)
1821{
1822 int reg;
1823 u32 val;
1824
1825 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1826 assert_pipe_enabled(dev_priv, pipe);
1827
1828 reg = DSPCNTR(plane);
1829 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001830 if (val & DISPLAY_PLANE_ENABLE)
1831 return;
1832
1833 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001834 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001835 intel_wait_for_vblank(dev_priv->dev, pipe);
1836}
1837
Jesse Barnesb24e7172011-01-04 15:09:30 -08001838/**
1839 * intel_disable_plane - disable a display plane
1840 * @dev_priv: i915 private structure
1841 * @plane: plane to disable
1842 * @pipe: pipe consuming the data
1843 *
1844 * Disable @plane; should be an independent operation.
1845 */
1846static void intel_disable_plane(struct drm_i915_private *dev_priv,
1847 enum plane plane, enum pipe pipe)
1848{
1849 int reg;
1850 u32 val;
1851
1852 reg = DSPCNTR(plane);
1853 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001854 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1855 return;
1856
1857 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001858 intel_flush_display_plane(dev_priv, plane);
1859 intel_wait_for_vblank(dev_priv->dev, pipe);
1860}
1861
Chris Wilson693db182013-03-05 14:52:39 +00001862static bool need_vtd_wa(struct drm_device *dev)
1863{
1864#ifdef CONFIG_INTEL_IOMMU
1865 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1866 return true;
1867#endif
1868 return false;
1869}
1870
Chris Wilson127bd2a2010-07-23 23:32:05 +01001871int
Chris Wilson48b956c2010-09-14 12:50:34 +01001872intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001873 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001874 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001875{
Chris Wilsonce453d82011-02-21 14:43:56 +00001876 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001877 u32 alignment;
1878 int ret;
1879
Chris Wilson05394f32010-11-08 19:18:58 +00001880 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001881 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001882 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1883 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001884 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001885 alignment = 4 * 1024;
1886 else
1887 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001888 break;
1889 case I915_TILING_X:
1890 /* pin() will align the object as required by fence */
1891 alignment = 0;
1892 break;
1893 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001894 /* Despite that we check this in framebuffer_init userspace can
1895 * screw us over and change the tiling after the fact. Only
1896 * pinned buffers can't change their tiling. */
1897 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001898 return -EINVAL;
1899 default:
1900 BUG();
1901 }
1902
Chris Wilson693db182013-03-05 14:52:39 +00001903 /* Note that the w/a also requires 64 PTE of padding following the
1904 * bo. We currently fill all unused PTE with the shadow page and so
1905 * we should always have valid PTE following the scanout preventing
1906 * the VT-d warning.
1907 */
1908 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1909 alignment = 256 * 1024;
1910
Chris Wilsonce453d82011-02-21 14:43:56 +00001911 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001912 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001913 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001914 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001915
1916 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1917 * fence, whereas 965+ only requires a fence if using
1918 * framebuffer compression. For simplicity, we always install
1919 * a fence as the cost is not that onerous.
1920 */
Chris Wilson06d98132012-04-17 15:31:24 +01001921 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001922 if (ret)
1923 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001924
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001925 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001926
Chris Wilsonce453d82011-02-21 14:43:56 +00001927 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001928 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001929
1930err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001931 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001932err_interruptible:
1933 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001934 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001935}
1936
Chris Wilson1690e1e2011-12-14 13:57:08 +01001937void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1938{
1939 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001940 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001941}
1942
Daniel Vetterc2c75132012-07-05 12:17:30 +02001943/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1944 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001945unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1946 unsigned int tiling_mode,
1947 unsigned int cpp,
1948 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001949{
Chris Wilsonbc752862013-02-21 20:04:31 +00001950 if (tiling_mode != I915_TILING_NONE) {
1951 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001952
Chris Wilsonbc752862013-02-21 20:04:31 +00001953 tile_rows = *y / 8;
1954 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001955
Chris Wilsonbc752862013-02-21 20:04:31 +00001956 tiles = *x / (512/cpp);
1957 *x %= 512/cpp;
1958
1959 return tile_rows * pitch * 8 + tiles * 4096;
1960 } else {
1961 unsigned int offset;
1962
1963 offset = *y * pitch + *x * cpp;
1964 *y = 0;
1965 *x = (offset & 4095) / cpp;
1966 return offset & -4096;
1967 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001968}
1969
Jesse Barnes17638cd2011-06-24 12:19:23 -07001970static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1971 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001972{
1973 struct drm_device *dev = crtc->dev;
1974 struct drm_i915_private *dev_priv = dev->dev_private;
1975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1976 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001977 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001978 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001979 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001980 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001981 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001982
1983 switch (plane) {
1984 case 0:
1985 case 1:
1986 break;
1987 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001988 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001989 return -EINVAL;
1990 }
1991
1992 intel_fb = to_intel_framebuffer(fb);
1993 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001994
Chris Wilson5eddb702010-09-11 13:48:45 +01001995 reg = DSPCNTR(plane);
1996 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001997 /* Mask out pixel format bits in case we change it */
1998 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001999 switch (fb->pixel_format) {
2000 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002001 dspcntr |= DISPPLANE_8BPP;
2002 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002003 case DRM_FORMAT_XRGB1555:
2004 case DRM_FORMAT_ARGB1555:
2005 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002006 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002007 case DRM_FORMAT_RGB565:
2008 dspcntr |= DISPPLANE_BGRX565;
2009 break;
2010 case DRM_FORMAT_XRGB8888:
2011 case DRM_FORMAT_ARGB8888:
2012 dspcntr |= DISPPLANE_BGRX888;
2013 break;
2014 case DRM_FORMAT_XBGR8888:
2015 case DRM_FORMAT_ABGR8888:
2016 dspcntr |= DISPPLANE_RGBX888;
2017 break;
2018 case DRM_FORMAT_XRGB2101010:
2019 case DRM_FORMAT_ARGB2101010:
2020 dspcntr |= DISPPLANE_BGRX101010;
2021 break;
2022 case DRM_FORMAT_XBGR2101010:
2023 case DRM_FORMAT_ABGR2101010:
2024 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002025 break;
2026 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002027 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002028 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002029
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002030 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002031 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002032 dspcntr |= DISPPLANE_TILED;
2033 else
2034 dspcntr &= ~DISPPLANE_TILED;
2035 }
2036
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002037 if (IS_G4X(dev))
2038 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2039
Chris Wilson5eddb702010-09-11 13:48:45 +01002040 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002041
Daniel Vettere506a0c2012-07-05 12:17:29 +02002042 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002043
Daniel Vetterc2c75132012-07-05 12:17:30 +02002044 if (INTEL_INFO(dev)->gen >= 4) {
2045 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002046 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2047 fb->bits_per_pixel / 8,
2048 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002049 linear_offset -= intel_crtc->dspaddr_offset;
2050 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002051 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002052 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002053
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002054 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2055 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2056 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002057 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002058 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002059 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002060 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002061 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002062 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002063 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002064 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002065 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002066
Jesse Barnes17638cd2011-06-24 12:19:23 -07002067 return 0;
2068}
2069
2070static int ironlake_update_plane(struct drm_crtc *crtc,
2071 struct drm_framebuffer *fb, int x, int y)
2072{
2073 struct drm_device *dev = crtc->dev;
2074 struct drm_i915_private *dev_priv = dev->dev_private;
2075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2076 struct intel_framebuffer *intel_fb;
2077 struct drm_i915_gem_object *obj;
2078 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002079 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002080 u32 dspcntr;
2081 u32 reg;
2082
2083 switch (plane) {
2084 case 0:
2085 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002086 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002087 break;
2088 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002089 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002090 return -EINVAL;
2091 }
2092
2093 intel_fb = to_intel_framebuffer(fb);
2094 obj = intel_fb->obj;
2095
2096 reg = DSPCNTR(plane);
2097 dspcntr = I915_READ(reg);
2098 /* Mask out pixel format bits in case we change it */
2099 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002100 switch (fb->pixel_format) {
2101 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 dspcntr |= DISPPLANE_8BPP;
2103 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002104 case DRM_FORMAT_RGB565:
2105 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002106 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002107 case DRM_FORMAT_XRGB8888:
2108 case DRM_FORMAT_ARGB8888:
2109 dspcntr |= DISPPLANE_BGRX888;
2110 break;
2111 case DRM_FORMAT_XBGR8888:
2112 case DRM_FORMAT_ABGR8888:
2113 dspcntr |= DISPPLANE_RGBX888;
2114 break;
2115 case DRM_FORMAT_XRGB2101010:
2116 case DRM_FORMAT_ARGB2101010:
2117 dspcntr |= DISPPLANE_BGRX101010;
2118 break;
2119 case DRM_FORMAT_XBGR2101010:
2120 case DRM_FORMAT_ABGR2101010:
2121 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002122 break;
2123 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002124 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002125 }
2126
2127 if (obj->tiling_mode != I915_TILING_NONE)
2128 dspcntr |= DISPPLANE_TILED;
2129 else
2130 dspcntr &= ~DISPPLANE_TILED;
2131
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002132 if (IS_HASWELL(dev))
2133 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2134 else
2135 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002136
2137 I915_WRITE(reg, dspcntr);
2138
Daniel Vettere506a0c2012-07-05 12:17:29 +02002139 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002140 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002141 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2142 fb->bits_per_pixel / 8,
2143 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002144 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002145
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002146 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2147 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2148 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002149 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002150 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002151 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002152 if (IS_HASWELL(dev)) {
2153 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2154 } else {
2155 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2156 I915_WRITE(DSPLINOFF(plane), linear_offset);
2157 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002158 POSTING_READ(reg);
2159
2160 return 0;
2161}
2162
2163/* Assume fb object is pinned & idle & fenced and just update base pointers */
2164static int
2165intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2166 int x, int y, enum mode_set_atomic state)
2167{
2168 struct drm_device *dev = crtc->dev;
2169 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002170
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002171 if (dev_priv->display.disable_fbc)
2172 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002173 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002174
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002175 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002176}
2177
Ville Syrjälä96a02912013-02-18 19:08:49 +02002178void intel_display_handle_reset(struct drm_device *dev)
2179{
2180 struct drm_i915_private *dev_priv = dev->dev_private;
2181 struct drm_crtc *crtc;
2182
2183 /*
2184 * Flips in the rings have been nuked by the reset,
2185 * so complete all pending flips so that user space
2186 * will get its events and not get stuck.
2187 *
2188 * Also update the base address of all primary
2189 * planes to the the last fb to make sure we're
2190 * showing the correct fb after a reset.
2191 *
2192 * Need to make two loops over the crtcs so that we
2193 * don't try to grab a crtc mutex before the
2194 * pending_flip_queue really got woken up.
2195 */
2196
2197 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2199 enum plane plane = intel_crtc->plane;
2200
2201 intel_prepare_page_flip(dev, plane);
2202 intel_finish_page_flip_plane(dev, plane);
2203 }
2204
2205 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2207
2208 mutex_lock(&crtc->mutex);
2209 if (intel_crtc->active)
2210 dev_priv->display.update_plane(crtc, crtc->fb,
2211 crtc->x, crtc->y);
2212 mutex_unlock(&crtc->mutex);
2213 }
2214}
2215
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002216static int
Chris Wilson14667a42012-04-03 17:58:35 +01002217intel_finish_fb(struct drm_framebuffer *old_fb)
2218{
2219 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2220 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2221 bool was_interruptible = dev_priv->mm.interruptible;
2222 int ret;
2223
Chris Wilson14667a42012-04-03 17:58:35 +01002224 /* Big Hammer, we also need to ensure that any pending
2225 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2226 * current scanout is retired before unpinning the old
2227 * framebuffer.
2228 *
2229 * This should only fail upon a hung GPU, in which case we
2230 * can safely continue.
2231 */
2232 dev_priv->mm.interruptible = false;
2233 ret = i915_gem_object_finish_gpu(obj);
2234 dev_priv->mm.interruptible = was_interruptible;
2235
2236 return ret;
2237}
2238
Ville Syrjälä198598d2012-10-31 17:50:24 +02002239static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2240{
2241 struct drm_device *dev = crtc->dev;
2242 struct drm_i915_master_private *master_priv;
2243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2244
2245 if (!dev->primary->master)
2246 return;
2247
2248 master_priv = dev->primary->master->driver_priv;
2249 if (!master_priv->sarea_priv)
2250 return;
2251
2252 switch (intel_crtc->pipe) {
2253 case 0:
2254 master_priv->sarea_priv->pipeA_x = x;
2255 master_priv->sarea_priv->pipeA_y = y;
2256 break;
2257 case 1:
2258 master_priv->sarea_priv->pipeB_x = x;
2259 master_priv->sarea_priv->pipeB_y = y;
2260 break;
2261 default:
2262 break;
2263 }
2264}
2265
Chris Wilson14667a42012-04-03 17:58:35 +01002266static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002267intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002268 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002269{
2270 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002271 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002273 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002274 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002275
2276 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002277 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002278 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002279 return 0;
2280 }
2281
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002282 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002283 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2284 plane_name(intel_crtc->plane),
2285 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002286 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002287 }
2288
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002289 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002290 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002291 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002292 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002293 if (ret != 0) {
2294 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002295 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002296 return ret;
2297 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002298
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002299 /*
2300 * Update pipe size and adjust fitter if needed: the reason for this is
2301 * that in compute_mode_changes we check the native mode (not the pfit
2302 * mode) to see if we can flip rather than do a full mode set. In the
2303 * fastboot case, we'll flip, but if we don't update the pipesrc and
2304 * pfit state, we'll end up with a big fb scanned out into the wrong
2305 * sized surface.
2306 *
2307 * To fix this properly, we need to hoist the checks up into
2308 * compute_mode_changes (or above), check the actual pfit state and
2309 * whether the platform allows pfit disable with pipe active, and only
2310 * then update the pipesrc and pfit state, even on the flip path.
2311 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002312 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002313 const struct drm_display_mode *adjusted_mode =
2314 &intel_crtc->config.adjusted_mode;
2315
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002316 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002317 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2318 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002319 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002320 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2321 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2322 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2323 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2324 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2325 }
2326 }
2327
Daniel Vetter94352cf2012-07-05 22:51:56 +02002328 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002329 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002330 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002331 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002332 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002333 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002334 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002335
Daniel Vetter94352cf2012-07-05 22:51:56 +02002336 old_fb = crtc->fb;
2337 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002338 crtc->x = x;
2339 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002340
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002341 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002342 if (intel_crtc->active && old_fb != fb)
2343 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002344 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002345 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002346
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002347 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002348 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002349 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002350
Ville Syrjälä198598d2012-10-31 17:50:24 +02002351 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002352
2353 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002354}
2355
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002356static void intel_fdi_normal_train(struct drm_crtc *crtc)
2357{
2358 struct drm_device *dev = crtc->dev;
2359 struct drm_i915_private *dev_priv = dev->dev_private;
2360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2361 int pipe = intel_crtc->pipe;
2362 u32 reg, temp;
2363
2364 /* enable normal train */
2365 reg = FDI_TX_CTL(pipe);
2366 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002367 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002368 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2369 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002370 } else {
2371 temp &= ~FDI_LINK_TRAIN_NONE;
2372 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002373 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002374 I915_WRITE(reg, temp);
2375
2376 reg = FDI_RX_CTL(pipe);
2377 temp = I915_READ(reg);
2378 if (HAS_PCH_CPT(dev)) {
2379 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2380 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2381 } else {
2382 temp &= ~FDI_LINK_TRAIN_NONE;
2383 temp |= FDI_LINK_TRAIN_NONE;
2384 }
2385 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2386
2387 /* wait one idle pattern time */
2388 POSTING_READ(reg);
2389 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002390
2391 /* IVB wants error correction enabled */
2392 if (IS_IVYBRIDGE(dev))
2393 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2394 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002395}
2396
Daniel Vetter1e833f42013-02-19 22:31:57 +01002397static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2398{
2399 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2400}
2401
Daniel Vetter01a415f2012-10-27 15:58:40 +02002402static void ivb_modeset_global_resources(struct drm_device *dev)
2403{
2404 struct drm_i915_private *dev_priv = dev->dev_private;
2405 struct intel_crtc *pipe_B_crtc =
2406 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2407 struct intel_crtc *pipe_C_crtc =
2408 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2409 uint32_t temp;
2410
Daniel Vetter1e833f42013-02-19 22:31:57 +01002411 /*
2412 * When everything is off disable fdi C so that we could enable fdi B
2413 * with all lanes. Note that we don't care about enabled pipes without
2414 * an enabled pch encoder.
2415 */
2416 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2417 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002418 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2419 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2420
2421 temp = I915_READ(SOUTH_CHICKEN1);
2422 temp &= ~FDI_BC_BIFURCATION_SELECT;
2423 DRM_DEBUG_KMS("disabling fdi C rx\n");
2424 I915_WRITE(SOUTH_CHICKEN1, temp);
2425 }
2426}
2427
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002428/* The FDI link training functions for ILK/Ibexpeak. */
2429static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2430{
2431 struct drm_device *dev = crtc->dev;
2432 struct drm_i915_private *dev_priv = dev->dev_private;
2433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2434 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002435 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002436 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002438 /* FDI needs bits from pipe & plane first */
2439 assert_pipe_enabled(dev_priv, pipe);
2440 assert_plane_enabled(dev_priv, plane);
2441
Adam Jacksone1a44742010-06-25 15:32:14 -04002442 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2443 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002444 reg = FDI_RX_IMR(pipe);
2445 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002446 temp &= ~FDI_RX_SYMBOL_LOCK;
2447 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002448 I915_WRITE(reg, temp);
2449 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002450 udelay(150);
2451
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002452 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002453 reg = FDI_TX_CTL(pipe);
2454 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002455 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2456 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002457 temp &= ~FDI_LINK_TRAIN_NONE;
2458 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002459 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002460
Chris Wilson5eddb702010-09-11 13:48:45 +01002461 reg = FDI_RX_CTL(pipe);
2462 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002463 temp &= ~FDI_LINK_TRAIN_NONE;
2464 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002465 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2466
2467 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002468 udelay(150);
2469
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002470 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002471 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2472 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2473 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002474
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002476 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002477 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2479
2480 if ((temp & FDI_RX_BIT_LOCK)) {
2481 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002482 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 break;
2484 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002485 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002486 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488
2489 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002490 reg = FDI_TX_CTL(pipe);
2491 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002492 temp &= ~FDI_LINK_TRAIN_NONE;
2493 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495
Chris Wilson5eddb702010-09-11 13:48:45 +01002496 reg = FDI_RX_CTL(pipe);
2497 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498 temp &= ~FDI_LINK_TRAIN_NONE;
2499 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 I915_WRITE(reg, temp);
2501
2502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002503 udelay(150);
2504
Chris Wilson5eddb702010-09-11 13:48:45 +01002505 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002506 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002508 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2509
2510 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002512 DRM_DEBUG_KMS("FDI train 2 done.\n");
2513 break;
2514 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002515 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002516 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002517 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002518
2519 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002520
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002521}
2522
Akshay Joshi0206e352011-08-16 15:34:10 -04002523static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002524 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2525 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2526 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2527 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2528};
2529
2530/* The FDI link training functions for SNB/Cougarpoint. */
2531static void gen6_fdi_link_train(struct drm_crtc *crtc)
2532{
2533 struct drm_device *dev = crtc->dev;
2534 struct drm_i915_private *dev_priv = dev->dev_private;
2535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2536 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002537 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002538
Adam Jacksone1a44742010-06-25 15:32:14 -04002539 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2540 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = FDI_RX_IMR(pipe);
2542 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002543 temp &= ~FDI_RX_SYMBOL_LOCK;
2544 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 I915_WRITE(reg, temp);
2546
2547 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002548 udelay(150);
2549
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002550 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002551 reg = FDI_TX_CTL(pipe);
2552 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002553 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2554 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002555 temp &= ~FDI_LINK_TRAIN_NONE;
2556 temp |= FDI_LINK_TRAIN_PATTERN_1;
2557 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2558 /* SNB-B */
2559 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002560 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002561
Daniel Vetterd74cf322012-10-26 10:58:13 +02002562 I915_WRITE(FDI_RX_MISC(pipe),
2563 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2564
Chris Wilson5eddb702010-09-11 13:48:45 +01002565 reg = FDI_RX_CTL(pipe);
2566 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002567 if (HAS_PCH_CPT(dev)) {
2568 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2569 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2570 } else {
2571 temp &= ~FDI_LINK_TRAIN_NONE;
2572 temp |= FDI_LINK_TRAIN_PATTERN_1;
2573 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002574 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2575
2576 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002577 udelay(150);
2578
Akshay Joshi0206e352011-08-16 15:34:10 -04002579 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002580 reg = FDI_TX_CTL(pipe);
2581 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002582 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2583 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002584 I915_WRITE(reg, temp);
2585
2586 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587 udelay(500);
2588
Sean Paulfa37d392012-03-02 12:53:39 -05002589 for (retry = 0; retry < 5; retry++) {
2590 reg = FDI_RX_IIR(pipe);
2591 temp = I915_READ(reg);
2592 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2593 if (temp & FDI_RX_BIT_LOCK) {
2594 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2595 DRM_DEBUG_KMS("FDI train 1 done.\n");
2596 break;
2597 }
2598 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002599 }
Sean Paulfa37d392012-03-02 12:53:39 -05002600 if (retry < 5)
2601 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002602 }
2603 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002604 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002605
2606 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002607 reg = FDI_TX_CTL(pipe);
2608 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002609 temp &= ~FDI_LINK_TRAIN_NONE;
2610 temp |= FDI_LINK_TRAIN_PATTERN_2;
2611 if (IS_GEN6(dev)) {
2612 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2613 /* SNB-B */
2614 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2615 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617
Chris Wilson5eddb702010-09-11 13:48:45 +01002618 reg = FDI_RX_CTL(pipe);
2619 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002620 if (HAS_PCH_CPT(dev)) {
2621 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2622 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2623 } else {
2624 temp &= ~FDI_LINK_TRAIN_NONE;
2625 temp |= FDI_LINK_TRAIN_PATTERN_2;
2626 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002627 I915_WRITE(reg, temp);
2628
2629 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002630 udelay(150);
2631
Akshay Joshi0206e352011-08-16 15:34:10 -04002632 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002633 reg = FDI_TX_CTL(pipe);
2634 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002635 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2636 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002637 I915_WRITE(reg, temp);
2638
2639 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002640 udelay(500);
2641
Sean Paulfa37d392012-03-02 12:53:39 -05002642 for (retry = 0; retry < 5; retry++) {
2643 reg = FDI_RX_IIR(pipe);
2644 temp = I915_READ(reg);
2645 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2646 if (temp & FDI_RX_SYMBOL_LOCK) {
2647 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2648 DRM_DEBUG_KMS("FDI train 2 done.\n");
2649 break;
2650 }
2651 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002652 }
Sean Paulfa37d392012-03-02 12:53:39 -05002653 if (retry < 5)
2654 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002655 }
2656 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002657 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002658
2659 DRM_DEBUG_KMS("FDI train done.\n");
2660}
2661
Jesse Barnes357555c2011-04-28 15:09:55 -07002662/* Manual link training for Ivy Bridge A0 parts */
2663static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2664{
2665 struct drm_device *dev = crtc->dev;
2666 struct drm_i915_private *dev_priv = dev->dev_private;
2667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2668 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002669 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002670
2671 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2672 for train result */
2673 reg = FDI_RX_IMR(pipe);
2674 temp = I915_READ(reg);
2675 temp &= ~FDI_RX_SYMBOL_LOCK;
2676 temp &= ~FDI_RX_BIT_LOCK;
2677 I915_WRITE(reg, temp);
2678
2679 POSTING_READ(reg);
2680 udelay(150);
2681
Daniel Vetter01a415f2012-10-27 15:58:40 +02002682 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2683 I915_READ(FDI_RX_IIR(pipe)));
2684
Jesse Barnes139ccd32013-08-19 11:04:55 -07002685 /* Try each vswing and preemphasis setting twice before moving on */
2686 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2687 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002688 reg = FDI_TX_CTL(pipe);
2689 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002690 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2691 temp &= ~FDI_TX_ENABLE;
2692 I915_WRITE(reg, temp);
2693
2694 reg = FDI_RX_CTL(pipe);
2695 temp = I915_READ(reg);
2696 temp &= ~FDI_LINK_TRAIN_AUTO;
2697 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2698 temp &= ~FDI_RX_ENABLE;
2699 I915_WRITE(reg, temp);
2700
2701 /* enable CPU FDI TX and PCH FDI RX */
2702 reg = FDI_TX_CTL(pipe);
2703 temp = I915_READ(reg);
2704 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2705 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2706 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002707 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002708 temp |= snb_b_fdi_train_param[j/2];
2709 temp |= FDI_COMPOSITE_SYNC;
2710 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2711
2712 I915_WRITE(FDI_RX_MISC(pipe),
2713 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2714
2715 reg = FDI_RX_CTL(pipe);
2716 temp = I915_READ(reg);
2717 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2718 temp |= FDI_COMPOSITE_SYNC;
2719 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2720
2721 POSTING_READ(reg);
2722 udelay(1); /* should be 0.5us */
2723
2724 for (i = 0; i < 4; i++) {
2725 reg = FDI_RX_IIR(pipe);
2726 temp = I915_READ(reg);
2727 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2728
2729 if (temp & FDI_RX_BIT_LOCK ||
2730 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2731 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2732 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2733 i);
2734 break;
2735 }
2736 udelay(1); /* should be 0.5us */
2737 }
2738 if (i == 4) {
2739 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2740 continue;
2741 }
2742
2743 /* Train 2 */
2744 reg = FDI_TX_CTL(pipe);
2745 temp = I915_READ(reg);
2746 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2747 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2748 I915_WRITE(reg, temp);
2749
2750 reg = FDI_RX_CTL(pipe);
2751 temp = I915_READ(reg);
2752 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2753 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002754 I915_WRITE(reg, temp);
2755
2756 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002757 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002758
Jesse Barnes139ccd32013-08-19 11:04:55 -07002759 for (i = 0; i < 4; i++) {
2760 reg = FDI_RX_IIR(pipe);
2761 temp = I915_READ(reg);
2762 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002763
Jesse Barnes139ccd32013-08-19 11:04:55 -07002764 if (temp & FDI_RX_SYMBOL_LOCK ||
2765 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2766 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2767 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2768 i);
2769 goto train_done;
2770 }
2771 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002772 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002773 if (i == 4)
2774 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002775 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002776
Jesse Barnes139ccd32013-08-19 11:04:55 -07002777train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002778 DRM_DEBUG_KMS("FDI train done.\n");
2779}
2780
Daniel Vetter88cefb62012-08-12 19:27:14 +02002781static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002782{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002783 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002784 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002785 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002786 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002787
Jesse Barnesc64e3112010-09-10 11:27:03 -07002788
Jesse Barnes0e23b992010-09-10 11:10:00 -07002789 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002790 reg = FDI_RX_CTL(pipe);
2791 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002792 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2793 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002794 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002795 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2796
2797 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002798 udelay(200);
2799
2800 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002801 temp = I915_READ(reg);
2802 I915_WRITE(reg, temp | FDI_PCDCLK);
2803
2804 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002805 udelay(200);
2806
Paulo Zanoni20749732012-11-23 15:30:38 -02002807 /* Enable CPU FDI TX PLL, always on for Ironlake */
2808 reg = FDI_TX_CTL(pipe);
2809 temp = I915_READ(reg);
2810 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2811 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002812
Paulo Zanoni20749732012-11-23 15:30:38 -02002813 POSTING_READ(reg);
2814 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002815 }
2816}
2817
Daniel Vetter88cefb62012-08-12 19:27:14 +02002818static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2819{
2820 struct drm_device *dev = intel_crtc->base.dev;
2821 struct drm_i915_private *dev_priv = dev->dev_private;
2822 int pipe = intel_crtc->pipe;
2823 u32 reg, temp;
2824
2825 /* Switch from PCDclk to Rawclk */
2826 reg = FDI_RX_CTL(pipe);
2827 temp = I915_READ(reg);
2828 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2829
2830 /* Disable CPU FDI TX PLL */
2831 reg = FDI_TX_CTL(pipe);
2832 temp = I915_READ(reg);
2833 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2834
2835 POSTING_READ(reg);
2836 udelay(100);
2837
2838 reg = FDI_RX_CTL(pipe);
2839 temp = I915_READ(reg);
2840 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2841
2842 /* Wait for the clocks to turn off. */
2843 POSTING_READ(reg);
2844 udelay(100);
2845}
2846
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002847static void ironlake_fdi_disable(struct drm_crtc *crtc)
2848{
2849 struct drm_device *dev = crtc->dev;
2850 struct drm_i915_private *dev_priv = dev->dev_private;
2851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2852 int pipe = intel_crtc->pipe;
2853 u32 reg, temp;
2854
2855 /* disable CPU FDI tx and PCH FDI rx */
2856 reg = FDI_TX_CTL(pipe);
2857 temp = I915_READ(reg);
2858 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2859 POSTING_READ(reg);
2860
2861 reg = FDI_RX_CTL(pipe);
2862 temp = I915_READ(reg);
2863 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002864 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002865 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2866
2867 POSTING_READ(reg);
2868 udelay(100);
2869
2870 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002871 if (HAS_PCH_IBX(dev)) {
2872 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002873 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002874
2875 /* still set train pattern 1 */
2876 reg = FDI_TX_CTL(pipe);
2877 temp = I915_READ(reg);
2878 temp &= ~FDI_LINK_TRAIN_NONE;
2879 temp |= FDI_LINK_TRAIN_PATTERN_1;
2880 I915_WRITE(reg, temp);
2881
2882 reg = FDI_RX_CTL(pipe);
2883 temp = I915_READ(reg);
2884 if (HAS_PCH_CPT(dev)) {
2885 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2886 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2887 } else {
2888 temp &= ~FDI_LINK_TRAIN_NONE;
2889 temp |= FDI_LINK_TRAIN_PATTERN_1;
2890 }
2891 /* BPC in FDI rx is consistent with that in PIPECONF */
2892 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002893 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002894 I915_WRITE(reg, temp);
2895
2896 POSTING_READ(reg);
2897 udelay(100);
2898}
2899
Chris Wilson5bb61642012-09-27 21:25:58 +01002900static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2901{
2902 struct drm_device *dev = crtc->dev;
2903 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002905 unsigned long flags;
2906 bool pending;
2907
Ville Syrjälä10d83732013-01-29 18:13:34 +02002908 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2909 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002910 return false;
2911
2912 spin_lock_irqsave(&dev->event_lock, flags);
2913 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2914 spin_unlock_irqrestore(&dev->event_lock, flags);
2915
2916 return pending;
2917}
2918
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002919static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2920{
Chris Wilson0f911282012-04-17 10:05:38 +01002921 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002922 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002923
2924 if (crtc->fb == NULL)
2925 return;
2926
Daniel Vetter2c10d572012-12-20 21:24:07 +01002927 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2928
Chris Wilson5bb61642012-09-27 21:25:58 +01002929 wait_event(dev_priv->pending_flip_queue,
2930 !intel_crtc_has_pending_flip(crtc));
2931
Chris Wilson0f911282012-04-17 10:05:38 +01002932 mutex_lock(&dev->struct_mutex);
2933 intel_finish_fb(crtc->fb);
2934 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002935}
2936
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002937/* Program iCLKIP clock to the desired frequency */
2938static void lpt_program_iclkip(struct drm_crtc *crtc)
2939{
2940 struct drm_device *dev = crtc->dev;
2941 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002942 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002943 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2944 u32 temp;
2945
Daniel Vetter09153002012-12-12 14:06:44 +01002946 mutex_lock(&dev_priv->dpio_lock);
2947
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002948 /* It is necessary to ungate the pixclk gate prior to programming
2949 * the divisors, and gate it back when it is done.
2950 */
2951 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2952
2953 /* Disable SSCCTL */
2954 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002955 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2956 SBI_SSCCTL_DISABLE,
2957 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002958
2959 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002960 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002961 auxdiv = 1;
2962 divsel = 0x41;
2963 phaseinc = 0x20;
2964 } else {
2965 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01002966 * but the adjusted_mode->crtc_clock in in KHz. To get the
2967 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002968 * convert the virtual clock precision to KHz here for higher
2969 * precision.
2970 */
2971 u32 iclk_virtual_root_freq = 172800 * 1000;
2972 u32 iclk_pi_range = 64;
2973 u32 desired_divisor, msb_divisor_value, pi_value;
2974
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002975 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002976 msb_divisor_value = desired_divisor / iclk_pi_range;
2977 pi_value = desired_divisor % iclk_pi_range;
2978
2979 auxdiv = 0;
2980 divsel = msb_divisor_value - 2;
2981 phaseinc = pi_value;
2982 }
2983
2984 /* This should not happen with any sane values */
2985 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2986 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2987 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2988 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2989
2990 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 +03002991 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002992 auxdiv,
2993 divsel,
2994 phasedir,
2995 phaseinc);
2996
2997 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002998 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002999 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3000 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3001 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3002 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3003 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3004 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003005 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003006
3007 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003008 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003009 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3010 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003011 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003012
3013 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003014 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003015 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003016 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003017
3018 /* Wait for initialization time */
3019 udelay(24);
3020
3021 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003022
3023 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003024}
3025
Daniel Vetter275f01b22013-05-03 11:49:47 +02003026static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3027 enum pipe pch_transcoder)
3028{
3029 struct drm_device *dev = crtc->base.dev;
3030 struct drm_i915_private *dev_priv = dev->dev_private;
3031 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3032
3033 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3034 I915_READ(HTOTAL(cpu_transcoder)));
3035 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3036 I915_READ(HBLANK(cpu_transcoder)));
3037 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3038 I915_READ(HSYNC(cpu_transcoder)));
3039
3040 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3041 I915_READ(VTOTAL(cpu_transcoder)));
3042 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3043 I915_READ(VBLANK(cpu_transcoder)));
3044 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3045 I915_READ(VSYNC(cpu_transcoder)));
3046 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3047 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3048}
3049
Jesse Barnesf67a5592011-01-05 10:31:48 -08003050/*
3051 * Enable PCH resources required for PCH ports:
3052 * - PCH PLLs
3053 * - FDI training & RX/TX
3054 * - update transcoder timings
3055 * - DP transcoding bits
3056 * - transcoder
3057 */
3058static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003059{
3060 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003061 struct drm_i915_private *dev_priv = dev->dev_private;
3062 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3063 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003064 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003065
Daniel Vetterab9412b2013-05-03 11:49:46 +02003066 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003067
Daniel Vettercd986ab2012-10-26 10:58:12 +02003068 /* Write the TU size bits before fdi link training, so that error
3069 * detection works. */
3070 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3071 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3072
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003073 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003074 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003075
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003076 /* We need to program the right clock selection before writing the pixel
3077 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003078 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003079 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003080
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003081 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003082 temp |= TRANS_DPLL_ENABLE(pipe);
3083 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003084 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003085 temp |= sel;
3086 else
3087 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003088 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003089 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003090
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003091 /* XXX: pch pll's can be enabled any time before we enable the PCH
3092 * transcoder, and we actually should do this to not upset any PCH
3093 * transcoder that already use the clock when we share it.
3094 *
3095 * Note that enable_shared_dpll tries to do the right thing, but
3096 * get_shared_dpll unconditionally resets the pll - we need that to have
3097 * the right LVDS enable sequence. */
3098 ironlake_enable_shared_dpll(intel_crtc);
3099
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003100 /* set transcoder timing, panel must allow it */
3101 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003102 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003103
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003104 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003105
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003106 /* For PCH DP, enable TRANS_DP_CTL */
3107 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003108 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3109 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003110 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003111 reg = TRANS_DP_CTL(pipe);
3112 temp = I915_READ(reg);
3113 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003114 TRANS_DP_SYNC_MASK |
3115 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003116 temp |= (TRANS_DP_OUTPUT_ENABLE |
3117 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003118 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003119
3120 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003121 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003122 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003123 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003124
3125 switch (intel_trans_dp_port_sel(crtc)) {
3126 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003127 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003128 break;
3129 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003130 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003131 break;
3132 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003133 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003134 break;
3135 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003136 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003137 }
3138
Chris Wilson5eddb702010-09-11 13:48:45 +01003139 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003140 }
3141
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003142 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003143}
3144
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003145static void lpt_pch_enable(struct drm_crtc *crtc)
3146{
3147 struct drm_device *dev = crtc->dev;
3148 struct drm_i915_private *dev_priv = dev->dev_private;
3149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003150 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003151
Daniel Vetterab9412b2013-05-03 11:49:46 +02003152 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003153
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003154 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003155
Paulo Zanoni0540e482012-10-31 18:12:40 -02003156 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003157 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003158
Paulo Zanoni937bb612012-10-31 18:12:47 -02003159 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003160}
3161
Daniel Vettere2b78262013-06-07 23:10:03 +02003162static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003163{
Daniel Vettere2b78262013-06-07 23:10:03 +02003164 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003165
3166 if (pll == NULL)
3167 return;
3168
3169 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003170 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003171 return;
3172 }
3173
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003174 if (--pll->refcount == 0) {
3175 WARN_ON(pll->on);
3176 WARN_ON(pll->active);
3177 }
3178
Daniel Vettera43f6e02013-06-07 23:10:32 +02003179 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003180}
3181
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003182static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003183{
Daniel Vettere2b78262013-06-07 23:10:03 +02003184 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3185 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3186 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003187
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003188 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003189 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3190 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003191 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003192 }
3193
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003194 if (HAS_PCH_IBX(dev_priv->dev)) {
3195 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003196 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003197 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003198
Daniel Vetter46edb022013-06-05 13:34:12 +02003199 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3200 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003201
3202 goto found;
3203 }
3204
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003205 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3206 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003207
3208 /* Only want to check enabled timings first */
3209 if (pll->refcount == 0)
3210 continue;
3211
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003212 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3213 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003214 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003215 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003216 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003217
3218 goto found;
3219 }
3220 }
3221
3222 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003223 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3224 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003225 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003226 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3227 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003228 goto found;
3229 }
3230 }
3231
3232 return NULL;
3233
3234found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003235 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003236 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3237 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003238
Daniel Vettercdbd2312013-06-05 13:34:03 +02003239 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003240 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3241 sizeof(pll->hw_state));
3242
Daniel Vetter46edb022013-06-05 13:34:12 +02003243 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003244 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003245 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003246
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003247 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003248 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003249 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003250
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003251 return pll;
3252}
3253
Daniel Vettera1520312013-05-03 11:49:50 +02003254static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003255{
3256 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003257 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003258 u32 temp;
3259
3260 temp = I915_READ(dslreg);
3261 udelay(500);
3262 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003263 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003264 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003265 }
3266}
3267
Jesse Barnesb074cec2013-04-25 12:55:02 -07003268static void ironlake_pfit_enable(struct intel_crtc *crtc)
3269{
3270 struct drm_device *dev = crtc->base.dev;
3271 struct drm_i915_private *dev_priv = dev->dev_private;
3272 int pipe = crtc->pipe;
3273
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003274 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003275 /* Force use of hard-coded filter coefficients
3276 * as some pre-programmed values are broken,
3277 * e.g. x201.
3278 */
3279 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3280 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3281 PF_PIPE_SEL_IVB(pipe));
3282 else
3283 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3284 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3285 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003286 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003287}
3288
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003289static void intel_enable_planes(struct drm_crtc *crtc)
3290{
3291 struct drm_device *dev = crtc->dev;
3292 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3293 struct intel_plane *intel_plane;
3294
3295 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3296 if (intel_plane->pipe == pipe)
3297 intel_plane_restore(&intel_plane->base);
3298}
3299
3300static void intel_disable_planes(struct drm_crtc *crtc)
3301{
3302 struct drm_device *dev = crtc->dev;
3303 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3304 struct intel_plane *intel_plane;
3305
3306 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3307 if (intel_plane->pipe == pipe)
3308 intel_plane_disable(&intel_plane->base);
3309}
3310
Paulo Zanonid77e4532013-09-24 13:52:55 -03003311static void hsw_enable_ips(struct intel_crtc *crtc)
3312{
3313 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3314
3315 if (!crtc->config.ips_enabled)
3316 return;
3317
3318 /* We can only enable IPS after we enable a plane and wait for a vblank.
3319 * We guarantee that the plane is enabled by calling intel_enable_ips
3320 * only after intel_enable_plane. And intel_enable_plane already waits
3321 * for a vblank, so all we need to do here is to enable the IPS bit. */
3322 assert_plane_enabled(dev_priv, crtc->plane);
3323 I915_WRITE(IPS_CTL, IPS_ENABLE);
3324}
3325
3326static void hsw_disable_ips(struct intel_crtc *crtc)
3327{
3328 struct drm_device *dev = crtc->base.dev;
3329 struct drm_i915_private *dev_priv = dev->dev_private;
3330
3331 if (!crtc->config.ips_enabled)
3332 return;
3333
3334 assert_plane_enabled(dev_priv, crtc->plane);
3335 I915_WRITE(IPS_CTL, 0);
3336 POSTING_READ(IPS_CTL);
3337
3338 /* We need to wait for a vblank before we can disable the plane. */
3339 intel_wait_for_vblank(dev, crtc->pipe);
3340}
3341
3342/** Loads the palette/gamma unit for the CRTC with the prepared values */
3343static void intel_crtc_load_lut(struct drm_crtc *crtc)
3344{
3345 struct drm_device *dev = crtc->dev;
3346 struct drm_i915_private *dev_priv = dev->dev_private;
3347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3348 enum pipe pipe = intel_crtc->pipe;
3349 int palreg = PALETTE(pipe);
3350 int i;
3351 bool reenable_ips = false;
3352
3353 /* The clocks have to be on to load the palette. */
3354 if (!crtc->enabled || !intel_crtc->active)
3355 return;
3356
3357 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3358 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3359 assert_dsi_pll_enabled(dev_priv);
3360 else
3361 assert_pll_enabled(dev_priv, pipe);
3362 }
3363
3364 /* use legacy palette for Ironlake */
3365 if (HAS_PCH_SPLIT(dev))
3366 palreg = LGC_PALETTE(pipe);
3367
3368 /* Workaround : Do not read or write the pipe palette/gamma data while
3369 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3370 */
3371 if (intel_crtc->config.ips_enabled &&
3372 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3373 GAMMA_MODE_MODE_SPLIT)) {
3374 hsw_disable_ips(intel_crtc);
3375 reenable_ips = true;
3376 }
3377
3378 for (i = 0; i < 256; i++) {
3379 I915_WRITE(palreg + 4 * i,
3380 (intel_crtc->lut_r[i] << 16) |
3381 (intel_crtc->lut_g[i] << 8) |
3382 intel_crtc->lut_b[i]);
3383 }
3384
3385 if (reenable_ips)
3386 hsw_enable_ips(intel_crtc);
3387}
3388
Jesse Barnesf67a5592011-01-05 10:31:48 -08003389static void ironlake_crtc_enable(struct drm_crtc *crtc)
3390{
3391 struct drm_device *dev = crtc->dev;
3392 struct drm_i915_private *dev_priv = dev->dev_private;
3393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003394 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003395 int pipe = intel_crtc->pipe;
3396 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003397
Daniel Vetter08a48462012-07-02 11:43:47 +02003398 WARN_ON(!crtc->enabled);
3399
Jesse Barnesf67a5592011-01-05 10:31:48 -08003400 if (intel_crtc->active)
3401 return;
3402
3403 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003404
3405 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3406 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3407
Daniel Vetterf6736a12013-06-05 13:34:30 +02003408 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003409 if (encoder->pre_enable)
3410 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003411
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003412 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003413 /* Note: FDI PLL enabling _must_ be done before we enable the
3414 * cpu pipes, hence this is separate from all the other fdi/pch
3415 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003416 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003417 } else {
3418 assert_fdi_tx_disabled(dev_priv, pipe);
3419 assert_fdi_rx_disabled(dev_priv, pipe);
3420 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003421
Jesse Barnesb074cec2013-04-25 12:55:02 -07003422 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003423
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003424 /*
3425 * On ILK+ LUT must be loaded before the pipe is running but with
3426 * clocks enabled
3427 */
3428 intel_crtc_load_lut(crtc);
3429
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003430 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003431 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003432 intel_crtc->config.has_pch_encoder, false);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003433 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003434 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003435 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003436
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003437 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003438 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003439
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003440 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003441 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003442 mutex_unlock(&dev->struct_mutex);
3443
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003444 for_each_encoder_on_crtc(dev, crtc, encoder)
3445 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003446
3447 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003448 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003449
3450 /*
3451 * There seems to be a race in PCH platform hw (at least on some
3452 * outputs) where an enabled pipe still completes any pageflip right
3453 * away (as if the pipe is off) instead of waiting for vblank. As soon
3454 * as the first vblank happend, everything works as expected. Hence just
3455 * wait for one vblank before returning to avoid strange things
3456 * happening.
3457 */
3458 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003459}
3460
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003461/* IPS only exists on ULT machines and is tied to pipe A. */
3462static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3463{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003464 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003465}
3466
Ville Syrjälädda9a662013-09-19 17:00:37 -03003467static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3468{
3469 struct drm_device *dev = crtc->dev;
3470 struct drm_i915_private *dev_priv = dev->dev_private;
3471 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3472 int pipe = intel_crtc->pipe;
3473 int plane = intel_crtc->plane;
3474
3475 intel_enable_plane(dev_priv, plane, pipe);
3476 intel_enable_planes(crtc);
3477 intel_crtc_update_cursor(crtc, true);
3478
3479 hsw_enable_ips(intel_crtc);
3480
3481 mutex_lock(&dev->struct_mutex);
3482 intel_update_fbc(dev);
3483 mutex_unlock(&dev->struct_mutex);
3484}
3485
3486static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3487{
3488 struct drm_device *dev = crtc->dev;
3489 struct drm_i915_private *dev_priv = dev->dev_private;
3490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3491 int pipe = intel_crtc->pipe;
3492 int plane = intel_crtc->plane;
3493
3494 intel_crtc_wait_for_pending_flips(crtc);
3495 drm_vblank_off(dev, pipe);
3496
3497 /* FBC must be disabled before disabling the plane on HSW. */
3498 if (dev_priv->fbc.plane == plane)
3499 intel_disable_fbc(dev);
3500
3501 hsw_disable_ips(intel_crtc);
3502
3503 intel_crtc_update_cursor(crtc, false);
3504 intel_disable_planes(crtc);
3505 intel_disable_plane(dev_priv, plane, pipe);
3506}
3507
Paulo Zanonie4916942013-09-20 16:21:19 -03003508/*
3509 * This implements the workaround described in the "notes" section of the mode
3510 * set sequence documentation. When going from no pipes or single pipe to
3511 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3512 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3513 */
3514static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3515{
3516 struct drm_device *dev = crtc->base.dev;
3517 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3518
3519 /* We want to get the other_active_crtc only if there's only 1 other
3520 * active crtc. */
3521 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3522 if (!crtc_it->active || crtc_it == crtc)
3523 continue;
3524
3525 if (other_active_crtc)
3526 return;
3527
3528 other_active_crtc = crtc_it;
3529 }
3530 if (!other_active_crtc)
3531 return;
3532
3533 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3534 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3535}
3536
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003537static void haswell_crtc_enable(struct drm_crtc *crtc)
3538{
3539 struct drm_device *dev = crtc->dev;
3540 struct drm_i915_private *dev_priv = dev->dev_private;
3541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3542 struct intel_encoder *encoder;
3543 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003544
3545 WARN_ON(!crtc->enabled);
3546
3547 if (intel_crtc->active)
3548 return;
3549
3550 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003551
3552 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3553 if (intel_crtc->config.has_pch_encoder)
3554 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3555
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003556 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003557 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003558
3559 for_each_encoder_on_crtc(dev, crtc, encoder)
3560 if (encoder->pre_enable)
3561 encoder->pre_enable(encoder);
3562
Paulo Zanoni1f544382012-10-24 11:32:00 -02003563 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003564
Jesse Barnesb074cec2013-04-25 12:55:02 -07003565 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003566
3567 /*
3568 * On ILK+ LUT must be loaded before the pipe is running but with
3569 * clocks enabled
3570 */
3571 intel_crtc_load_lut(crtc);
3572
Paulo Zanoni1f544382012-10-24 11:32:00 -02003573 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003574 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003575
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003576 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003577 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003578 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003579
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003580 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003581 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003582
Jani Nikula8807e552013-08-30 19:40:32 +03003583 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003584 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003585 intel_opregion_notify_encoder(encoder, true);
3586 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003587
Paulo Zanonie4916942013-09-20 16:21:19 -03003588 /* If we change the relative order between pipe/planes enabling, we need
3589 * to change the workaround. */
3590 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003591 haswell_crtc_enable_planes(crtc);
3592
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003593 /*
3594 * There seems to be a race in PCH platform hw (at least on some
3595 * outputs) where an enabled pipe still completes any pageflip right
3596 * away (as if the pipe is off) instead of waiting for vblank. As soon
3597 * as the first vblank happend, everything works as expected. Hence just
3598 * wait for one vblank before returning to avoid strange things
3599 * happening.
3600 */
3601 intel_wait_for_vblank(dev, intel_crtc->pipe);
3602}
3603
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003604static void ironlake_pfit_disable(struct intel_crtc *crtc)
3605{
3606 struct drm_device *dev = crtc->base.dev;
3607 struct drm_i915_private *dev_priv = dev->dev_private;
3608 int pipe = crtc->pipe;
3609
3610 /* To avoid upsetting the power well on haswell only disable the pfit if
3611 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003612 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003613 I915_WRITE(PF_CTL(pipe), 0);
3614 I915_WRITE(PF_WIN_POS(pipe), 0);
3615 I915_WRITE(PF_WIN_SZ(pipe), 0);
3616 }
3617}
3618
Jesse Barnes6be4a602010-09-10 10:26:01 -07003619static void ironlake_crtc_disable(struct drm_crtc *crtc)
3620{
3621 struct drm_device *dev = crtc->dev;
3622 struct drm_i915_private *dev_priv = dev->dev_private;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003624 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003625 int pipe = intel_crtc->pipe;
3626 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003627 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003628
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003629
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003630 if (!intel_crtc->active)
3631 return;
3632
Daniel Vetterea9d7582012-07-10 10:42:52 +02003633 for_each_encoder_on_crtc(dev, crtc, encoder)
3634 encoder->disable(encoder);
3635
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003636 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003637 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003638
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003639 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003640 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003641
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003642 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003643 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003644 intel_disable_plane(dev_priv, plane, pipe);
3645
Daniel Vetterd925c592013-06-05 13:34:04 +02003646 if (intel_crtc->config.has_pch_encoder)
3647 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3648
Jesse Barnesb24e7172011-01-04 15:09:30 -08003649 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003650
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003651 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003652
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003653 for_each_encoder_on_crtc(dev, crtc, encoder)
3654 if (encoder->post_disable)
3655 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003656
Daniel Vetterd925c592013-06-05 13:34:04 +02003657 if (intel_crtc->config.has_pch_encoder) {
3658 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003659
Daniel Vetterd925c592013-06-05 13:34:04 +02003660 ironlake_disable_pch_transcoder(dev_priv, pipe);
3661 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003662
Daniel Vetterd925c592013-06-05 13:34:04 +02003663 if (HAS_PCH_CPT(dev)) {
3664 /* disable TRANS_DP_CTL */
3665 reg = TRANS_DP_CTL(pipe);
3666 temp = I915_READ(reg);
3667 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3668 TRANS_DP_PORT_SEL_MASK);
3669 temp |= TRANS_DP_PORT_SEL_NONE;
3670 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003671
Daniel Vetterd925c592013-06-05 13:34:04 +02003672 /* disable DPLL_SEL */
3673 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003674 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003675 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003676 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003677
3678 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003679 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003680
3681 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003682 }
3683
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003684 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003685 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003686
3687 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003688 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003689 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003690}
3691
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003692static void haswell_crtc_disable(struct drm_crtc *crtc)
3693{
3694 struct drm_device *dev = crtc->dev;
3695 struct drm_i915_private *dev_priv = dev->dev_private;
3696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3697 struct intel_encoder *encoder;
3698 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003699 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003700
3701 if (!intel_crtc->active)
3702 return;
3703
Ville Syrjälädda9a662013-09-19 17:00:37 -03003704 haswell_crtc_disable_planes(crtc);
3705
Jani Nikula8807e552013-08-30 19:40:32 +03003706 for_each_encoder_on_crtc(dev, crtc, encoder) {
3707 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003708 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003709 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003710
Paulo Zanoni86642812013-04-12 17:57:57 -03003711 if (intel_crtc->config.has_pch_encoder)
3712 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003713 intel_disable_pipe(dev_priv, pipe);
3714
Paulo Zanoniad80a812012-10-24 16:06:19 -02003715 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003716
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003717 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003718
Paulo Zanoni1f544382012-10-24 11:32:00 -02003719 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003720
3721 for_each_encoder_on_crtc(dev, crtc, encoder)
3722 if (encoder->post_disable)
3723 encoder->post_disable(encoder);
3724
Daniel Vetter88adfff2013-03-28 10:42:01 +01003725 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003726 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003727 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003728 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003729 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003730
3731 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003732 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003733
3734 mutex_lock(&dev->struct_mutex);
3735 intel_update_fbc(dev);
3736 mutex_unlock(&dev->struct_mutex);
3737}
3738
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003739static void ironlake_crtc_off(struct drm_crtc *crtc)
3740{
3741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003742 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003743}
3744
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003745static void haswell_crtc_off(struct drm_crtc *crtc)
3746{
3747 intel_ddi_put_crtc_pll(crtc);
3748}
3749
Daniel Vetter02e792f2009-09-15 22:57:34 +02003750static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3751{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003752 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003753 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003754 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003755
Chris Wilson23f09ce2010-08-12 13:53:37 +01003756 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003757 dev_priv->mm.interruptible = false;
3758 (void) intel_overlay_switch_off(intel_crtc->overlay);
3759 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003760 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003761 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003762
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003763 /* Let userspace switch the overlay on again. In most cases userspace
3764 * has to recompute where to put it anyway.
3765 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003766}
3767
Egbert Eich61bc95c2013-03-04 09:24:38 -05003768/**
3769 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3770 * cursor plane briefly if not already running after enabling the display
3771 * plane.
3772 * This workaround avoids occasional blank screens when self refresh is
3773 * enabled.
3774 */
3775static void
3776g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3777{
3778 u32 cntl = I915_READ(CURCNTR(pipe));
3779
3780 if ((cntl & CURSOR_MODE) == 0) {
3781 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3782
3783 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3784 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3785 intel_wait_for_vblank(dev_priv->dev, pipe);
3786 I915_WRITE(CURCNTR(pipe), cntl);
3787 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3788 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3789 }
3790}
3791
Jesse Barnes2dd24552013-04-25 12:55:01 -07003792static void i9xx_pfit_enable(struct intel_crtc *crtc)
3793{
3794 struct drm_device *dev = crtc->base.dev;
3795 struct drm_i915_private *dev_priv = dev->dev_private;
3796 struct intel_crtc_config *pipe_config = &crtc->config;
3797
Daniel Vetter328d8e82013-05-08 10:36:31 +02003798 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003799 return;
3800
Daniel Vetterc0b03412013-05-28 12:05:54 +02003801 /*
3802 * The panel fitter should only be adjusted whilst the pipe is disabled,
3803 * according to register description and PRM.
3804 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003805 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3806 assert_pipe_disabled(dev_priv, crtc->pipe);
3807
Jesse Barnesb074cec2013-04-25 12:55:02 -07003808 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3809 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003810
3811 /* Border color in case we don't scale up to the full screen. Black by
3812 * default, change to something else for debugging. */
3813 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003814}
3815
Jesse Barnes89b667f2013-04-18 14:51:36 -07003816static void valleyview_crtc_enable(struct drm_crtc *crtc)
3817{
3818 struct drm_device *dev = crtc->dev;
3819 struct drm_i915_private *dev_priv = dev->dev_private;
3820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3821 struct intel_encoder *encoder;
3822 int pipe = intel_crtc->pipe;
3823 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03003824 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003825
3826 WARN_ON(!crtc->enabled);
3827
3828 if (intel_crtc->active)
3829 return;
3830
3831 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07003832
Jesse Barnes89b667f2013-04-18 14:51:36 -07003833 for_each_encoder_on_crtc(dev, crtc, encoder)
3834 if (encoder->pre_pll_enable)
3835 encoder->pre_pll_enable(encoder);
3836
Jani Nikula23538ef2013-08-27 15:12:22 +03003837 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
3838
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003839 if (!is_dsi)
3840 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003841
3842 for_each_encoder_on_crtc(dev, crtc, encoder)
3843 if (encoder->pre_enable)
3844 encoder->pre_enable(encoder);
3845
Jesse Barnes2dd24552013-04-25 12:55:01 -07003846 i9xx_pfit_enable(intel_crtc);
3847
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003848 intel_crtc_load_lut(crtc);
3849
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003850 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003851 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003852 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003853 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003854 intel_crtc_update_cursor(crtc, true);
3855
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003856 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03003857
3858 for_each_encoder_on_crtc(dev, crtc, encoder)
3859 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003860}
3861
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003862static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003863{
3864 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003865 struct drm_i915_private *dev_priv = dev->dev_private;
3866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003867 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003868 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003869 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003870
Daniel Vetter08a48462012-07-02 11:43:47 +02003871 WARN_ON(!crtc->enabled);
3872
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003873 if (intel_crtc->active)
3874 return;
3875
3876 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003877
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003878 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003879 if (encoder->pre_enable)
3880 encoder->pre_enable(encoder);
3881
Daniel Vetterf6736a12013-06-05 13:34:30 +02003882 i9xx_enable_pll(intel_crtc);
3883
Jesse Barnes2dd24552013-04-25 12:55:01 -07003884 i9xx_pfit_enable(intel_crtc);
3885
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003886 intel_crtc_load_lut(crtc);
3887
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003888 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03003889 intel_enable_pipe(dev_priv, pipe, false, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003890 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003891 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003892 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003893 if (IS_G4X(dev))
3894 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003895 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003896
3897 /* Give the overlay scaler a chance to enable if it's on this pipe */
3898 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003899
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003900 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003901
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003902 for_each_encoder_on_crtc(dev, crtc, encoder)
3903 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003904}
3905
Daniel Vetter87476d62013-04-11 16:29:06 +02003906static void i9xx_pfit_disable(struct intel_crtc *crtc)
3907{
3908 struct drm_device *dev = crtc->base.dev;
3909 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003910
3911 if (!crtc->config.gmch_pfit.control)
3912 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003913
3914 assert_pipe_disabled(dev_priv, crtc->pipe);
3915
Daniel Vetter328d8e82013-05-08 10:36:31 +02003916 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3917 I915_READ(PFIT_CONTROL));
3918 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003919}
3920
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003921static void i9xx_crtc_disable(struct drm_crtc *crtc)
3922{
3923 struct drm_device *dev = crtc->dev;
3924 struct drm_i915_private *dev_priv = dev->dev_private;
3925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003926 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003927 int pipe = intel_crtc->pipe;
3928 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003929
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003930 if (!intel_crtc->active)
3931 return;
3932
Daniel Vetterea9d7582012-07-10 10:42:52 +02003933 for_each_encoder_on_crtc(dev, crtc, encoder)
3934 encoder->disable(encoder);
3935
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003936 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003937 intel_crtc_wait_for_pending_flips(crtc);
3938 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003939
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003940 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003941 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003942
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003943 intel_crtc_dpms_overlay(intel_crtc, false);
3944 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003945 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003946 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003947
Jesse Barnesb24e7172011-01-04 15:09:30 -08003948 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003949
Daniel Vetter87476d62013-04-11 16:29:06 +02003950 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003951
Jesse Barnes89b667f2013-04-18 14:51:36 -07003952 for_each_encoder_on_crtc(dev, crtc, encoder)
3953 if (encoder->post_disable)
3954 encoder->post_disable(encoder);
3955
Jesse Barnesf6071162013-10-01 10:41:38 -07003956 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3957 vlv_disable_pll(dev_priv, pipe);
3958 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03003959 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003960
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003961 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003962 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003963
Chris Wilson6b383a72010-09-13 13:54:26 +01003964 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003965}
3966
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003967static void i9xx_crtc_off(struct drm_crtc *crtc)
3968{
3969}
3970
Daniel Vetter976f8a22012-07-08 22:34:21 +02003971static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3972 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003973{
3974 struct drm_device *dev = crtc->dev;
3975 struct drm_i915_master_private *master_priv;
3976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3977 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003978
3979 if (!dev->primary->master)
3980 return;
3981
3982 master_priv = dev->primary->master->driver_priv;
3983 if (!master_priv->sarea_priv)
3984 return;
3985
Jesse Barnes79e53942008-11-07 14:24:08 -08003986 switch (pipe) {
3987 case 0:
3988 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3989 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3990 break;
3991 case 1:
3992 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3993 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3994 break;
3995 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003996 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003997 break;
3998 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003999}
4000
Daniel Vetter976f8a22012-07-08 22:34:21 +02004001/**
4002 * Sets the power management mode of the pipe and plane.
4003 */
4004void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004005{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004006 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004007 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004008 struct intel_encoder *intel_encoder;
4009 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004010
Daniel Vetter976f8a22012-07-08 22:34:21 +02004011 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4012 enable |= intel_encoder->connectors_active;
4013
4014 if (enable)
4015 dev_priv->display.crtc_enable(crtc);
4016 else
4017 dev_priv->display.crtc_disable(crtc);
4018
4019 intel_crtc_update_sarea(crtc, enable);
4020}
4021
Daniel Vetter976f8a22012-07-08 22:34:21 +02004022static void intel_crtc_disable(struct drm_crtc *crtc)
4023{
4024 struct drm_device *dev = crtc->dev;
4025 struct drm_connector *connector;
4026 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004028
4029 /* crtc should still be enabled when we disable it. */
4030 WARN_ON(!crtc->enabled);
4031
4032 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004033 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004034 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004035 dev_priv->display.off(crtc);
4036
Chris Wilson931872f2012-01-16 23:01:13 +00004037 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004038 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004039 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004040
4041 if (crtc->fb) {
4042 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004043 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004044 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004045 crtc->fb = NULL;
4046 }
4047
4048 /* Update computed state. */
4049 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4050 if (!connector->encoder || !connector->encoder->crtc)
4051 continue;
4052
4053 if (connector->encoder->crtc != crtc)
4054 continue;
4055
4056 connector->dpms = DRM_MODE_DPMS_OFF;
4057 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004058 }
4059}
4060
Chris Wilsonea5b2132010-08-04 13:50:23 +01004061void intel_encoder_destroy(struct drm_encoder *encoder)
4062{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004063 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004064
Chris Wilsonea5b2132010-08-04 13:50:23 +01004065 drm_encoder_cleanup(encoder);
4066 kfree(intel_encoder);
4067}
4068
Damien Lespiau92373292013-08-08 22:28:57 +01004069/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004070 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4071 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004072static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004073{
4074 if (mode == DRM_MODE_DPMS_ON) {
4075 encoder->connectors_active = true;
4076
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004077 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004078 } else {
4079 encoder->connectors_active = false;
4080
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004081 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004082 }
4083}
4084
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004085/* Cross check the actual hw state with our own modeset state tracking (and it's
4086 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004087static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004088{
4089 if (connector->get_hw_state(connector)) {
4090 struct intel_encoder *encoder = connector->encoder;
4091 struct drm_crtc *crtc;
4092 bool encoder_enabled;
4093 enum pipe pipe;
4094
4095 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4096 connector->base.base.id,
4097 drm_get_connector_name(&connector->base));
4098
4099 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4100 "wrong connector dpms state\n");
4101 WARN(connector->base.encoder != &encoder->base,
4102 "active connector not linked to encoder\n");
4103 WARN(!encoder->connectors_active,
4104 "encoder->connectors_active not set\n");
4105
4106 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4107 WARN(!encoder_enabled, "encoder not enabled\n");
4108 if (WARN_ON(!encoder->base.crtc))
4109 return;
4110
4111 crtc = encoder->base.crtc;
4112
4113 WARN(!crtc->enabled, "crtc not enabled\n");
4114 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4115 WARN(pipe != to_intel_crtc(crtc)->pipe,
4116 "encoder active on the wrong pipe\n");
4117 }
4118}
4119
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004120/* Even simpler default implementation, if there's really no special case to
4121 * consider. */
4122void intel_connector_dpms(struct drm_connector *connector, int mode)
4123{
4124 struct intel_encoder *encoder = intel_attached_encoder(connector);
4125
4126 /* All the simple cases only support two dpms states. */
4127 if (mode != DRM_MODE_DPMS_ON)
4128 mode = DRM_MODE_DPMS_OFF;
4129
4130 if (mode == connector->dpms)
4131 return;
4132
4133 connector->dpms = mode;
4134
4135 /* Only need to change hw state when actually enabled */
4136 if (encoder->base.crtc)
4137 intel_encoder_dpms(encoder, mode);
4138 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02004139 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004140
Daniel Vetterb9805142012-08-31 17:37:33 +02004141 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004142}
4143
Daniel Vetterf0947c32012-07-02 13:10:34 +02004144/* Simple connector->get_hw_state implementation for encoders that support only
4145 * one connector and no cloning and hence the encoder state determines the state
4146 * of the connector. */
4147bool intel_connector_get_hw_state(struct intel_connector *connector)
4148{
Daniel Vetter24929352012-07-02 20:28:59 +02004149 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004150 struct intel_encoder *encoder = connector->encoder;
4151
4152 return encoder->get_hw_state(encoder, &pipe);
4153}
4154
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004155static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4156 struct intel_crtc_config *pipe_config)
4157{
4158 struct drm_i915_private *dev_priv = dev->dev_private;
4159 struct intel_crtc *pipe_B_crtc =
4160 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4161
4162 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4163 pipe_name(pipe), pipe_config->fdi_lanes);
4164 if (pipe_config->fdi_lanes > 4) {
4165 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4166 pipe_name(pipe), pipe_config->fdi_lanes);
4167 return false;
4168 }
4169
4170 if (IS_HASWELL(dev)) {
4171 if (pipe_config->fdi_lanes > 2) {
4172 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4173 pipe_config->fdi_lanes);
4174 return false;
4175 } else {
4176 return true;
4177 }
4178 }
4179
4180 if (INTEL_INFO(dev)->num_pipes == 2)
4181 return true;
4182
4183 /* Ivybridge 3 pipe is really complicated */
4184 switch (pipe) {
4185 case PIPE_A:
4186 return true;
4187 case PIPE_B:
4188 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4189 pipe_config->fdi_lanes > 2) {
4190 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4191 pipe_name(pipe), pipe_config->fdi_lanes);
4192 return false;
4193 }
4194 return true;
4195 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004196 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004197 pipe_B_crtc->config.fdi_lanes <= 2) {
4198 if (pipe_config->fdi_lanes > 2) {
4199 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4200 pipe_name(pipe), pipe_config->fdi_lanes);
4201 return false;
4202 }
4203 } else {
4204 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4205 return false;
4206 }
4207 return true;
4208 default:
4209 BUG();
4210 }
4211}
4212
Daniel Vettere29c22c2013-02-21 00:00:16 +01004213#define RETRY 1
4214static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4215 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004216{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004217 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004218 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004219 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004220 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004221
Daniel Vettere29c22c2013-02-21 00:00:16 +01004222retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004223 /* FDI is a binary signal running at ~2.7GHz, encoding
4224 * each output octet as 10 bits. The actual frequency
4225 * is stored as a divider into a 100MHz clock, and the
4226 * mode pixel clock is stored in units of 1KHz.
4227 * Hence the bw of each lane in terms of the mode signal
4228 * is:
4229 */
4230 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4231
Damien Lespiau241bfc32013-09-25 16:45:37 +01004232 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004233
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004234 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004235 pipe_config->pipe_bpp);
4236
4237 pipe_config->fdi_lanes = lane;
4238
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004239 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004240 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004241
Daniel Vettere29c22c2013-02-21 00:00:16 +01004242 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4243 intel_crtc->pipe, pipe_config);
4244 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4245 pipe_config->pipe_bpp -= 2*3;
4246 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4247 pipe_config->pipe_bpp);
4248 needs_recompute = true;
4249 pipe_config->bw_constrained = true;
4250
4251 goto retry;
4252 }
4253
4254 if (needs_recompute)
4255 return RETRY;
4256
4257 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004258}
4259
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004260static void hsw_compute_ips_config(struct intel_crtc *crtc,
4261 struct intel_crtc_config *pipe_config)
4262{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004263 pipe_config->ips_enabled = i915_enable_ips &&
4264 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004265 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004266}
4267
Daniel Vettera43f6e02013-06-07 23:10:32 +02004268static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004269 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004270{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004271 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004272 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004273
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004274 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004275 if (INTEL_INFO(dev)->gen < 4) {
4276 struct drm_i915_private *dev_priv = dev->dev_private;
4277 int clock_limit =
4278 dev_priv->display.get_display_clock_speed(dev);
4279
4280 /*
4281 * Enable pixel doubling when the dot clock
4282 * is > 90% of the (display) core speed.
4283 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004284 * GDG double wide on either pipe,
4285 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004286 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004287 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004288 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004289 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004290 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004291 }
4292
Damien Lespiau241bfc32013-09-25 16:45:37 +01004293 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004294 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004295 }
Chris Wilson89749352010-09-12 18:25:19 +01004296
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004297 /*
4298 * Pipe horizontal size must be even in:
4299 * - DVO ganged mode
4300 * - LVDS dual channel mode
4301 * - Double wide pipe
4302 */
4303 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4304 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4305 pipe_config->pipe_src_w &= ~1;
4306
Damien Lespiau8693a822013-05-03 18:48:11 +01004307 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4308 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004309 */
4310 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4311 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004312 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004313
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004314 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004315 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004316 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004317 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4318 * for lvds. */
4319 pipe_config->pipe_bpp = 8*3;
4320 }
4321
Damien Lespiauf5adf942013-06-24 18:29:34 +01004322 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004323 hsw_compute_ips_config(crtc, pipe_config);
4324
4325 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4326 * clock survives for now. */
4327 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4328 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004329
Daniel Vetter877d48d2013-04-19 11:24:43 +02004330 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004331 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004332
Daniel Vettere29c22c2013-02-21 00:00:16 +01004333 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004334}
4335
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004336static int valleyview_get_display_clock_speed(struct drm_device *dev)
4337{
4338 return 400000; /* FIXME */
4339}
4340
Jesse Barnese70236a2009-09-21 10:42:27 -07004341static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004342{
Jesse Barnese70236a2009-09-21 10:42:27 -07004343 return 400000;
4344}
Jesse Barnes79e53942008-11-07 14:24:08 -08004345
Jesse Barnese70236a2009-09-21 10:42:27 -07004346static int i915_get_display_clock_speed(struct drm_device *dev)
4347{
4348 return 333000;
4349}
Jesse Barnes79e53942008-11-07 14:24:08 -08004350
Jesse Barnese70236a2009-09-21 10:42:27 -07004351static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4352{
4353 return 200000;
4354}
Jesse Barnes79e53942008-11-07 14:24:08 -08004355
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004356static int pnv_get_display_clock_speed(struct drm_device *dev)
4357{
4358 u16 gcfgc = 0;
4359
4360 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4361
4362 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4363 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4364 return 267000;
4365 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4366 return 333000;
4367 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4368 return 444000;
4369 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4370 return 200000;
4371 default:
4372 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4373 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4374 return 133000;
4375 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4376 return 167000;
4377 }
4378}
4379
Jesse Barnese70236a2009-09-21 10:42:27 -07004380static int i915gm_get_display_clock_speed(struct drm_device *dev)
4381{
4382 u16 gcfgc = 0;
4383
4384 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4385
4386 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004387 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004388 else {
4389 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4390 case GC_DISPLAY_CLOCK_333_MHZ:
4391 return 333000;
4392 default:
4393 case GC_DISPLAY_CLOCK_190_200_MHZ:
4394 return 190000;
4395 }
4396 }
4397}
Jesse Barnes79e53942008-11-07 14:24:08 -08004398
Jesse Barnese70236a2009-09-21 10:42:27 -07004399static int i865_get_display_clock_speed(struct drm_device *dev)
4400{
4401 return 266000;
4402}
4403
4404static int i855_get_display_clock_speed(struct drm_device *dev)
4405{
4406 u16 hpllcc = 0;
4407 /* Assume that the hardware is in the high speed state. This
4408 * should be the default.
4409 */
4410 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4411 case GC_CLOCK_133_200:
4412 case GC_CLOCK_100_200:
4413 return 200000;
4414 case GC_CLOCK_166_250:
4415 return 250000;
4416 case GC_CLOCK_100_133:
4417 return 133000;
4418 }
4419
4420 /* Shouldn't happen */
4421 return 0;
4422}
4423
4424static int i830_get_display_clock_speed(struct drm_device *dev)
4425{
4426 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004427}
4428
Zhenyu Wang2c072452009-06-05 15:38:42 +08004429static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004430intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004431{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004432 while (*num > DATA_LINK_M_N_MASK ||
4433 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004434 *num >>= 1;
4435 *den >>= 1;
4436 }
4437}
4438
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004439static void compute_m_n(unsigned int m, unsigned int n,
4440 uint32_t *ret_m, uint32_t *ret_n)
4441{
4442 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4443 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4444 intel_reduce_m_n_ratio(ret_m, ret_n);
4445}
4446
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004447void
4448intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4449 int pixel_clock, int link_clock,
4450 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004451{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004452 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004453
4454 compute_m_n(bits_per_pixel * pixel_clock,
4455 link_clock * nlanes * 8,
4456 &m_n->gmch_m, &m_n->gmch_n);
4457
4458 compute_m_n(pixel_clock, link_clock,
4459 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004460}
4461
Chris Wilsona7615032011-01-12 17:04:08 +00004462static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4463{
Keith Packard72bbe582011-09-26 16:09:45 -07004464 if (i915_panel_use_ssc >= 0)
4465 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004466 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004467 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004468}
4469
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004470static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4471{
4472 struct drm_device *dev = crtc->dev;
4473 struct drm_i915_private *dev_priv = dev->dev_private;
4474 int refclk;
4475
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004476 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004477 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004478 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004479 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004480 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004481 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4482 refclk / 1000);
4483 } else if (!IS_GEN2(dev)) {
4484 refclk = 96000;
4485 } else {
4486 refclk = 48000;
4487 }
4488
4489 return refclk;
4490}
4491
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004492static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004493{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004494 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004495}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004496
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004497static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4498{
4499 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004500}
4501
Daniel Vetterf47709a2013-03-28 10:42:02 +01004502static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004503 intel_clock_t *reduced_clock)
4504{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004505 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004506 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004507 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004508 u32 fp, fp2 = 0;
4509
4510 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004511 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004512 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004513 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004514 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004515 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004516 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004517 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004518 }
4519
4520 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004521 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004522
Daniel Vetterf47709a2013-03-28 10:42:02 +01004523 crtc->lowfreq_avail = false;
4524 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004525 reduced_clock && i915_powersave) {
4526 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004527 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004528 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004529 } else {
4530 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004531 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004532 }
4533}
4534
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004535static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4536 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004537{
4538 u32 reg_val;
4539
4540 /*
4541 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4542 * and set it to a reasonable value instead.
4543 */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004544 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004545 reg_val &= 0xffffff00;
4546 reg_val |= 0x00000030;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004547 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004548
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004549 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004550 reg_val &= 0x8cffffff;
4551 reg_val = 0x8c000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004552 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004553
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004554 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004555 reg_val &= 0xffffff00;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004556 vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004557
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004558 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004559 reg_val &= 0x00ffffff;
4560 reg_val |= 0xb0000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004561 vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004562}
4563
Daniel Vetterb5518422013-05-03 11:49:48 +02004564static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4565 struct intel_link_m_n *m_n)
4566{
4567 struct drm_device *dev = crtc->base.dev;
4568 struct drm_i915_private *dev_priv = dev->dev_private;
4569 int pipe = crtc->pipe;
4570
Daniel Vettere3b95f12013-05-03 11:49:49 +02004571 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4572 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4573 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4574 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004575}
4576
4577static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4578 struct intel_link_m_n *m_n)
4579{
4580 struct drm_device *dev = crtc->base.dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582 int pipe = crtc->pipe;
4583 enum transcoder transcoder = crtc->config.cpu_transcoder;
4584
4585 if (INTEL_INFO(dev)->gen >= 5) {
4586 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4587 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4588 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4589 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4590 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004591 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4592 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4593 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4594 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004595 }
4596}
4597
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004598static void intel_dp_set_m_n(struct intel_crtc *crtc)
4599{
4600 if (crtc->config.has_pch_encoder)
4601 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4602 else
4603 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4604}
4605
Daniel Vetterf47709a2013-03-28 10:42:02 +01004606static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004607{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004608 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004609 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004610 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004611 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004612 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004613 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004614
Daniel Vetter09153002012-12-12 14:06:44 +01004615 mutex_lock(&dev_priv->dpio_lock);
4616
Daniel Vetterf47709a2013-03-28 10:42:02 +01004617 bestn = crtc->config.dpll.n;
4618 bestm1 = crtc->config.dpll.m1;
4619 bestm2 = crtc->config.dpll.m2;
4620 bestp1 = crtc->config.dpll.p1;
4621 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004622
Jesse Barnes89b667f2013-04-18 14:51:36 -07004623 /* See eDP HDMI DPIO driver vbios notes doc */
4624
4625 /* PLL B needs special handling */
4626 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004627 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004628
4629 /* Set up Tx target for periodic Rcomp update */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004630 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004631
4632 /* Disable target IRef on PLL */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004633 reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004634 reg_val &= 0x00ffffff;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004635 vlv_dpio_write(dev_priv, pipe, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004636
4637 /* Disable fast lock */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004638 vlv_dpio_write(dev_priv, pipe, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004639
4640 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004641 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4642 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4643 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004644 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004645
4646 /*
4647 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4648 * but we don't support that).
4649 * Note: don't use the DAC post divider as it seems unstable.
4650 */
4651 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004652 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004653
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004654 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004655 vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004656
Jesse Barnes89b667f2013-04-18 14:51:36 -07004657 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004658 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004659 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004660 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004661 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004662 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004663 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004664 vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004665 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004666
Jesse Barnes89b667f2013-04-18 14:51:36 -07004667 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4668 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4669 /* Use SSC source */
4670 if (!pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004671 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004672 0x0df40000);
4673 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004674 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004675 0x0df70000);
4676 } else { /* HDMI or VGA */
4677 /* Use bend source */
4678 if (!pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004679 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004680 0x0df70000);
4681 else
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004682 vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004683 0x0df40000);
4684 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004685
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004686 coreclk = vlv_dpio_read(dev_priv, pipe, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004687 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4688 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4689 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4690 coreclk |= 0x01000000;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004691 vlv_dpio_write(dev_priv, pipe, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004692
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004693 vlv_dpio_write(dev_priv, pipe, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004694
Jesse Barnes89b667f2013-04-18 14:51:36 -07004695 /* Enable DPIO clock input */
4696 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4697 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004698 /* We should never disable this, set it here for state tracking */
4699 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004700 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004701 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004702 crtc->config.dpll_hw_state.dpll = dpll;
4703
Daniel Vetteref1b4602013-06-01 17:17:04 +02004704 dpll_md = (crtc->config.pixel_multiplier - 1)
4705 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004706 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4707
Daniel Vetterf47709a2013-03-28 10:42:02 +01004708 if (crtc->config.has_dp_encoder)
4709 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304710
Daniel Vetter09153002012-12-12 14:06:44 +01004711 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004712}
4713
Daniel Vetterf47709a2013-03-28 10:42:02 +01004714static void i9xx_update_pll(struct intel_crtc *crtc,
4715 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004716 int num_connectors)
4717{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004718 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004719 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004720 u32 dpll;
4721 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004722 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004723
Daniel Vetterf47709a2013-03-28 10:42:02 +01004724 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304725
Daniel Vetterf47709a2013-03-28 10:42:02 +01004726 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4727 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004728
4729 dpll = DPLL_VGA_MODE_DIS;
4730
Daniel Vetterf47709a2013-03-28 10:42:02 +01004731 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004732 dpll |= DPLLB_MODE_LVDS;
4733 else
4734 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004735
Daniel Vetteref1b4602013-06-01 17:17:04 +02004736 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004737 dpll |= (crtc->config.pixel_multiplier - 1)
4738 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004739 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004740
4741 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004742 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004743
Daniel Vetterf47709a2013-03-28 10:42:02 +01004744 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004745 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004746
4747 /* compute bitmask from p1 value */
4748 if (IS_PINEVIEW(dev))
4749 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4750 else {
4751 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4752 if (IS_G4X(dev) && reduced_clock)
4753 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4754 }
4755 switch (clock->p2) {
4756 case 5:
4757 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4758 break;
4759 case 7:
4760 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4761 break;
4762 case 10:
4763 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4764 break;
4765 case 14:
4766 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4767 break;
4768 }
4769 if (INTEL_INFO(dev)->gen >= 4)
4770 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4771
Daniel Vetter09ede542013-04-30 14:01:45 +02004772 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004773 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004774 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004775 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4776 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4777 else
4778 dpll |= PLL_REF_INPUT_DREFCLK;
4779
4780 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004781 crtc->config.dpll_hw_state.dpll = dpll;
4782
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004783 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004784 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4785 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004786 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004787 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004788
4789 if (crtc->config.has_dp_encoder)
4790 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004791}
4792
Daniel Vetterf47709a2013-03-28 10:42:02 +01004793static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004794 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004795 int num_connectors)
4796{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004797 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004798 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004799 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004800 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004801
Daniel Vetterf47709a2013-03-28 10:42:02 +01004802 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304803
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004804 dpll = DPLL_VGA_MODE_DIS;
4805
Daniel Vetterf47709a2013-03-28 10:42:02 +01004806 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004807 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4808 } else {
4809 if (clock->p1 == 2)
4810 dpll |= PLL_P1_DIVIDE_BY_TWO;
4811 else
4812 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4813 if (clock->p2 == 4)
4814 dpll |= PLL_P2_DIVIDE_BY_4;
4815 }
4816
Daniel Vetter4a33e482013-07-06 12:52:05 +02004817 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4818 dpll |= DPLL_DVO_2X_MODE;
4819
Daniel Vetterf47709a2013-03-28 10:42:02 +01004820 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004821 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4822 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4823 else
4824 dpll |= PLL_REF_INPUT_DREFCLK;
4825
4826 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004827 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004828}
4829
Daniel Vetter8a654f32013-06-01 17:16:22 +02004830static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004831{
4832 struct drm_device *dev = intel_crtc->base.dev;
4833 struct drm_i915_private *dev_priv = dev->dev_private;
4834 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004835 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004836 struct drm_display_mode *adjusted_mode =
4837 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004838 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4839
4840 /* We need to be careful not to changed the adjusted mode, for otherwise
4841 * the hw state checker will get angry at the mismatch. */
4842 crtc_vtotal = adjusted_mode->crtc_vtotal;
4843 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004844
4845 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4846 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004847 crtc_vtotal -= 1;
4848 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004849 vsyncshift = adjusted_mode->crtc_hsync_start
4850 - adjusted_mode->crtc_htotal / 2;
4851 } else {
4852 vsyncshift = 0;
4853 }
4854
4855 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004856 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004857
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004858 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004859 (adjusted_mode->crtc_hdisplay - 1) |
4860 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004861 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004862 (adjusted_mode->crtc_hblank_start - 1) |
4863 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004864 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004865 (adjusted_mode->crtc_hsync_start - 1) |
4866 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4867
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004868 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004869 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004870 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004871 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004872 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004873 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004874 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004875 (adjusted_mode->crtc_vsync_start - 1) |
4876 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4877
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004878 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4879 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4880 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4881 * bits. */
4882 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4883 (pipe == PIPE_B || pipe == PIPE_C))
4884 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4885
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004886 /* pipesrc controls the size that is scaled from, which should
4887 * always be the user's requested size.
4888 */
4889 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004890 ((intel_crtc->config.pipe_src_w - 1) << 16) |
4891 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004892}
4893
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004894static void intel_get_pipe_timings(struct intel_crtc *crtc,
4895 struct intel_crtc_config *pipe_config)
4896{
4897 struct drm_device *dev = crtc->base.dev;
4898 struct drm_i915_private *dev_priv = dev->dev_private;
4899 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4900 uint32_t tmp;
4901
4902 tmp = I915_READ(HTOTAL(cpu_transcoder));
4903 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4904 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4905 tmp = I915_READ(HBLANK(cpu_transcoder));
4906 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4907 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4908 tmp = I915_READ(HSYNC(cpu_transcoder));
4909 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4910 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4911
4912 tmp = I915_READ(VTOTAL(cpu_transcoder));
4913 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4914 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4915 tmp = I915_READ(VBLANK(cpu_transcoder));
4916 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4917 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4918 tmp = I915_READ(VSYNC(cpu_transcoder));
4919 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4920 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4921
4922 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4923 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4924 pipe_config->adjusted_mode.crtc_vtotal += 1;
4925 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4926 }
4927
4928 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03004929 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
4930 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
4931
4932 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
4933 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004934}
4935
Jesse Barnesbabea612013-06-26 18:57:38 +03004936static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4937 struct intel_crtc_config *pipe_config)
4938{
4939 struct drm_crtc *crtc = &intel_crtc->base;
4940
4941 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4942 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4943 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4944 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4945
4946 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4947 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4948 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4949 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4950
4951 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4952
Damien Lespiau241bfc32013-09-25 16:45:37 +01004953 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03004954 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4955}
4956
Daniel Vetter84b046f2013-02-19 18:48:54 +01004957static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4958{
4959 struct drm_device *dev = intel_crtc->base.dev;
4960 struct drm_i915_private *dev_priv = dev->dev_private;
4961 uint32_t pipeconf;
4962
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004963 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004964
Daniel Vetter67c72a12013-09-24 11:46:14 +02004965 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
4966 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
4967 pipeconf |= PIPECONF_ENABLE;
4968
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004969 if (intel_crtc->config.double_wide)
4970 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004971
Daniel Vetterff9ce462013-04-24 14:57:17 +02004972 /* only g4x and later have fancy bpc/dither controls */
4973 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004974 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4975 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4976 pipeconf |= PIPECONF_DITHER_EN |
4977 PIPECONF_DITHER_TYPE_SP;
4978
4979 switch (intel_crtc->config.pipe_bpp) {
4980 case 18:
4981 pipeconf |= PIPECONF_6BPC;
4982 break;
4983 case 24:
4984 pipeconf |= PIPECONF_8BPC;
4985 break;
4986 case 30:
4987 pipeconf |= PIPECONF_10BPC;
4988 break;
4989 default:
4990 /* Case prevented by intel_choose_pipe_bpp_dither. */
4991 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004992 }
4993 }
4994
4995 if (HAS_PIPE_CXSR(dev)) {
4996 if (intel_crtc->lowfreq_avail) {
4997 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4998 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4999 } else {
5000 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005001 }
5002 }
5003
Daniel Vetter84b046f2013-02-19 18:48:54 +01005004 if (!IS_GEN2(dev) &&
5005 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5006 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5007 else
5008 pipeconf |= PIPECONF_PROGRESSIVE;
5009
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005010 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5011 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005012
Daniel Vetter84b046f2013-02-19 18:48:54 +01005013 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5014 POSTING_READ(PIPECONF(intel_crtc->pipe));
5015}
5016
Eric Anholtf564048e2011-03-30 13:01:02 -07005017static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005018 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005019 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005020{
5021 struct drm_device *dev = crtc->dev;
5022 struct drm_i915_private *dev_priv = dev->dev_private;
5023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5024 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005025 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005026 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005027 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005028 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005029 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005030 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005031 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005032 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005033 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005034
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005035 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005036 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005037 case INTEL_OUTPUT_LVDS:
5038 is_lvds = true;
5039 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005040 case INTEL_OUTPUT_DSI:
5041 is_dsi = true;
5042 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005043 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005044
Eric Anholtc751ce42010-03-25 11:48:48 -07005045 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005046 }
5047
Jani Nikulaf2335332013-09-13 11:03:09 +03005048 if (is_dsi)
5049 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005050
Jani Nikulaf2335332013-09-13 11:03:09 +03005051 if (!intel_crtc->config.clock_set) {
5052 refclk = i9xx_get_refclk(crtc, num_connectors);
5053
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005054 /*
5055 * Returns a set of divisors for the desired target clock with
5056 * the given refclk, or FALSE. The returned values represent
5057 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5058 * 2) / p1 / p2.
5059 */
5060 limit = intel_limit(crtc, refclk);
5061 ok = dev_priv->display.find_dpll(limit, crtc,
5062 intel_crtc->config.port_clock,
5063 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005064 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005065 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5066 return -EINVAL;
5067 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005068
Jani Nikulaf2335332013-09-13 11:03:09 +03005069 if (is_lvds && dev_priv->lvds_downclock_avail) {
5070 /*
5071 * Ensure we match the reduced clock's P to the target
5072 * clock. If the clocks don't match, we can't switch
5073 * the display clock by using the FP0/FP1. In such case
5074 * we will disable the LVDS downclock feature.
5075 */
5076 has_reduced_clock =
5077 dev_priv->display.find_dpll(limit, crtc,
5078 dev_priv->lvds_downclock,
5079 refclk, &clock,
5080 &reduced_clock);
5081 }
5082 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005083 intel_crtc->config.dpll.n = clock.n;
5084 intel_crtc->config.dpll.m1 = clock.m1;
5085 intel_crtc->config.dpll.m2 = clock.m2;
5086 intel_crtc->config.dpll.p1 = clock.p1;
5087 intel_crtc->config.dpll.p2 = clock.p2;
5088 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005089
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005090 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005091 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305092 has_reduced_clock ? &reduced_clock : NULL,
5093 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005094 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005095 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005096 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005097 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005098 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005099 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005100 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005101
Jani Nikulaf2335332013-09-13 11:03:09 +03005102skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005103 /* Set up the display plane register */
5104 dspcntr = DISPPLANE_GAMMA_ENABLE;
5105
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005106 if (!IS_VALLEYVIEW(dev)) {
5107 if (pipe == 0)
5108 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5109 else
5110 dspcntr |= DISPPLANE_SEL_PIPE_B;
5111 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005112
Daniel Vetter8a654f32013-06-01 17:16:22 +02005113 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005114
5115 /* pipesrc and dspsize control the size that is scaled from,
5116 * which should always be the user's requested size.
5117 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005118 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005119 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5120 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005121 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005122
Daniel Vetter84b046f2013-02-19 18:48:54 +01005123 i9xx_set_pipeconf(intel_crtc);
5124
Eric Anholtf564048e2011-03-30 13:01:02 -07005125 I915_WRITE(DSPCNTR(plane), dspcntr);
5126 POSTING_READ(DSPCNTR(plane));
5127
Daniel Vetter94352cf2012-07-05 22:51:56 +02005128 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005129
Eric Anholtf564048e2011-03-30 13:01:02 -07005130 return ret;
5131}
5132
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005133static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5134 struct intel_crtc_config *pipe_config)
5135{
5136 struct drm_device *dev = crtc->base.dev;
5137 struct drm_i915_private *dev_priv = dev->dev_private;
5138 uint32_t tmp;
5139
5140 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005141 if (!(tmp & PFIT_ENABLE))
5142 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005143
Daniel Vetter06922822013-07-11 13:35:40 +02005144 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005145 if (INTEL_INFO(dev)->gen < 4) {
5146 if (crtc->pipe != PIPE_B)
5147 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005148 } else {
5149 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5150 return;
5151 }
5152
Daniel Vetter06922822013-07-11 13:35:40 +02005153 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005154 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5155 if (INTEL_INFO(dev)->gen < 5)
5156 pipe_config->gmch_pfit.lvds_border_bits =
5157 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5158}
5159
Jesse Barnesacbec812013-09-20 11:29:32 -07005160static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5161 struct intel_crtc_config *pipe_config)
5162{
5163 struct drm_device *dev = crtc->base.dev;
5164 struct drm_i915_private *dev_priv = dev->dev_private;
5165 int pipe = pipe_config->cpu_transcoder;
5166 intel_clock_t clock;
5167 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005168 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005169
5170 mutex_lock(&dev_priv->dpio_lock);
5171 mdiv = vlv_dpio_read(dev_priv, pipe, DPIO_DIV(pipe));
5172 mutex_unlock(&dev_priv->dpio_lock);
5173
5174 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5175 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5176 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5177 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5178 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5179
Chris Wilson662c6ec2013-09-25 14:24:01 -07005180 clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
5181 clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
Jesse Barnesacbec812013-09-20 11:29:32 -07005182
5183 pipe_config->port_clock = clock.dot / 10;
5184}
5185
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005186static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5187 struct intel_crtc_config *pipe_config)
5188{
5189 struct drm_device *dev = crtc->base.dev;
5190 struct drm_i915_private *dev_priv = dev->dev_private;
5191 uint32_t tmp;
5192
Daniel Vettere143a212013-07-04 12:01:15 +02005193 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005194 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005195
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005196 tmp = I915_READ(PIPECONF(crtc->pipe));
5197 if (!(tmp & PIPECONF_ENABLE))
5198 return false;
5199
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005200 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5201 switch (tmp & PIPECONF_BPC_MASK) {
5202 case PIPECONF_6BPC:
5203 pipe_config->pipe_bpp = 18;
5204 break;
5205 case PIPECONF_8BPC:
5206 pipe_config->pipe_bpp = 24;
5207 break;
5208 case PIPECONF_10BPC:
5209 pipe_config->pipe_bpp = 30;
5210 break;
5211 default:
5212 break;
5213 }
5214 }
5215
Ville Syrjälä282740f2013-09-04 18:30:03 +03005216 if (INTEL_INFO(dev)->gen < 4)
5217 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5218
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005219 intel_get_pipe_timings(crtc, pipe_config);
5220
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005221 i9xx_get_pfit_config(crtc, pipe_config);
5222
Daniel Vetter6c49f242013-06-06 12:45:25 +02005223 if (INTEL_INFO(dev)->gen >= 4) {
5224 tmp = I915_READ(DPLL_MD(crtc->pipe));
5225 pipe_config->pixel_multiplier =
5226 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5227 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005228 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005229 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5230 tmp = I915_READ(DPLL(crtc->pipe));
5231 pipe_config->pixel_multiplier =
5232 ((tmp & SDVO_MULTIPLIER_MASK)
5233 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5234 } else {
5235 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5236 * port and will be fixed up in the encoder->get_config
5237 * function. */
5238 pipe_config->pixel_multiplier = 1;
5239 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005240 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5241 if (!IS_VALLEYVIEW(dev)) {
5242 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5243 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005244 } else {
5245 /* Mask out read-only status bits. */
5246 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5247 DPLL_PORTC_READY_MASK |
5248 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005249 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005250
Jesse Barnesacbec812013-09-20 11:29:32 -07005251 if (IS_VALLEYVIEW(dev))
5252 vlv_crtc_clock_get(crtc, pipe_config);
5253 else
5254 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005255
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005256 return true;
5257}
5258
Paulo Zanonidde86e22012-12-01 12:04:25 -02005259static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005260{
5261 struct drm_i915_private *dev_priv = dev->dev_private;
5262 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005263 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005264 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005265 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005266 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005267 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005268 bool has_ck505 = false;
5269 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005270
5271 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005272 list_for_each_entry(encoder, &mode_config->encoder_list,
5273 base.head) {
5274 switch (encoder->type) {
5275 case INTEL_OUTPUT_LVDS:
5276 has_panel = true;
5277 has_lvds = true;
5278 break;
5279 case INTEL_OUTPUT_EDP:
5280 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005281 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005282 has_cpu_edp = true;
5283 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005284 }
5285 }
5286
Keith Packard99eb6a02011-09-26 14:29:12 -07005287 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005288 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005289 can_ssc = has_ck505;
5290 } else {
5291 has_ck505 = false;
5292 can_ssc = true;
5293 }
5294
Imre Deak2de69052013-05-08 13:14:04 +03005295 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5296 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005297
5298 /* Ironlake: try to setup display ref clock before DPLL
5299 * enabling. This is only under driver's control after
5300 * PCH B stepping, previous chipset stepping should be
5301 * ignoring this setting.
5302 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005303 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005304
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005305 /* As we must carefully and slowly disable/enable each source in turn,
5306 * compute the final state we want first and check if we need to
5307 * make any changes at all.
5308 */
5309 final = val;
5310 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005311 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005312 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005313 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005314 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5315
5316 final &= ~DREF_SSC_SOURCE_MASK;
5317 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5318 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005319
Keith Packard199e5d72011-09-22 12:01:57 -07005320 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005321 final |= DREF_SSC_SOURCE_ENABLE;
5322
5323 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5324 final |= DREF_SSC1_ENABLE;
5325
5326 if (has_cpu_edp) {
5327 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5328 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5329 else
5330 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5331 } else
5332 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5333 } else {
5334 final |= DREF_SSC_SOURCE_DISABLE;
5335 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5336 }
5337
5338 if (final == val)
5339 return;
5340
5341 /* Always enable nonspread source */
5342 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5343
5344 if (has_ck505)
5345 val |= DREF_NONSPREAD_CK505_ENABLE;
5346 else
5347 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5348
5349 if (has_panel) {
5350 val &= ~DREF_SSC_SOURCE_MASK;
5351 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005352
Keith Packard199e5d72011-09-22 12:01:57 -07005353 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005354 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005355 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005356 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005357 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005358 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005359
5360 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005361 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005362 POSTING_READ(PCH_DREF_CONTROL);
5363 udelay(200);
5364
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005365 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005366
5367 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005368 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005369 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005370 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005371 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005372 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005373 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005374 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005375 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005376 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005377
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005378 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005379 POSTING_READ(PCH_DREF_CONTROL);
5380 udelay(200);
5381 } else {
5382 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5383
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005384 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005385
5386 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005387 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005388
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005389 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005390 POSTING_READ(PCH_DREF_CONTROL);
5391 udelay(200);
5392
5393 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005394 val &= ~DREF_SSC_SOURCE_MASK;
5395 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005396
5397 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005398 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005399
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005400 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005401 POSTING_READ(PCH_DREF_CONTROL);
5402 udelay(200);
5403 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005404
5405 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005406}
5407
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005408static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005409{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005410 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005411
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005412 tmp = I915_READ(SOUTH_CHICKEN2);
5413 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5414 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005415
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005416 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5417 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5418 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005419
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005420 tmp = I915_READ(SOUTH_CHICKEN2);
5421 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5422 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005423
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005424 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5425 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5426 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005427}
5428
5429/* WaMPhyProgramming:hsw */
5430static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5431{
5432 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005433
5434 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5435 tmp &= ~(0xFF << 24);
5436 tmp |= (0x12 << 24);
5437 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5438
Paulo Zanonidde86e22012-12-01 12:04:25 -02005439 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5440 tmp |= (1 << 11);
5441 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5442
5443 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5444 tmp |= (1 << 11);
5445 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5446
Paulo Zanonidde86e22012-12-01 12:04:25 -02005447 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5448 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5449 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5450
5451 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5452 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5453 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5454
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005455 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5456 tmp &= ~(7 << 13);
5457 tmp |= (5 << 13);
5458 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005459
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005460 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5461 tmp &= ~(7 << 13);
5462 tmp |= (5 << 13);
5463 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005464
5465 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5466 tmp &= ~0xFF;
5467 tmp |= 0x1C;
5468 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5469
5470 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5471 tmp &= ~0xFF;
5472 tmp |= 0x1C;
5473 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5474
5475 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5476 tmp &= ~(0xFF << 16);
5477 tmp |= (0x1C << 16);
5478 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5479
5480 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5481 tmp &= ~(0xFF << 16);
5482 tmp |= (0x1C << 16);
5483 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5484
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005485 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5486 tmp |= (1 << 27);
5487 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005488
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005489 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5490 tmp |= (1 << 27);
5491 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005492
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005493 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5494 tmp &= ~(0xF << 28);
5495 tmp |= (4 << 28);
5496 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005497
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005498 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5499 tmp &= ~(0xF << 28);
5500 tmp |= (4 << 28);
5501 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005502}
5503
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005504/* Implements 3 different sequences from BSpec chapter "Display iCLK
5505 * Programming" based on the parameters passed:
5506 * - Sequence to enable CLKOUT_DP
5507 * - Sequence to enable CLKOUT_DP without spread
5508 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5509 */
5510static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5511 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005512{
5513 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005514 uint32_t reg, tmp;
5515
5516 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5517 with_spread = true;
5518 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5519 with_fdi, "LP PCH doesn't have FDI\n"))
5520 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005521
5522 mutex_lock(&dev_priv->dpio_lock);
5523
5524 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5525 tmp &= ~SBI_SSCCTL_DISABLE;
5526 tmp |= SBI_SSCCTL_PATHALT;
5527 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5528
5529 udelay(24);
5530
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005531 if (with_spread) {
5532 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5533 tmp &= ~SBI_SSCCTL_PATHALT;
5534 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005535
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005536 if (with_fdi) {
5537 lpt_reset_fdi_mphy(dev_priv);
5538 lpt_program_fdi_mphy(dev_priv);
5539 }
5540 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005541
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005542 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5543 SBI_GEN0 : SBI_DBUFF0;
5544 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5545 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5546 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005547
5548 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005549}
5550
Paulo Zanoni47701c32013-07-23 11:19:25 -03005551/* Sequence to disable CLKOUT_DP */
5552static void lpt_disable_clkout_dp(struct drm_device *dev)
5553{
5554 struct drm_i915_private *dev_priv = dev->dev_private;
5555 uint32_t reg, tmp;
5556
5557 mutex_lock(&dev_priv->dpio_lock);
5558
5559 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5560 SBI_GEN0 : SBI_DBUFF0;
5561 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5562 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5563 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5564
5565 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5566 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5567 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5568 tmp |= SBI_SSCCTL_PATHALT;
5569 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5570 udelay(32);
5571 }
5572 tmp |= SBI_SSCCTL_DISABLE;
5573 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5574 }
5575
5576 mutex_unlock(&dev_priv->dpio_lock);
5577}
5578
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005579static void lpt_init_pch_refclk(struct drm_device *dev)
5580{
5581 struct drm_mode_config *mode_config = &dev->mode_config;
5582 struct intel_encoder *encoder;
5583 bool has_vga = false;
5584
5585 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5586 switch (encoder->type) {
5587 case INTEL_OUTPUT_ANALOG:
5588 has_vga = true;
5589 break;
5590 }
5591 }
5592
Paulo Zanoni47701c32013-07-23 11:19:25 -03005593 if (has_vga)
5594 lpt_enable_clkout_dp(dev, true, true);
5595 else
5596 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005597}
5598
Paulo Zanonidde86e22012-12-01 12:04:25 -02005599/*
5600 * Initialize reference clocks when the driver loads
5601 */
5602void intel_init_pch_refclk(struct drm_device *dev)
5603{
5604 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5605 ironlake_init_pch_refclk(dev);
5606 else if (HAS_PCH_LPT(dev))
5607 lpt_init_pch_refclk(dev);
5608}
5609
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005610static int ironlake_get_refclk(struct drm_crtc *crtc)
5611{
5612 struct drm_device *dev = crtc->dev;
5613 struct drm_i915_private *dev_priv = dev->dev_private;
5614 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005615 int num_connectors = 0;
5616 bool is_lvds = false;
5617
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005618 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005619 switch (encoder->type) {
5620 case INTEL_OUTPUT_LVDS:
5621 is_lvds = true;
5622 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005623 }
5624 num_connectors++;
5625 }
5626
5627 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5628 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005629 dev_priv->vbt.lvds_ssc_freq);
5630 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005631 }
5632
5633 return 120000;
5634}
5635
Daniel Vetter6ff93602013-04-19 11:24:36 +02005636static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005637{
5638 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5640 int pipe = intel_crtc->pipe;
5641 uint32_t val;
5642
Daniel Vetter78114072013-06-13 00:54:57 +02005643 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005644
Daniel Vetter965e0c42013-03-27 00:44:57 +01005645 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005646 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005647 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005648 break;
5649 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005650 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005651 break;
5652 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005653 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005654 break;
5655 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005656 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005657 break;
5658 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005659 /* Case prevented by intel_choose_pipe_bpp_dither. */
5660 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005661 }
5662
Daniel Vetterd8b32242013-04-25 17:54:44 +02005663 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005664 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5665
Daniel Vetter6ff93602013-04-19 11:24:36 +02005666 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005667 val |= PIPECONF_INTERLACED_ILK;
5668 else
5669 val |= PIPECONF_PROGRESSIVE;
5670
Daniel Vetter50f3b012013-03-27 00:44:56 +01005671 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005672 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005673
Paulo Zanonic8203562012-09-12 10:06:29 -03005674 I915_WRITE(PIPECONF(pipe), val);
5675 POSTING_READ(PIPECONF(pipe));
5676}
5677
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005678/*
5679 * Set up the pipe CSC unit.
5680 *
5681 * Currently only full range RGB to limited range RGB conversion
5682 * is supported, but eventually this should handle various
5683 * RGB<->YCbCr scenarios as well.
5684 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005685static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005686{
5687 struct drm_device *dev = crtc->dev;
5688 struct drm_i915_private *dev_priv = dev->dev_private;
5689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5690 int pipe = intel_crtc->pipe;
5691 uint16_t coeff = 0x7800; /* 1.0 */
5692
5693 /*
5694 * TODO: Check what kind of values actually come out of the pipe
5695 * with these coeff/postoff values and adjust to get the best
5696 * accuracy. Perhaps we even need to take the bpc value into
5697 * consideration.
5698 */
5699
Daniel Vetter50f3b012013-03-27 00:44:56 +01005700 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005701 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5702
5703 /*
5704 * GY/GU and RY/RU should be the other way around according
5705 * to BSpec, but reality doesn't agree. Just set them up in
5706 * a way that results in the correct picture.
5707 */
5708 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5709 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5710
5711 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5712 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5713
5714 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5715 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5716
5717 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5718 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5719 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5720
5721 if (INTEL_INFO(dev)->gen > 6) {
5722 uint16_t postoff = 0;
5723
Daniel Vetter50f3b012013-03-27 00:44:56 +01005724 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005725 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5726
5727 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5728 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5729 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5730
5731 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5732 } else {
5733 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5734
Daniel Vetter50f3b012013-03-27 00:44:56 +01005735 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005736 mode |= CSC_BLACK_SCREEN_OFFSET;
5737
5738 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5739 }
5740}
5741
Daniel Vetter6ff93602013-04-19 11:24:36 +02005742static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005743{
5744 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005746 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005747 uint32_t val;
5748
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005749 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005750
Daniel Vetterd8b32242013-04-25 17:54:44 +02005751 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005752 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5753
Daniel Vetter6ff93602013-04-19 11:24:36 +02005754 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005755 val |= PIPECONF_INTERLACED_ILK;
5756 else
5757 val |= PIPECONF_PROGRESSIVE;
5758
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005759 I915_WRITE(PIPECONF(cpu_transcoder), val);
5760 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005761
5762 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5763 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005764}
5765
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005766static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005767 intel_clock_t *clock,
5768 bool *has_reduced_clock,
5769 intel_clock_t *reduced_clock)
5770{
5771 struct drm_device *dev = crtc->dev;
5772 struct drm_i915_private *dev_priv = dev->dev_private;
5773 struct intel_encoder *intel_encoder;
5774 int refclk;
5775 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02005776 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005777
5778 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5779 switch (intel_encoder->type) {
5780 case INTEL_OUTPUT_LVDS:
5781 is_lvds = true;
5782 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005783 }
5784 }
5785
5786 refclk = ironlake_get_refclk(crtc);
5787
5788 /*
5789 * Returns a set of divisors for the desired target clock with the given
5790 * refclk, or FALSE. The returned values represent the clock equation:
5791 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5792 */
5793 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005794 ret = dev_priv->display.find_dpll(limit, crtc,
5795 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005796 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005797 if (!ret)
5798 return false;
5799
5800 if (is_lvds && dev_priv->lvds_downclock_avail) {
5801 /*
5802 * Ensure we match the reduced clock's P to the target clock.
5803 * If the clocks don't match, we can't switch the display clock
5804 * by using the FP0/FP1. In such case we will disable the LVDS
5805 * downclock feature.
5806 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005807 *has_reduced_clock =
5808 dev_priv->display.find_dpll(limit, crtc,
5809 dev_priv->lvds_downclock,
5810 refclk, clock,
5811 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005812 }
5813
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005814 return true;
5815}
5816
Daniel Vetter01a415f2012-10-27 15:58:40 +02005817static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5818{
5819 struct drm_i915_private *dev_priv = dev->dev_private;
5820 uint32_t temp;
5821
5822 temp = I915_READ(SOUTH_CHICKEN1);
5823 if (temp & FDI_BC_BIFURCATION_SELECT)
5824 return;
5825
5826 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5827 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5828
5829 temp |= FDI_BC_BIFURCATION_SELECT;
5830 DRM_DEBUG_KMS("enabling fdi C rx\n");
5831 I915_WRITE(SOUTH_CHICKEN1, temp);
5832 POSTING_READ(SOUTH_CHICKEN1);
5833}
5834
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005835static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005836{
5837 struct drm_device *dev = intel_crtc->base.dev;
5838 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005839
5840 switch (intel_crtc->pipe) {
5841 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005842 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005843 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005844 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005845 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5846 else
5847 cpt_enable_fdi_bc_bifurcation(dev);
5848
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005849 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005850 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005851 cpt_enable_fdi_bc_bifurcation(dev);
5852
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005853 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005854 default:
5855 BUG();
5856 }
5857}
5858
Paulo Zanonid4b19312012-11-29 11:29:32 -02005859int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5860{
5861 /*
5862 * Account for spread spectrum to avoid
5863 * oversubscribing the link. Max center spread
5864 * is 2.5%; use 5% for safety's sake.
5865 */
5866 u32 bps = target_clock * bpp * 21 / 20;
5867 return bps / (link_bw * 8) + 1;
5868}
5869
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005870static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005871{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005872 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005873}
5874
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005875static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005876 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005877 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005878{
5879 struct drm_crtc *crtc = &intel_crtc->base;
5880 struct drm_device *dev = crtc->dev;
5881 struct drm_i915_private *dev_priv = dev->dev_private;
5882 struct intel_encoder *intel_encoder;
5883 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005884 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005885 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005886
5887 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5888 switch (intel_encoder->type) {
5889 case INTEL_OUTPUT_LVDS:
5890 is_lvds = true;
5891 break;
5892 case INTEL_OUTPUT_SDVO:
5893 case INTEL_OUTPUT_HDMI:
5894 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005895 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005896 }
5897
5898 num_connectors++;
5899 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005900
Chris Wilsonc1858122010-12-03 21:35:48 +00005901 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005902 factor = 21;
5903 if (is_lvds) {
5904 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005905 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005906 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005907 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005908 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005909 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005910
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005911 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005912 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005913
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005914 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5915 *fp2 |= FP_CB_TUNE;
5916
Chris Wilson5eddb702010-09-11 13:48:45 +01005917 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005918
Eric Anholta07d6782011-03-30 13:01:08 -07005919 if (is_lvds)
5920 dpll |= DPLLB_MODE_LVDS;
5921 else
5922 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005923
Daniel Vetteref1b4602013-06-01 17:17:04 +02005924 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5925 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005926
5927 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005928 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005929 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005930 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005931
Eric Anholta07d6782011-03-30 13:01:08 -07005932 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005933 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005934 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005935 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005936
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005937 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005938 case 5:
5939 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5940 break;
5941 case 7:
5942 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5943 break;
5944 case 10:
5945 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5946 break;
5947 case 14:
5948 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5949 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005950 }
5951
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005952 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005953 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005954 else
5955 dpll |= PLL_REF_INPUT_DREFCLK;
5956
Daniel Vetter959e16d2013-06-05 13:34:21 +02005957 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005958}
5959
Jesse Barnes79e53942008-11-07 14:24:08 -08005960static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005961 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005962 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005963{
5964 struct drm_device *dev = crtc->dev;
5965 struct drm_i915_private *dev_priv = dev->dev_private;
5966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5967 int pipe = intel_crtc->pipe;
5968 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005969 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005970 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005971 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005972 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005973 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005974 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005975 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005976 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005977
5978 for_each_encoder_on_crtc(dev, crtc, encoder) {
5979 switch (encoder->type) {
5980 case INTEL_OUTPUT_LVDS:
5981 is_lvds = true;
5982 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005983 }
5984
5985 num_connectors++;
5986 }
5987
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005988 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5989 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5990
Daniel Vetterff9a6752013-06-01 17:16:21 +02005991 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005992 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005993 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005994 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5995 return -EINVAL;
5996 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005997 /* Compat-code for transition, will disappear. */
5998 if (!intel_crtc->config.clock_set) {
5999 intel_crtc->config.dpll.n = clock.n;
6000 intel_crtc->config.dpll.m1 = clock.m1;
6001 intel_crtc->config.dpll.m2 = clock.m2;
6002 intel_crtc->config.dpll.p1 = clock.p1;
6003 intel_crtc->config.dpll.p2 = clock.p2;
6004 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006005
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006006 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006007 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006008 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006009 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006010 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006011
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006012 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006013 &fp, &reduced_clock,
6014 has_reduced_clock ? &fp2 : NULL);
6015
Daniel Vetter959e16d2013-06-05 13:34:21 +02006016 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006017 intel_crtc->config.dpll_hw_state.fp0 = fp;
6018 if (has_reduced_clock)
6019 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6020 else
6021 intel_crtc->config.dpll_hw_state.fp1 = fp;
6022
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006023 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006024 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006025 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6026 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006027 return -EINVAL;
6028 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006029 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006030 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006031
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006032 if (intel_crtc->config.has_dp_encoder)
6033 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006034
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006035 if (is_lvds && has_reduced_clock && i915_powersave)
6036 intel_crtc->lowfreq_avail = true;
6037 else
6038 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006039
6040 if (intel_crtc->config.has_pch_encoder) {
6041 pll = intel_crtc_to_shared_dpll(intel_crtc);
6042
Jesse Barnes79e53942008-11-07 14:24:08 -08006043 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006044
Daniel Vetter8a654f32013-06-01 17:16:22 +02006045 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006046
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006047 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006048 intel_cpu_transcoder_set_m_n(intel_crtc,
6049 &intel_crtc->config.fdi_m_n);
6050 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006051
Daniel Vetterebfd86f2013-04-19 11:24:44 +02006052 if (IS_IVYBRIDGE(dev))
6053 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006054
Daniel Vetter6ff93602013-04-19 11:24:36 +02006055 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006056
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006057 /* Set up the display plane register */
6058 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006059 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006060
Daniel Vetter94352cf2012-07-05 22:51:56 +02006061 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006062
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006063 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006064}
6065
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006066static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6067 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006068{
6069 struct drm_device *dev = crtc->base.dev;
6070 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006071 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006072
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006073 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6074 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6075 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6076 & ~TU_SIZE_MASK;
6077 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6078 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6079 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6080}
6081
6082static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6083 enum transcoder transcoder,
6084 struct intel_link_m_n *m_n)
6085{
6086 struct drm_device *dev = crtc->base.dev;
6087 struct drm_i915_private *dev_priv = dev->dev_private;
6088 enum pipe pipe = crtc->pipe;
6089
6090 if (INTEL_INFO(dev)->gen >= 5) {
6091 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6092 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6093 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6094 & ~TU_SIZE_MASK;
6095 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6096 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6097 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6098 } else {
6099 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6100 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6101 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6102 & ~TU_SIZE_MASK;
6103 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6104 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6105 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6106 }
6107}
6108
6109void intel_dp_get_m_n(struct intel_crtc *crtc,
6110 struct intel_crtc_config *pipe_config)
6111{
6112 if (crtc->config.has_pch_encoder)
6113 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6114 else
6115 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6116 &pipe_config->dp_m_n);
6117}
6118
Daniel Vetter72419202013-04-04 13:28:53 +02006119static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6120 struct intel_crtc_config *pipe_config)
6121{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006122 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6123 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006124}
6125
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006126static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6127 struct intel_crtc_config *pipe_config)
6128{
6129 struct drm_device *dev = crtc->base.dev;
6130 struct drm_i915_private *dev_priv = dev->dev_private;
6131 uint32_t tmp;
6132
6133 tmp = I915_READ(PF_CTL(crtc->pipe));
6134
6135 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006136 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006137 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6138 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006139
6140 /* We currently do not free assignements of panel fitters on
6141 * ivb/hsw (since we don't use the higher upscaling modes which
6142 * differentiates them) so just WARN about this case for now. */
6143 if (IS_GEN7(dev)) {
6144 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6145 PF_PIPE_SEL_IVB(crtc->pipe));
6146 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006147 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006148}
6149
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006150static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6151 struct intel_crtc_config *pipe_config)
6152{
6153 struct drm_device *dev = crtc->base.dev;
6154 struct drm_i915_private *dev_priv = dev->dev_private;
6155 uint32_t tmp;
6156
Daniel Vettere143a212013-07-04 12:01:15 +02006157 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006158 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006159
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006160 tmp = I915_READ(PIPECONF(crtc->pipe));
6161 if (!(tmp & PIPECONF_ENABLE))
6162 return false;
6163
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006164 switch (tmp & PIPECONF_BPC_MASK) {
6165 case PIPECONF_6BPC:
6166 pipe_config->pipe_bpp = 18;
6167 break;
6168 case PIPECONF_8BPC:
6169 pipe_config->pipe_bpp = 24;
6170 break;
6171 case PIPECONF_10BPC:
6172 pipe_config->pipe_bpp = 30;
6173 break;
6174 case PIPECONF_12BPC:
6175 pipe_config->pipe_bpp = 36;
6176 break;
6177 default:
6178 break;
6179 }
6180
Daniel Vetterab9412b2013-05-03 11:49:46 +02006181 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006182 struct intel_shared_dpll *pll;
6183
Daniel Vetter88adfff2013-03-28 10:42:01 +01006184 pipe_config->has_pch_encoder = true;
6185
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006186 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6187 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6188 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006189
6190 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006191
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006192 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006193 pipe_config->shared_dpll =
6194 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006195 } else {
6196 tmp = I915_READ(PCH_DPLL_SEL);
6197 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6198 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6199 else
6200 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6201 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006202
6203 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6204
6205 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6206 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006207
6208 tmp = pipe_config->dpll_hw_state.dpll;
6209 pipe_config->pixel_multiplier =
6210 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6211 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006212
6213 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006214 } else {
6215 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006216 }
6217
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006218 intel_get_pipe_timings(crtc, pipe_config);
6219
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006220 ironlake_get_pfit_config(crtc, pipe_config);
6221
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006222 return true;
6223}
6224
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006225static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6226{
6227 struct drm_device *dev = dev_priv->dev;
6228 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6229 struct intel_crtc *crtc;
6230 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006231 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006232
6233 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6234 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6235 pipe_name(crtc->pipe));
6236
6237 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6238 WARN(plls->spll_refcount, "SPLL enabled\n");
6239 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6240 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6241 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6242 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6243 "CPU PWM1 enabled\n");
6244 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6245 "CPU PWM2 enabled\n");
6246 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6247 "PCH PWM1 enabled\n");
6248 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6249 "Utility pin enabled\n");
6250 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6251
6252 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6253 val = I915_READ(DEIMR);
6254 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6255 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6256 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006257 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006258 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6259 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6260}
6261
6262/*
6263 * This function implements pieces of two sequences from BSpec:
6264 * - Sequence for display software to disable LCPLL
6265 * - Sequence for display software to allow package C8+
6266 * The steps implemented here are just the steps that actually touch the LCPLL
6267 * register. Callers should take care of disabling all the display engine
6268 * functions, doing the mode unset, fixing interrupts, etc.
6269 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006270static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6271 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006272{
6273 uint32_t val;
6274
6275 assert_can_disable_lcpll(dev_priv);
6276
6277 val = I915_READ(LCPLL_CTL);
6278
6279 if (switch_to_fclk) {
6280 val |= LCPLL_CD_SOURCE_FCLK;
6281 I915_WRITE(LCPLL_CTL, val);
6282
6283 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6284 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6285 DRM_ERROR("Switching to FCLK failed\n");
6286
6287 val = I915_READ(LCPLL_CTL);
6288 }
6289
6290 val |= LCPLL_PLL_DISABLE;
6291 I915_WRITE(LCPLL_CTL, val);
6292 POSTING_READ(LCPLL_CTL);
6293
6294 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6295 DRM_ERROR("LCPLL still locked\n");
6296
6297 val = I915_READ(D_COMP);
6298 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006299 mutex_lock(&dev_priv->rps.hw_lock);
6300 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6301 DRM_ERROR("Failed to disable D_COMP\n");
6302 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006303 POSTING_READ(D_COMP);
6304 ndelay(100);
6305
6306 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6307 DRM_ERROR("D_COMP RCOMP still in progress\n");
6308
6309 if (allow_power_down) {
6310 val = I915_READ(LCPLL_CTL);
6311 val |= LCPLL_POWER_DOWN_ALLOW;
6312 I915_WRITE(LCPLL_CTL, val);
6313 POSTING_READ(LCPLL_CTL);
6314 }
6315}
6316
6317/*
6318 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6319 * source.
6320 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006321static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006322{
6323 uint32_t val;
6324
6325 val = I915_READ(LCPLL_CTL);
6326
6327 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6328 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6329 return;
6330
Paulo Zanoni215733f2013-08-19 13:18:07 -03006331 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6332 * we'll hang the machine! */
6333 dev_priv->uncore.funcs.force_wake_get(dev_priv);
6334
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006335 if (val & LCPLL_POWER_DOWN_ALLOW) {
6336 val &= ~LCPLL_POWER_DOWN_ALLOW;
6337 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006338 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006339 }
6340
6341 val = I915_READ(D_COMP);
6342 val |= D_COMP_COMP_FORCE;
6343 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006344 mutex_lock(&dev_priv->rps.hw_lock);
6345 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6346 DRM_ERROR("Failed to enable D_COMP\n");
6347 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006348 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006349
6350 val = I915_READ(LCPLL_CTL);
6351 val &= ~LCPLL_PLL_DISABLE;
6352 I915_WRITE(LCPLL_CTL, val);
6353
6354 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6355 DRM_ERROR("LCPLL not locked yet\n");
6356
6357 if (val & LCPLL_CD_SOURCE_FCLK) {
6358 val = I915_READ(LCPLL_CTL);
6359 val &= ~LCPLL_CD_SOURCE_FCLK;
6360 I915_WRITE(LCPLL_CTL, val);
6361
6362 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6363 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6364 DRM_ERROR("Switching back to LCPLL failed\n");
6365 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006366
6367 dev_priv->uncore.funcs.force_wake_put(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006368}
6369
Paulo Zanonic67a4702013-08-19 13:18:09 -03006370void hsw_enable_pc8_work(struct work_struct *__work)
6371{
6372 struct drm_i915_private *dev_priv =
6373 container_of(to_delayed_work(__work), struct drm_i915_private,
6374 pc8.enable_work);
6375 struct drm_device *dev = dev_priv->dev;
6376 uint32_t val;
6377
6378 if (dev_priv->pc8.enabled)
6379 return;
6380
6381 DRM_DEBUG_KMS("Enabling package C8+\n");
6382
6383 dev_priv->pc8.enabled = true;
6384
6385 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6386 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6387 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6388 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6389 }
6390
6391 lpt_disable_clkout_dp(dev);
6392 hsw_pc8_disable_interrupts(dev);
6393 hsw_disable_lcpll(dev_priv, true, true);
6394}
6395
6396static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6397{
6398 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6399 WARN(dev_priv->pc8.disable_count < 1,
6400 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6401
6402 dev_priv->pc8.disable_count--;
6403 if (dev_priv->pc8.disable_count != 0)
6404 return;
6405
6406 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006407 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006408}
6409
6410static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6411{
6412 struct drm_device *dev = dev_priv->dev;
6413 uint32_t val;
6414
6415 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6416 WARN(dev_priv->pc8.disable_count < 0,
6417 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6418
6419 dev_priv->pc8.disable_count++;
6420 if (dev_priv->pc8.disable_count != 1)
6421 return;
6422
6423 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6424 if (!dev_priv->pc8.enabled)
6425 return;
6426
6427 DRM_DEBUG_KMS("Disabling package C8+\n");
6428
6429 hsw_restore_lcpll(dev_priv);
6430 hsw_pc8_restore_interrupts(dev);
6431 lpt_init_pch_refclk(dev);
6432
6433 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6434 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6435 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6436 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6437 }
6438
6439 intel_prepare_ddi(dev);
6440 i915_gem_init_swizzling(dev);
6441 mutex_lock(&dev_priv->rps.hw_lock);
6442 gen6_update_ring_freq(dev);
6443 mutex_unlock(&dev_priv->rps.hw_lock);
6444 dev_priv->pc8.enabled = false;
6445}
6446
6447void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6448{
6449 mutex_lock(&dev_priv->pc8.lock);
6450 __hsw_enable_package_c8(dev_priv);
6451 mutex_unlock(&dev_priv->pc8.lock);
6452}
6453
6454void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6455{
6456 mutex_lock(&dev_priv->pc8.lock);
6457 __hsw_disable_package_c8(dev_priv);
6458 mutex_unlock(&dev_priv->pc8.lock);
6459}
6460
6461static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6462{
6463 struct drm_device *dev = dev_priv->dev;
6464 struct intel_crtc *crtc;
6465 uint32_t val;
6466
6467 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6468 if (crtc->base.enabled)
6469 return false;
6470
6471 /* This case is still possible since we have the i915.disable_power_well
6472 * parameter and also the KVMr or something else might be requesting the
6473 * power well. */
6474 val = I915_READ(HSW_PWR_WELL_DRIVER);
6475 if (val != 0) {
6476 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6477 return false;
6478 }
6479
6480 return true;
6481}
6482
6483/* Since we're called from modeset_global_resources there's no way to
6484 * symmetrically increase and decrease the refcount, so we use
6485 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6486 * or not.
6487 */
6488static void hsw_update_package_c8(struct drm_device *dev)
6489{
6490 struct drm_i915_private *dev_priv = dev->dev_private;
6491 bool allow;
6492
6493 if (!i915_enable_pc8)
6494 return;
6495
6496 mutex_lock(&dev_priv->pc8.lock);
6497
6498 allow = hsw_can_enable_package_c8(dev_priv);
6499
6500 if (allow == dev_priv->pc8.requirements_met)
6501 goto done;
6502
6503 dev_priv->pc8.requirements_met = allow;
6504
6505 if (allow)
6506 __hsw_enable_package_c8(dev_priv);
6507 else
6508 __hsw_disable_package_c8(dev_priv);
6509
6510done:
6511 mutex_unlock(&dev_priv->pc8.lock);
6512}
6513
6514static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6515{
6516 if (!dev_priv->pc8.gpu_idle) {
6517 dev_priv->pc8.gpu_idle = true;
6518 hsw_enable_package_c8(dev_priv);
6519 }
6520}
6521
6522static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6523{
6524 if (dev_priv->pc8.gpu_idle) {
6525 dev_priv->pc8.gpu_idle = false;
6526 hsw_disable_package_c8(dev_priv);
6527 }
Daniel Vetter94352cf2012-07-05 22:51:56 +02006528}
Eric Anholtf564048e2011-03-30 13:01:02 -07006529
6530static void haswell_modeset_global_resources(struct drm_device *dev)
6531{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006532 bool enable = false;
6533 struct intel_crtc *crtc;
Eric Anholt0b701d22011-03-30 13:01:03 -07006534
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006535 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6536 if (!crtc->base.enabled)
6537 continue;
Eric Anholt0b701d22011-03-30 13:01:03 -07006538
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006539 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
Jesse Barnes79e53942008-11-07 14:24:08 -08006540 crtc->config.cpu_transcoder != TRANSCODER_EDP)
6541 enable = true;
6542 }
6543
6544 intel_set_power_well(dev, enable);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006545
6546 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006547}
6548
6549static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6550 int x, int y,
6551 struct drm_framebuffer *fb)
6552{
6553 struct drm_device *dev = crtc->dev;
6554 struct drm_i915_private *dev_priv = dev->dev_private;
6555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6556 int plane = intel_crtc->plane;
6557 int ret;
6558
6559 if (!intel_ddi_pll_mode_set(crtc))
6560 return -EINVAL;
6561
Chris Wilson560b85b2010-08-07 11:01:38 +01006562 if (intel_crtc->config.has_dp_encoder)
6563 intel_dp_set_m_n(intel_crtc);
6564
6565 intel_crtc->lowfreq_avail = false;
6566
6567 intel_set_pipe_timings(intel_crtc);
6568
6569 if (intel_crtc->config.has_pch_encoder) {
6570 intel_cpu_transcoder_set_m_n(intel_crtc,
6571 &intel_crtc->config.fdi_m_n);
6572 }
6573
6574 haswell_set_pipeconf(crtc);
6575
6576 intel_set_pipe_csc(crtc);
6577
6578 /* Set up the display plane register */
6579 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6580 POSTING_READ(DSPCNTR(plane));
6581
6582 ret = intel_pipe_set_base(crtc, x, y, fb);
6583
Chris Wilson560b85b2010-08-07 11:01:38 +01006584 return ret;
6585}
6586
6587static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6588 struct intel_crtc_config *pipe_config)
6589{
6590 struct drm_device *dev = crtc->base.dev;
6591 struct drm_i915_private *dev_priv = dev->dev_private;
6592 enum intel_display_power_domain pfit_domain;
6593 uint32_t tmp;
6594
6595 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6596 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6597
6598 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6599 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6600 enum pipe trans_edp_pipe;
6601 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6602 default:
6603 WARN(1, "unknown pipe linked to edp transcoder\n");
6604 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6605 case TRANS_DDI_EDP_INPUT_A_ON:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006606 trans_edp_pipe = PIPE_A;
Chris Wilson6b383a72010-09-13 13:54:26 +01006607 break;
6608 case TRANS_DDI_EDP_INPUT_B_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006609 trans_edp_pipe = PIPE_B;
6610 break;
6611 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6612 trans_edp_pipe = PIPE_C;
6613 break;
6614 }
6615
Chris Wilson560b85b2010-08-07 11:01:38 +01006616 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006617 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6618 }
6619
6620 if (!intel_display_power_enabled(dev,
Chris Wilson6b383a72010-09-13 13:54:26 +01006621 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006622 return false;
6623
6624 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6625 if (!(tmp & PIPECONF_ENABLE))
6626 return false;
6627
6628 /*
6629 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6630 * DDI E. So just check whether this pipe is wired to DDI E and whether
6631 * the PCH transcoder is on.
6632 */
6633 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6634 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6635 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6636 pipe_config->has_pch_encoder = true;
6637
6638 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6639 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6640 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6641
6642 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6643 }
6644
6645 intel_get_pipe_timings(crtc, pipe_config);
6646
6647 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6648 if (intel_display_power_enabled(dev, pfit_domain))
6649 ironlake_get_pfit_config(crtc, pipe_config);
Chris Wilson560b85b2010-08-07 11:01:38 +01006650
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006651 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6652 (I915_READ(IPS_CTL) & IPS_ENABLE);
6653
Chris Wilson560b85b2010-08-07 11:01:38 +01006654 pipe_config->pixel_multiplier = 1;
6655
6656 return true;
6657}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006658
6659static int intel_crtc_mode_set(struct drm_crtc *crtc,
6660 int x, int y,
6661 struct drm_framebuffer *fb)
6662{
Jesse Barnes79e53942008-11-07 14:24:08 -08006663 struct drm_device *dev = crtc->dev;
Chris Wilson05394f32010-11-08 19:18:58 +00006664 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006665 struct intel_encoder *encoder;
6666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07006667 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6668 int pipe = intel_crtc->pipe;
6669 int ret;
6670
Eric Anholt0b701d22011-03-30 13:01:03 -07006671 drm_vblank_pre_modeset(dev, pipe);
6672
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006673 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6674
Jesse Barnes79e53942008-11-07 14:24:08 -08006675 drm_vblank_post_modeset(dev, pipe);
6676
Daniel Vetter9256aa12012-10-31 19:26:13 +01006677 if (ret != 0)
6678 return ret;
6679
6680 for_each_encoder_on_crtc(dev, crtc, encoder) {
6681 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6682 encoder->base.base.id,
6683 drm_get_encoder_name(&encoder->base),
6684 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02006685 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01006686 }
6687
6688 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006689}
6690
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006691static bool intel_eld_uptodate(struct drm_connector *connector,
6692 int reg_eldv, uint32_t bits_eldv,
6693 int reg_elda, uint32_t bits_elda,
6694 int reg_edid)
6695{
6696 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6697 uint8_t *eld = connector->eld;
6698 uint32_t i;
6699
6700 i = I915_READ(reg_eldv);
6701 i &= bits_eldv;
6702
6703 if (!eld[0])
6704 return !i;
6705
6706 if (!i)
6707 return false;
6708
6709 i = I915_READ(reg_elda);
6710 i &= ~bits_elda;
6711 I915_WRITE(reg_elda, i);
6712
6713 for (i = 0; i < eld[2]; i++)
6714 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6715 return false;
6716
6717 return true;
6718}
6719
Wu Fengguange0dac652011-09-05 14:25:34 +08006720static void g4x_write_eld(struct drm_connector *connector,
6721 struct drm_crtc *crtc)
6722{
6723 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6724 uint8_t *eld = connector->eld;
6725 uint32_t eldv;
6726 uint32_t len;
6727 uint32_t i;
6728
6729 i = I915_READ(G4X_AUD_VID_DID);
6730
6731 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6732 eldv = G4X_ELDV_DEVCL_DEVBLC;
6733 else
6734 eldv = G4X_ELDV_DEVCTG;
6735
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006736 if (intel_eld_uptodate(connector,
6737 G4X_AUD_CNTL_ST, eldv,
6738 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6739 G4X_HDMIW_HDMIEDID))
6740 return;
6741
Wu Fengguange0dac652011-09-05 14:25:34 +08006742 i = I915_READ(G4X_AUD_CNTL_ST);
6743 i &= ~(eldv | G4X_ELD_ADDR);
6744 len = (i >> 9) & 0x1f; /* ELD buffer size */
6745 I915_WRITE(G4X_AUD_CNTL_ST, i);
6746
6747 if (!eld[0])
6748 return;
6749
6750 len = min_t(uint8_t, eld[2], len);
6751 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6752 for (i = 0; i < len; i++)
6753 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6754
6755 i = I915_READ(G4X_AUD_CNTL_ST);
6756 i |= eldv;
6757 I915_WRITE(G4X_AUD_CNTL_ST, i);
6758}
6759
Wang Xingchao83358c852012-08-16 22:43:37 +08006760static void haswell_write_eld(struct drm_connector *connector,
6761 struct drm_crtc *crtc)
6762{
6763 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6764 uint8_t *eld = connector->eld;
6765 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006767 uint32_t eldv;
6768 uint32_t i;
6769 int len;
6770 int pipe = to_intel_crtc(crtc)->pipe;
6771 int tmp;
6772
6773 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6774 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6775 int aud_config = HSW_AUD_CFG(pipe);
6776 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6777
6778
6779 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6780
6781 /* Audio output enable */
6782 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6783 tmp = I915_READ(aud_cntrl_st2);
6784 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6785 I915_WRITE(aud_cntrl_st2, tmp);
6786
6787 /* Wait for 1 vertical blank */
6788 intel_wait_for_vblank(dev, pipe);
6789
6790 /* Set ELD valid state */
6791 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006792 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006793 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6794 I915_WRITE(aud_cntrl_st2, tmp);
6795 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006796 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006797
6798 /* Enable HDMI mode */
6799 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02006800 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08006801 /* clear N_programing_enable and N_value_index */
6802 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6803 I915_WRITE(aud_config, tmp);
6804
6805 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6806
6807 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006808 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006809
6810 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6811 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6812 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6813 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6814 } else
6815 I915_WRITE(aud_config, 0);
6816
6817 if (intel_eld_uptodate(connector,
6818 aud_cntrl_st2, eldv,
6819 aud_cntl_st, IBX_ELD_ADDRESS,
6820 hdmiw_hdmiedid))
6821 return;
6822
6823 i = I915_READ(aud_cntrl_st2);
6824 i &= ~eldv;
6825 I915_WRITE(aud_cntrl_st2, i);
6826
6827 if (!eld[0])
6828 return;
6829
6830 i = I915_READ(aud_cntl_st);
6831 i &= ~IBX_ELD_ADDRESS;
6832 I915_WRITE(aud_cntl_st, i);
6833 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6834 DRM_DEBUG_DRIVER("port num:%d\n", i);
6835
6836 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6837 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6838 for (i = 0; i < len; i++)
6839 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6840
6841 i = I915_READ(aud_cntrl_st2);
6842 i |= eldv;
6843 I915_WRITE(aud_cntrl_st2, i);
6844
6845}
6846
Wu Fengguange0dac652011-09-05 14:25:34 +08006847static void ironlake_write_eld(struct drm_connector *connector,
6848 struct drm_crtc *crtc)
6849{
6850 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6851 uint8_t *eld = connector->eld;
6852 uint32_t eldv;
6853 uint32_t i;
6854 int len;
6855 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006856 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006857 int aud_cntl_st;
6858 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006859 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006860
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006861 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006862 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6863 aud_config = IBX_AUD_CFG(pipe);
6864 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006865 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006866 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006867 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6868 aud_config = CPT_AUD_CFG(pipe);
6869 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006870 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006871 }
6872
Wang Xingchao9b138a82012-08-09 16:52:18 +08006873 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006874
6875 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006876 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006877 if (!i) {
6878 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6879 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006880 eldv = IBX_ELD_VALIDB;
6881 eldv |= IBX_ELD_VALIDB << 4;
6882 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006883 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006884 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006885 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006886 }
6887
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006888 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6889 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6890 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006891 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6892 } else
6893 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006894
6895 if (intel_eld_uptodate(connector,
6896 aud_cntrl_st2, eldv,
6897 aud_cntl_st, IBX_ELD_ADDRESS,
6898 hdmiw_hdmiedid))
6899 return;
6900
Wu Fengguange0dac652011-09-05 14:25:34 +08006901 i = I915_READ(aud_cntrl_st2);
6902 i &= ~eldv;
6903 I915_WRITE(aud_cntrl_st2, i);
6904
6905 if (!eld[0])
6906 return;
6907
Wu Fengguange0dac652011-09-05 14:25:34 +08006908 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006909 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006910 I915_WRITE(aud_cntl_st, i);
6911
6912 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6913 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6914 for (i = 0; i < len; i++)
6915 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6916
6917 i = I915_READ(aud_cntrl_st2);
6918 i |= eldv;
6919 I915_WRITE(aud_cntrl_st2, i);
6920}
6921
6922void intel_write_eld(struct drm_encoder *encoder,
6923 struct drm_display_mode *mode)
6924{
6925 struct drm_crtc *crtc = encoder->crtc;
6926 struct drm_connector *connector;
6927 struct drm_device *dev = encoder->dev;
6928 struct drm_i915_private *dev_priv = dev->dev_private;
6929
6930 connector = drm_select_eld(encoder, mode);
6931 if (!connector)
6932 return;
6933
6934 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6935 connector->base.id,
6936 drm_get_connector_name(connector),
6937 connector->encoder->base.id,
6938 drm_get_encoder_name(connector->encoder));
6939
6940 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6941
6942 if (dev_priv->display.write_eld)
6943 dev_priv->display.write_eld(connector, crtc);
6944}
6945
Jesse Barnes79e53942008-11-07 14:24:08 -08006946static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6947{
6948 struct drm_device *dev = crtc->dev;
6949 struct drm_i915_private *dev_priv = dev->dev_private;
6950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6951 bool visible = base != 0;
6952 u32 cntl;
6953
6954 if (intel_crtc->cursor_visible == visible)
6955 return;
6956
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006957 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08006958 if (visible) {
6959 /* On these chipsets we can only modify the base whilst
6960 * the cursor is disabled.
6961 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006962 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006963
6964 cntl &= ~(CURSOR_FORMAT_MASK);
6965 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6966 cntl |= CURSOR_ENABLE |
6967 CURSOR_GAMMA_ENABLE |
6968 CURSOR_FORMAT_ARGB;
6969 } else
6970 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006971 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08006972
6973 intel_crtc->cursor_visible = visible;
6974}
6975
6976static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6977{
6978 struct drm_device *dev = crtc->dev;
6979 struct drm_i915_private *dev_priv = dev->dev_private;
6980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6981 int pipe = intel_crtc->pipe;
6982 bool visible = base != 0;
6983
6984 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006985 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006986 if (base) {
6987 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6988 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6989 cntl |= pipe << 28; /* Connect to correct pipe */
6990 } else {
6991 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6992 cntl |= CURSOR_MODE_DISABLE;
6993 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006994 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08006995
6996 intel_crtc->cursor_visible = visible;
6997 }
6998 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006999 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007000}
7001
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007002static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7003{
7004 struct drm_device *dev = crtc->dev;
7005 struct drm_i915_private *dev_priv = dev->dev_private;
7006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7007 int pipe = intel_crtc->pipe;
7008 bool visible = base != 0;
7009
7010 if (intel_crtc->cursor_visible != visible) {
7011 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7012 if (base) {
7013 cntl &= ~CURSOR_MODE;
7014 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7015 } else {
7016 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7017 cntl |= CURSOR_MODE_DISABLE;
7018 }
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007019 if (IS_HASWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007020 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007021 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7022 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007023 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7024
7025 intel_crtc->cursor_visible = visible;
7026 }
7027 /* and commit changes on next vblank */
7028 I915_WRITE(CURBASE_IVB(pipe), base);
7029}
7030
Jesse Barnes79e53942008-11-07 14:24:08 -08007031/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7032static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7033 bool on)
7034{
7035 struct drm_device *dev = crtc->dev;
7036 struct drm_i915_private *dev_priv = dev->dev_private;
7037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7038 int pipe = intel_crtc->pipe;
7039 int x = intel_crtc->cursor_x;
7040 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007041 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007042 bool visible;
7043
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007044 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007045 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007046
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007047 if (x >= intel_crtc->config.pipe_src_w)
7048 base = 0;
7049
7050 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007051 base = 0;
7052
7053 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007054 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007055 base = 0;
7056
7057 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7058 x = -x;
7059 }
7060 pos |= x << CURSOR_X_SHIFT;
7061
7062 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007063 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007064 base = 0;
7065
7066 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7067 y = -y;
7068 }
7069 pos |= y << CURSOR_Y_SHIFT;
7070
7071 visible = base != 0;
7072 if (!visible && !intel_crtc->cursor_visible)
7073 return;
7074
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03007075 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007076 I915_WRITE(CURPOS_IVB(pipe), pos);
7077 ivb_update_cursor(crtc, base);
7078 } else {
7079 I915_WRITE(CURPOS(pipe), pos);
7080 if (IS_845G(dev) || IS_I865G(dev))
7081 i845_update_cursor(crtc, base);
7082 else
7083 i9xx_update_cursor(crtc, base);
7084 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007085}
7086
7087static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7088 struct drm_file *file,
7089 uint32_t handle,
7090 uint32_t width, uint32_t height)
7091{
7092 struct drm_device *dev = crtc->dev;
7093 struct drm_i915_private *dev_priv = dev->dev_private;
7094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007095 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007096 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007097 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007098
Jesse Barnes79e53942008-11-07 14:24:08 -08007099 /* if we want to turn off the cursor ignore width and height */
7100 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007101 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007102 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007103 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007104 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007105 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007106 }
7107
7108 /* Currently we only support 64x64 cursors */
7109 if (width != 64 || height != 64) {
7110 DRM_ERROR("we currently only support 64x64 cursors\n");
7111 return -EINVAL;
7112 }
7113
Chris Wilson05394f32010-11-08 19:18:58 +00007114 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007115 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007116 return -ENOENT;
7117
Chris Wilson05394f32010-11-08 19:18:58 +00007118 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007119 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007120 ret = -ENOMEM;
7121 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007122 }
7123
Dave Airlie71acb5e2008-12-30 20:31:46 +10007124 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007125 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007126 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007127 unsigned alignment;
7128
Chris Wilsond9e86c02010-11-10 16:40:20 +00007129 if (obj->tiling_mode) {
7130 DRM_ERROR("cursor cannot be tiled\n");
7131 ret = -EINVAL;
7132 goto fail_locked;
7133 }
7134
Chris Wilson693db182013-03-05 14:52:39 +00007135 /* Note that the w/a also requires 2 PTE of padding following
7136 * the bo. We currently fill all unused PTE with the shadow
7137 * page and so we should always have valid PTE following the
7138 * cursor preventing the VT-d warning.
7139 */
7140 alignment = 0;
7141 if (need_vtd_wa(dev))
7142 alignment = 64*1024;
7143
7144 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007145 if (ret) {
7146 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007147 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007148 }
7149
Chris Wilsond9e86c02010-11-10 16:40:20 +00007150 ret = i915_gem_object_put_fence(obj);
7151 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007152 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007153 goto fail_unpin;
7154 }
7155
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007156 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007157 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007158 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007159 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007160 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7161 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007162 if (ret) {
7163 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007164 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007165 }
Chris Wilson05394f32010-11-08 19:18:58 +00007166 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007167 }
7168
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007169 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007170 I915_WRITE(CURSIZE, (height << 12) | width);
7171
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007172 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007173 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007174 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007175 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007176 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7177 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007178 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007179 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007180 }
Jesse Barnes80824002009-09-10 15:28:06 -07007181
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007182 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007183
7184 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007185 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007186 intel_crtc->cursor_width = width;
7187 intel_crtc->cursor_height = height;
7188
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007189 if (intel_crtc->active)
7190 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007191
Jesse Barnes79e53942008-11-07 14:24:08 -08007192 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007193fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007194 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007195fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007196 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007197fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007198 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007199 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007200}
7201
7202static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7203{
Jesse Barnes79e53942008-11-07 14:24:08 -08007204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007205
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007206 intel_crtc->cursor_x = x;
7207 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07007208
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007209 if (intel_crtc->active)
7210 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007211
7212 return 0;
7213}
7214
Jesse Barnes79e53942008-11-07 14:24:08 -08007215static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007216 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007217{
James Simmons72034252010-08-03 01:33:19 +01007218 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007220
James Simmons72034252010-08-03 01:33:19 +01007221 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007222 intel_crtc->lut_r[i] = red[i] >> 8;
7223 intel_crtc->lut_g[i] = green[i] >> 8;
7224 intel_crtc->lut_b[i] = blue[i] >> 8;
7225 }
7226
7227 intel_crtc_load_lut(crtc);
7228}
7229
Jesse Barnes79e53942008-11-07 14:24:08 -08007230/* VESA 640x480x72Hz mode to set on the pipe */
7231static struct drm_display_mode load_detect_mode = {
7232 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7233 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7234};
7235
Chris Wilsond2dff872011-04-19 08:36:26 +01007236static struct drm_framebuffer *
7237intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007238 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007239 struct drm_i915_gem_object *obj)
7240{
7241 struct intel_framebuffer *intel_fb;
7242 int ret;
7243
7244 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7245 if (!intel_fb) {
7246 drm_gem_object_unreference_unlocked(&obj->base);
7247 return ERR_PTR(-ENOMEM);
7248 }
7249
7250 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
7251 if (ret) {
7252 drm_gem_object_unreference_unlocked(&obj->base);
7253 kfree(intel_fb);
7254 return ERR_PTR(ret);
7255 }
7256
7257 return &intel_fb->base;
7258}
7259
7260static u32
7261intel_framebuffer_pitch_for_width(int width, int bpp)
7262{
7263 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7264 return ALIGN(pitch, 64);
7265}
7266
7267static u32
7268intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7269{
7270 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7271 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7272}
7273
7274static struct drm_framebuffer *
7275intel_framebuffer_create_for_mode(struct drm_device *dev,
7276 struct drm_display_mode *mode,
7277 int depth, int bpp)
7278{
7279 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007280 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007281
7282 obj = i915_gem_alloc_object(dev,
7283 intel_framebuffer_size_for_mode(mode, bpp));
7284 if (obj == NULL)
7285 return ERR_PTR(-ENOMEM);
7286
7287 mode_cmd.width = mode->hdisplay;
7288 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007289 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7290 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007291 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007292
7293 return intel_framebuffer_create(dev, &mode_cmd, obj);
7294}
7295
7296static struct drm_framebuffer *
7297mode_fits_in_fbdev(struct drm_device *dev,
7298 struct drm_display_mode *mode)
7299{
7300 struct drm_i915_private *dev_priv = dev->dev_private;
7301 struct drm_i915_gem_object *obj;
7302 struct drm_framebuffer *fb;
7303
7304 if (dev_priv->fbdev == NULL)
7305 return NULL;
7306
7307 obj = dev_priv->fbdev->ifb.obj;
7308 if (obj == NULL)
7309 return NULL;
7310
7311 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007312 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7313 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007314 return NULL;
7315
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007316 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007317 return NULL;
7318
7319 return fb;
7320}
7321
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007322bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007323 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007324 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007325{
7326 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007327 struct intel_encoder *intel_encoder =
7328 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007329 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007330 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007331 struct drm_crtc *crtc = NULL;
7332 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007333 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007334 int i = -1;
7335
Chris Wilsond2dff872011-04-19 08:36:26 +01007336 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7337 connector->base.id, drm_get_connector_name(connector),
7338 encoder->base.id, drm_get_encoder_name(encoder));
7339
Jesse Barnes79e53942008-11-07 14:24:08 -08007340 /*
7341 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007342 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007343 * - if the connector already has an assigned crtc, use it (but make
7344 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007345 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007346 * - try to find the first unused crtc that can drive this connector,
7347 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007348 */
7349
7350 /* See if we already have a CRTC for this connector */
7351 if (encoder->crtc) {
7352 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007353
Daniel Vetter7b240562012-12-12 00:35:33 +01007354 mutex_lock(&crtc->mutex);
7355
Daniel Vetter24218aa2012-08-12 19:27:11 +02007356 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007357 old->load_detect_temp = false;
7358
7359 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007360 if (connector->dpms != DRM_MODE_DPMS_ON)
7361 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007362
Chris Wilson71731882011-04-19 23:10:58 +01007363 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007364 }
7365
7366 /* Find an unused one (if possible) */
7367 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7368 i++;
7369 if (!(encoder->possible_crtcs & (1 << i)))
7370 continue;
7371 if (!possible_crtc->enabled) {
7372 crtc = possible_crtc;
7373 break;
7374 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007375 }
7376
7377 /*
7378 * If we didn't find an unused CRTC, don't use any.
7379 */
7380 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007381 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7382 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007383 }
7384
Daniel Vetter7b240562012-12-12 00:35:33 +01007385 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007386 intel_encoder->new_crtc = to_intel_crtc(crtc);
7387 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007388
7389 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007390 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007391 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007392 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007393
Chris Wilson64927112011-04-20 07:25:26 +01007394 if (!mode)
7395 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007396
Chris Wilsond2dff872011-04-19 08:36:26 +01007397 /* We need a framebuffer large enough to accommodate all accesses
7398 * that the plane may generate whilst we perform load detection.
7399 * We can not rely on the fbcon either being present (we get called
7400 * during its initialisation to detect all boot displays, or it may
7401 * not even exist) or that it is large enough to satisfy the
7402 * requested mode.
7403 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007404 fb = mode_fits_in_fbdev(dev, mode);
7405 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007406 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007407 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7408 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007409 } else
7410 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007411 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007412 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007413 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007414 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007415 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007416
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007417 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007418 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007419 if (old->release_fb)
7420 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007421 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007422 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007423 }
Chris Wilson71731882011-04-19 23:10:58 +01007424
Jesse Barnes79e53942008-11-07 14:24:08 -08007425 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007426 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007427 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007428}
7429
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007430void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007431 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007432{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007433 struct intel_encoder *intel_encoder =
7434 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007435 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007436 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007437
Chris Wilsond2dff872011-04-19 08:36:26 +01007438 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7439 connector->base.id, drm_get_connector_name(connector),
7440 encoder->base.id, drm_get_encoder_name(encoder));
7441
Chris Wilson8261b192011-04-19 23:18:09 +01007442 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007443 to_intel_connector(connector)->new_encoder = NULL;
7444 intel_encoder->new_crtc = NULL;
7445 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007446
Daniel Vetter36206362012-12-10 20:42:17 +01007447 if (old->release_fb) {
7448 drm_framebuffer_unregister_private(old->release_fb);
7449 drm_framebuffer_unreference(old->release_fb);
7450 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007451
Daniel Vetter67c96402013-01-23 16:25:09 +00007452 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007453 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007454 }
7455
Eric Anholtc751ce42010-03-25 11:48:48 -07007456 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007457 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7458 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007459
7460 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007461}
7462
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007463static int i9xx_pll_refclk(struct drm_device *dev,
7464 const struct intel_crtc_config *pipe_config)
7465{
7466 struct drm_i915_private *dev_priv = dev->dev_private;
7467 u32 dpll = pipe_config->dpll_hw_state.dpll;
7468
7469 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7470 return dev_priv->vbt.lvds_ssc_freq * 1000;
7471 else if (HAS_PCH_SPLIT(dev))
7472 return 120000;
7473 else if (!IS_GEN2(dev))
7474 return 96000;
7475 else
7476 return 48000;
7477}
7478
Jesse Barnes79e53942008-11-07 14:24:08 -08007479/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007480static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7481 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007482{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007483 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007484 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007485 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007486 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007487 u32 fp;
7488 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007489 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007490
7491 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007492 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007493 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007494 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007495
7496 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007497 if (IS_PINEVIEW(dev)) {
7498 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7499 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007500 } else {
7501 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7502 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7503 }
7504
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007505 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007506 if (IS_PINEVIEW(dev))
7507 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7508 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007509 else
7510 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007511 DPLL_FPA01_P1_POST_DIV_SHIFT);
7512
7513 switch (dpll & DPLL_MODE_MASK) {
7514 case DPLLB_MODE_DAC_SERIAL:
7515 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7516 5 : 10;
7517 break;
7518 case DPLLB_MODE_LVDS:
7519 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7520 7 : 14;
7521 break;
7522 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007523 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007524 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007525 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007526 }
7527
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007528 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007529 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007530 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007531 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007532 } else {
7533 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7534
7535 if (is_lvds) {
7536 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7537 DPLL_FPA01_P1_POST_DIV_SHIFT);
7538 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007539 } else {
7540 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7541 clock.p1 = 2;
7542 else {
7543 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7544 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7545 }
7546 if (dpll & PLL_P2_DIVIDE_BY_4)
7547 clock.p2 = 4;
7548 else
7549 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007550 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007551
7552 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007553 }
7554
Ville Syrjälä18442d02013-09-13 16:00:08 +03007555 /*
7556 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007557 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007558 * encoder's get_config() function.
7559 */
7560 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007561}
7562
Ville Syrjälä6878da02013-09-13 15:59:11 +03007563int intel_dotclock_calculate(int link_freq,
7564 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007565{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007566 /*
7567 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007568 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007569 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007570 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007571 *
7572 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007573 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007574 */
7575
Ville Syrjälä6878da02013-09-13 15:59:11 +03007576 if (!m_n->link_n)
7577 return 0;
7578
7579 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7580}
7581
Ville Syrjälä18442d02013-09-13 16:00:08 +03007582static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7583 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007584{
7585 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007586
7587 /* read out port_clock from the DPLL */
7588 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007589
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007590 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007591 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007592 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007593 * agree once we know their relationship in the encoder's
7594 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007595 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007596 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007597 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7598 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007599}
7600
7601/** Returns the currently programmed mode of the given pipe. */
7602struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7603 struct drm_crtc *crtc)
7604{
Jesse Barnes548f2452011-02-17 10:40:53 -08007605 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007607 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007608 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007609 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007610 int htot = I915_READ(HTOTAL(cpu_transcoder));
7611 int hsync = I915_READ(HSYNC(cpu_transcoder));
7612 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7613 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03007614 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08007615
7616 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7617 if (!mode)
7618 return NULL;
7619
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007620 /*
7621 * Construct a pipe_config sufficient for getting the clock info
7622 * back out of crtc_clock_get.
7623 *
7624 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7625 * to use a real value here instead.
7626 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03007627 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007628 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007629 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
7630 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
7631 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007632 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7633
Ville Syrjälä773ae032013-09-23 17:48:20 +03007634 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08007635 mode->hdisplay = (htot & 0xffff) + 1;
7636 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7637 mode->hsync_start = (hsync & 0xffff) + 1;
7638 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7639 mode->vdisplay = (vtot & 0xffff) + 1;
7640 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7641 mode->vsync_start = (vsync & 0xffff) + 1;
7642 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7643
7644 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007645
7646 return mode;
7647}
7648
Daniel Vetter3dec0092010-08-20 21:40:52 +02007649static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007650{
7651 struct drm_device *dev = crtc->dev;
7652 drm_i915_private_t *dev_priv = dev->dev_private;
7653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7654 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007655 int dpll_reg = DPLL(pipe);
7656 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007657
Eric Anholtbad720f2009-10-22 16:11:14 -07007658 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007659 return;
7660
7661 if (!dev_priv->lvds_downclock_avail)
7662 return;
7663
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007664 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007665 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007666 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007667
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007668 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007669
7670 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7671 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007672 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007673
Jesse Barnes652c3932009-08-17 13:31:43 -07007674 dpll = I915_READ(dpll_reg);
7675 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007676 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007677 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007678}
7679
7680static void intel_decrease_pllclock(struct drm_crtc *crtc)
7681{
7682 struct drm_device *dev = crtc->dev;
7683 drm_i915_private_t *dev_priv = dev->dev_private;
7684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007685
Eric Anholtbad720f2009-10-22 16:11:14 -07007686 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007687 return;
7688
7689 if (!dev_priv->lvds_downclock_avail)
7690 return;
7691
7692 /*
7693 * Since this is called by a timer, we should never get here in
7694 * the manual case.
7695 */
7696 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007697 int pipe = intel_crtc->pipe;
7698 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007699 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007700
Zhao Yakui44d98a62009-10-09 11:39:40 +08007701 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007702
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007703 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007704
Chris Wilson074b5e12012-05-02 12:07:06 +01007705 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007706 dpll |= DISPLAY_RATE_SELECT_FPA1;
7707 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007708 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007709 dpll = I915_READ(dpll_reg);
7710 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007711 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007712 }
7713
7714}
7715
Chris Wilsonf047e392012-07-21 12:31:41 +01007716void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007717{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007718 struct drm_i915_private *dev_priv = dev->dev_private;
7719
7720 hsw_package_c8_gpu_busy(dev_priv);
7721 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01007722}
7723
7724void intel_mark_idle(struct drm_device *dev)
7725{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007726 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00007727 struct drm_crtc *crtc;
7728
Paulo Zanonic67a4702013-08-19 13:18:09 -03007729 hsw_package_c8_gpu_idle(dev_priv);
7730
Chris Wilson725a5b52013-01-08 11:02:57 +00007731 if (!i915_powersave)
7732 return;
7733
7734 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7735 if (!crtc->fb)
7736 continue;
7737
7738 intel_decrease_pllclock(crtc);
7739 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01007740
7741 if (dev_priv->info->gen >= 6)
7742 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01007743}
7744
Chris Wilsonc65355b2013-06-06 16:53:41 -03007745void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7746 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007747{
7748 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007749 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007750
7751 if (!i915_powersave)
7752 return;
7753
Jesse Barnes652c3932009-08-17 13:31:43 -07007754 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007755 if (!crtc->fb)
7756 continue;
7757
Chris Wilsonc65355b2013-06-06 16:53:41 -03007758 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7759 continue;
7760
7761 intel_increase_pllclock(crtc);
7762 if (ring && intel_fbc_enabled(dev))
7763 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007764 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007765}
7766
Jesse Barnes79e53942008-11-07 14:24:08 -08007767static void intel_crtc_destroy(struct drm_crtc *crtc)
7768{
7769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007770 struct drm_device *dev = crtc->dev;
7771 struct intel_unpin_work *work;
7772 unsigned long flags;
7773
7774 spin_lock_irqsave(&dev->event_lock, flags);
7775 work = intel_crtc->unpin_work;
7776 intel_crtc->unpin_work = NULL;
7777 spin_unlock_irqrestore(&dev->event_lock, flags);
7778
7779 if (work) {
7780 cancel_work_sync(&work->work);
7781 kfree(work);
7782 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007783
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007784 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7785
Jesse Barnes79e53942008-11-07 14:24:08 -08007786 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007787
Jesse Barnes79e53942008-11-07 14:24:08 -08007788 kfree(intel_crtc);
7789}
7790
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007791static void intel_unpin_work_fn(struct work_struct *__work)
7792{
7793 struct intel_unpin_work *work =
7794 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007795 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007796
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007797 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007798 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007799 drm_gem_object_unreference(&work->pending_flip_obj->base);
7800 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007801
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007802 intel_update_fbc(dev);
7803 mutex_unlock(&dev->struct_mutex);
7804
7805 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7806 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7807
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007808 kfree(work);
7809}
7810
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007811static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007812 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007813{
7814 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7816 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007817 unsigned long flags;
7818
7819 /* Ignore early vblank irqs */
7820 if (intel_crtc == NULL)
7821 return;
7822
7823 spin_lock_irqsave(&dev->event_lock, flags);
7824 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007825
7826 /* Ensure we don't miss a work->pending update ... */
7827 smp_rmb();
7828
7829 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007830 spin_unlock_irqrestore(&dev->event_lock, flags);
7831 return;
7832 }
7833
Chris Wilsone7d841c2012-12-03 11:36:30 +00007834 /* and that the unpin work is consistent wrt ->pending. */
7835 smp_rmb();
7836
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007837 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007838
Rob Clark45a066e2012-10-08 14:50:40 -05007839 if (work->event)
7840 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007841
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007842 drm_vblank_put(dev, intel_crtc->pipe);
7843
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007844 spin_unlock_irqrestore(&dev->event_lock, flags);
7845
Daniel Vetter2c10d572012-12-20 21:24:07 +01007846 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007847
7848 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007849
7850 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007851}
7852
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007853void intel_finish_page_flip(struct drm_device *dev, int pipe)
7854{
7855 drm_i915_private_t *dev_priv = dev->dev_private;
7856 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7857
Mario Kleiner49b14a52010-12-09 07:00:07 +01007858 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007859}
7860
7861void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7862{
7863 drm_i915_private_t *dev_priv = dev->dev_private;
7864 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7865
Mario Kleiner49b14a52010-12-09 07:00:07 +01007866 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007867}
7868
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007869void intel_prepare_page_flip(struct drm_device *dev, int plane)
7870{
7871 drm_i915_private_t *dev_priv = dev->dev_private;
7872 struct intel_crtc *intel_crtc =
7873 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7874 unsigned long flags;
7875
Chris Wilsone7d841c2012-12-03 11:36:30 +00007876 /* NB: An MMIO update of the plane base pointer will also
7877 * generate a page-flip completion irq, i.e. every modeset
7878 * is also accompanied by a spurious intel_prepare_page_flip().
7879 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007880 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007881 if (intel_crtc->unpin_work)
7882 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007883 spin_unlock_irqrestore(&dev->event_lock, flags);
7884}
7885
Chris Wilsone7d841c2012-12-03 11:36:30 +00007886inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7887{
7888 /* Ensure that the work item is consistent when activating it ... */
7889 smp_wmb();
7890 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7891 /* and that it is marked active as soon as the irq could fire. */
7892 smp_wmb();
7893}
7894
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007895static int intel_gen2_queue_flip(struct drm_device *dev,
7896 struct drm_crtc *crtc,
7897 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007898 struct drm_i915_gem_object *obj,
7899 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007900{
7901 struct drm_i915_private *dev_priv = dev->dev_private;
7902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007903 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007904 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007905 int ret;
7906
Daniel Vetter6d90c952012-04-26 23:28:05 +02007907 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007908 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007909 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007910
Daniel Vetter6d90c952012-04-26 23:28:05 +02007911 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007912 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007913 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007914
7915 /* Can't queue multiple flips, so wait for the previous
7916 * one to finish before executing the next.
7917 */
7918 if (intel_crtc->plane)
7919 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7920 else
7921 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007922 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7923 intel_ring_emit(ring, MI_NOOP);
7924 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7925 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7926 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007927 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007928 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007929
7930 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007931 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007932 return 0;
7933
7934err_unpin:
7935 intel_unpin_fb_obj(obj);
7936err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007937 return ret;
7938}
7939
7940static int intel_gen3_queue_flip(struct drm_device *dev,
7941 struct drm_crtc *crtc,
7942 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007943 struct drm_i915_gem_object *obj,
7944 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007945{
7946 struct drm_i915_private *dev_priv = dev->dev_private;
7947 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007948 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007949 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007950 int ret;
7951
Daniel Vetter6d90c952012-04-26 23:28:05 +02007952 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007953 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007954 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007955
Daniel Vetter6d90c952012-04-26 23:28:05 +02007956 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007957 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007958 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007959
7960 if (intel_crtc->plane)
7961 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7962 else
7963 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007964 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7965 intel_ring_emit(ring, MI_NOOP);
7966 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7967 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7968 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007969 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007970 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007971
Chris Wilsone7d841c2012-12-03 11:36:30 +00007972 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01007973 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007974 return 0;
7975
7976err_unpin:
7977 intel_unpin_fb_obj(obj);
7978err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007979 return ret;
7980}
7981
7982static int intel_gen4_queue_flip(struct drm_device *dev,
7983 struct drm_crtc *crtc,
7984 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07007985 struct drm_i915_gem_object *obj,
7986 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007987{
7988 struct drm_i915_private *dev_priv = dev->dev_private;
7989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7990 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007991 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007992 int ret;
7993
Daniel Vetter6d90c952012-04-26 23:28:05 +02007994 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007995 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007996 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007997
Daniel Vetter6d90c952012-04-26 23:28:05 +02007998 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007999 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008000 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008001
8002 /* i965+ uses the linear or tiled offsets from the
8003 * Display Registers (which do not change across a page-flip)
8004 * so we need only reprogram the base address.
8005 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008006 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8007 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8008 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008009 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008010 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008011 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008012
8013 /* XXX Enabling the panel-fitter across page-flip is so far
8014 * untested on non-native modes, so ignore it for now.
8015 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8016 */
8017 pf = 0;
8018 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008019 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008020
8021 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008022 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008023 return 0;
8024
8025err_unpin:
8026 intel_unpin_fb_obj(obj);
8027err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008028 return ret;
8029}
8030
8031static int intel_gen6_queue_flip(struct drm_device *dev,
8032 struct drm_crtc *crtc,
8033 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008034 struct drm_i915_gem_object *obj,
8035 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008036{
8037 struct drm_i915_private *dev_priv = dev->dev_private;
8038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008039 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008040 uint32_t pf, pipesrc;
8041 int ret;
8042
Daniel Vetter6d90c952012-04-26 23:28:05 +02008043 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008044 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008045 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008046
Daniel Vetter6d90c952012-04-26 23:28:05 +02008047 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008048 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008049 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008050
Daniel Vetter6d90c952012-04-26 23:28:05 +02008051 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8052 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8053 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008054 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008055
Chris Wilson99d9acd2012-04-17 20:37:00 +01008056 /* Contrary to the suggestions in the documentation,
8057 * "Enable Panel Fitter" does not seem to be required when page
8058 * flipping with a non-native mode, and worse causes a normal
8059 * modeset to fail.
8060 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8061 */
8062 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008063 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008064 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008065
8066 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008067 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008068 return 0;
8069
8070err_unpin:
8071 intel_unpin_fb_obj(obj);
8072err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008073 return ret;
8074}
8075
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008076static int intel_gen7_queue_flip(struct drm_device *dev,
8077 struct drm_crtc *crtc,
8078 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008079 struct drm_i915_gem_object *obj,
8080 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008081{
8082 struct drm_i915_private *dev_priv = dev->dev_private;
8083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008084 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008085 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008086 int len, ret;
8087
8088 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008089 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008090 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008091
8092 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8093 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008094 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008095
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008096 switch(intel_crtc->plane) {
8097 case PLANE_A:
8098 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8099 break;
8100 case PLANE_B:
8101 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8102 break;
8103 case PLANE_C:
8104 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8105 break;
8106 default:
8107 WARN_ONCE(1, "unknown plane in flip command\n");
8108 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008109 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008110 }
8111
Chris Wilsonffe74d72013-08-26 20:58:12 +01008112 len = 4;
8113 if (ring->id == RCS)
8114 len += 6;
8115
8116 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008117 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008118 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008119
Chris Wilsonffe74d72013-08-26 20:58:12 +01008120 /* Unmask the flip-done completion message. Note that the bspec says that
8121 * we should do this for both the BCS and RCS, and that we must not unmask
8122 * more than one flip event at any time (or ensure that one flip message
8123 * can be sent by waiting for flip-done prior to queueing new flips).
8124 * Experimentation says that BCS works despite DERRMR masking all
8125 * flip-done completion events and that unmasking all planes at once
8126 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8127 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8128 */
8129 if (ring->id == RCS) {
8130 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8131 intel_ring_emit(ring, DERRMR);
8132 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8133 DERRMR_PIPEB_PRI_FLIP_DONE |
8134 DERRMR_PIPEC_PRI_FLIP_DONE));
8135 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8136 intel_ring_emit(ring, DERRMR);
8137 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8138 }
8139
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008140 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008141 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008142 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008143 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008144
8145 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008146 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008147 return 0;
8148
8149err_unpin:
8150 intel_unpin_fb_obj(obj);
8151err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008152 return ret;
8153}
8154
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008155static int intel_default_queue_flip(struct drm_device *dev,
8156 struct drm_crtc *crtc,
8157 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008158 struct drm_i915_gem_object *obj,
8159 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008160{
8161 return -ENODEV;
8162}
8163
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008164static int intel_crtc_page_flip(struct drm_crtc *crtc,
8165 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008166 struct drm_pending_vblank_event *event,
8167 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008168{
8169 struct drm_device *dev = crtc->dev;
8170 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008171 struct drm_framebuffer *old_fb = crtc->fb;
8172 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8174 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008175 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008176 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008177
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008178 /* Can't change pixel format via MI display flips. */
8179 if (fb->pixel_format != crtc->fb->pixel_format)
8180 return -EINVAL;
8181
8182 /*
8183 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8184 * Note that pitch changes could also affect these register.
8185 */
8186 if (INTEL_INFO(dev)->gen > 3 &&
8187 (fb->offsets[0] != crtc->fb->offsets[0] ||
8188 fb->pitches[0] != crtc->fb->pitches[0]))
8189 return -EINVAL;
8190
Daniel Vetterb14c5672013-09-19 12:18:32 +02008191 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008192 if (work == NULL)
8193 return -ENOMEM;
8194
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008195 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008196 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008197 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008198 INIT_WORK(&work->work, intel_unpin_work_fn);
8199
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008200 ret = drm_vblank_get(dev, intel_crtc->pipe);
8201 if (ret)
8202 goto free_work;
8203
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008204 /* We borrow the event spin lock for protecting unpin_work */
8205 spin_lock_irqsave(&dev->event_lock, flags);
8206 if (intel_crtc->unpin_work) {
8207 spin_unlock_irqrestore(&dev->event_lock, flags);
8208 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008209 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008210
8211 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008212 return -EBUSY;
8213 }
8214 intel_crtc->unpin_work = work;
8215 spin_unlock_irqrestore(&dev->event_lock, flags);
8216
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008217 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8218 flush_workqueue(dev_priv->wq);
8219
Chris Wilson79158102012-05-23 11:13:58 +01008220 ret = i915_mutex_lock_interruptible(dev);
8221 if (ret)
8222 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008223
Jesse Barnes75dfca82010-02-10 15:09:44 -08008224 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008225 drm_gem_object_reference(&work->old_fb_obj->base);
8226 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008227
8228 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008229
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008230 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008231
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008232 work->enable_stall_check = true;
8233
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008234 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008235 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008236
Keith Packarded8d1972013-07-22 18:49:58 -07008237 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008238 if (ret)
8239 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008240
Chris Wilson7782de32011-07-08 12:22:41 +01008241 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008242 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008243 mutex_unlock(&dev->struct_mutex);
8244
Jesse Barnese5510fa2010-07-01 16:48:37 -07008245 trace_i915_flip_request(intel_crtc->plane, obj);
8246
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008247 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008248
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008249cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008250 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008251 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008252 drm_gem_object_unreference(&work->old_fb_obj->base);
8253 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008254 mutex_unlock(&dev->struct_mutex);
8255
Chris Wilson79158102012-05-23 11:13:58 +01008256cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008257 spin_lock_irqsave(&dev->event_lock, flags);
8258 intel_crtc->unpin_work = NULL;
8259 spin_unlock_irqrestore(&dev->event_lock, flags);
8260
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008261 drm_vblank_put(dev, intel_crtc->pipe);
8262free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008263 kfree(work);
8264
8265 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008266}
8267
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008268static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008269 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8270 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008271};
8272
Daniel Vetter50f56112012-07-02 09:35:43 +02008273static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8274 struct drm_crtc *crtc)
8275{
8276 struct drm_device *dev;
8277 struct drm_crtc *tmp;
8278 int crtc_mask = 1;
8279
8280 WARN(!crtc, "checking null crtc?\n");
8281
8282 dev = crtc->dev;
8283
8284 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8285 if (tmp == crtc)
8286 break;
8287 crtc_mask <<= 1;
8288 }
8289
8290 if (encoder->possible_crtcs & crtc_mask)
8291 return true;
8292 return false;
8293}
8294
Daniel Vetter9a935852012-07-05 22:34:27 +02008295/**
8296 * intel_modeset_update_staged_output_state
8297 *
8298 * Updates the staged output configuration state, e.g. after we've read out the
8299 * current hw state.
8300 */
8301static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8302{
8303 struct intel_encoder *encoder;
8304 struct intel_connector *connector;
8305
8306 list_for_each_entry(connector, &dev->mode_config.connector_list,
8307 base.head) {
8308 connector->new_encoder =
8309 to_intel_encoder(connector->base.encoder);
8310 }
8311
8312 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8313 base.head) {
8314 encoder->new_crtc =
8315 to_intel_crtc(encoder->base.crtc);
8316 }
8317}
8318
8319/**
8320 * intel_modeset_commit_output_state
8321 *
8322 * This function copies the stage display pipe configuration to the real one.
8323 */
8324static void intel_modeset_commit_output_state(struct drm_device *dev)
8325{
8326 struct intel_encoder *encoder;
8327 struct intel_connector *connector;
8328
8329 list_for_each_entry(connector, &dev->mode_config.connector_list,
8330 base.head) {
8331 connector->base.encoder = &connector->new_encoder->base;
8332 }
8333
8334 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8335 base.head) {
8336 encoder->base.crtc = &encoder->new_crtc->base;
8337 }
8338}
8339
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008340static void
8341connected_sink_compute_bpp(struct intel_connector * connector,
8342 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008343{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008344 int bpp = pipe_config->pipe_bpp;
8345
8346 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8347 connector->base.base.id,
8348 drm_get_connector_name(&connector->base));
8349
8350 /* Don't use an invalid EDID bpc value */
8351 if (connector->base.display_info.bpc &&
8352 connector->base.display_info.bpc * 3 < bpp) {
8353 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8354 bpp, connector->base.display_info.bpc*3);
8355 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8356 }
8357
8358 /* Clamp bpp to 8 on screens without EDID 1.4 */
8359 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8360 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8361 bpp);
8362 pipe_config->pipe_bpp = 24;
8363 }
8364}
8365
8366static int
8367compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8368 struct drm_framebuffer *fb,
8369 struct intel_crtc_config *pipe_config)
8370{
8371 struct drm_device *dev = crtc->base.dev;
8372 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008373 int bpp;
8374
Daniel Vetterd42264b2013-03-28 16:38:08 +01008375 switch (fb->pixel_format) {
8376 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008377 bpp = 8*3; /* since we go through a colormap */
8378 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008379 case DRM_FORMAT_XRGB1555:
8380 case DRM_FORMAT_ARGB1555:
8381 /* checked in intel_framebuffer_init already */
8382 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8383 return -EINVAL;
8384 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008385 bpp = 6*3; /* min is 18bpp */
8386 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008387 case DRM_FORMAT_XBGR8888:
8388 case DRM_FORMAT_ABGR8888:
8389 /* checked in intel_framebuffer_init already */
8390 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8391 return -EINVAL;
8392 case DRM_FORMAT_XRGB8888:
8393 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008394 bpp = 8*3;
8395 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008396 case DRM_FORMAT_XRGB2101010:
8397 case DRM_FORMAT_ARGB2101010:
8398 case DRM_FORMAT_XBGR2101010:
8399 case DRM_FORMAT_ABGR2101010:
8400 /* checked in intel_framebuffer_init already */
8401 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008402 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008403 bpp = 10*3;
8404 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008405 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008406 default:
8407 DRM_DEBUG_KMS("unsupported depth\n");
8408 return -EINVAL;
8409 }
8410
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008411 pipe_config->pipe_bpp = bpp;
8412
8413 /* Clamp display bpp to EDID value */
8414 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008415 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008416 if (!connector->new_encoder ||
8417 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008418 continue;
8419
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008420 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008421 }
8422
8423 return bpp;
8424}
8425
Daniel Vetter644db712013-09-19 14:53:58 +02008426static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8427{
8428 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8429 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008430 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008431 mode->crtc_hdisplay, mode->crtc_hsync_start,
8432 mode->crtc_hsync_end, mode->crtc_htotal,
8433 mode->crtc_vdisplay, mode->crtc_vsync_start,
8434 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8435}
8436
Daniel Vetterc0b03412013-05-28 12:05:54 +02008437static void intel_dump_pipe_config(struct intel_crtc *crtc,
8438 struct intel_crtc_config *pipe_config,
8439 const char *context)
8440{
8441 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8442 context, pipe_name(crtc->pipe));
8443
8444 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8445 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8446 pipe_config->pipe_bpp, pipe_config->dither);
8447 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8448 pipe_config->has_pch_encoder,
8449 pipe_config->fdi_lanes,
8450 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8451 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8452 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008453 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8454 pipe_config->has_dp_encoder,
8455 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8456 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8457 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008458 DRM_DEBUG_KMS("requested mode:\n");
8459 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8460 DRM_DEBUG_KMS("adjusted mode:\n");
8461 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008462 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008463 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008464 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8465 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008466 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8467 pipe_config->gmch_pfit.control,
8468 pipe_config->gmch_pfit.pgm_ratios,
8469 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008470 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008471 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008472 pipe_config->pch_pfit.size,
8473 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008474 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008475 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008476}
8477
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008478static bool check_encoder_cloning(struct drm_crtc *crtc)
8479{
8480 int num_encoders = 0;
8481 bool uncloneable_encoders = false;
8482 struct intel_encoder *encoder;
8483
8484 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8485 base.head) {
8486 if (&encoder->new_crtc->base != crtc)
8487 continue;
8488
8489 num_encoders++;
8490 if (!encoder->cloneable)
8491 uncloneable_encoders = true;
8492 }
8493
8494 return !(num_encoders > 1 && uncloneable_encoders);
8495}
8496
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008497static struct intel_crtc_config *
8498intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008499 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008500 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008501{
8502 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008503 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008504 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008505 int plane_bpp, ret = -EINVAL;
8506 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008507
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008508 if (!check_encoder_cloning(crtc)) {
8509 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8510 return ERR_PTR(-EINVAL);
8511 }
8512
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008513 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8514 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008515 return ERR_PTR(-ENOMEM);
8516
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008517 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8518 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008519
Daniel Vettere143a212013-07-04 12:01:15 +02008520 pipe_config->cpu_transcoder =
8521 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008522 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008523
Imre Deak2960bc92013-07-30 13:36:32 +03008524 /*
8525 * Sanitize sync polarity flags based on requested ones. If neither
8526 * positive or negative polarity is requested, treat this as meaning
8527 * negative polarity.
8528 */
8529 if (!(pipe_config->adjusted_mode.flags &
8530 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8531 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8532
8533 if (!(pipe_config->adjusted_mode.flags &
8534 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8535 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8536
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008537 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8538 * plane pixel format and any sink constraints into account. Returns the
8539 * source plane bpp so that dithering can be selected on mismatches
8540 * after encoders and crtc also have had their say. */
8541 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8542 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008543 if (plane_bpp < 0)
8544 goto fail;
8545
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008546 /*
8547 * Determine the real pipe dimensions. Note that stereo modes can
8548 * increase the actual pipe size due to the frame doubling and
8549 * insertion of additional space for blanks between the frame. This
8550 * is stored in the crtc timings. We use the requested mode to do this
8551 * computation to clearly distinguish it from the adjusted mode, which
8552 * can be changed by the connectors in the below retry loop.
8553 */
8554 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8555 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8556 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8557
Daniel Vettere29c22c2013-02-21 00:00:16 +01008558encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008559 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008560 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008561 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008562
Daniel Vetter135c81b2013-07-21 21:37:09 +02008563 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008564 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008565
Daniel Vetter7758a112012-07-08 19:40:39 +02008566 /* Pass our mode to the connectors and the CRTC to give them a chance to
8567 * adjust it according to limitations or connector properties, and also
8568 * a chance to reject the mode entirely.
8569 */
8570 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8571 base.head) {
8572
8573 if (&encoder->new_crtc->base != crtc)
8574 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008575
Daniel Vetterefea6e82013-07-21 21:36:59 +02008576 if (!(encoder->compute_config(encoder, pipe_config))) {
8577 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008578 goto fail;
8579 }
8580 }
8581
Daniel Vetterff9a6752013-06-01 17:16:21 +02008582 /* Set default port clock if not overwritten by the encoder. Needs to be
8583 * done afterwards in case the encoder adjusts the mode. */
8584 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008585 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8586 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008587
Daniel Vettera43f6e02013-06-07 23:10:32 +02008588 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008589 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008590 DRM_DEBUG_KMS("CRTC fixup failed\n");
8591 goto fail;
8592 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008593
8594 if (ret == RETRY) {
8595 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8596 ret = -EINVAL;
8597 goto fail;
8598 }
8599
8600 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8601 retry = false;
8602 goto encoder_retry;
8603 }
8604
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008605 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8606 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8607 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8608
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008609 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008610fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008611 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008612 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008613}
8614
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008615/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8616 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8617static void
8618intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8619 unsigned *prepare_pipes, unsigned *disable_pipes)
8620{
8621 struct intel_crtc *intel_crtc;
8622 struct drm_device *dev = crtc->dev;
8623 struct intel_encoder *encoder;
8624 struct intel_connector *connector;
8625 struct drm_crtc *tmp_crtc;
8626
8627 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8628
8629 /* Check which crtcs have changed outputs connected to them, these need
8630 * to be part of the prepare_pipes mask. We don't (yet) support global
8631 * modeset across multiple crtcs, so modeset_pipes will only have one
8632 * bit set at most. */
8633 list_for_each_entry(connector, &dev->mode_config.connector_list,
8634 base.head) {
8635 if (connector->base.encoder == &connector->new_encoder->base)
8636 continue;
8637
8638 if (connector->base.encoder) {
8639 tmp_crtc = connector->base.encoder->crtc;
8640
8641 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8642 }
8643
8644 if (connector->new_encoder)
8645 *prepare_pipes |=
8646 1 << connector->new_encoder->new_crtc->pipe;
8647 }
8648
8649 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8650 base.head) {
8651 if (encoder->base.crtc == &encoder->new_crtc->base)
8652 continue;
8653
8654 if (encoder->base.crtc) {
8655 tmp_crtc = encoder->base.crtc;
8656
8657 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8658 }
8659
8660 if (encoder->new_crtc)
8661 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8662 }
8663
8664 /* Check for any pipes that will be fully disabled ... */
8665 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8666 base.head) {
8667 bool used = false;
8668
8669 /* Don't try to disable disabled crtcs. */
8670 if (!intel_crtc->base.enabled)
8671 continue;
8672
8673 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8674 base.head) {
8675 if (encoder->new_crtc == intel_crtc)
8676 used = true;
8677 }
8678
8679 if (!used)
8680 *disable_pipes |= 1 << intel_crtc->pipe;
8681 }
8682
8683
8684 /* set_mode is also used to update properties on life display pipes. */
8685 intel_crtc = to_intel_crtc(crtc);
8686 if (crtc->enabled)
8687 *prepare_pipes |= 1 << intel_crtc->pipe;
8688
Daniel Vetterb6c51642013-04-12 18:48:43 +02008689 /*
8690 * For simplicity do a full modeset on any pipe where the output routing
8691 * changed. We could be more clever, but that would require us to be
8692 * more careful with calling the relevant encoder->mode_set functions.
8693 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008694 if (*prepare_pipes)
8695 *modeset_pipes = *prepare_pipes;
8696
8697 /* ... and mask these out. */
8698 *modeset_pipes &= ~(*disable_pipes);
8699 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008700
8701 /*
8702 * HACK: We don't (yet) fully support global modesets. intel_set_config
8703 * obies this rule, but the modeset restore mode of
8704 * intel_modeset_setup_hw_state does not.
8705 */
8706 *modeset_pipes &= 1 << intel_crtc->pipe;
8707 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008708
8709 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8710 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008711}
8712
Daniel Vetterea9d7582012-07-10 10:42:52 +02008713static bool intel_crtc_in_use(struct drm_crtc *crtc)
8714{
8715 struct drm_encoder *encoder;
8716 struct drm_device *dev = crtc->dev;
8717
8718 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8719 if (encoder->crtc == crtc)
8720 return true;
8721
8722 return false;
8723}
8724
8725static void
8726intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8727{
8728 struct intel_encoder *intel_encoder;
8729 struct intel_crtc *intel_crtc;
8730 struct drm_connector *connector;
8731
8732 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8733 base.head) {
8734 if (!intel_encoder->base.crtc)
8735 continue;
8736
8737 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8738
8739 if (prepare_pipes & (1 << intel_crtc->pipe))
8740 intel_encoder->connectors_active = false;
8741 }
8742
8743 intel_modeset_commit_output_state(dev);
8744
8745 /* Update computed state. */
8746 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8747 base.head) {
8748 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8749 }
8750
8751 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8752 if (!connector->encoder || !connector->encoder->crtc)
8753 continue;
8754
8755 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8756
8757 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008758 struct drm_property *dpms_property =
8759 dev->mode_config.dpms_property;
8760
Daniel Vetterea9d7582012-07-10 10:42:52 +02008761 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008762 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008763 dpms_property,
8764 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008765
8766 intel_encoder = to_intel_encoder(connector->encoder);
8767 intel_encoder->connectors_active = true;
8768 }
8769 }
8770
8771}
8772
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008773static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008774{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03008775 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008776
8777 if (clock1 == clock2)
8778 return true;
8779
8780 if (!clock1 || !clock2)
8781 return false;
8782
8783 diff = abs(clock1 - clock2);
8784
8785 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8786 return true;
8787
8788 return false;
8789}
8790
Daniel Vetter25c5b262012-07-08 22:08:04 +02008791#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8792 list_for_each_entry((intel_crtc), \
8793 &(dev)->mode_config.crtc_list, \
8794 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008795 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008796
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008797static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008798intel_pipe_config_compare(struct drm_device *dev,
8799 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008800 struct intel_crtc_config *pipe_config)
8801{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008802#define PIPE_CONF_CHECK_X(name) \
8803 if (current_config->name != pipe_config->name) { \
8804 DRM_ERROR("mismatch in " #name " " \
8805 "(expected 0x%08x, found 0x%08x)\n", \
8806 current_config->name, \
8807 pipe_config->name); \
8808 return false; \
8809 }
8810
Daniel Vetter08a24032013-04-19 11:25:34 +02008811#define PIPE_CONF_CHECK_I(name) \
8812 if (current_config->name != pipe_config->name) { \
8813 DRM_ERROR("mismatch in " #name " " \
8814 "(expected %i, found %i)\n", \
8815 current_config->name, \
8816 pipe_config->name); \
8817 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008818 }
8819
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008820#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8821 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008822 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008823 "(expected %i, found %i)\n", \
8824 current_config->name & (mask), \
8825 pipe_config->name & (mask)); \
8826 return false; \
8827 }
8828
Ville Syrjälä5e550652013-09-06 23:29:07 +03008829#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
8830 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8831 DRM_ERROR("mismatch in " #name " " \
8832 "(expected %i, found %i)\n", \
8833 current_config->name, \
8834 pipe_config->name); \
8835 return false; \
8836 }
8837
Daniel Vetterbb760062013-06-06 14:55:52 +02008838#define PIPE_CONF_QUIRK(quirk) \
8839 ((current_config->quirks | pipe_config->quirks) & (quirk))
8840
Daniel Vettereccb1402013-05-22 00:50:22 +02008841 PIPE_CONF_CHECK_I(cpu_transcoder);
8842
Daniel Vetter08a24032013-04-19 11:25:34 +02008843 PIPE_CONF_CHECK_I(has_pch_encoder);
8844 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008845 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8846 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8847 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8848 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8849 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008850
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008851 PIPE_CONF_CHECK_I(has_dp_encoder);
8852 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
8853 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
8854 PIPE_CONF_CHECK_I(dp_m_n.link_m);
8855 PIPE_CONF_CHECK_I(dp_m_n.link_n);
8856 PIPE_CONF_CHECK_I(dp_m_n.tu);
8857
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008858 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8859 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8860 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8861 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8862 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8863 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8864
8865 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8866 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8867 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8868 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8869 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8870 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8871
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008872 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008873
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008874 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8875 DRM_MODE_FLAG_INTERLACE);
8876
Daniel Vetterbb760062013-06-06 14:55:52 +02008877 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8878 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8879 DRM_MODE_FLAG_PHSYNC);
8880 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8881 DRM_MODE_FLAG_NHSYNC);
8882 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8883 DRM_MODE_FLAG_PVSYNC);
8884 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8885 DRM_MODE_FLAG_NVSYNC);
8886 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008887
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008888 PIPE_CONF_CHECK_I(pipe_src_w);
8889 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008890
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008891 PIPE_CONF_CHECK_I(gmch_pfit.control);
8892 /* pfit ratios are autocomputed by the hw on gen4+ */
8893 if (INTEL_INFO(dev)->gen < 4)
8894 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8895 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008896 PIPE_CONF_CHECK_I(pch_pfit.enabled);
8897 if (current_config->pch_pfit.enabled) {
8898 PIPE_CONF_CHECK_I(pch_pfit.pos);
8899 PIPE_CONF_CHECK_I(pch_pfit.size);
8900 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008901
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008902 PIPE_CONF_CHECK_I(ips_enabled);
8903
Ville Syrjälä282740f2013-09-04 18:30:03 +03008904 PIPE_CONF_CHECK_I(double_wide);
8905
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008906 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008907 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008908 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008909 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8910 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008911
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008912 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
8913 PIPE_CONF_CHECK_I(pipe_bpp);
8914
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008915 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01008916 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008917 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
8918 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03008919
Daniel Vetter66e985c2013-06-05 13:34:20 +02008920#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008921#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008922#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03008923#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02008924#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008925
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008926 return true;
8927}
8928
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008929static void
8930check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008931{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008932 struct intel_connector *connector;
8933
8934 list_for_each_entry(connector, &dev->mode_config.connector_list,
8935 base.head) {
8936 /* This also checks the encoder/connector hw state with the
8937 * ->get_hw_state callbacks. */
8938 intel_connector_check_state(connector);
8939
8940 WARN(&connector->new_encoder->base != connector->base.encoder,
8941 "connector's staged encoder doesn't match current encoder\n");
8942 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008943}
8944
8945static void
8946check_encoder_state(struct drm_device *dev)
8947{
8948 struct intel_encoder *encoder;
8949 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008950
8951 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8952 base.head) {
8953 bool enabled = false;
8954 bool active = false;
8955 enum pipe pipe, tracked_pipe;
8956
8957 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8958 encoder->base.base.id,
8959 drm_get_encoder_name(&encoder->base));
8960
8961 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8962 "encoder's stage crtc doesn't match current crtc\n");
8963 WARN(encoder->connectors_active && !encoder->base.crtc,
8964 "encoder's active_connectors set, but no crtc\n");
8965
8966 list_for_each_entry(connector, &dev->mode_config.connector_list,
8967 base.head) {
8968 if (connector->base.encoder != &encoder->base)
8969 continue;
8970 enabled = true;
8971 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8972 active = true;
8973 }
8974 WARN(!!encoder->base.crtc != enabled,
8975 "encoder's enabled state mismatch "
8976 "(expected %i, found %i)\n",
8977 !!encoder->base.crtc, enabled);
8978 WARN(active && !encoder->base.crtc,
8979 "active encoder with no crtc\n");
8980
8981 WARN(encoder->connectors_active != active,
8982 "encoder's computed active state doesn't match tracked active state "
8983 "(expected %i, found %i)\n", active, encoder->connectors_active);
8984
8985 active = encoder->get_hw_state(encoder, &pipe);
8986 WARN(active != encoder->connectors_active,
8987 "encoder's hw state doesn't match sw tracking "
8988 "(expected %i, found %i)\n",
8989 encoder->connectors_active, active);
8990
8991 if (!encoder->base.crtc)
8992 continue;
8993
8994 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8995 WARN(active && pipe != tracked_pipe,
8996 "active encoder's pipe doesn't match"
8997 "(expected %i, found %i)\n",
8998 tracked_pipe, pipe);
8999
9000 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009001}
9002
9003static void
9004check_crtc_state(struct drm_device *dev)
9005{
9006 drm_i915_private_t *dev_priv = dev->dev_private;
9007 struct intel_crtc *crtc;
9008 struct intel_encoder *encoder;
9009 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009010
9011 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9012 base.head) {
9013 bool enabled = false;
9014 bool active = false;
9015
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009016 memset(&pipe_config, 0, sizeof(pipe_config));
9017
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009018 DRM_DEBUG_KMS("[CRTC:%d]\n",
9019 crtc->base.base.id);
9020
9021 WARN(crtc->active && !crtc->base.enabled,
9022 "active crtc, but not enabled in sw tracking\n");
9023
9024 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9025 base.head) {
9026 if (encoder->base.crtc != &crtc->base)
9027 continue;
9028 enabled = true;
9029 if (encoder->connectors_active)
9030 active = true;
9031 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009032
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009033 WARN(active != crtc->active,
9034 "crtc's computed active state doesn't match tracked active state "
9035 "(expected %i, found %i)\n", active, crtc->active);
9036 WARN(enabled != crtc->base.enabled,
9037 "crtc's computed enabled state doesn't match tracked enabled state "
9038 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9039
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009040 active = dev_priv->display.get_pipe_config(crtc,
9041 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009042
9043 /* hw state is inconsistent with the pipe A quirk */
9044 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9045 active = crtc->active;
9046
Daniel Vetter6c49f242013-06-06 12:45:25 +02009047 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9048 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009049 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009050 if (encoder->base.crtc != &crtc->base)
9051 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009052 if (encoder->get_config &&
9053 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009054 encoder->get_config(encoder, &pipe_config);
9055 }
9056
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009057 WARN(crtc->active != active,
9058 "crtc active state doesn't match with hw state "
9059 "(expected %i, found %i)\n", crtc->active, active);
9060
Daniel Vetterc0b03412013-05-28 12:05:54 +02009061 if (active &&
9062 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9063 WARN(1, "pipe state doesn't match!\n");
9064 intel_dump_pipe_config(crtc, &pipe_config,
9065 "[hw state]");
9066 intel_dump_pipe_config(crtc, &crtc->config,
9067 "[sw state]");
9068 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009069 }
9070}
9071
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009072static void
9073check_shared_dpll_state(struct drm_device *dev)
9074{
9075 drm_i915_private_t *dev_priv = dev->dev_private;
9076 struct intel_crtc *crtc;
9077 struct intel_dpll_hw_state dpll_hw_state;
9078 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009079
9080 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9081 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9082 int enabled_crtcs = 0, active_crtcs = 0;
9083 bool active;
9084
9085 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9086
9087 DRM_DEBUG_KMS("%s\n", pll->name);
9088
9089 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9090
9091 WARN(pll->active > pll->refcount,
9092 "more active pll users than references: %i vs %i\n",
9093 pll->active, pll->refcount);
9094 WARN(pll->active && !pll->on,
9095 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009096 WARN(pll->on && !pll->active,
9097 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009098 WARN(pll->on != active,
9099 "pll on state mismatch (expected %i, found %i)\n",
9100 pll->on, active);
9101
9102 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9103 base.head) {
9104 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9105 enabled_crtcs++;
9106 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9107 active_crtcs++;
9108 }
9109 WARN(pll->active != active_crtcs,
9110 "pll active crtcs mismatch (expected %i, found %i)\n",
9111 pll->active, active_crtcs);
9112 WARN(pll->refcount != enabled_crtcs,
9113 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9114 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009115
9116 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9117 sizeof(dpll_hw_state)),
9118 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009119 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009120}
9121
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009122void
9123intel_modeset_check_state(struct drm_device *dev)
9124{
9125 check_connector_state(dev);
9126 check_encoder_state(dev);
9127 check_crtc_state(dev);
9128 check_shared_dpll_state(dev);
9129}
9130
Ville Syrjälä18442d02013-09-13 16:00:08 +03009131void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9132 int dotclock)
9133{
9134 /*
9135 * FDI already provided one idea for the dotclock.
9136 * Yell if the encoder disagrees.
9137 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009138 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009139 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009140 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009141}
9142
Daniel Vetterf30da182013-04-11 20:22:50 +02009143static int __intel_set_mode(struct drm_crtc *crtc,
9144 struct drm_display_mode *mode,
9145 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009146{
9147 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009148 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009149 struct drm_display_mode *saved_mode, *saved_hwmode;
9150 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009151 struct intel_crtc *intel_crtc;
9152 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009153 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009154
Daniel Vettera1e22652013-09-21 00:35:38 +02009155 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009156 if (!saved_mode)
9157 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009158 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009159
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009160 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009161 &prepare_pipes, &disable_pipes);
9162
Tim Gardner3ac18232012-12-07 07:54:26 -07009163 *saved_hwmode = crtc->hwmode;
9164 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009165
Daniel Vetter25c5b262012-07-08 22:08:04 +02009166 /* Hack: Because we don't (yet) support global modeset on multiple
9167 * crtcs, we don't keep track of the new mode for more than one crtc.
9168 * Hence simply check whether any bit is set in modeset_pipes in all the
9169 * pieces of code that are not yet converted to deal with mutliple crtcs
9170 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009171 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009172 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009173 if (IS_ERR(pipe_config)) {
9174 ret = PTR_ERR(pipe_config);
9175 pipe_config = NULL;
9176
Tim Gardner3ac18232012-12-07 07:54:26 -07009177 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009178 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009179 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9180 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009181 }
9182
Daniel Vetter460da9162013-03-27 00:44:51 +01009183 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9184 intel_crtc_disable(&intel_crtc->base);
9185
Daniel Vetterea9d7582012-07-10 10:42:52 +02009186 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9187 if (intel_crtc->base.enabled)
9188 dev_priv->display.crtc_disable(&intel_crtc->base);
9189 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009190
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009191 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9192 * to set it here already despite that we pass it down the callchain.
9193 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009194 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009195 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009196 /* mode_set/enable/disable functions rely on a correct pipe
9197 * config. */
9198 to_intel_crtc(crtc)->config = *pipe_config;
9199 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009200
Daniel Vetterea9d7582012-07-10 10:42:52 +02009201 /* Only after disabling all output pipelines that will be changed can we
9202 * update the the output configuration. */
9203 intel_modeset_update_state(dev, prepare_pipes);
9204
Daniel Vetter47fab732012-10-26 10:58:18 +02009205 if (dev_priv->display.modeset_global_resources)
9206 dev_priv->display.modeset_global_resources(dev);
9207
Daniel Vettera6778b32012-07-02 09:56:42 +02009208 /* Set up the DPLL and any encoders state that needs to adjust or depend
9209 * on the DPLL.
9210 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009211 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009212 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009213 x, y, fb);
9214 if (ret)
9215 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009216 }
9217
9218 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009219 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9220 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009221
Daniel Vetter25c5b262012-07-08 22:08:04 +02009222 if (modeset_pipes) {
9223 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009224 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009225
Daniel Vetter25c5b262012-07-08 22:08:04 +02009226 /* Calculate and store various constants which
9227 * are later needed by vblank and swap-completion
9228 * timestamping. They are derived from true hwmode.
9229 */
9230 drm_calc_timestamping_constants(crtc);
9231 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009232
9233 /* FIXME: add subpixel order */
9234done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009235 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009236 crtc->hwmode = *saved_hwmode;
9237 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009238 }
9239
Tim Gardner3ac18232012-12-07 07:54:26 -07009240out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009241 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009242 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009243 return ret;
9244}
9245
Damien Lespiaue7457a92013-08-08 22:28:59 +01009246static int intel_set_mode(struct drm_crtc *crtc,
9247 struct drm_display_mode *mode,
9248 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009249{
9250 int ret;
9251
9252 ret = __intel_set_mode(crtc, mode, x, y, fb);
9253
9254 if (ret == 0)
9255 intel_modeset_check_state(crtc->dev);
9256
9257 return ret;
9258}
9259
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009260void intel_crtc_restore_mode(struct drm_crtc *crtc)
9261{
9262 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9263}
9264
Daniel Vetter25c5b262012-07-08 22:08:04 +02009265#undef for_each_intel_crtc_masked
9266
Daniel Vetterd9e55602012-07-04 22:16:09 +02009267static void intel_set_config_free(struct intel_set_config *config)
9268{
9269 if (!config)
9270 return;
9271
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009272 kfree(config->save_connector_encoders);
9273 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009274 kfree(config);
9275}
9276
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009277static int intel_set_config_save_state(struct drm_device *dev,
9278 struct intel_set_config *config)
9279{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009280 struct drm_encoder *encoder;
9281 struct drm_connector *connector;
9282 int count;
9283
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009284 config->save_encoder_crtcs =
9285 kcalloc(dev->mode_config.num_encoder,
9286 sizeof(struct drm_crtc *), GFP_KERNEL);
9287 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009288 return -ENOMEM;
9289
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009290 config->save_connector_encoders =
9291 kcalloc(dev->mode_config.num_connector,
9292 sizeof(struct drm_encoder *), GFP_KERNEL);
9293 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009294 return -ENOMEM;
9295
9296 /* Copy data. Note that driver private data is not affected.
9297 * Should anything bad happen only the expected state is
9298 * restored, not the drivers personal bookkeeping.
9299 */
9300 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009301 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009302 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009303 }
9304
9305 count = 0;
9306 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009307 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009308 }
9309
9310 return 0;
9311}
9312
9313static void intel_set_config_restore_state(struct drm_device *dev,
9314 struct intel_set_config *config)
9315{
Daniel Vetter9a935852012-07-05 22:34:27 +02009316 struct intel_encoder *encoder;
9317 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009318 int count;
9319
9320 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009321 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9322 encoder->new_crtc =
9323 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009324 }
9325
9326 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009327 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9328 connector->new_encoder =
9329 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009330 }
9331}
9332
Imre Deake3de42b2013-05-03 19:44:07 +02009333static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009334is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009335{
9336 int i;
9337
Chris Wilson2e57f472013-07-17 12:14:40 +01009338 if (set->num_connectors == 0)
9339 return false;
9340
9341 if (WARN_ON(set->connectors == NULL))
9342 return false;
9343
9344 for (i = 0; i < set->num_connectors; i++)
9345 if (set->connectors[i]->encoder &&
9346 set->connectors[i]->encoder->crtc == set->crtc &&
9347 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009348 return true;
9349
9350 return false;
9351}
9352
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009353static void
9354intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9355 struct intel_set_config *config)
9356{
9357
9358 /* We should be able to check here if the fb has the same properties
9359 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009360 if (is_crtc_connector_off(set)) {
9361 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009362 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009363 /* If we have no fb then treat it as a full mode set */
9364 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009365 struct intel_crtc *intel_crtc =
9366 to_intel_crtc(set->crtc);
9367
9368 if (intel_crtc->active && i915_fastboot) {
9369 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9370 config->fb_changed = true;
9371 } else {
9372 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9373 config->mode_changed = true;
9374 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009375 } else if (set->fb == NULL) {
9376 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009377 } else if (set->fb->pixel_format !=
9378 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009379 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009380 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009381 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009382 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009383 }
9384
Daniel Vetter835c5872012-07-10 18:11:08 +02009385 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009386 config->fb_changed = true;
9387
9388 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9389 DRM_DEBUG_KMS("modes are different, full mode set\n");
9390 drm_mode_debug_printmodeline(&set->crtc->mode);
9391 drm_mode_debug_printmodeline(set->mode);
9392 config->mode_changed = true;
9393 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009394
9395 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9396 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009397}
9398
Daniel Vetter2e431052012-07-04 22:42:15 +02009399static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009400intel_modeset_stage_output_state(struct drm_device *dev,
9401 struct drm_mode_set *set,
9402 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009403{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009404 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009405 struct intel_connector *connector;
9406 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009407 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009408
Damien Lespiau9abdda72013-02-13 13:29:23 +00009409 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009410 * of connectors. For paranoia, double-check this. */
9411 WARN_ON(!set->fb && (set->num_connectors != 0));
9412 WARN_ON(set->fb && (set->num_connectors == 0));
9413
Daniel Vetter9a935852012-07-05 22:34:27 +02009414 list_for_each_entry(connector, &dev->mode_config.connector_list,
9415 base.head) {
9416 /* Otherwise traverse passed in connector list and get encoders
9417 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009418 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009419 if (set->connectors[ro] == &connector->base) {
9420 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009421 break;
9422 }
9423 }
9424
Daniel Vetter9a935852012-07-05 22:34:27 +02009425 /* If we disable the crtc, disable all its connectors. Also, if
9426 * the connector is on the changing crtc but not on the new
9427 * connector list, disable it. */
9428 if ((!set->fb || ro == set->num_connectors) &&
9429 connector->base.encoder &&
9430 connector->base.encoder->crtc == set->crtc) {
9431 connector->new_encoder = NULL;
9432
9433 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9434 connector->base.base.id,
9435 drm_get_connector_name(&connector->base));
9436 }
9437
9438
9439 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009440 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009441 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009442 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009443 }
9444 /* connector->new_encoder is now updated for all connectors. */
9445
9446 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009447 list_for_each_entry(connector, &dev->mode_config.connector_list,
9448 base.head) {
9449 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009450 continue;
9451
Daniel Vetter9a935852012-07-05 22:34:27 +02009452 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009453
9454 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009455 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009456 new_crtc = set->crtc;
9457 }
9458
9459 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009460 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9461 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009462 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009463 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009464 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9465
9466 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9467 connector->base.base.id,
9468 drm_get_connector_name(&connector->base),
9469 new_crtc->base.id);
9470 }
9471
9472 /* Check for any encoders that needs to be disabled. */
9473 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9474 base.head) {
9475 list_for_each_entry(connector,
9476 &dev->mode_config.connector_list,
9477 base.head) {
9478 if (connector->new_encoder == encoder) {
9479 WARN_ON(!connector->new_encoder->new_crtc);
9480
9481 goto next_encoder;
9482 }
9483 }
9484 encoder->new_crtc = NULL;
9485next_encoder:
9486 /* Only now check for crtc changes so we don't miss encoders
9487 * that will be disabled. */
9488 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009489 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009490 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009491 }
9492 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009493 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009494
Daniel Vetter2e431052012-07-04 22:42:15 +02009495 return 0;
9496}
9497
9498static int intel_crtc_set_config(struct drm_mode_set *set)
9499{
9500 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009501 struct drm_mode_set save_set;
9502 struct intel_set_config *config;
9503 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009504
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009505 BUG_ON(!set);
9506 BUG_ON(!set->crtc);
9507 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009508
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009509 /* Enforce sane interface api - has been abused by the fb helper. */
9510 BUG_ON(!set->mode && set->fb);
9511 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009512
Daniel Vetter2e431052012-07-04 22:42:15 +02009513 if (set->fb) {
9514 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9515 set->crtc->base.id, set->fb->base.id,
9516 (int)set->num_connectors, set->x, set->y);
9517 } else {
9518 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009519 }
9520
9521 dev = set->crtc->dev;
9522
9523 ret = -ENOMEM;
9524 config = kzalloc(sizeof(*config), GFP_KERNEL);
9525 if (!config)
9526 goto out_config;
9527
9528 ret = intel_set_config_save_state(dev, config);
9529 if (ret)
9530 goto out_config;
9531
9532 save_set.crtc = set->crtc;
9533 save_set.mode = &set->crtc->mode;
9534 save_set.x = set->crtc->x;
9535 save_set.y = set->crtc->y;
9536 save_set.fb = set->crtc->fb;
9537
9538 /* Compute whether we need a full modeset, only an fb base update or no
9539 * change at all. In the future we might also check whether only the
9540 * mode changed, e.g. for LVDS where we only change the panel fitter in
9541 * such cases. */
9542 intel_set_config_compute_mode_changes(set, config);
9543
Daniel Vetter9a935852012-07-05 22:34:27 +02009544 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009545 if (ret)
9546 goto fail;
9547
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009548 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009549 ret = intel_set_mode(set->crtc, set->mode,
9550 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009551 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009552 intel_crtc_wait_for_pending_flips(set->crtc);
9553
Daniel Vetter4f660f42012-07-02 09:47:37 +02009554 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009555 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009556 }
9557
Chris Wilson2d05eae2013-05-03 17:36:25 +01009558 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009559 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9560 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009561fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009562 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009563
Chris Wilson2d05eae2013-05-03 17:36:25 +01009564 /* Try to restore the config */
9565 if (config->mode_changed &&
9566 intel_set_mode(save_set.crtc, save_set.mode,
9567 save_set.x, save_set.y, save_set.fb))
9568 DRM_ERROR("failed to restore config after modeset failure\n");
9569 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009570
Daniel Vetterd9e55602012-07-04 22:16:09 +02009571out_config:
9572 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009573 return ret;
9574}
9575
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009576static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009577 .cursor_set = intel_crtc_cursor_set,
9578 .cursor_move = intel_crtc_cursor_move,
9579 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009580 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009581 .destroy = intel_crtc_destroy,
9582 .page_flip = intel_crtc_page_flip,
9583};
9584
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009585static void intel_cpu_pll_init(struct drm_device *dev)
9586{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009587 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009588 intel_ddi_pll_init(dev);
9589}
9590
Daniel Vetter53589012013-06-05 13:34:16 +02009591static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9592 struct intel_shared_dpll *pll,
9593 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009594{
Daniel Vetter53589012013-06-05 13:34:16 +02009595 uint32_t val;
9596
9597 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009598 hw_state->dpll = val;
9599 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9600 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02009601
9602 return val & DPLL_VCO_ENABLE;
9603}
9604
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009605static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9606 struct intel_shared_dpll *pll)
9607{
9608 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9609 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9610}
9611
Daniel Vettere7b903d2013-06-05 13:34:14 +02009612static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9613 struct intel_shared_dpll *pll)
9614{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009615 /* PCH refclock must be enabled first */
9616 assert_pch_refclk_enabled(dev_priv);
9617
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009618 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9619
9620 /* Wait for the clocks to stabilize. */
9621 POSTING_READ(PCH_DPLL(pll->id));
9622 udelay(150);
9623
9624 /* The pixel multiplier can only be updated once the
9625 * DPLL is enabled and the clocks are stable.
9626 *
9627 * So write it again.
9628 */
9629 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9630 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009631 udelay(200);
9632}
9633
9634static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9635 struct intel_shared_dpll *pll)
9636{
9637 struct drm_device *dev = dev_priv->dev;
9638 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009639
9640 /* Make sure no transcoder isn't still depending on us. */
9641 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9642 if (intel_crtc_to_shared_dpll(crtc) == pll)
9643 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9644 }
9645
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009646 I915_WRITE(PCH_DPLL(pll->id), 0);
9647 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009648 udelay(200);
9649}
9650
Daniel Vetter46edb022013-06-05 13:34:12 +02009651static char *ibx_pch_dpll_names[] = {
9652 "PCH DPLL A",
9653 "PCH DPLL B",
9654};
9655
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009656static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009657{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009658 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009659 int i;
9660
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009661 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009662
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009663 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02009664 dev_priv->shared_dplls[i].id = i;
9665 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009666 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009667 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9668 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009669 dev_priv->shared_dplls[i].get_hw_state =
9670 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009671 }
9672}
9673
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009674static void intel_shared_dpll_init(struct drm_device *dev)
9675{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009676 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009677
9678 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9679 ibx_pch_dpll_init(dev);
9680 else
9681 dev_priv->num_shared_dpll = 0;
9682
9683 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9684 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9685 dev_priv->num_shared_dpll);
9686}
9687
Hannes Ederb358d0a2008-12-18 21:18:47 +01009688static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009689{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009690 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009691 struct intel_crtc *intel_crtc;
9692 int i;
9693
Daniel Vetter955382f2013-09-19 14:05:45 +02009694 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009695 if (intel_crtc == NULL)
9696 return;
9697
9698 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9699
9700 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009701 for (i = 0; i < 256; i++) {
9702 intel_crtc->lut_r[i] = i;
9703 intel_crtc->lut_g[i] = i;
9704 intel_crtc->lut_b[i] = i;
9705 }
9706
Jesse Barnes80824002009-09-10 15:28:06 -07009707 /* Swap pipes & planes for FBC on pre-965 */
9708 intel_crtc->pipe = pipe;
9709 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009710 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009711 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009712 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009713 }
9714
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009715 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9716 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9717 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9718 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9719
Jesse Barnes79e53942008-11-07 14:24:08 -08009720 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009721}
9722
Carl Worth08d7b3d2009-04-29 14:43:54 -07009723int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009724 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009725{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009726 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009727 struct drm_mode_object *drmmode_obj;
9728 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009729
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009730 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9731 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009732
Daniel Vetterc05422d2009-08-11 16:05:30 +02009733 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9734 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009735
Daniel Vetterc05422d2009-08-11 16:05:30 +02009736 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009737 DRM_ERROR("no such CRTC id\n");
9738 return -EINVAL;
9739 }
9740
Daniel Vetterc05422d2009-08-11 16:05:30 +02009741 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9742 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009743
Daniel Vetterc05422d2009-08-11 16:05:30 +02009744 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009745}
9746
Daniel Vetter66a92782012-07-12 20:08:18 +02009747static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009748{
Daniel Vetter66a92782012-07-12 20:08:18 +02009749 struct drm_device *dev = encoder->base.dev;
9750 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009751 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009752 int entry = 0;
9753
Daniel Vetter66a92782012-07-12 20:08:18 +02009754 list_for_each_entry(source_encoder,
9755 &dev->mode_config.encoder_list, base.head) {
9756
9757 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009758 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009759
9760 /* Intel hw has only one MUX where enocoders could be cloned. */
9761 if (encoder->cloneable && source_encoder->cloneable)
9762 index_mask |= (1 << entry);
9763
Jesse Barnes79e53942008-11-07 14:24:08 -08009764 entry++;
9765 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009766
Jesse Barnes79e53942008-11-07 14:24:08 -08009767 return index_mask;
9768}
9769
Chris Wilson4d302442010-12-14 19:21:29 +00009770static bool has_edp_a(struct drm_device *dev)
9771{
9772 struct drm_i915_private *dev_priv = dev->dev_private;
9773
9774 if (!IS_MOBILE(dev))
9775 return false;
9776
9777 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9778 return false;
9779
9780 if (IS_GEN5(dev) &&
9781 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9782 return false;
9783
9784 return true;
9785}
9786
Jesse Barnes79e53942008-11-07 14:24:08 -08009787static void intel_setup_outputs(struct drm_device *dev)
9788{
Eric Anholt725e30a2009-01-22 13:01:02 -08009789 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009790 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009791 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009792
Daniel Vetterc9093352013-06-06 22:22:47 +02009793 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009794
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009795 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009796 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009797
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009798 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009799 int found;
9800
9801 /* Haswell uses DDI functions to detect digital outputs */
9802 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9803 /* DDI A only supports eDP */
9804 if (found)
9805 intel_ddi_init(dev, PORT_A);
9806
9807 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9808 * register */
9809 found = I915_READ(SFUSE_STRAP);
9810
9811 if (found & SFUSE_STRAP_DDIB_DETECTED)
9812 intel_ddi_init(dev, PORT_B);
9813 if (found & SFUSE_STRAP_DDIC_DETECTED)
9814 intel_ddi_init(dev, PORT_C);
9815 if (found & SFUSE_STRAP_DDID_DETECTED)
9816 intel_ddi_init(dev, PORT_D);
9817 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009818 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009819 dpd_is_edp = intel_dpd_is_edp(dev);
9820
9821 if (has_edp_a(dev))
9822 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009823
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009824 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009825 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009826 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009827 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009828 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009829 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009830 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009831 }
9832
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009833 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009834 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009835
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009836 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009837 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009838
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009839 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009840 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009841
Daniel Vetter270b3042012-10-27 15:52:05 +02009842 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009843 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009844 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309845 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Jesse Barnes6f6005a2013-08-09 09:34:35 -07009846 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
9847 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
9848 PORT_C);
9849 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9850 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
9851 PORT_C);
9852 }
Gajanan Bhat19c03922012-09-27 19:13:07 +05309853
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009854 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009855 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9856 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009857 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9858 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009859 }
Jani Nikula3cfca972013-08-27 15:12:26 +03009860
9861 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +08009862 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009863 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009864
Paulo Zanonie2debe92013-02-18 19:00:27 -03009865 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009866 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009867 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009868 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9869 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009870 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009871 }
Ma Ling27185ae2009-08-24 13:50:23 +08009872
Imre Deake7281ea2013-05-08 13:14:08 +03009873 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009874 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009875 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009876
9877 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009878
Paulo Zanonie2debe92013-02-18 19:00:27 -03009879 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009880 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009881 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009882 }
Ma Ling27185ae2009-08-24 13:50:23 +08009883
Paulo Zanonie2debe92013-02-18 19:00:27 -03009884 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009885
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009886 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9887 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009888 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009889 }
Imre Deake7281ea2013-05-08 13:14:08 +03009890 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009891 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009892 }
Ma Ling27185ae2009-08-24 13:50:23 +08009893
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009894 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009895 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009896 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009897 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009898 intel_dvo_init(dev);
9899
Zhenyu Wang103a1962009-11-27 11:44:36 +08009900 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009901 intel_tv_init(dev);
9902
Chris Wilson4ef69c72010-09-09 15:14:28 +01009903 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9904 encoder->base.possible_crtcs = encoder->crtc_mask;
9905 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009906 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009907 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009908
Paulo Zanonidde86e22012-12-01 12:04:25 -02009909 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009910
9911 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009912}
9913
Chris Wilsonddfe1562013-08-06 17:43:07 +01009914void intel_framebuffer_fini(struct intel_framebuffer *fb)
9915{
9916 drm_framebuffer_cleanup(&fb->base);
9917 drm_gem_object_unreference_unlocked(&fb->obj->base);
9918}
9919
Jesse Barnes79e53942008-11-07 14:24:08 -08009920static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9921{
9922 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009923
Chris Wilsonddfe1562013-08-06 17:43:07 +01009924 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009925 kfree(intel_fb);
9926}
9927
9928static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009929 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009930 unsigned int *handle)
9931{
9932 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009933 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009934
Chris Wilson05394f32010-11-08 19:18:58 +00009935 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009936}
9937
9938static const struct drm_framebuffer_funcs intel_fb_funcs = {
9939 .destroy = intel_user_framebuffer_destroy,
9940 .create_handle = intel_user_framebuffer_create_handle,
9941};
9942
Dave Airlie38651672010-03-30 05:34:13 +00009943int intel_framebuffer_init(struct drm_device *dev,
9944 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009945 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009946 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009947{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009948 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009949 int ret;
9950
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009951 if (obj->tiling_mode == I915_TILING_Y) {
9952 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009953 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009954 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009955
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009956 if (mode_cmd->pitches[0] & 63) {
9957 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9958 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009959 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009960 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009961
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009962 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9963 pitch_limit = 32*1024;
9964 } else if (INTEL_INFO(dev)->gen >= 4) {
9965 if (obj->tiling_mode)
9966 pitch_limit = 16*1024;
9967 else
9968 pitch_limit = 32*1024;
9969 } else if (INTEL_INFO(dev)->gen >= 3) {
9970 if (obj->tiling_mode)
9971 pitch_limit = 8*1024;
9972 else
9973 pitch_limit = 16*1024;
9974 } else
9975 /* XXX DSPC is limited to 4k tiled */
9976 pitch_limit = 8*1024;
9977
9978 if (mode_cmd->pitches[0] > pitch_limit) {
9979 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9980 obj->tiling_mode ? "tiled" : "linear",
9981 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009982 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009983 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009984
9985 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009986 mode_cmd->pitches[0] != obj->stride) {
9987 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9988 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009989 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009990 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009991
Ville Syrjälä57779d02012-10-31 17:50:14 +02009992 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009993 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009994 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009995 case DRM_FORMAT_RGB565:
9996 case DRM_FORMAT_XRGB8888:
9997 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009998 break;
9999 case DRM_FORMAT_XRGB1555:
10000 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010001 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010002 DRM_DEBUG("unsupported pixel format: %s\n",
10003 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010004 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010005 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010006 break;
10007 case DRM_FORMAT_XBGR8888:
10008 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010009 case DRM_FORMAT_XRGB2101010:
10010 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010011 case DRM_FORMAT_XBGR2101010:
10012 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010013 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010014 DRM_DEBUG("unsupported pixel format: %s\n",
10015 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010016 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010017 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010018 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010019 case DRM_FORMAT_YUYV:
10020 case DRM_FORMAT_UYVY:
10021 case DRM_FORMAT_YVYU:
10022 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010023 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010024 DRM_DEBUG("unsupported pixel format: %s\n",
10025 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010026 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010027 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010028 break;
10029 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010030 DRM_DEBUG("unsupported pixel format: %s\n",
10031 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010032 return -EINVAL;
10033 }
10034
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010035 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10036 if (mode_cmd->offsets[0] != 0)
10037 return -EINVAL;
10038
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010039 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10040 intel_fb->obj = obj;
10041
Jesse Barnes79e53942008-11-07 14:24:08 -080010042 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10043 if (ret) {
10044 DRM_ERROR("framebuffer init failed %d\n", ret);
10045 return ret;
10046 }
10047
Jesse Barnes79e53942008-11-07 14:24:08 -080010048 return 0;
10049}
10050
Jesse Barnes79e53942008-11-07 14:24:08 -080010051static struct drm_framebuffer *
10052intel_user_framebuffer_create(struct drm_device *dev,
10053 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010054 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010055{
Chris Wilson05394f32010-11-08 19:18:58 +000010056 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010057
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010058 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10059 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010060 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010061 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010062
Chris Wilsond2dff872011-04-19 08:36:26 +010010063 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010064}
10065
Jesse Barnes79e53942008-11-07 14:24:08 -080010066static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010067 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +000010068 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010069};
10070
Jesse Barnese70236a2009-09-21 10:42:27 -070010071/* Set up chip specific display functions */
10072static void intel_init_display(struct drm_device *dev)
10073{
10074 struct drm_i915_private *dev_priv = dev->dev_private;
10075
Daniel Vetteree9300b2013-06-03 22:40:22 +020010076 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10077 dev_priv->display.find_dpll = g4x_find_best_dpll;
10078 else if (IS_VALLEYVIEW(dev))
10079 dev_priv->display.find_dpll = vlv_find_best_dpll;
10080 else if (IS_PINEVIEW(dev))
10081 dev_priv->display.find_dpll = pnv_find_best_dpll;
10082 else
10083 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10084
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010085 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010086 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010087 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010088 dev_priv->display.crtc_enable = haswell_crtc_enable;
10089 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010090 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010091 dev_priv->display.update_plane = ironlake_update_plane;
10092 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010093 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010094 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010095 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10096 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010097 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010098 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010099 } else if (IS_VALLEYVIEW(dev)) {
10100 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10101 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10102 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10103 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10104 dev_priv->display.off = i9xx_crtc_off;
10105 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010106 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010107 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010108 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010109 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10110 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010111 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010112 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010113 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010114
Jesse Barnese70236a2009-09-21 10:42:27 -070010115 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010116 if (IS_VALLEYVIEW(dev))
10117 dev_priv->display.get_display_clock_speed =
10118 valleyview_get_display_clock_speed;
10119 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010120 dev_priv->display.get_display_clock_speed =
10121 i945_get_display_clock_speed;
10122 else if (IS_I915G(dev))
10123 dev_priv->display.get_display_clock_speed =
10124 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010125 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010126 dev_priv->display.get_display_clock_speed =
10127 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010128 else if (IS_PINEVIEW(dev))
10129 dev_priv->display.get_display_clock_speed =
10130 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010131 else if (IS_I915GM(dev))
10132 dev_priv->display.get_display_clock_speed =
10133 i915gm_get_display_clock_speed;
10134 else if (IS_I865G(dev))
10135 dev_priv->display.get_display_clock_speed =
10136 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010137 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010138 dev_priv->display.get_display_clock_speed =
10139 i855_get_display_clock_speed;
10140 else /* 852, 830 */
10141 dev_priv->display.get_display_clock_speed =
10142 i830_get_display_clock_speed;
10143
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010144 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010145 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010146 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010147 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010148 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010149 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010150 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010151 } else if (IS_IVYBRIDGE(dev)) {
10152 /* FIXME: detect B0+ stepping and use auto training */
10153 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010154 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010155 dev_priv->display.modeset_global_resources =
10156 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010157 } else if (IS_HASWELL(dev)) {
10158 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010159 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010160 dev_priv->display.modeset_global_resources =
10161 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010162 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010163 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010164 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010165 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010166
10167 /* Default just returns -ENODEV to indicate unsupported */
10168 dev_priv->display.queue_flip = intel_default_queue_flip;
10169
10170 switch (INTEL_INFO(dev)->gen) {
10171 case 2:
10172 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10173 break;
10174
10175 case 3:
10176 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10177 break;
10178
10179 case 4:
10180 case 5:
10181 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10182 break;
10183
10184 case 6:
10185 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10186 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010187 case 7:
10188 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10189 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010190 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010191}
10192
Jesse Barnesb690e962010-07-19 13:53:12 -070010193/*
10194 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10195 * resume, or other times. This quirk makes sure that's the case for
10196 * affected systems.
10197 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010198static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010199{
10200 struct drm_i915_private *dev_priv = dev->dev_private;
10201
10202 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010203 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010204}
10205
Keith Packard435793d2011-07-12 14:56:22 -070010206/*
10207 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10208 */
10209static void quirk_ssc_force_disable(struct drm_device *dev)
10210{
10211 struct drm_i915_private *dev_priv = dev->dev_private;
10212 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010213 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010214}
10215
Carsten Emde4dca20e2012-03-15 15:56:26 +010010216/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010217 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10218 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010219 */
10220static void quirk_invert_brightness(struct drm_device *dev)
10221{
10222 struct drm_i915_private *dev_priv = dev->dev_private;
10223 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010224 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010225}
10226
Kamal Mostafae85843b2013-07-19 15:02:01 -070010227/*
10228 * Some machines (Dell XPS13) suffer broken backlight controls if
10229 * BLM_PCH_PWM_ENABLE is set.
10230 */
10231static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
10232{
10233 struct drm_i915_private *dev_priv = dev->dev_private;
10234 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
10235 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
10236}
10237
Jesse Barnesb690e962010-07-19 13:53:12 -070010238struct intel_quirk {
10239 int device;
10240 int subsystem_vendor;
10241 int subsystem_device;
10242 void (*hook)(struct drm_device *dev);
10243};
10244
Egbert Eich5f85f1762012-10-14 15:46:38 +020010245/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10246struct intel_dmi_quirk {
10247 void (*hook)(struct drm_device *dev);
10248 const struct dmi_system_id (*dmi_id_list)[];
10249};
10250
10251static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10252{
10253 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10254 return 1;
10255}
10256
10257static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10258 {
10259 .dmi_id_list = &(const struct dmi_system_id[]) {
10260 {
10261 .callback = intel_dmi_reverse_brightness,
10262 .ident = "NCR Corporation",
10263 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10264 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10265 },
10266 },
10267 { } /* terminating entry */
10268 },
10269 .hook = quirk_invert_brightness,
10270 },
10271};
10272
Ben Widawskyc43b5632012-04-16 14:07:40 -070010273static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010274 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010275 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010276
Jesse Barnesb690e962010-07-19 13:53:12 -070010277 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10278 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10279
Jesse Barnesb690e962010-07-19 13:53:12 -070010280 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10281 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10282
Daniel Vetterccd0d362012-10-10 23:13:59 +020010283 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -070010284 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010285 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010286
10287 /* Lenovo U160 cannot use SSC on LVDS */
10288 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010289
10290 /* Sony Vaio Y cannot use SSC on LVDS */
10291 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010292
Jani Nikulaee1452d2013-09-20 15:05:30 +030010293 /*
10294 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10295 * seem to use inverted backlight PWM.
10296 */
10297 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Kamal Mostafae85843b2013-07-19 15:02:01 -070010298
10299 /* Dell XPS13 HD Sandy Bridge */
10300 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
10301 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
10302 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
Jesse Barnesb690e962010-07-19 13:53:12 -070010303};
10304
10305static void intel_init_quirks(struct drm_device *dev)
10306{
10307 struct pci_dev *d = dev->pdev;
10308 int i;
10309
10310 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10311 struct intel_quirk *q = &intel_quirks[i];
10312
10313 if (d->device == q->device &&
10314 (d->subsystem_vendor == q->subsystem_vendor ||
10315 q->subsystem_vendor == PCI_ANY_ID) &&
10316 (d->subsystem_device == q->subsystem_device ||
10317 q->subsystem_device == PCI_ANY_ID))
10318 q->hook(dev);
10319 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010320 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10321 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10322 intel_dmi_quirks[i].hook(dev);
10323 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010324}
10325
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010326/* Disable the VGA plane that we never use */
10327static void i915_disable_vga(struct drm_device *dev)
10328{
10329 struct drm_i915_private *dev_priv = dev->dev_private;
10330 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010331 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010332
10333 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010334 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010335 sr1 = inb(VGA_SR_DATA);
10336 outb(sr1 | 1<<5, VGA_SR_DATA);
10337 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10338 udelay(300);
10339
10340 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10341 POSTING_READ(vga_reg);
10342}
10343
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010344static void i915_enable_vga_mem(struct drm_device *dev)
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010345{
10346 /* Enable VGA memory on Intel HD */
10347 if (HAS_PCH_SPLIT(dev)) {
10348 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10349 outb(inb(VGA_MSR_READ) | VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10350 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10351 VGA_RSRC_LEGACY_MEM |
10352 VGA_RSRC_NORMAL_IO |
10353 VGA_RSRC_NORMAL_MEM);
10354 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10355 }
10356}
10357
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010358void i915_disable_vga_mem(struct drm_device *dev)
10359{
10360 /* Disable VGA memory on Intel HD */
10361 if (HAS_PCH_SPLIT(dev)) {
10362 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10363 outb(inb(VGA_MSR_READ) & ~VGA_MSR_MEM_EN, VGA_MSR_WRITE);
10364 vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
10365 VGA_RSRC_NORMAL_IO |
10366 VGA_RSRC_NORMAL_MEM);
10367 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10368 }
10369}
10370
Daniel Vetterf8175862012-04-10 15:50:11 +020010371void intel_modeset_init_hw(struct drm_device *dev)
10372{
Jesse Barnesf6071162013-10-01 10:41:38 -070010373 struct drm_i915_private *dev_priv = dev->dev_private;
10374
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010375 intel_prepare_ddi(dev);
10376
Daniel Vetterf8175862012-04-10 15:50:11 +020010377 intel_init_clock_gating(dev);
10378
Jesse Barnesf6071162013-10-01 10:41:38 -070010379 /* Enable the CRI clock source so we can get at the display */
10380 if (IS_VALLEYVIEW(dev))
10381 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10382 DPLL_INTEGRATED_CRI_CLK_VLV);
10383
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010384 intel_init_dpio(dev);
10385
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010386 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010387 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010388 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010389}
10390
Imre Deak7d708ee2013-04-17 14:04:50 +030010391void intel_modeset_suspend_hw(struct drm_device *dev)
10392{
10393 intel_suspend_hw(dev);
10394}
10395
Jesse Barnes79e53942008-11-07 14:24:08 -080010396void intel_modeset_init(struct drm_device *dev)
10397{
Jesse Barnes652c3932009-08-17 13:31:43 -070010398 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010399 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010400
10401 drm_mode_config_init(dev);
10402
10403 dev->mode_config.min_width = 0;
10404 dev->mode_config.min_height = 0;
10405
Dave Airlie019d96c2011-09-29 16:20:42 +010010406 dev->mode_config.preferred_depth = 24;
10407 dev->mode_config.prefer_shadow = 1;
10408
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010409 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010410
Jesse Barnesb690e962010-07-19 13:53:12 -070010411 intel_init_quirks(dev);
10412
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010413 intel_init_pm(dev);
10414
Ben Widawskye3c74752013-04-05 13:12:39 -070010415 if (INTEL_INFO(dev)->num_pipes == 0)
10416 return;
10417
Jesse Barnese70236a2009-09-21 10:42:27 -070010418 intel_init_display(dev);
10419
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010420 if (IS_GEN2(dev)) {
10421 dev->mode_config.max_width = 2048;
10422 dev->mode_config.max_height = 2048;
10423 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010424 dev->mode_config.max_width = 4096;
10425 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010426 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010427 dev->mode_config.max_width = 8192;
10428 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010429 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010430 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010431
Zhao Yakui28c97732009-10-09 11:39:41 +080010432 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010433 INTEL_INFO(dev)->num_pipes,
10434 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010435
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010436 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010437 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010438 for (j = 0; j < dev_priv->num_plane; j++) {
10439 ret = intel_plane_init(dev, i, j);
10440 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010441 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10442 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010443 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010444 }
10445
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010446 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010447 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010448
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010449 /* Just disable it once at startup */
10450 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010451 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010452
10453 /* Just in case the BIOS is doing something questionable. */
10454 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010455}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010456
Daniel Vetter24929352012-07-02 20:28:59 +020010457static void
10458intel_connector_break_all_links(struct intel_connector *connector)
10459{
10460 connector->base.dpms = DRM_MODE_DPMS_OFF;
10461 connector->base.encoder = NULL;
10462 connector->encoder->connectors_active = false;
10463 connector->encoder->base.crtc = NULL;
10464}
10465
Daniel Vetter7fad7982012-07-04 17:51:47 +020010466static void intel_enable_pipe_a(struct drm_device *dev)
10467{
10468 struct intel_connector *connector;
10469 struct drm_connector *crt = NULL;
10470 struct intel_load_detect_pipe load_detect_temp;
10471
10472 /* We can't just switch on the pipe A, we need to set things up with a
10473 * proper mode and output configuration. As a gross hack, enable pipe A
10474 * by enabling the load detect pipe once. */
10475 list_for_each_entry(connector,
10476 &dev->mode_config.connector_list,
10477 base.head) {
10478 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10479 crt = &connector->base;
10480 break;
10481 }
10482 }
10483
10484 if (!crt)
10485 return;
10486
10487 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10488 intel_release_load_detect_pipe(crt, &load_detect_temp);
10489
10490
10491}
10492
Daniel Vetterfa555832012-10-10 23:14:00 +020010493static bool
10494intel_check_plane_mapping(struct intel_crtc *crtc)
10495{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010496 struct drm_device *dev = crtc->base.dev;
10497 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010498 u32 reg, val;
10499
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010500 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010501 return true;
10502
10503 reg = DSPCNTR(!crtc->plane);
10504 val = I915_READ(reg);
10505
10506 if ((val & DISPLAY_PLANE_ENABLE) &&
10507 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10508 return false;
10509
10510 return true;
10511}
10512
Daniel Vetter24929352012-07-02 20:28:59 +020010513static void intel_sanitize_crtc(struct intel_crtc *crtc)
10514{
10515 struct drm_device *dev = crtc->base.dev;
10516 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010517 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010518
Daniel Vetter24929352012-07-02 20:28:59 +020010519 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010520 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010521 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10522
10523 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010524 * disable the crtc (and hence change the state) if it is wrong. Note
10525 * that gen4+ has a fixed plane -> pipe mapping. */
10526 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010527 struct intel_connector *connector;
10528 bool plane;
10529
Daniel Vetter24929352012-07-02 20:28:59 +020010530 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10531 crtc->base.base.id);
10532
10533 /* Pipe has the wrong plane attached and the plane is active.
10534 * Temporarily change the plane mapping and disable everything
10535 * ... */
10536 plane = crtc->plane;
10537 crtc->plane = !plane;
10538 dev_priv->display.crtc_disable(&crtc->base);
10539 crtc->plane = plane;
10540
10541 /* ... and break all links. */
10542 list_for_each_entry(connector, &dev->mode_config.connector_list,
10543 base.head) {
10544 if (connector->encoder->base.crtc != &crtc->base)
10545 continue;
10546
10547 intel_connector_break_all_links(connector);
10548 }
10549
10550 WARN_ON(crtc->active);
10551 crtc->base.enabled = false;
10552 }
Daniel Vetter24929352012-07-02 20:28:59 +020010553
Daniel Vetter7fad7982012-07-04 17:51:47 +020010554 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10555 crtc->pipe == PIPE_A && !crtc->active) {
10556 /* BIOS forgot to enable pipe A, this mostly happens after
10557 * resume. Force-enable the pipe to fix this, the update_dpms
10558 * call below we restore the pipe to the right state, but leave
10559 * the required bits on. */
10560 intel_enable_pipe_a(dev);
10561 }
10562
Daniel Vetter24929352012-07-02 20:28:59 +020010563 /* Adjust the state of the output pipe according to whether we
10564 * have active connectors/encoders. */
10565 intel_crtc_update_dpms(&crtc->base);
10566
10567 if (crtc->active != crtc->base.enabled) {
10568 struct intel_encoder *encoder;
10569
10570 /* This can happen either due to bugs in the get_hw_state
10571 * functions or because the pipe is force-enabled due to the
10572 * pipe A quirk. */
10573 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10574 crtc->base.base.id,
10575 crtc->base.enabled ? "enabled" : "disabled",
10576 crtc->active ? "enabled" : "disabled");
10577
10578 crtc->base.enabled = crtc->active;
10579
10580 /* Because we only establish the connector -> encoder ->
10581 * crtc links if something is active, this means the
10582 * crtc is now deactivated. Break the links. connector
10583 * -> encoder links are only establish when things are
10584 * actually up, hence no need to break them. */
10585 WARN_ON(crtc->active);
10586
10587 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10588 WARN_ON(encoder->connectors_active);
10589 encoder->base.crtc = NULL;
10590 }
10591 }
10592}
10593
10594static void intel_sanitize_encoder(struct intel_encoder *encoder)
10595{
10596 struct intel_connector *connector;
10597 struct drm_device *dev = encoder->base.dev;
10598
10599 /* We need to check both for a crtc link (meaning that the
10600 * encoder is active and trying to read from a pipe) and the
10601 * pipe itself being active. */
10602 bool has_active_crtc = encoder->base.crtc &&
10603 to_intel_crtc(encoder->base.crtc)->active;
10604
10605 if (encoder->connectors_active && !has_active_crtc) {
10606 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10607 encoder->base.base.id,
10608 drm_get_encoder_name(&encoder->base));
10609
10610 /* Connector is active, but has no active pipe. This is
10611 * fallout from our resume register restoring. Disable
10612 * the encoder manually again. */
10613 if (encoder->base.crtc) {
10614 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10615 encoder->base.base.id,
10616 drm_get_encoder_name(&encoder->base));
10617 encoder->disable(encoder);
10618 }
10619
10620 /* Inconsistent output/port/pipe state happens presumably due to
10621 * a bug in one of the get_hw_state functions. Or someplace else
10622 * in our code, like the register restore mess on resume. Clamp
10623 * things to off as a safer default. */
10624 list_for_each_entry(connector,
10625 &dev->mode_config.connector_list,
10626 base.head) {
10627 if (connector->encoder != encoder)
10628 continue;
10629
10630 intel_connector_break_all_links(connector);
10631 }
10632 }
10633 /* Enabled encoders without active connectors will be fixed in
10634 * the crtc fixup. */
10635}
10636
Daniel Vetter44cec742013-01-25 17:53:21 +010010637void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010638{
10639 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010640 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010641
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010642 /* This function can be called both from intel_modeset_setup_hw_state or
10643 * at a very early point in our resume sequence, where the power well
10644 * structures are not yet restored. Since this function is at a very
10645 * paranoid "someone might have enabled VGA while we were not looking"
10646 * level, just check if the power well is enabled instead of trying to
10647 * follow the "don't touch the power well if we don't need it" policy
10648 * the rest of the driver uses. */
10649 if (HAS_POWER_WELL(dev) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030010650 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010651 return;
10652
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010653 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10654 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020010655 i915_disable_vga(dev);
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010656 i915_disable_vga_mem(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010657 }
10658}
10659
Daniel Vetter30e984d2013-06-05 13:34:17 +020010660static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020010661{
10662 struct drm_i915_private *dev_priv = dev->dev_private;
10663 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020010664 struct intel_crtc *crtc;
10665 struct intel_encoder *encoder;
10666 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020010667 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020010668
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010669 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10670 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010010671 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020010672
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010673 crtc->active = dev_priv->display.get_pipe_config(crtc,
10674 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010675
10676 crtc->base.enabled = crtc->active;
10677
10678 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10679 crtc->base.base.id,
10680 crtc->active ? "enabled" : "disabled");
10681 }
10682
Daniel Vetter53589012013-06-05 13:34:16 +020010683 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010684 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010685 intel_ddi_setup_hw_pll_state(dev);
10686
Daniel Vetter53589012013-06-05 13:34:16 +020010687 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10688 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10689
10690 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10691 pll->active = 0;
10692 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10693 base.head) {
10694 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10695 pll->active++;
10696 }
10697 pll->refcount = pll->active;
10698
Daniel Vetter35c95372013-07-17 06:55:04 +020010699 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10700 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020010701 }
10702
Daniel Vetter24929352012-07-02 20:28:59 +020010703 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10704 base.head) {
10705 pipe = 0;
10706
10707 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010708 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10709 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070010710 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010711 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010712 } else {
10713 encoder->base.crtc = NULL;
10714 }
10715
10716 encoder->connectors_active = false;
10717 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10718 encoder->base.base.id,
10719 drm_get_encoder_name(&encoder->base),
10720 encoder->base.crtc ? "enabled" : "disabled",
10721 pipe);
10722 }
10723
10724 list_for_each_entry(connector, &dev->mode_config.connector_list,
10725 base.head) {
10726 if (connector->get_hw_state(connector)) {
10727 connector->base.dpms = DRM_MODE_DPMS_ON;
10728 connector->encoder->connectors_active = true;
10729 connector->base.encoder = &connector->encoder->base;
10730 } else {
10731 connector->base.dpms = DRM_MODE_DPMS_OFF;
10732 connector->base.encoder = NULL;
10733 }
10734 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10735 connector->base.base.id,
10736 drm_get_connector_name(&connector->base),
10737 connector->base.encoder ? "enabled" : "disabled");
10738 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010739}
10740
10741/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10742 * and i915 state tracking structures. */
10743void intel_modeset_setup_hw_state(struct drm_device *dev,
10744 bool force_restore)
10745{
10746 struct drm_i915_private *dev_priv = dev->dev_private;
10747 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010748 struct intel_crtc *crtc;
10749 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020010750 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010751
10752 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010753
Jesse Barnesbabea612013-06-26 18:57:38 +030010754 /*
10755 * Now that we have the config, copy it to each CRTC struct
10756 * Note that this could go away if we move to using crtc_config
10757 * checking everywhere.
10758 */
10759 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10760 base.head) {
10761 if (crtc->active && i915_fastboot) {
10762 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10763
10764 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10765 crtc->base.base.id);
10766 drm_mode_debug_printmodeline(&crtc->base.mode);
10767 }
10768 }
10769
Daniel Vetter24929352012-07-02 20:28:59 +020010770 /* HW state is read out, now we need to sanitize this mess. */
10771 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10772 base.head) {
10773 intel_sanitize_encoder(encoder);
10774 }
10775
10776 for_each_pipe(pipe) {
10777 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10778 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010779 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010780 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010781
Daniel Vetter35c95372013-07-17 06:55:04 +020010782 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10783 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10784
10785 if (!pll->on || pll->active)
10786 continue;
10787
10788 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10789
10790 pll->disable(dev_priv, pll);
10791 pll->on = false;
10792 }
10793
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010794 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030010795 i915_redisable_vga(dev);
10796
Daniel Vetterf30da182013-04-11 20:22:50 +020010797 /*
10798 * We need to use raw interfaces for restoring state to avoid
10799 * checking (bogus) intermediate states.
10800 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010801 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010802 struct drm_crtc *crtc =
10803 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010804
10805 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10806 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010807 }
10808 } else {
10809 intel_modeset_update_staged_output_state(dev);
10810 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010811
10812 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010813
10814 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010815}
10816
10817void intel_modeset_gem_init(struct drm_device *dev)
10818{
Chris Wilson1833b132012-05-09 11:56:28 +010010819 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010820
10821 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010822
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010823 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010824}
10825
10826void intel_modeset_cleanup(struct drm_device *dev)
10827{
Jesse Barnes652c3932009-08-17 13:31:43 -070010828 struct drm_i915_private *dev_priv = dev->dev_private;
10829 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030010830 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070010831
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010832 /*
10833 * Interrupts and polling as the first thing to avoid creating havoc.
10834 * Too much stuff here (turning of rps, connectors, ...) would
10835 * experience fancy races otherwise.
10836 */
10837 drm_irq_uninstall(dev);
10838 cancel_work_sync(&dev_priv->hotplug_work);
10839 /*
10840 * Due to the hpd irq storm handling the hotplug work can re-arm the
10841 * poll handlers. Hence disable polling after hpd handling is shut down.
10842 */
Keith Packardf87ea762010-10-03 19:36:26 -070010843 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010844
Jesse Barnes652c3932009-08-17 13:31:43 -070010845 mutex_lock(&dev->struct_mutex);
10846
Jesse Barnes723bfd72010-10-07 16:01:13 -070010847 intel_unregister_dsm_handler();
10848
Jesse Barnes652c3932009-08-17 13:31:43 -070010849 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10850 /* Skip inactive CRTCs */
10851 if (!crtc->fb)
10852 continue;
10853
Daniel Vetter3dec0092010-08-20 21:40:52 +020010854 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010855 }
10856
Chris Wilson973d04f2011-07-08 12:22:37 +010010857 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010858
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +030010859 i915_enable_vga_mem(dev);
Alex Williamson81b5c7b2013-08-28 09:39:08 -060010860
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010861 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010862
Daniel Vetter930ebb42012-06-29 23:32:16 +020010863 ironlake_teardown_rc6(dev);
10864
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010865 mutex_unlock(&dev->struct_mutex);
10866
Chris Wilson1630fe72011-07-08 12:22:42 +010010867 /* flush any delayed tasks or pending work */
10868 flush_scheduled_work();
10869
Jani Nikuladc652f92013-04-12 15:18:38 +030010870 /* destroy backlight, if any, before the connectors */
10871 intel_panel_destroy_backlight(dev);
10872
Paulo Zanonid9255d52013-09-26 20:05:59 -030010873 /* destroy the sysfs files before encoders/connectors */
10874 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
10875 drm_sysfs_connector_remove(connector);
10876
Jesse Barnes79e53942008-11-07 14:24:08 -080010877 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010878
10879 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010880}
10881
Dave Airlie28d52042009-09-21 14:33:58 +100010882/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010883 * Return which encoder is currently attached for connector.
10884 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010885struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010886{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010887 return &intel_attached_encoder(connector)->base;
10888}
Jesse Barnes79e53942008-11-07 14:24:08 -080010889
Chris Wilsondf0e9242010-09-09 16:20:55 +010010890void intel_connector_attach_encoder(struct intel_connector *connector,
10891 struct intel_encoder *encoder)
10892{
10893 connector->encoder = encoder;
10894 drm_mode_connector_attach_encoder(&connector->base,
10895 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010896}
Dave Airlie28d52042009-09-21 14:33:58 +100010897
10898/*
10899 * set vga decode state - true == enable VGA decode
10900 */
10901int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10902{
10903 struct drm_i915_private *dev_priv = dev->dev_private;
10904 u16 gmch_ctrl;
10905
10906 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10907 if (state)
10908 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10909 else
10910 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10911 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10912 return 0;
10913}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010914
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010915struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010916
10917 u32 power_well_driver;
10918
Chris Wilson63b66e52013-08-08 15:12:06 +020010919 int num_transcoders;
10920
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010921 struct intel_cursor_error_state {
10922 u32 control;
10923 u32 position;
10924 u32 base;
10925 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010926 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010927
10928 struct intel_pipe_error_state {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010929 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010010930 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010931
10932 struct intel_plane_error_state {
10933 u32 control;
10934 u32 stride;
10935 u32 size;
10936 u32 pos;
10937 u32 addr;
10938 u32 surface;
10939 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010940 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020010941
10942 struct intel_transcoder_error_state {
10943 enum transcoder cpu_transcoder;
10944
10945 u32 conf;
10946
10947 u32 htotal;
10948 u32 hblank;
10949 u32 hsync;
10950 u32 vtotal;
10951 u32 vblank;
10952 u32 vsync;
10953 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010954};
10955
10956struct intel_display_error_state *
10957intel_display_capture_error_state(struct drm_device *dev)
10958{
Akshay Joshi0206e352011-08-16 15:34:10 -040010959 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010960 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020010961 int transcoders[] = {
10962 TRANSCODER_A,
10963 TRANSCODER_B,
10964 TRANSCODER_C,
10965 TRANSCODER_EDP,
10966 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010967 int i;
10968
Chris Wilson63b66e52013-08-08 15:12:06 +020010969 if (INTEL_INFO(dev)->num_pipes == 0)
10970 return NULL;
10971
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010972 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10973 if (error == NULL)
10974 return NULL;
10975
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010976 if (HAS_POWER_WELL(dev))
10977 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10978
Damien Lespiau52331302012-08-15 19:23:25 +010010979 for_each_pipe(i) {
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010980 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10981 error->cursor[i].control = I915_READ(CURCNTR(i));
10982 error->cursor[i].position = I915_READ(CURPOS(i));
10983 error->cursor[i].base = I915_READ(CURBASE(i));
10984 } else {
10985 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10986 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10987 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10988 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010989
10990 error->plane[i].control = I915_READ(DSPCNTR(i));
10991 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010992 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010993 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010994 error->plane[i].pos = I915_READ(DSPPOS(i));
10995 }
Paulo Zanonica291362013-03-06 20:03:14 -030010996 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10997 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010998 if (INTEL_INFO(dev)->gen >= 4) {
10999 error->plane[i].surface = I915_READ(DSPSURF(i));
11000 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11001 }
11002
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011003 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011004 }
11005
11006 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11007 if (HAS_DDI(dev_priv->dev))
11008 error->num_transcoders++; /* Account for eDP. */
11009
11010 for (i = 0; i < error->num_transcoders; i++) {
11011 enum transcoder cpu_transcoder = transcoders[i];
11012
11013 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11014
11015 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11016 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11017 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11018 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11019 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11020 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11021 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011022 }
11023
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011024 /* In the code above we read the registers without checking if the power
11025 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
11026 * prevent the next I915_WRITE from detecting it and printing an error
11027 * message. */
Chris Wilson907b28c2013-07-19 20:36:52 +010011028 intel_uncore_clear_errors(dev);
Paulo Zanoni12d217c2013-05-03 12:15:38 -030011029
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011030 return error;
11031}
11032
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011033#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11034
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011035void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011036intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011037 struct drm_device *dev,
11038 struct intel_display_error_state *error)
11039{
11040 int i;
11041
Chris Wilson63b66e52013-08-08 15:12:06 +020011042 if (!error)
11043 return;
11044
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011045 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011046 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011047 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011048 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011049 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011050 err_printf(m, "Pipe [%d]:\n", i);
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011051 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011052
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011053 err_printf(m, "Plane [%d]:\n", i);
11054 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11055 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011056 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011057 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11058 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011059 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011060 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011061 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011062 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011063 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11064 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011065 }
11066
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011067 err_printf(m, "Cursor [%d]:\n", i);
11068 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11069 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11070 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011071 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011072
11073 for (i = 0; i < error->num_transcoders; i++) {
11074 err_printf(m, " CPU transcoder: %c\n",
11075 transcoder_name(error->transcoder[i].cpu_transcoder));
11076 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11077 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11078 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11079 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11080 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11081 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11082 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11083 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011084}