blob: 1f7af63b31df17788a15020c66507e6770a0b06e [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Daniel Vetter3dec0092010-08-20 21:40:52 +020044static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010045static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080046
Jesse Barnesf1f644d2013-06-27 00:39:25 +030047static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030049static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030051
Damien Lespiaue7457a92013-08-08 22:28:59 +010052static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
54
55
Jesse Barnes79e53942008-11-07 14:24:08 -080056typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040057 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_range_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int dot_limit;
62 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080063} intel_p2_t;
64
Ma Lingd4906092009-03-18 20:13:27 +080065typedef struct intel_limit intel_limit_t;
66struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 intel_range_t dot, vco, n, m, m1, m2, p, p1;
68 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080069};
Jesse Barnes79e53942008-11-07 14:24:08 -080070
Daniel Vetterd2acd212012-10-20 20:57:43 +020071int
72intel_pch_rawclk(struct drm_device *dev)
73{
74 struct drm_i915_private *dev_priv = dev->dev_private;
75
76 WARN_ON(!HAS_PCH_SPLIT(dev));
77
78 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
79}
80
Chris Wilson021357a2010-09-07 20:54:59 +010081static inline u32 /* units of 100MHz */
82intel_fdi_link_freq(struct drm_device *dev)
83{
Chris Wilson8b99e682010-10-13 09:59:17 +010084 if (IS_GEN5(dev)) {
85 struct drm_i915_private *dev_priv = dev->dev_private;
86 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 } else
88 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010089}
90
Daniel Vetter5d536e22013-07-06 12:52:06 +020091static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040092 .dot = { .min = 25000, .max = 350000 },
93 .vco = { .min = 930000, .max = 1400000 },
94 .n = { .min = 3, .max = 16 },
95 .m = { .min = 96, .max = 140 },
96 .m1 = { .min = 18, .max = 26 },
97 .m2 = { .min = 6, .max = 16 },
98 .p = { .min = 4, .max = 128 },
99 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700100 .p2 = { .dot_limit = 165000,
101 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700102};
103
Daniel Vetter5d536e22013-07-06 12:52:06 +0200104static const intel_limit_t intel_limits_i8xx_dvo = {
105 .dot = { .min = 25000, .max = 350000 },
106 .vco = { .min = 930000, .max = 1400000 },
107 .n = { .min = 3, .max = 16 },
108 .m = { .min = 96, .max = 140 },
109 .m1 = { .min = 18, .max = 26 },
110 .m2 = { .min = 6, .max = 16 },
111 .p = { .min = 4, .max = 128 },
112 .p1 = { .min = 2, .max = 33 },
113 .p2 = { .dot_limit = 165000,
114 .p2_slow = 4, .p2_fast = 4 },
115};
116
Keith Packarde4b36692009-06-05 19:22:17 -0700117static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 .dot = { .min = 25000, .max = 350000 },
119 .vco = { .min = 930000, .max = 1400000 },
120 .n = { .min = 3, .max = 16 },
121 .m = { .min = 96, .max = 140 },
122 .m1 = { .min = 18, .max = 26 },
123 .m2 = { .min = 6, .max = 16 },
124 .p = { .min = 4, .max = 128 },
125 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700126 .p2 = { .dot_limit = 165000,
127 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700128};
Eric Anholt273e27c2011-03-30 13:01:10 -0700129
Keith Packarde4b36692009-06-05 19:22:17 -0700130static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 .dot = { .min = 20000, .max = 400000 },
132 .vco = { .min = 1400000, .max = 2800000 },
133 .n = { .min = 1, .max = 6 },
134 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100135 .m1 = { .min = 8, .max = 18 },
136 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .p = { .min = 5, .max = 80 },
138 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700139 .p2 = { .dot_limit = 200000,
140 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700141};
142
143static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400144 .dot = { .min = 20000, .max = 400000 },
145 .vco = { .min = 1400000, .max = 2800000 },
146 .n = { .min = 1, .max = 6 },
147 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100148 .m1 = { .min = 8, .max = 18 },
149 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .p = { .min = 7, .max = 98 },
151 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700152 .p2 = { .dot_limit = 112000,
153 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700154};
155
Eric Anholt273e27c2011-03-30 13:01:10 -0700156
Keith Packarde4b36692009-06-05 19:22:17 -0700157static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .dot = { .min = 25000, .max = 270000 },
159 .vco = { .min = 1750000, .max = 3500000},
160 .n = { .min = 1, .max = 4 },
161 .m = { .min = 104, .max = 138 },
162 .m1 = { .min = 17, .max = 23 },
163 .m2 = { .min = 5, .max = 11 },
164 .p = { .min = 10, .max = 30 },
165 .p1 = { .min = 1, .max = 3},
166 .p2 = { .dot_limit = 270000,
167 .p2_slow = 10,
168 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800169 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
172static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .dot = { .min = 22000, .max = 400000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 16, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8},
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 20000, .max = 115000 },
187 .vco = { .min = 1750000, .max = 3500000 },
188 .n = { .min = 1, .max = 3 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 28, .max = 112 },
193 .p1 = { .min = 2, .max = 8 },
194 .p2 = { .dot_limit = 0,
195 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800196 },
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
198
199static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 80000, .max = 224000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 14, .max = 42 },
207 .p1 = { .min = 2, .max = 6 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800210 },
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500213static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400214 .dot = { .min = 20000, .max = 400000},
215 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .n = { .min = 3, .max = 6 },
218 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .m1 = { .min = 0, .max = 0 },
221 .m2 = { .min = 0, .max = 254 },
222 .p = { .min = 5, .max = 80 },
223 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700224 .p2 = { .dot_limit = 200000,
225 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500228static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 20000, .max = 400000 },
230 .vco = { .min = 1700000, .max = 3500000 },
231 .n = { .min = 3, .max = 6 },
232 .m = { .min = 2, .max = 256 },
233 .m1 = { .min = 0, .max = 0 },
234 .m2 = { .min = 0, .max = 254 },
235 .p = { .min = 7, .max = 112 },
236 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700237 .p2 = { .dot_limit = 112000,
238 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
Eric Anholt273e27c2011-03-30 13:01:10 -0700241/* Ironlake / Sandybridge
242 *
243 * We calculate clock using (register_value + 2) for N/M1/M2, so here
244 * the range value for them is (actual_value - 2).
245 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800246static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 25000, .max = 350000 },
248 .vco = { .min = 1760000, .max = 3510000 },
249 .n = { .min = 1, .max = 5 },
250 .m = { .min = 79, .max = 127 },
251 .m1 = { .min = 12, .max = 22 },
252 .m2 = { .min = 5, .max = 9 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
255 .p2 = { .dot_limit = 225000,
256 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800259static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .dot = { .min = 25000, .max = 350000 },
261 .vco = { .min = 1760000, .max = 3510000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 79, .max = 118 },
264 .m1 = { .min = 12, .max = 22 },
265 .m2 = { .min = 5, .max = 9 },
266 .p = { .min = 28, .max = 112 },
267 .p1 = { .min = 2, .max = 8 },
268 .p2 = { .dot_limit = 225000,
269 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800270};
271
272static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .dot = { .min = 25000, .max = 350000 },
274 .vco = { .min = 1760000, .max = 3510000 },
275 .n = { .min = 1, .max = 3 },
276 .m = { .min = 79, .max = 127 },
277 .m1 = { .min = 12, .max = 22 },
278 .m2 = { .min = 5, .max = 9 },
279 .p = { .min = 14, .max = 56 },
280 .p1 = { .min = 2, .max = 8 },
281 .p2 = { .dot_limit = 225000,
282 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800286static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .dot = { .min = 25000, .max = 350000 },
288 .vco = { .min = 1760000, .max = 3510000 },
289 .n = { .min = 1, .max = 2 },
290 .m = { .min = 79, .max = 126 },
291 .m1 = { .min = 12, .max = 22 },
292 .m2 = { .min = 5, .max = 9 },
293 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .p2 = { .dot_limit = 225000,
296 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 126 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800310};
311
Ville Syrjälädc730512013-09-24 21:26:30 +0300312static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300313 /*
314 * These are the data rate limits (measured in fast clocks)
315 * since those are the strictest limits we have. The fast
316 * clock and actual rate limits are more relaxed, so checking
317 * them would make no difference.
318 */
319 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200320 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700321 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .m1 = { .min = 2, .max = 3 },
323 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300324 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300325 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700326};
327
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300328static void vlv_clock(int refclk, intel_clock_t *clock)
329{
330 clock->m = clock->m1 * clock->m2;
331 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200332 if (WARN_ON(clock->n == 0 || clock->p == 0))
333 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300334 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
335 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300336}
337
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300338/**
339 * Returns whether any output on the specified pipe is of the specified type
340 */
341static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
342{
343 struct drm_device *dev = crtc->dev;
344 struct intel_encoder *encoder;
345
346 for_each_encoder_on_crtc(dev, crtc, encoder)
347 if (encoder->type == type)
348 return true;
349
350 return false;
351}
352
Chris Wilson1b894b52010-12-14 20:04:54 +0000353static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
354 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800355{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800356 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800357 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800358
359 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100360 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000361 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362 limit = &intel_limits_ironlake_dual_lvds_100m;
363 else
364 limit = &intel_limits_ironlake_dual_lvds;
365 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000366 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800367 limit = &intel_limits_ironlake_single_lvds_100m;
368 else
369 limit = &intel_limits_ironlake_single_lvds;
370 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200371 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800372 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800373
374 return limit;
375}
376
Ma Ling044c7c42009-03-18 20:13:23 +0800377static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
378{
379 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800380 const intel_limit_t *limit;
381
382 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100383 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700384 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800385 else
Keith Packarde4b36692009-06-05 19:22:17 -0700386 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800387 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
388 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700389 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800390 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700391 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800392 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700393 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800394
395 return limit;
396}
397
Chris Wilson1b894b52010-12-14 20:04:54 +0000398static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800399{
400 struct drm_device *dev = crtc->dev;
401 const intel_limit_t *limit;
402
Eric Anholtbad720f2009-10-22 16:11:14 -0700403 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000404 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800405 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800406 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500407 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800408 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500409 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800410 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500411 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700412 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300413 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100414 } else if (!IS_GEN2(dev)) {
415 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
416 limit = &intel_limits_i9xx_lvds;
417 else
418 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800419 } else {
420 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700421 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200422 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700423 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200424 else
425 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800426 }
427 return limit;
428}
429
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500430/* m1 is reserved as 0 in Pineview, n is a ring counter */
431static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800432{
Shaohua Li21778322009-02-23 15:19:16 +0800433 clock->m = clock->m2 + 2;
434 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200435 if (WARN_ON(clock->n == 0 || clock->p == 0))
436 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300437 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
438 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800439}
440
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200441static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
442{
443 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
444}
445
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200446static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800447{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200448 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800449 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200450 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
451 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300452 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
453 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800454}
455
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800456#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800457/**
458 * Returns whether the given set of divisors are valid for a given refclk with
459 * the given connectors.
460 */
461
Chris Wilson1b894b52010-12-14 20:04:54 +0000462static bool intel_PLL_is_valid(struct drm_device *dev,
463 const intel_limit_t *limit,
464 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800465{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300466 if (clock->n < limit->n.min || limit->n.max < clock->n)
467 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800468 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400469 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800470 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400471 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800472 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400473 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300474
475 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
476 if (clock->m1 <= clock->m2)
477 INTELPllInvalid("m1 <= m2\n");
478
479 if (!IS_VALLEYVIEW(dev)) {
480 if (clock->p < limit->p.min || limit->p.max < clock->p)
481 INTELPllInvalid("p out of range\n");
482 if (clock->m < limit->m.min || limit->m.max < clock->m)
483 INTELPllInvalid("m out of range\n");
484 }
485
Jesse Barnes79e53942008-11-07 14:24:08 -0800486 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400487 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800488 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
489 * connector, etc., rather than just a single range.
490 */
491 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400492 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800493
494 return true;
495}
496
Ma Lingd4906092009-03-18 20:13:27 +0800497static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200498i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800499 int target, int refclk, intel_clock_t *match_clock,
500 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800501{
502 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800503 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800504 int err = target;
505
Daniel Vettera210b022012-11-26 17:22:08 +0100506 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100508 * For LVDS just rely on its current settings for dual-channel.
509 * We haven't figured out how to reliably set up different
510 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800511 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100512 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 clock.p2 = limit->p2.p2_fast;
514 else
515 clock.p2 = limit->p2.p2_slow;
516 } else {
517 if (target < limit->p2.dot_limit)
518 clock.p2 = limit->p2.p2_slow;
519 else
520 clock.p2 = limit->p2.p2_fast;
521 }
522
Akshay Joshi0206e352011-08-16 15:34:10 -0400523 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800524
Zhao Yakui42158662009-11-20 11:24:18 +0800525 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
526 clock.m1++) {
527 for (clock.m2 = limit->m2.min;
528 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200529 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800530 break;
531 for (clock.n = limit->n.min;
532 clock.n <= limit->n.max; clock.n++) {
533 for (clock.p1 = limit->p1.min;
534 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800535 int this_err;
536
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200537 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000538 if (!intel_PLL_is_valid(dev, limit,
539 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800540 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800541 if (match_clock &&
542 clock.p != match_clock->p)
543 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800544
545 this_err = abs(clock.dot - target);
546 if (this_err < err) {
547 *best_clock = clock;
548 err = this_err;
549 }
550 }
551 }
552 }
553 }
554
555 return (err != target);
556}
557
Ma Lingd4906092009-03-18 20:13:27 +0800558static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200559pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
560 int target, int refclk, intel_clock_t *match_clock,
561 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200562{
563 struct drm_device *dev = crtc->dev;
564 intel_clock_t clock;
565 int err = target;
566
567 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
568 /*
569 * For LVDS just rely on its current settings for dual-channel.
570 * We haven't figured out how to reliably set up different
571 * single/dual channel state, if we even can.
572 */
573 if (intel_is_dual_link_lvds(dev))
574 clock.p2 = limit->p2.p2_fast;
575 else
576 clock.p2 = limit->p2.p2_slow;
577 } else {
578 if (target < limit->p2.dot_limit)
579 clock.p2 = limit->p2.p2_slow;
580 else
581 clock.p2 = limit->p2.p2_fast;
582 }
583
584 memset(best_clock, 0, sizeof(*best_clock));
585
586 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
587 clock.m1++) {
588 for (clock.m2 = limit->m2.min;
589 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200590 for (clock.n = limit->n.min;
591 clock.n <= limit->n.max; clock.n++) {
592 for (clock.p1 = limit->p1.min;
593 clock.p1 <= limit->p1.max; clock.p1++) {
594 int this_err;
595
596 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 if (!intel_PLL_is_valid(dev, limit,
598 &clock))
599 continue;
600 if (match_clock &&
601 clock.p != match_clock->p)
602 continue;
603
604 this_err = abs(clock.dot - target);
605 if (this_err < err) {
606 *best_clock = clock;
607 err = this_err;
608 }
609 }
610 }
611 }
612 }
613
614 return (err != target);
615}
616
Ma Lingd4906092009-03-18 20:13:27 +0800617static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200618g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
619 int target, int refclk, intel_clock_t *match_clock,
620 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800621{
622 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800623 intel_clock_t clock;
624 int max_n;
625 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400626 /* approximately equals target * 0.00585 */
627 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800628 found = false;
629
630 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100631 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800632 clock.p2 = limit->p2.p2_fast;
633 else
634 clock.p2 = limit->p2.p2_slow;
635 } else {
636 if (target < limit->p2.dot_limit)
637 clock.p2 = limit->p2.p2_slow;
638 else
639 clock.p2 = limit->p2.p2_fast;
640 }
641
642 memset(best_clock, 0, sizeof(*best_clock));
643 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200644 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800645 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200646 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800647 for (clock.m1 = limit->m1.max;
648 clock.m1 >= limit->m1.min; clock.m1--) {
649 for (clock.m2 = limit->m2.max;
650 clock.m2 >= limit->m2.min; clock.m2--) {
651 for (clock.p1 = limit->p1.max;
652 clock.p1 >= limit->p1.min; clock.p1--) {
653 int this_err;
654
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200655 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000656 if (!intel_PLL_is_valid(dev, limit,
657 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800658 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000659
660 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800661 if (this_err < err_most) {
662 *best_clock = clock;
663 err_most = this_err;
664 max_n = clock.n;
665 found = true;
666 }
667 }
668 }
669 }
670 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800671 return found;
672}
Ma Lingd4906092009-03-18 20:13:27 +0800673
Zhenyu Wang2c072452009-06-05 15:38:42 +0800674static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200675vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
676 int target, int refclk, intel_clock_t *match_clock,
677 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700678{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300679 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300680 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300681 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300682 /* min update 19.2 MHz */
683 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300684 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700685
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300686 target *= 5; /* fast clock */
687
688 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700689
690 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300691 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300692 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300693 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300694 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300695 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700696 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300697 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300698 unsigned int ppm, diff;
699
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300700 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
701 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300702
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300703 vlv_clock(refclk, &clock);
704
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300705 if (!intel_PLL_is_valid(dev, limit,
706 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300707 continue;
708
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300709 diff = abs(clock.dot - target);
710 ppm = div_u64(1000000ULL * diff, target);
711
712 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300713 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300714 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300715 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300716 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300717
Ville Syrjäläc6861222013-09-24 21:26:21 +0300718 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300719 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300720 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300721 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700722 }
723 }
724 }
725 }
726 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700727
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300728 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700729}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700730
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300731bool intel_crtc_active(struct drm_crtc *crtc)
732{
733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
734
735 /* Be paranoid as we can arrive here with only partial
736 * state retrieved from the hardware during setup.
737 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100738 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300739 * as Haswell has gained clock readout/fastboot support.
740 *
741 * We can ditch the crtc->fb check as soon as we can
742 * properly reconstruct framebuffers.
743 */
744 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100745 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300746}
747
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200748enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
749 enum pipe pipe)
750{
751 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
753
Daniel Vetter3b117c82013-04-17 20:15:07 +0200754 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200755}
756
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200757static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300758{
759 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200760 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300761
762 frame = I915_READ(frame_reg);
763
764 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
765 DRM_DEBUG_KMS("vblank wait timed out\n");
766}
767
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700768/**
769 * intel_wait_for_vblank - wait for vblank on a given pipe
770 * @dev: drm device
771 * @pipe: pipe to wait for
772 *
773 * Wait for vblank to occur on a given pipe. Needed for various bits of
774 * mode setting code.
775 */
776void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800777{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700778 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800779 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700780
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200781 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
782 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300783 return;
784 }
785
Chris Wilson300387c2010-09-05 20:25:43 +0100786 /* Clear existing vblank status. Note this will clear any other
787 * sticky status fields as well.
788 *
789 * This races with i915_driver_irq_handler() with the result
790 * that either function could miss a vblank event. Here it is not
791 * fatal, as we will either wait upon the next vblank interrupt or
792 * timeout. Generally speaking intel_wait_for_vblank() is only
793 * called during modeset at which time the GPU should be idle and
794 * should *not* be performing page flips and thus not waiting on
795 * vblanks...
796 * Currently, the result of us stealing a vblank from the irq
797 * handler is that a single frame will be skipped during swapbuffers.
798 */
799 I915_WRITE(pipestat_reg,
800 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
801
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700802 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100803 if (wait_for(I915_READ(pipestat_reg) &
804 PIPE_VBLANK_INTERRUPT_STATUS,
805 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700806 DRM_DEBUG_KMS("vblank wait timed out\n");
807}
808
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300809static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
810{
811 struct drm_i915_private *dev_priv = dev->dev_private;
812 u32 reg = PIPEDSL(pipe);
813 u32 line1, line2;
814 u32 line_mask;
815
816 if (IS_GEN2(dev))
817 line_mask = DSL_LINEMASK_GEN2;
818 else
819 line_mask = DSL_LINEMASK_GEN3;
820
821 line1 = I915_READ(reg) & line_mask;
822 mdelay(5);
823 line2 = I915_READ(reg) & line_mask;
824
825 return line1 == line2;
826}
827
Keith Packardab7ad7f2010-10-03 00:33:06 -0700828/*
829 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700830 * @dev: drm device
831 * @pipe: pipe to wait for
832 *
833 * After disabling a pipe, we can't wait for vblank in the usual way,
834 * spinning on the vblank interrupt status bit, since we won't actually
835 * see an interrupt when the pipe is disabled.
836 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700837 * On Gen4 and above:
838 * wait for the pipe register state bit to turn off
839 *
840 * Otherwise:
841 * wait for the display line value to settle (it usually
842 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100843 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700844 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100845void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700846{
847 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200848 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
849 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700850
Keith Packardab7ad7f2010-10-03 00:33:06 -0700851 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200852 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700853
Keith Packardab7ad7f2010-10-03 00:33:06 -0700854 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100855 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
856 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200857 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700858 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700859 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300860 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200861 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700862 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800863}
864
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000865/*
866 * ibx_digital_port_connected - is the specified port connected?
867 * @dev_priv: i915 private structure
868 * @port: the port to test
869 *
870 * Returns true if @port is connected, false otherwise.
871 */
872bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
873 struct intel_digital_port *port)
874{
875 u32 bit;
876
Damien Lespiauc36346e2012-12-13 16:09:03 +0000877 if (HAS_PCH_IBX(dev_priv->dev)) {
878 switch(port->port) {
879 case PORT_B:
880 bit = SDE_PORTB_HOTPLUG;
881 break;
882 case PORT_C:
883 bit = SDE_PORTC_HOTPLUG;
884 break;
885 case PORT_D:
886 bit = SDE_PORTD_HOTPLUG;
887 break;
888 default:
889 return true;
890 }
891 } else {
892 switch(port->port) {
893 case PORT_B:
894 bit = SDE_PORTB_HOTPLUG_CPT;
895 break;
896 case PORT_C:
897 bit = SDE_PORTC_HOTPLUG_CPT;
898 break;
899 case PORT_D:
900 bit = SDE_PORTD_HOTPLUG_CPT;
901 break;
902 default:
903 return true;
904 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000905 }
906
907 return I915_READ(SDEISR) & bit;
908}
909
Jesse Barnesb24e7172011-01-04 15:09:30 -0800910static const char *state_string(bool enabled)
911{
912 return enabled ? "on" : "off";
913}
914
915/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200916void assert_pll(struct drm_i915_private *dev_priv,
917 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800918{
919 int reg;
920 u32 val;
921 bool cur_state;
922
923 reg = DPLL(pipe);
924 val = I915_READ(reg);
925 cur_state = !!(val & DPLL_VCO_ENABLE);
926 WARN(cur_state != state,
927 "PLL state assertion failure (expected %s, current %s)\n",
928 state_string(state), state_string(cur_state));
929}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800930
Jani Nikula23538ef2013-08-27 15:12:22 +0300931/* XXX: the dsi pll is shared between MIPI DSI ports */
932static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
933{
934 u32 val;
935 bool cur_state;
936
937 mutex_lock(&dev_priv->dpio_lock);
938 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
939 mutex_unlock(&dev_priv->dpio_lock);
940
941 cur_state = val & DSI_PLL_VCO_EN;
942 WARN(cur_state != state,
943 "DSI PLL state assertion failure (expected %s, current %s)\n",
944 state_string(state), state_string(cur_state));
945}
946#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
947#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
948
Daniel Vetter55607e82013-06-16 21:42:39 +0200949struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200950intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800951{
Daniel Vettere2b78262013-06-07 23:10:03 +0200952 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
953
Daniel Vettera43f6e02013-06-07 23:10:32 +0200954 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200955 return NULL;
956
Daniel Vettera43f6e02013-06-07 23:10:32 +0200957 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200958}
959
Jesse Barnesb24e7172011-01-04 15:09:30 -0800960/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200961void assert_shared_dpll(struct drm_i915_private *dev_priv,
962 struct intel_shared_dpll *pll,
963 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800964{
Jesse Barnes040484a2011-01-03 12:14:26 -0800965 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200966 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800967
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300968 if (HAS_PCH_LPT(dev_priv->dev)) {
969 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
970 return;
971 }
972
Chris Wilson92b27b02012-05-20 18:10:50 +0100973 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200974 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100975 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100976
Daniel Vetter53589012013-06-05 13:34:16 +0200977 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100978 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200979 "%s assertion failure (expected %s, current %s)\n",
980 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800981}
Jesse Barnes040484a2011-01-03 12:14:26 -0800982
983static void assert_fdi_tx(struct drm_i915_private *dev_priv,
984 enum pipe pipe, bool state)
985{
986 int reg;
987 u32 val;
988 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200989 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
990 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800991
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200992 if (HAS_DDI(dev_priv->dev)) {
993 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200994 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300995 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200996 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300997 } else {
998 reg = FDI_TX_CTL(pipe);
999 val = I915_READ(reg);
1000 cur_state = !!(val & FDI_TX_ENABLE);
1001 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001002 WARN(cur_state != state,
1003 "FDI TX state assertion failure (expected %s, current %s)\n",
1004 state_string(state), state_string(cur_state));
1005}
1006#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1007#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1008
1009static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1010 enum pipe pipe, bool state)
1011{
1012 int reg;
1013 u32 val;
1014 bool cur_state;
1015
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001016 reg = FDI_RX_CTL(pipe);
1017 val = I915_READ(reg);
1018 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001019 WARN(cur_state != state,
1020 "FDI RX state assertion failure (expected %s, current %s)\n",
1021 state_string(state), state_string(cur_state));
1022}
1023#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1024#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1025
1026static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 int reg;
1030 u32 val;
1031
1032 /* ILK FDI PLL is always enabled */
1033 if (dev_priv->info->gen == 5)
1034 return;
1035
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001036 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001037 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001038 return;
1039
Jesse Barnes040484a2011-01-03 12:14:26 -08001040 reg = FDI_TX_CTL(pipe);
1041 val = I915_READ(reg);
1042 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1043}
1044
Daniel Vetter55607e82013-06-16 21:42:39 +02001045void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1046 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001047{
1048 int reg;
1049 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001050 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001051
1052 reg = FDI_RX_CTL(pipe);
1053 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001054 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1055 WARN(cur_state != state,
1056 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1057 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001058}
1059
Jesse Barnesea0760c2011-01-04 15:09:32 -08001060static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1061 enum pipe pipe)
1062{
1063 int pp_reg, lvds_reg;
1064 u32 val;
1065 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001066 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001067
1068 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1069 pp_reg = PCH_PP_CONTROL;
1070 lvds_reg = PCH_LVDS;
1071 } else {
1072 pp_reg = PP_CONTROL;
1073 lvds_reg = LVDS;
1074 }
1075
1076 val = I915_READ(pp_reg);
1077 if (!(val & PANEL_POWER_ON) ||
1078 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1079 locked = false;
1080
1081 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1082 panel_pipe = PIPE_B;
1083
1084 WARN(panel_pipe == pipe && locked,
1085 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001086 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001087}
1088
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001089static void assert_cursor(struct drm_i915_private *dev_priv,
1090 enum pipe pipe, bool state)
1091{
1092 struct drm_device *dev = dev_priv->dev;
1093 bool cur_state;
1094
1095 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1096 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1097 else if (IS_845G(dev) || IS_I865G(dev))
1098 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1099 else
1100 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1101
1102 WARN(cur_state != state,
1103 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1104 pipe_name(pipe), state_string(state), state_string(cur_state));
1105}
1106#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1107#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1108
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001109void assert_pipe(struct drm_i915_private *dev_priv,
1110 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001111{
1112 int reg;
1113 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001114 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001115 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1116 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001117
Daniel Vetter8e636782012-01-22 01:36:48 +01001118 /* if we need the pipe A quirk it must be always on */
1119 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1120 state = true;
1121
Paulo Zanonib97186f2013-05-03 12:15:36 -03001122 if (!intel_display_power_enabled(dev_priv->dev,
1123 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001124 cur_state = false;
1125 } else {
1126 reg = PIPECONF(cpu_transcoder);
1127 val = I915_READ(reg);
1128 cur_state = !!(val & PIPECONF_ENABLE);
1129 }
1130
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001131 WARN(cur_state != state,
1132 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001133 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134}
1135
Chris Wilson931872f2012-01-16 23:01:13 +00001136static void assert_plane(struct drm_i915_private *dev_priv,
1137 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138{
1139 int reg;
1140 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001141 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142
1143 reg = DSPCNTR(plane);
1144 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001145 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1146 WARN(cur_state != state,
1147 "plane %c assertion failure (expected %s, current %s)\n",
1148 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149}
1150
Chris Wilson931872f2012-01-16 23:01:13 +00001151#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1152#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1153
Jesse Barnesb24e7172011-01-04 15:09:30 -08001154static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1155 enum pipe pipe)
1156{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001157 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001158 int reg, i;
1159 u32 val;
1160 int cur_pipe;
1161
Ville Syrjälä653e1022013-06-04 13:49:05 +03001162 /* Primary planes are fixed to pipes on gen4+ */
1163 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001164 reg = DSPCNTR(pipe);
1165 val = I915_READ(reg);
1166 WARN((val & DISPLAY_PLANE_ENABLE),
1167 "plane %c assertion failure, should be disabled but not\n",
1168 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001169 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001170 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001171
Jesse Barnesb24e7172011-01-04 15:09:30 -08001172 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001173 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001174 reg = DSPCNTR(i);
1175 val = I915_READ(reg);
1176 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1177 DISPPLANE_SEL_PIPE_SHIFT;
1178 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001179 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1180 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001181 }
1182}
1183
Jesse Barnes19332d72013-03-28 09:55:38 -07001184static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1185 enum pipe pipe)
1186{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001187 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001188 int reg, i;
1189 u32 val;
1190
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001191 if (IS_VALLEYVIEW(dev)) {
1192 for (i = 0; i < dev_priv->num_plane; i++) {
1193 reg = SPCNTR(pipe, i);
1194 val = I915_READ(reg);
1195 WARN((val & SP_ENABLE),
1196 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1197 sprite_name(pipe, i), pipe_name(pipe));
1198 }
1199 } else if (INTEL_INFO(dev)->gen >= 7) {
1200 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001201 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001202 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001203 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001204 plane_name(pipe), pipe_name(pipe));
1205 } else if (INTEL_INFO(dev)->gen >= 5) {
1206 reg = DVSCNTR(pipe);
1207 val = I915_READ(reg);
1208 WARN((val & DVS_ENABLE),
1209 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1210 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001211 }
1212}
1213
Jesse Barnes92f25842011-01-04 15:09:34 -08001214static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1215{
1216 u32 val;
1217 bool enabled;
1218
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001219 if (HAS_PCH_LPT(dev_priv->dev)) {
1220 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1221 return;
1222 }
1223
Jesse Barnes92f25842011-01-04 15:09:34 -08001224 val = I915_READ(PCH_DREF_CONTROL);
1225 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1226 DREF_SUPERSPREAD_SOURCE_MASK));
1227 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1228}
1229
Daniel Vetterab9412b2013-05-03 11:49:46 +02001230static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001232{
1233 int reg;
1234 u32 val;
1235 bool enabled;
1236
Daniel Vetterab9412b2013-05-03 11:49:46 +02001237 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001238 val = I915_READ(reg);
1239 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001240 WARN(enabled,
1241 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1242 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001243}
1244
Keith Packard4e634382011-08-06 10:39:45 -07001245static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001247{
1248 if ((val & DP_PORT_EN) == 0)
1249 return false;
1250
1251 if (HAS_PCH_CPT(dev_priv->dev)) {
1252 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1253 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1254 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1255 return false;
1256 } else {
1257 if ((val & DP_PIPE_MASK) != (pipe << 30))
1258 return false;
1259 }
1260 return true;
1261}
1262
Keith Packard1519b992011-08-06 10:35:34 -07001263static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1264 enum pipe pipe, u32 val)
1265{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001266 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001267 return false;
1268
1269 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001270 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001271 return false;
1272 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001273 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001274 return false;
1275 }
1276 return true;
1277}
1278
1279static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1280 enum pipe pipe, u32 val)
1281{
1282 if ((val & LVDS_PORT_EN) == 0)
1283 return false;
1284
1285 if (HAS_PCH_CPT(dev_priv->dev)) {
1286 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1287 return false;
1288 } else {
1289 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1290 return false;
1291 }
1292 return true;
1293}
1294
1295static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, u32 val)
1297{
1298 if ((val & ADPA_DAC_ENABLE) == 0)
1299 return false;
1300 if (HAS_PCH_CPT(dev_priv->dev)) {
1301 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1302 return false;
1303 } else {
1304 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1305 return false;
1306 }
1307 return true;
1308}
1309
Jesse Barnes291906f2011-02-02 12:28:03 -08001310static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001311 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001312{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001313 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001314 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001315 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001316 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001317
Daniel Vetter75c5da22012-09-10 21:58:29 +02001318 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1319 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001320 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001321}
1322
1323static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1324 enum pipe pipe, int reg)
1325{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001326 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001327 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001328 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001329 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001330
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001331 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001332 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001333 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001334}
1335
1336static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1337 enum pipe pipe)
1338{
1339 int reg;
1340 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001341
Keith Packardf0575e92011-07-25 22:12:43 -07001342 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1343 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1344 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001345
1346 reg = PCH_ADPA;
1347 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001348 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001349 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001350 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001351
1352 reg = PCH_LVDS;
1353 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001354 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001355 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001356 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001357
Paulo Zanonie2debe92013-02-18 19:00:27 -03001358 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1359 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1360 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001361}
1362
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001363static void intel_init_dpio(struct drm_device *dev)
1364{
1365 struct drm_i915_private *dev_priv = dev->dev_private;
1366
1367 if (!IS_VALLEYVIEW(dev))
1368 return;
1369
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001370 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001371 /*
1372 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1373 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1374 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1375 * b. The other bits such as sfr settings / modesel may all be set
1376 * to 0.
1377 *
1378 * This should only be done on init and resume from S3 with both
1379 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1380 */
1381 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1382}
1383
Daniel Vetter426115c2013-07-11 22:13:42 +02001384static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001385{
Daniel Vetter426115c2013-07-11 22:13:42 +02001386 struct drm_device *dev = crtc->base.dev;
1387 struct drm_i915_private *dev_priv = dev->dev_private;
1388 int reg = DPLL(crtc->pipe);
1389 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001390
Daniel Vetter426115c2013-07-11 22:13:42 +02001391 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001392
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001393 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001394 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1395
1396 /* PLL is protected by panel, make sure we can write it */
1397 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001398 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001399
Daniel Vetter426115c2013-07-11 22:13:42 +02001400 I915_WRITE(reg, dpll);
1401 POSTING_READ(reg);
1402 udelay(150);
1403
1404 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1405 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1406
1407 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1408 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001409
1410 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001411 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001412 POSTING_READ(reg);
1413 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001414 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001415 POSTING_READ(reg);
1416 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001417 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001418 POSTING_READ(reg);
1419 udelay(150); /* wait for warmup */
1420}
1421
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001422static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001423{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001424 struct drm_device *dev = crtc->base.dev;
1425 struct drm_i915_private *dev_priv = dev->dev_private;
1426 int reg = DPLL(crtc->pipe);
1427 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001428
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001429 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001430
1431 /* No really, not for ILK+ */
1432 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001433
1434 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001435 if (IS_MOBILE(dev) && !IS_I830(dev))
1436 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001437
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001438 I915_WRITE(reg, dpll);
1439
1440 /* Wait for the clocks to stabilize. */
1441 POSTING_READ(reg);
1442 udelay(150);
1443
1444 if (INTEL_INFO(dev)->gen >= 4) {
1445 I915_WRITE(DPLL_MD(crtc->pipe),
1446 crtc->config.dpll_hw_state.dpll_md);
1447 } else {
1448 /* The pixel multiplier can only be updated once the
1449 * DPLL is enabled and the clocks are stable.
1450 *
1451 * So write it again.
1452 */
1453 I915_WRITE(reg, dpll);
1454 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001455
1456 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001457 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001458 POSTING_READ(reg);
1459 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001460 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001461 POSTING_READ(reg);
1462 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001463 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001464 POSTING_READ(reg);
1465 udelay(150); /* wait for warmup */
1466}
1467
1468/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001469 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001470 * @dev_priv: i915 private structure
1471 * @pipe: pipe PLL to disable
1472 *
1473 * Disable the PLL for @pipe, making sure the pipe is off first.
1474 *
1475 * Note! This is for pre-ILK only.
1476 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001477static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001478{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001479 /* Don't disable pipe A or pipe A PLLs if needed */
1480 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1481 return;
1482
1483 /* Make sure the pipe isn't still relying on us */
1484 assert_pipe_disabled(dev_priv, pipe);
1485
Daniel Vetter50b44a42013-06-05 13:34:33 +02001486 I915_WRITE(DPLL(pipe), 0);
1487 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001488}
1489
Jesse Barnesf6071162013-10-01 10:41:38 -07001490static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1491{
1492 u32 val = 0;
1493
1494 /* Make sure the pipe isn't still relying on us */
1495 assert_pipe_disabled(dev_priv, pipe);
1496
1497 /* Leave integrated clock source enabled */
1498 if (pipe == PIPE_B)
1499 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1500 I915_WRITE(DPLL(pipe), val);
1501 POSTING_READ(DPLL(pipe));
1502}
1503
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001504void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1505 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001506{
1507 u32 port_mask;
1508
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001509 switch (dport->port) {
1510 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001511 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001512 break;
1513 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001514 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001515 break;
1516 default:
1517 BUG();
1518 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001519
1520 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1521 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Ville Syrjäläbe46ffd2013-11-29 13:21:49 +02001522 port_name(dport->port), I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001523}
1524
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001525/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001526 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001527 * @dev_priv: i915 private structure
1528 * @pipe: pipe PLL to enable
1529 *
1530 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1531 * drives the transcoder clock.
1532 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001533static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001534{
Daniel Vettere2b78262013-06-07 23:10:03 +02001535 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1536 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001537
Chris Wilson48da64a2012-05-13 20:16:12 +01001538 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001539 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001540 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001541 return;
1542
1543 if (WARN_ON(pll->refcount == 0))
1544 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001545
Daniel Vetter46edb022013-06-05 13:34:12 +02001546 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1547 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001548 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001549
Daniel Vettercdbd2312013-06-05 13:34:03 +02001550 if (pll->active++) {
1551 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001552 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001553 return;
1554 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001555 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001556
Daniel Vetter46edb022013-06-05 13:34:12 +02001557 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001558 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001559 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001560}
1561
Daniel Vettere2b78262013-06-07 23:10:03 +02001562static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001563{
Daniel Vettere2b78262013-06-07 23:10:03 +02001564 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1565 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001566
Jesse Barnes92f25842011-01-04 15:09:34 -08001567 /* PCH only available on ILK+ */
1568 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001569 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001570 return;
1571
Chris Wilson48da64a2012-05-13 20:16:12 +01001572 if (WARN_ON(pll->refcount == 0))
1573 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001574
Daniel Vetter46edb022013-06-05 13:34:12 +02001575 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1576 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001577 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001578
Chris Wilson48da64a2012-05-13 20:16:12 +01001579 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001580 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001581 return;
1582 }
1583
Daniel Vettere9d69442013-06-05 13:34:15 +02001584 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001585 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001586 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001587 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001588
Daniel Vetter46edb022013-06-05 13:34:12 +02001589 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001590 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001591 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001592}
1593
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001594static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1595 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001596{
Daniel Vetter23670b322012-11-01 09:15:30 +01001597 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001598 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001599 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001600 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001601
1602 /* PCH only available on ILK+ */
1603 BUG_ON(dev_priv->info->gen < 5);
1604
1605 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001606 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001607 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001608
1609 /* FDI must be feeding us bits for PCH ports */
1610 assert_fdi_tx_enabled(dev_priv, pipe);
1611 assert_fdi_rx_enabled(dev_priv, pipe);
1612
Daniel Vetter23670b322012-11-01 09:15:30 +01001613 if (HAS_PCH_CPT(dev)) {
1614 /* Workaround: Set the timing override bit before enabling the
1615 * pch transcoder. */
1616 reg = TRANS_CHICKEN2(pipe);
1617 val = I915_READ(reg);
1618 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1619 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001620 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001621
Daniel Vetterab9412b2013-05-03 11:49:46 +02001622 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001623 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001624 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001625
1626 if (HAS_PCH_IBX(dev_priv->dev)) {
1627 /*
1628 * make the BPC in transcoder be consistent with
1629 * that in pipeconf reg.
1630 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001631 val &= ~PIPECONF_BPC_MASK;
1632 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001633 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001634
1635 val &= ~TRANS_INTERLACE_MASK;
1636 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001637 if (HAS_PCH_IBX(dev_priv->dev) &&
1638 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1639 val |= TRANS_LEGACY_INTERLACED_ILK;
1640 else
1641 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001642 else
1643 val |= TRANS_PROGRESSIVE;
1644
Jesse Barnes040484a2011-01-03 12:14:26 -08001645 I915_WRITE(reg, val | TRANS_ENABLE);
1646 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001647 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001648}
1649
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001650static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001651 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001652{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001653 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001654
1655 /* PCH only available on ILK+ */
1656 BUG_ON(dev_priv->info->gen < 5);
1657
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001658 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001659 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001660 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001661
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001662 /* Workaround: set timing override bit. */
1663 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001664 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001665 I915_WRITE(_TRANSA_CHICKEN2, val);
1666
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001667 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001668 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001669
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001670 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1671 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001672 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001673 else
1674 val |= TRANS_PROGRESSIVE;
1675
Daniel Vetterab9412b2013-05-03 11:49:46 +02001676 I915_WRITE(LPT_TRANSCONF, val);
1677 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001678 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001679}
1680
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001681static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1682 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001683{
Daniel Vetter23670b322012-11-01 09:15:30 +01001684 struct drm_device *dev = dev_priv->dev;
1685 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001686
1687 /* FDI relies on the transcoder */
1688 assert_fdi_tx_disabled(dev_priv, pipe);
1689 assert_fdi_rx_disabled(dev_priv, pipe);
1690
Jesse Barnes291906f2011-02-02 12:28:03 -08001691 /* Ports must be off as well */
1692 assert_pch_ports_disabled(dev_priv, pipe);
1693
Daniel Vetterab9412b2013-05-03 11:49:46 +02001694 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001695 val = I915_READ(reg);
1696 val &= ~TRANS_ENABLE;
1697 I915_WRITE(reg, val);
1698 /* wait for PCH transcoder off, transcoder state */
1699 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001700 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001701
1702 if (!HAS_PCH_IBX(dev)) {
1703 /* Workaround: Clear the timing override chicken bit again. */
1704 reg = TRANS_CHICKEN2(pipe);
1705 val = I915_READ(reg);
1706 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1707 I915_WRITE(reg, val);
1708 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001709}
1710
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001711static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001712{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001713 u32 val;
1714
Daniel Vetterab9412b2013-05-03 11:49:46 +02001715 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001716 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001717 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001718 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001719 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001720 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001721
1722 /* Workaround: clear timing override bit. */
1723 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001724 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001725 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001726}
1727
1728/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001729 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001730 * @dev_priv: i915 private structure
1731 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001732 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001733 *
1734 * Enable @pipe, making sure that various hardware specific requirements
1735 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1736 *
1737 * @pipe should be %PIPE_A or %PIPE_B.
1738 *
1739 * Will wait until the pipe is actually running (i.e. first vblank) before
1740 * returning.
1741 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001742static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001743 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001744{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001745 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1746 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001747 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001748 int reg;
1749 u32 val;
1750
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001751 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001752 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001753 assert_sprites_disabled(dev_priv, pipe);
1754
Paulo Zanoni681e5812012-12-06 11:12:38 -02001755 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001756 pch_transcoder = TRANSCODER_A;
1757 else
1758 pch_transcoder = pipe;
1759
Jesse Barnesb24e7172011-01-04 15:09:30 -08001760 /*
1761 * A pipe without a PLL won't actually be able to drive bits from
1762 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1763 * need the check.
1764 */
1765 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001766 if (dsi)
1767 assert_dsi_pll_enabled(dev_priv);
1768 else
1769 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001770 else {
1771 if (pch_port) {
1772 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001773 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001774 assert_fdi_tx_pll_enabled(dev_priv,
1775 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001776 }
1777 /* FIXME: assert CPU port conditions for SNB+ */
1778 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001779
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001780 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001781 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001782 if (val & PIPECONF_ENABLE)
1783 return;
1784
1785 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001786 intel_wait_for_vblank(dev_priv->dev, pipe);
1787}
1788
1789/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001790 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001791 * @dev_priv: i915 private structure
1792 * @pipe: pipe to disable
1793 *
1794 * Disable @pipe, making sure that various hardware specific requirements
1795 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1796 *
1797 * @pipe should be %PIPE_A or %PIPE_B.
1798 *
1799 * Will wait until the pipe has shut down before returning.
1800 */
1801static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1802 enum pipe pipe)
1803{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001804 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1805 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001806 int reg;
1807 u32 val;
1808
1809 /*
1810 * Make sure planes won't keep trying to pump pixels to us,
1811 * or we might hang the display.
1812 */
1813 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001814 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001815 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001816
1817 /* Don't disable pipe A or pipe A PLLs if needed */
1818 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1819 return;
1820
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001821 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001822 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001823 if ((val & PIPECONF_ENABLE) == 0)
1824 return;
1825
1826 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001827 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1828}
1829
Keith Packardd74362c2011-07-28 14:47:14 -07001830/*
1831 * Plane regs are double buffered, going from enabled->disabled needs a
1832 * trigger in order to latch. The display address reg provides this.
1833 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001834void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1835 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001836{
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001837 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1838
1839 I915_WRITE(reg, I915_READ(reg));
1840 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001841}
1842
Jesse Barnesb24e7172011-01-04 15:09:30 -08001843/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001844 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001845 * @dev_priv: i915 private structure
1846 * @plane: plane to enable
1847 * @pipe: pipe being fed
1848 *
1849 * Enable @plane on @pipe, making sure that @pipe is running first.
1850 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001851static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1852 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001853{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001854 struct intel_crtc *intel_crtc =
1855 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001856 int reg;
1857 u32 val;
1858
1859 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1860 assert_pipe_enabled(dev_priv, pipe);
1861
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001862 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001863
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001864 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001865
Jesse Barnesb24e7172011-01-04 15:09:30 -08001866 reg = DSPCNTR(plane);
1867 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001868 if (val & DISPLAY_PLANE_ENABLE)
1869 return;
1870
1871 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001872 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001873 intel_wait_for_vblank(dev_priv->dev, pipe);
1874}
1875
Jesse Barnesb24e7172011-01-04 15:09:30 -08001876/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001877 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001878 * @dev_priv: i915 private structure
1879 * @plane: plane to disable
1880 * @pipe: pipe consuming the data
1881 *
1882 * Disable @plane; should be an independent operation.
1883 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001884static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1885 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001886{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001887 struct intel_crtc *intel_crtc =
1888 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001889 int reg;
1890 u32 val;
1891
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001892 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001893
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001894 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001895
Jesse Barnesb24e7172011-01-04 15:09:30 -08001896 reg = DSPCNTR(plane);
1897 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001898 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1899 return;
1900
1901 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001902 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001903 intel_wait_for_vblank(dev_priv->dev, pipe);
1904}
1905
Chris Wilson693db182013-03-05 14:52:39 +00001906static bool need_vtd_wa(struct drm_device *dev)
1907{
1908#ifdef CONFIG_INTEL_IOMMU
1909 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1910 return true;
1911#endif
1912 return false;
1913}
1914
Chris Wilson127bd2a2010-07-23 23:32:05 +01001915int
Chris Wilson48b956c2010-09-14 12:50:34 +01001916intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001917 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001918 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001919{
Chris Wilsonce453d82011-02-21 14:43:56 +00001920 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001921 u32 alignment;
1922 int ret;
1923
Chris Wilson05394f32010-11-08 19:18:58 +00001924 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001925 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001926 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1927 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001928 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001929 alignment = 4 * 1024;
1930 else
1931 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001932 break;
1933 case I915_TILING_X:
1934 /* pin() will align the object as required by fence */
1935 alignment = 0;
1936 break;
1937 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001938 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001939 return -EINVAL;
1940 default:
1941 BUG();
1942 }
1943
Chris Wilson693db182013-03-05 14:52:39 +00001944 /* Note that the w/a also requires 64 PTE of padding following the
1945 * bo. We currently fill all unused PTE with the shadow page and so
1946 * we should always have valid PTE following the scanout preventing
1947 * the VT-d warning.
1948 */
1949 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1950 alignment = 256 * 1024;
1951
Chris Wilsonce453d82011-02-21 14:43:56 +00001952 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001953 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001954 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001955 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001956
1957 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1958 * fence, whereas 965+ only requires a fence if using
1959 * framebuffer compression. For simplicity, we always install
1960 * a fence as the cost is not that onerous.
1961 */
Chris Wilson06d98132012-04-17 15:31:24 +01001962 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001963 if (ret)
1964 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001965
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001966 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001967
Chris Wilsonce453d82011-02-21 14:43:56 +00001968 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001969 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001970
1971err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001972 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001973err_interruptible:
1974 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001975 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001976}
1977
Chris Wilson1690e1e2011-12-14 13:57:08 +01001978void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1979{
1980 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001981 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001982}
1983
Daniel Vetterc2c75132012-07-05 12:17:30 +02001984/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1985 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001986unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1987 unsigned int tiling_mode,
1988 unsigned int cpp,
1989 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001990{
Chris Wilsonbc752862013-02-21 20:04:31 +00001991 if (tiling_mode != I915_TILING_NONE) {
1992 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001993
Chris Wilsonbc752862013-02-21 20:04:31 +00001994 tile_rows = *y / 8;
1995 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001996
Chris Wilsonbc752862013-02-21 20:04:31 +00001997 tiles = *x / (512/cpp);
1998 *x %= 512/cpp;
1999
2000 return tile_rows * pitch * 8 + tiles * 4096;
2001 } else {
2002 unsigned int offset;
2003
2004 offset = *y * pitch + *x * cpp;
2005 *y = 0;
2006 *x = (offset & 4095) / cpp;
2007 return offset & -4096;
2008 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002009}
2010
Jesse Barnes17638cd2011-06-24 12:19:23 -07002011static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2012 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002013{
2014 struct drm_device *dev = crtc->dev;
2015 struct drm_i915_private *dev_priv = dev->dev_private;
2016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2017 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002018 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002019 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002020 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002021 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002022 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002023
2024 switch (plane) {
2025 case 0:
2026 case 1:
2027 break;
2028 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002029 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002030 return -EINVAL;
2031 }
2032
2033 intel_fb = to_intel_framebuffer(fb);
2034 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002035
Chris Wilson5eddb702010-09-11 13:48:45 +01002036 reg = DSPCNTR(plane);
2037 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002038 /* Mask out pixel format bits in case we change it */
2039 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002040 switch (fb->pixel_format) {
2041 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002042 dspcntr |= DISPPLANE_8BPP;
2043 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002044 case DRM_FORMAT_XRGB1555:
2045 case DRM_FORMAT_ARGB1555:
2046 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002047 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002048 case DRM_FORMAT_RGB565:
2049 dspcntr |= DISPPLANE_BGRX565;
2050 break;
2051 case DRM_FORMAT_XRGB8888:
2052 case DRM_FORMAT_ARGB8888:
2053 dspcntr |= DISPPLANE_BGRX888;
2054 break;
2055 case DRM_FORMAT_XBGR8888:
2056 case DRM_FORMAT_ABGR8888:
2057 dspcntr |= DISPPLANE_RGBX888;
2058 break;
2059 case DRM_FORMAT_XRGB2101010:
2060 case DRM_FORMAT_ARGB2101010:
2061 dspcntr |= DISPPLANE_BGRX101010;
2062 break;
2063 case DRM_FORMAT_XBGR2101010:
2064 case DRM_FORMAT_ABGR2101010:
2065 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002066 break;
2067 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002068 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002069 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002070
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002071 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002072 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002073 dspcntr |= DISPPLANE_TILED;
2074 else
2075 dspcntr &= ~DISPPLANE_TILED;
2076 }
2077
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002078 if (IS_G4X(dev))
2079 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2080
Chris Wilson5eddb702010-09-11 13:48:45 +01002081 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002082
Daniel Vettere506a0c2012-07-05 12:17:29 +02002083 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002084
Daniel Vetterc2c75132012-07-05 12:17:30 +02002085 if (INTEL_INFO(dev)->gen >= 4) {
2086 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002087 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2088 fb->bits_per_pixel / 8,
2089 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002090 linear_offset -= intel_crtc->dspaddr_offset;
2091 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002092 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002093 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002094
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002095 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2096 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2097 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002098 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002099 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002100 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002101 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002102 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002103 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002104 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002105 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002106 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002107
Jesse Barnes17638cd2011-06-24 12:19:23 -07002108 return 0;
2109}
2110
2111static int ironlake_update_plane(struct drm_crtc *crtc,
2112 struct drm_framebuffer *fb, int x, int y)
2113{
2114 struct drm_device *dev = crtc->dev;
2115 struct drm_i915_private *dev_priv = dev->dev_private;
2116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2117 struct intel_framebuffer *intel_fb;
2118 struct drm_i915_gem_object *obj;
2119 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002120 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002121 u32 dspcntr;
2122 u32 reg;
2123
2124 switch (plane) {
2125 case 0:
2126 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002127 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002128 break;
2129 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002130 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002131 return -EINVAL;
2132 }
2133
2134 intel_fb = to_intel_framebuffer(fb);
2135 obj = intel_fb->obj;
2136
2137 reg = DSPCNTR(plane);
2138 dspcntr = I915_READ(reg);
2139 /* Mask out pixel format bits in case we change it */
2140 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002141 switch (fb->pixel_format) {
2142 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002143 dspcntr |= DISPPLANE_8BPP;
2144 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002145 case DRM_FORMAT_RGB565:
2146 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002147 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002148 case DRM_FORMAT_XRGB8888:
2149 case DRM_FORMAT_ARGB8888:
2150 dspcntr |= DISPPLANE_BGRX888;
2151 break;
2152 case DRM_FORMAT_XBGR8888:
2153 case DRM_FORMAT_ABGR8888:
2154 dspcntr |= DISPPLANE_RGBX888;
2155 break;
2156 case DRM_FORMAT_XRGB2101010:
2157 case DRM_FORMAT_ARGB2101010:
2158 dspcntr |= DISPPLANE_BGRX101010;
2159 break;
2160 case DRM_FORMAT_XBGR2101010:
2161 case DRM_FORMAT_ABGR2101010:
2162 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002163 break;
2164 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002165 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002166 }
2167
2168 if (obj->tiling_mode != I915_TILING_NONE)
2169 dspcntr |= DISPPLANE_TILED;
2170 else
2171 dspcntr &= ~DISPPLANE_TILED;
2172
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002173 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002174 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2175 else
2176 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002177
2178 I915_WRITE(reg, dspcntr);
2179
Daniel Vettere506a0c2012-07-05 12:17:29 +02002180 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002181 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002182 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2183 fb->bits_per_pixel / 8,
2184 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002185 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002186
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002187 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2188 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2189 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002190 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002191 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002192 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002193 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002194 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2195 } else {
2196 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2197 I915_WRITE(DSPLINOFF(plane), linear_offset);
2198 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002199 POSTING_READ(reg);
2200
2201 return 0;
2202}
2203
2204/* Assume fb object is pinned & idle & fenced and just update base pointers */
2205static int
2206intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2207 int x, int y, enum mode_set_atomic state)
2208{
2209 struct drm_device *dev = crtc->dev;
2210 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002211
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002212 if (dev_priv->display.disable_fbc)
2213 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002214 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002215
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002216 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002217}
2218
Ville Syrjälä96a02912013-02-18 19:08:49 +02002219void intel_display_handle_reset(struct drm_device *dev)
2220{
2221 struct drm_i915_private *dev_priv = dev->dev_private;
2222 struct drm_crtc *crtc;
2223
2224 /*
2225 * Flips in the rings have been nuked by the reset,
2226 * so complete all pending flips so that user space
2227 * will get its events and not get stuck.
2228 *
2229 * Also update the base address of all primary
2230 * planes to the the last fb to make sure we're
2231 * showing the correct fb after a reset.
2232 *
2233 * Need to make two loops over the crtcs so that we
2234 * don't try to grab a crtc mutex before the
2235 * pending_flip_queue really got woken up.
2236 */
2237
2238 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2240 enum plane plane = intel_crtc->plane;
2241
2242 intel_prepare_page_flip(dev, plane);
2243 intel_finish_page_flip_plane(dev, plane);
2244 }
2245
2246 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2248
2249 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002250 /*
2251 * FIXME: Once we have proper support for primary planes (and
2252 * disabling them without disabling the entire crtc) allow again
2253 * a NULL crtc->fb.
2254 */
2255 if (intel_crtc->active && crtc->fb)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002256 dev_priv->display.update_plane(crtc, crtc->fb,
2257 crtc->x, crtc->y);
2258 mutex_unlock(&crtc->mutex);
2259 }
2260}
2261
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002262static int
Chris Wilson14667a42012-04-03 17:58:35 +01002263intel_finish_fb(struct drm_framebuffer *old_fb)
2264{
2265 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2266 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2267 bool was_interruptible = dev_priv->mm.interruptible;
2268 int ret;
2269
Chris Wilson14667a42012-04-03 17:58:35 +01002270 /* Big Hammer, we also need to ensure that any pending
2271 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2272 * current scanout is retired before unpinning the old
2273 * framebuffer.
2274 *
2275 * This should only fail upon a hung GPU, in which case we
2276 * can safely continue.
2277 */
2278 dev_priv->mm.interruptible = false;
2279 ret = i915_gem_object_finish_gpu(obj);
2280 dev_priv->mm.interruptible = was_interruptible;
2281
2282 return ret;
2283}
2284
Ville Syrjälä198598d2012-10-31 17:50:24 +02002285static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2286{
2287 struct drm_device *dev = crtc->dev;
2288 struct drm_i915_master_private *master_priv;
2289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2290
2291 if (!dev->primary->master)
2292 return;
2293
2294 master_priv = dev->primary->master->driver_priv;
2295 if (!master_priv->sarea_priv)
2296 return;
2297
2298 switch (intel_crtc->pipe) {
2299 case 0:
2300 master_priv->sarea_priv->pipeA_x = x;
2301 master_priv->sarea_priv->pipeA_y = y;
2302 break;
2303 case 1:
2304 master_priv->sarea_priv->pipeB_x = x;
2305 master_priv->sarea_priv->pipeB_y = y;
2306 break;
2307 default:
2308 break;
2309 }
2310}
2311
Chris Wilson14667a42012-04-03 17:58:35 +01002312static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002313intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002314 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002315{
2316 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002317 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002319 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002320 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002321
2322 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002323 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002324 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002325 return 0;
2326 }
2327
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002328 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002329 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2330 plane_name(intel_crtc->plane),
2331 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002332 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002333 }
2334
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002335 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002336 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002337 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002338 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002339 if (ret != 0) {
2340 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002341 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002342 return ret;
2343 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002344
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002345 /*
2346 * Update pipe size and adjust fitter if needed: the reason for this is
2347 * that in compute_mode_changes we check the native mode (not the pfit
2348 * mode) to see if we can flip rather than do a full mode set. In the
2349 * fastboot case, we'll flip, but if we don't update the pipesrc and
2350 * pfit state, we'll end up with a big fb scanned out into the wrong
2351 * sized surface.
2352 *
2353 * To fix this properly, we need to hoist the checks up into
2354 * compute_mode_changes (or above), check the actual pfit state and
2355 * whether the platform allows pfit disable with pipe active, and only
2356 * then update the pipesrc and pfit state, even on the flip path.
2357 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002358 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002359 const struct drm_display_mode *adjusted_mode =
2360 &intel_crtc->config.adjusted_mode;
2361
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002362 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002363 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2364 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002365 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002366 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2367 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2368 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2369 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2370 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2371 }
2372 }
2373
Daniel Vetter94352cf2012-07-05 22:51:56 +02002374 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002375 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002376 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002377 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002378 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002379 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002380 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002381
Daniel Vetter94352cf2012-07-05 22:51:56 +02002382 old_fb = crtc->fb;
2383 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002384 crtc->x = x;
2385 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002386
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002387 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002388 if (intel_crtc->active && old_fb != fb)
2389 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002390 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002391 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002392
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002393 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002394 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002395 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002396
Ville Syrjälä198598d2012-10-31 17:50:24 +02002397 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002398
2399 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002400}
2401
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002402static void intel_fdi_normal_train(struct drm_crtc *crtc)
2403{
2404 struct drm_device *dev = crtc->dev;
2405 struct drm_i915_private *dev_priv = dev->dev_private;
2406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2407 int pipe = intel_crtc->pipe;
2408 u32 reg, temp;
2409
2410 /* enable normal train */
2411 reg = FDI_TX_CTL(pipe);
2412 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002413 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002414 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2415 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002416 } else {
2417 temp &= ~FDI_LINK_TRAIN_NONE;
2418 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002419 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002420 I915_WRITE(reg, temp);
2421
2422 reg = FDI_RX_CTL(pipe);
2423 temp = I915_READ(reg);
2424 if (HAS_PCH_CPT(dev)) {
2425 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2426 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2427 } else {
2428 temp &= ~FDI_LINK_TRAIN_NONE;
2429 temp |= FDI_LINK_TRAIN_NONE;
2430 }
2431 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2432
2433 /* wait one idle pattern time */
2434 POSTING_READ(reg);
2435 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002436
2437 /* IVB wants error correction enabled */
2438 if (IS_IVYBRIDGE(dev))
2439 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2440 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002441}
2442
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002443static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002444{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002445 return crtc->base.enabled && crtc->active &&
2446 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002447}
2448
Daniel Vetter01a415f2012-10-27 15:58:40 +02002449static void ivb_modeset_global_resources(struct drm_device *dev)
2450{
2451 struct drm_i915_private *dev_priv = dev->dev_private;
2452 struct intel_crtc *pipe_B_crtc =
2453 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2454 struct intel_crtc *pipe_C_crtc =
2455 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2456 uint32_t temp;
2457
Daniel Vetter1e833f42013-02-19 22:31:57 +01002458 /*
2459 * When everything is off disable fdi C so that we could enable fdi B
2460 * with all lanes. Note that we don't care about enabled pipes without
2461 * an enabled pch encoder.
2462 */
2463 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2464 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002465 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2466 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2467
2468 temp = I915_READ(SOUTH_CHICKEN1);
2469 temp &= ~FDI_BC_BIFURCATION_SELECT;
2470 DRM_DEBUG_KMS("disabling fdi C rx\n");
2471 I915_WRITE(SOUTH_CHICKEN1, temp);
2472 }
2473}
2474
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002475/* The FDI link training functions for ILK/Ibexpeak. */
2476static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2477{
2478 struct drm_device *dev = crtc->dev;
2479 struct drm_i915_private *dev_priv = dev->dev_private;
2480 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2481 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002482 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002483 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002484
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002485 /* FDI needs bits from pipe & plane first */
2486 assert_pipe_enabled(dev_priv, pipe);
2487 assert_plane_enabled(dev_priv, plane);
2488
Adam Jacksone1a44742010-06-25 15:32:14 -04002489 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2490 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 reg = FDI_RX_IMR(pipe);
2492 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002493 temp &= ~FDI_RX_SYMBOL_LOCK;
2494 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002495 I915_WRITE(reg, temp);
2496 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002497 udelay(150);
2498
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002499 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 reg = FDI_TX_CTL(pipe);
2501 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002502 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2503 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504 temp &= ~FDI_LINK_TRAIN_NONE;
2505 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002506 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002507
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 reg = FDI_RX_CTL(pipe);
2509 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002510 temp &= ~FDI_LINK_TRAIN_NONE;
2511 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2513
2514 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002515 udelay(150);
2516
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002517 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002518 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2519 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2520 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002521
Chris Wilson5eddb702010-09-11 13:48:45 +01002522 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002523 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002524 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2526
2527 if ((temp & FDI_RX_BIT_LOCK)) {
2528 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002529 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 break;
2531 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002532 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002533 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002534 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002535
2536 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002537 reg = FDI_TX_CTL(pipe);
2538 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539 temp &= ~FDI_LINK_TRAIN_NONE;
2540 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002542
Chris Wilson5eddb702010-09-11 13:48:45 +01002543 reg = FDI_RX_CTL(pipe);
2544 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002545 temp &= ~FDI_LINK_TRAIN_NONE;
2546 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002547 I915_WRITE(reg, temp);
2548
2549 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002550 udelay(150);
2551
Chris Wilson5eddb702010-09-11 13:48:45 +01002552 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002553 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002554 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002555 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2556
2557 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002558 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559 DRM_DEBUG_KMS("FDI train 2 done.\n");
2560 break;
2561 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002563 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002564 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002565
2566 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002567
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002568}
2569
Akshay Joshi0206e352011-08-16 15:34:10 -04002570static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2572 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2573 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2574 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2575};
2576
2577/* The FDI link training functions for SNB/Cougarpoint. */
2578static void gen6_fdi_link_train(struct drm_crtc *crtc)
2579{
2580 struct drm_device *dev = crtc->dev;
2581 struct drm_i915_private *dev_priv = dev->dev_private;
2582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2583 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002584 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002585
Adam Jacksone1a44742010-06-25 15:32:14 -04002586 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2587 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002588 reg = FDI_RX_IMR(pipe);
2589 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002590 temp &= ~FDI_RX_SYMBOL_LOCK;
2591 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 I915_WRITE(reg, temp);
2593
2594 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002595 udelay(150);
2596
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002598 reg = FDI_TX_CTL(pipe);
2599 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002600 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2601 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002602 temp &= ~FDI_LINK_TRAIN_NONE;
2603 temp |= FDI_LINK_TRAIN_PATTERN_1;
2604 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2605 /* SNB-B */
2606 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002607 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002608
Daniel Vetterd74cf322012-10-26 10:58:13 +02002609 I915_WRITE(FDI_RX_MISC(pipe),
2610 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2611
Chris Wilson5eddb702010-09-11 13:48:45 +01002612 reg = FDI_RX_CTL(pipe);
2613 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002614 if (HAS_PCH_CPT(dev)) {
2615 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2616 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2617 } else {
2618 temp &= ~FDI_LINK_TRAIN_NONE;
2619 temp |= FDI_LINK_TRAIN_PATTERN_1;
2620 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002621 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2622
2623 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002624 udelay(150);
2625
Akshay Joshi0206e352011-08-16 15:34:10 -04002626 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002627 reg = FDI_TX_CTL(pipe);
2628 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002629 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2630 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002631 I915_WRITE(reg, temp);
2632
2633 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002634 udelay(500);
2635
Sean Paulfa37d392012-03-02 12:53:39 -05002636 for (retry = 0; retry < 5; retry++) {
2637 reg = FDI_RX_IIR(pipe);
2638 temp = I915_READ(reg);
2639 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2640 if (temp & FDI_RX_BIT_LOCK) {
2641 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2642 DRM_DEBUG_KMS("FDI train 1 done.\n");
2643 break;
2644 }
2645 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002646 }
Sean Paulfa37d392012-03-02 12:53:39 -05002647 if (retry < 5)
2648 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002649 }
2650 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002651 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002652
2653 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002654 reg = FDI_TX_CTL(pipe);
2655 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002656 temp &= ~FDI_LINK_TRAIN_NONE;
2657 temp |= FDI_LINK_TRAIN_PATTERN_2;
2658 if (IS_GEN6(dev)) {
2659 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2660 /* SNB-B */
2661 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2662 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002663 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002664
Chris Wilson5eddb702010-09-11 13:48:45 +01002665 reg = FDI_RX_CTL(pipe);
2666 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002667 if (HAS_PCH_CPT(dev)) {
2668 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2669 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2670 } else {
2671 temp &= ~FDI_LINK_TRAIN_NONE;
2672 temp |= FDI_LINK_TRAIN_PATTERN_2;
2673 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002674 I915_WRITE(reg, temp);
2675
2676 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002677 udelay(150);
2678
Akshay Joshi0206e352011-08-16 15:34:10 -04002679 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002680 reg = FDI_TX_CTL(pipe);
2681 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002682 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2683 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002684 I915_WRITE(reg, temp);
2685
2686 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002687 udelay(500);
2688
Sean Paulfa37d392012-03-02 12:53:39 -05002689 for (retry = 0; retry < 5; retry++) {
2690 reg = FDI_RX_IIR(pipe);
2691 temp = I915_READ(reg);
2692 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2693 if (temp & FDI_RX_SYMBOL_LOCK) {
2694 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2695 DRM_DEBUG_KMS("FDI train 2 done.\n");
2696 break;
2697 }
2698 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002699 }
Sean Paulfa37d392012-03-02 12:53:39 -05002700 if (retry < 5)
2701 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002702 }
2703 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002704 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002705
2706 DRM_DEBUG_KMS("FDI train done.\n");
2707}
2708
Jesse Barnes357555c2011-04-28 15:09:55 -07002709/* Manual link training for Ivy Bridge A0 parts */
2710static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2711{
2712 struct drm_device *dev = crtc->dev;
2713 struct drm_i915_private *dev_priv = dev->dev_private;
2714 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2715 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002716 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002717
2718 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2719 for train result */
2720 reg = FDI_RX_IMR(pipe);
2721 temp = I915_READ(reg);
2722 temp &= ~FDI_RX_SYMBOL_LOCK;
2723 temp &= ~FDI_RX_BIT_LOCK;
2724 I915_WRITE(reg, temp);
2725
2726 POSTING_READ(reg);
2727 udelay(150);
2728
Daniel Vetter01a415f2012-10-27 15:58:40 +02002729 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2730 I915_READ(FDI_RX_IIR(pipe)));
2731
Jesse Barnes139ccd32013-08-19 11:04:55 -07002732 /* Try each vswing and preemphasis setting twice before moving on */
2733 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2734 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002735 reg = FDI_TX_CTL(pipe);
2736 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002737 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2738 temp &= ~FDI_TX_ENABLE;
2739 I915_WRITE(reg, temp);
2740
2741 reg = FDI_RX_CTL(pipe);
2742 temp = I915_READ(reg);
2743 temp &= ~FDI_LINK_TRAIN_AUTO;
2744 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2745 temp &= ~FDI_RX_ENABLE;
2746 I915_WRITE(reg, temp);
2747
2748 /* enable CPU FDI TX and PCH FDI RX */
2749 reg = FDI_TX_CTL(pipe);
2750 temp = I915_READ(reg);
2751 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2752 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2753 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002754 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002755 temp |= snb_b_fdi_train_param[j/2];
2756 temp |= FDI_COMPOSITE_SYNC;
2757 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2758
2759 I915_WRITE(FDI_RX_MISC(pipe),
2760 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2761
2762 reg = FDI_RX_CTL(pipe);
2763 temp = I915_READ(reg);
2764 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2765 temp |= FDI_COMPOSITE_SYNC;
2766 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2767
2768 POSTING_READ(reg);
2769 udelay(1); /* should be 0.5us */
2770
2771 for (i = 0; i < 4; i++) {
2772 reg = FDI_RX_IIR(pipe);
2773 temp = I915_READ(reg);
2774 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2775
2776 if (temp & FDI_RX_BIT_LOCK ||
2777 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2778 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2779 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2780 i);
2781 break;
2782 }
2783 udelay(1); /* should be 0.5us */
2784 }
2785 if (i == 4) {
2786 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2787 continue;
2788 }
2789
2790 /* Train 2 */
2791 reg = FDI_TX_CTL(pipe);
2792 temp = I915_READ(reg);
2793 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2794 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2795 I915_WRITE(reg, temp);
2796
2797 reg = FDI_RX_CTL(pipe);
2798 temp = I915_READ(reg);
2799 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2800 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002801 I915_WRITE(reg, temp);
2802
2803 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002804 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002805
Jesse Barnes139ccd32013-08-19 11:04:55 -07002806 for (i = 0; i < 4; i++) {
2807 reg = FDI_RX_IIR(pipe);
2808 temp = I915_READ(reg);
2809 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002810
Jesse Barnes139ccd32013-08-19 11:04:55 -07002811 if (temp & FDI_RX_SYMBOL_LOCK ||
2812 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2813 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2814 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2815 i);
2816 goto train_done;
2817 }
2818 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002819 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002820 if (i == 4)
2821 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002822 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002823
Jesse Barnes139ccd32013-08-19 11:04:55 -07002824train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002825 DRM_DEBUG_KMS("FDI train done.\n");
2826}
2827
Daniel Vetter88cefb62012-08-12 19:27:14 +02002828static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002829{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002830 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002831 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002832 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002833 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002834
Jesse Barnesc64e3112010-09-10 11:27:03 -07002835
Jesse Barnes0e23b992010-09-10 11:10:00 -07002836 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002837 reg = FDI_RX_CTL(pipe);
2838 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002839 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2840 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002841 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002842 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2843
2844 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002845 udelay(200);
2846
2847 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002848 temp = I915_READ(reg);
2849 I915_WRITE(reg, temp | FDI_PCDCLK);
2850
2851 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002852 udelay(200);
2853
Paulo Zanoni20749732012-11-23 15:30:38 -02002854 /* Enable CPU FDI TX PLL, always on for Ironlake */
2855 reg = FDI_TX_CTL(pipe);
2856 temp = I915_READ(reg);
2857 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2858 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002859
Paulo Zanoni20749732012-11-23 15:30:38 -02002860 POSTING_READ(reg);
2861 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002862 }
2863}
2864
Daniel Vetter88cefb62012-08-12 19:27:14 +02002865static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2866{
2867 struct drm_device *dev = intel_crtc->base.dev;
2868 struct drm_i915_private *dev_priv = dev->dev_private;
2869 int pipe = intel_crtc->pipe;
2870 u32 reg, temp;
2871
2872 /* Switch from PCDclk to Rawclk */
2873 reg = FDI_RX_CTL(pipe);
2874 temp = I915_READ(reg);
2875 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2876
2877 /* Disable CPU FDI TX PLL */
2878 reg = FDI_TX_CTL(pipe);
2879 temp = I915_READ(reg);
2880 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2881
2882 POSTING_READ(reg);
2883 udelay(100);
2884
2885 reg = FDI_RX_CTL(pipe);
2886 temp = I915_READ(reg);
2887 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2888
2889 /* Wait for the clocks to turn off. */
2890 POSTING_READ(reg);
2891 udelay(100);
2892}
2893
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002894static void ironlake_fdi_disable(struct drm_crtc *crtc)
2895{
2896 struct drm_device *dev = crtc->dev;
2897 struct drm_i915_private *dev_priv = dev->dev_private;
2898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2899 int pipe = intel_crtc->pipe;
2900 u32 reg, temp;
2901
2902 /* disable CPU FDI tx and PCH FDI rx */
2903 reg = FDI_TX_CTL(pipe);
2904 temp = I915_READ(reg);
2905 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2906 POSTING_READ(reg);
2907
2908 reg = FDI_RX_CTL(pipe);
2909 temp = I915_READ(reg);
2910 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002911 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002912 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2913
2914 POSTING_READ(reg);
2915 udelay(100);
2916
2917 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002918 if (HAS_PCH_IBX(dev)) {
2919 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002920 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002921
2922 /* still set train pattern 1 */
2923 reg = FDI_TX_CTL(pipe);
2924 temp = I915_READ(reg);
2925 temp &= ~FDI_LINK_TRAIN_NONE;
2926 temp |= FDI_LINK_TRAIN_PATTERN_1;
2927 I915_WRITE(reg, temp);
2928
2929 reg = FDI_RX_CTL(pipe);
2930 temp = I915_READ(reg);
2931 if (HAS_PCH_CPT(dev)) {
2932 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2933 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2934 } else {
2935 temp &= ~FDI_LINK_TRAIN_NONE;
2936 temp |= FDI_LINK_TRAIN_PATTERN_1;
2937 }
2938 /* BPC in FDI rx is consistent with that in PIPECONF */
2939 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002940 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002941 I915_WRITE(reg, temp);
2942
2943 POSTING_READ(reg);
2944 udelay(100);
2945}
2946
Chris Wilson5bb61642012-09-27 21:25:58 +01002947static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2948{
2949 struct drm_device *dev = crtc->dev;
2950 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002952 unsigned long flags;
2953 bool pending;
2954
Ville Syrjälä10d83732013-01-29 18:13:34 +02002955 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2956 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002957 return false;
2958
2959 spin_lock_irqsave(&dev->event_lock, flags);
2960 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2961 spin_unlock_irqrestore(&dev->event_lock, flags);
2962
2963 return pending;
2964}
2965
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002966static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2967{
Chris Wilson0f911282012-04-17 10:05:38 +01002968 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002969 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002970
2971 if (crtc->fb == NULL)
2972 return;
2973
Daniel Vetter2c10d572012-12-20 21:24:07 +01002974 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2975
Chris Wilson5bb61642012-09-27 21:25:58 +01002976 wait_event(dev_priv->pending_flip_queue,
2977 !intel_crtc_has_pending_flip(crtc));
2978
Chris Wilson0f911282012-04-17 10:05:38 +01002979 mutex_lock(&dev->struct_mutex);
2980 intel_finish_fb(crtc->fb);
2981 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002982}
2983
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002984/* Program iCLKIP clock to the desired frequency */
2985static void lpt_program_iclkip(struct drm_crtc *crtc)
2986{
2987 struct drm_device *dev = crtc->dev;
2988 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002989 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002990 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2991 u32 temp;
2992
Daniel Vetter09153002012-12-12 14:06:44 +01002993 mutex_lock(&dev_priv->dpio_lock);
2994
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002995 /* It is necessary to ungate the pixclk gate prior to programming
2996 * the divisors, and gate it back when it is done.
2997 */
2998 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2999
3000 /* Disable SSCCTL */
3001 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003002 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3003 SBI_SSCCTL_DISABLE,
3004 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003005
3006 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003007 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003008 auxdiv = 1;
3009 divsel = 0x41;
3010 phaseinc = 0x20;
3011 } else {
3012 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003013 * but the adjusted_mode->crtc_clock in in KHz. To get the
3014 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003015 * convert the virtual clock precision to KHz here for higher
3016 * precision.
3017 */
3018 u32 iclk_virtual_root_freq = 172800 * 1000;
3019 u32 iclk_pi_range = 64;
3020 u32 desired_divisor, msb_divisor_value, pi_value;
3021
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003022 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003023 msb_divisor_value = desired_divisor / iclk_pi_range;
3024 pi_value = desired_divisor % iclk_pi_range;
3025
3026 auxdiv = 0;
3027 divsel = msb_divisor_value - 2;
3028 phaseinc = pi_value;
3029 }
3030
3031 /* This should not happen with any sane values */
3032 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3033 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3034 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3035 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3036
3037 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 +03003038 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003039 auxdiv,
3040 divsel,
3041 phasedir,
3042 phaseinc);
3043
3044 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003045 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003046 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3047 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3048 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3049 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3050 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3051 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003052 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003053
3054 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003055 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003056 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3057 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003058 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003059
3060 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003061 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003062 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003063 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003064
3065 /* Wait for initialization time */
3066 udelay(24);
3067
3068 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003069
3070 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003071}
3072
Daniel Vetter275f01b22013-05-03 11:49:47 +02003073static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3074 enum pipe pch_transcoder)
3075{
3076 struct drm_device *dev = crtc->base.dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3079
3080 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3081 I915_READ(HTOTAL(cpu_transcoder)));
3082 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3083 I915_READ(HBLANK(cpu_transcoder)));
3084 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3085 I915_READ(HSYNC(cpu_transcoder)));
3086
3087 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3088 I915_READ(VTOTAL(cpu_transcoder)));
3089 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3090 I915_READ(VBLANK(cpu_transcoder)));
3091 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3092 I915_READ(VSYNC(cpu_transcoder)));
3093 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3094 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3095}
3096
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003097static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3098{
3099 struct drm_i915_private *dev_priv = dev->dev_private;
3100 uint32_t temp;
3101
3102 temp = I915_READ(SOUTH_CHICKEN1);
3103 if (temp & FDI_BC_BIFURCATION_SELECT)
3104 return;
3105
3106 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3107 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3108
3109 temp |= FDI_BC_BIFURCATION_SELECT;
3110 DRM_DEBUG_KMS("enabling fdi C rx\n");
3111 I915_WRITE(SOUTH_CHICKEN1, temp);
3112 POSTING_READ(SOUTH_CHICKEN1);
3113}
3114
3115static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3116{
3117 struct drm_device *dev = intel_crtc->base.dev;
3118 struct drm_i915_private *dev_priv = dev->dev_private;
3119
3120 switch (intel_crtc->pipe) {
3121 case PIPE_A:
3122 break;
3123 case PIPE_B:
3124 if (intel_crtc->config.fdi_lanes > 2)
3125 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3126 else
3127 cpt_enable_fdi_bc_bifurcation(dev);
3128
3129 break;
3130 case PIPE_C:
3131 cpt_enable_fdi_bc_bifurcation(dev);
3132
3133 break;
3134 default:
3135 BUG();
3136 }
3137}
3138
Jesse Barnesf67a5592011-01-05 10:31:48 -08003139/*
3140 * Enable PCH resources required for PCH ports:
3141 * - PCH PLLs
3142 * - FDI training & RX/TX
3143 * - update transcoder timings
3144 * - DP transcoding bits
3145 * - transcoder
3146 */
3147static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003148{
3149 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003150 struct drm_i915_private *dev_priv = dev->dev_private;
3151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3152 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003153 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003154
Daniel Vetterab9412b2013-05-03 11:49:46 +02003155 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003156
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003157 if (IS_IVYBRIDGE(dev))
3158 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3159
Daniel Vettercd986ab2012-10-26 10:58:12 +02003160 /* Write the TU size bits before fdi link training, so that error
3161 * detection works. */
3162 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3163 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3164
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003165 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003166 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003167
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003168 /* We need to program the right clock selection before writing the pixel
3169 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003170 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003171 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003172
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003173 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003174 temp |= TRANS_DPLL_ENABLE(pipe);
3175 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003176 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003177 temp |= sel;
3178 else
3179 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003180 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003181 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003182
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003183 /* XXX: pch pll's can be enabled any time before we enable the PCH
3184 * transcoder, and we actually should do this to not upset any PCH
3185 * transcoder that already use the clock when we share it.
3186 *
3187 * Note that enable_shared_dpll tries to do the right thing, but
3188 * get_shared_dpll unconditionally resets the pll - we need that to have
3189 * the right LVDS enable sequence. */
3190 ironlake_enable_shared_dpll(intel_crtc);
3191
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003192 /* set transcoder timing, panel must allow it */
3193 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003194 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003195
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003196 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003197
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003198 /* For PCH DP, enable TRANS_DP_CTL */
3199 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003200 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3201 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003202 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003203 reg = TRANS_DP_CTL(pipe);
3204 temp = I915_READ(reg);
3205 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003206 TRANS_DP_SYNC_MASK |
3207 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003208 temp |= (TRANS_DP_OUTPUT_ENABLE |
3209 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003210 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003211
3212 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003213 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003214 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003215 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003216
3217 switch (intel_trans_dp_port_sel(crtc)) {
3218 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003219 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003220 break;
3221 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003222 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003223 break;
3224 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003225 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003226 break;
3227 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003228 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003229 }
3230
Chris Wilson5eddb702010-09-11 13:48:45 +01003231 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003232 }
3233
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003234 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003235}
3236
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003237static void lpt_pch_enable(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
3240 struct drm_i915_private *dev_priv = dev->dev_private;
3241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003242 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003243
Daniel Vetterab9412b2013-05-03 11:49:46 +02003244 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003245
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003246 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003247
Paulo Zanoni0540e482012-10-31 18:12:40 -02003248 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003249 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003250
Paulo Zanoni937bb612012-10-31 18:12:47 -02003251 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003252}
3253
Daniel Vettere2b78262013-06-07 23:10:03 +02003254static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003255{
Daniel Vettere2b78262013-06-07 23:10:03 +02003256 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003257
3258 if (pll == NULL)
3259 return;
3260
3261 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003262 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003263 return;
3264 }
3265
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003266 if (--pll->refcount == 0) {
3267 WARN_ON(pll->on);
3268 WARN_ON(pll->active);
3269 }
3270
Daniel Vettera43f6e02013-06-07 23:10:32 +02003271 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003272}
3273
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003274static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003275{
Daniel Vettere2b78262013-06-07 23:10:03 +02003276 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3277 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3278 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003279
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003280 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003281 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3282 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003283 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003284 }
3285
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003286 if (HAS_PCH_IBX(dev_priv->dev)) {
3287 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003288 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003289 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003290
Daniel Vetter46edb022013-06-05 13:34:12 +02003291 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3292 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003293
3294 goto found;
3295 }
3296
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003297 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3298 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003299
3300 /* Only want to check enabled timings first */
3301 if (pll->refcount == 0)
3302 continue;
3303
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003304 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3305 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003306 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003307 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003308 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003309
3310 goto found;
3311 }
3312 }
3313
3314 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003315 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3316 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003317 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003318 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3319 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003320 goto found;
3321 }
3322 }
3323
3324 return NULL;
3325
3326found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003327 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003328 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3329 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003330
Daniel Vettercdbd2312013-06-05 13:34:03 +02003331 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003332 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3333 sizeof(pll->hw_state));
3334
Daniel Vetter46edb022013-06-05 13:34:12 +02003335 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003336 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003337 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003338
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003339 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003340 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003341 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003342
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003343 return pll;
3344}
3345
Daniel Vettera1520312013-05-03 11:49:50 +02003346static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003347{
3348 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003349 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003350 u32 temp;
3351
3352 temp = I915_READ(dslreg);
3353 udelay(500);
3354 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003355 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003356 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003357 }
3358}
3359
Jesse Barnesb074cec2013-04-25 12:55:02 -07003360static void ironlake_pfit_enable(struct intel_crtc *crtc)
3361{
3362 struct drm_device *dev = crtc->base.dev;
3363 struct drm_i915_private *dev_priv = dev->dev_private;
3364 int pipe = crtc->pipe;
3365
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003366 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003367 /* Force use of hard-coded filter coefficients
3368 * as some pre-programmed values are broken,
3369 * e.g. x201.
3370 */
3371 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3372 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3373 PF_PIPE_SEL_IVB(pipe));
3374 else
3375 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3376 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3377 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003378 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003379}
3380
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003381static void intel_enable_planes(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
3384 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3385 struct intel_plane *intel_plane;
3386
3387 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3388 if (intel_plane->pipe == pipe)
3389 intel_plane_restore(&intel_plane->base);
3390}
3391
3392static void intel_disable_planes(struct drm_crtc *crtc)
3393{
3394 struct drm_device *dev = crtc->dev;
3395 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3396 struct intel_plane *intel_plane;
3397
3398 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3399 if (intel_plane->pipe == pipe)
3400 intel_plane_disable(&intel_plane->base);
3401}
3402
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003403void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003404{
3405 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3406
3407 if (!crtc->config.ips_enabled)
3408 return;
3409
3410 /* We can only enable IPS after we enable a plane and wait for a vblank.
3411 * We guarantee that the plane is enabled by calling intel_enable_ips
3412 * only after intel_enable_plane. And intel_enable_plane already waits
3413 * for a vblank, so all we need to do here is to enable the IPS bit. */
3414 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003415 if (IS_BROADWELL(crtc->base.dev)) {
3416 mutex_lock(&dev_priv->rps.hw_lock);
3417 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3418 mutex_unlock(&dev_priv->rps.hw_lock);
3419 /* Quoting Art Runyan: "its not safe to expect any particular
3420 * value in IPS_CTL bit 31 after enabling IPS through the
3421 * mailbox." Therefore we need to defer waiting on the state
3422 * change.
3423 * TODO: need to fix this for state checker
3424 */
3425 } else {
3426 I915_WRITE(IPS_CTL, IPS_ENABLE);
3427 /* The bit only becomes 1 in the next vblank, so this wait here
3428 * is essentially intel_wait_for_vblank. If we don't have this
3429 * and don't wait for vblanks until the end of crtc_enable, then
3430 * the HW state readout code will complain that the expected
3431 * IPS_CTL value is not the one we read. */
3432 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3433 DRM_ERROR("Timed out waiting for IPS enable\n");
3434 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003435}
3436
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003437void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003438{
3439 struct drm_device *dev = crtc->base.dev;
3440 struct drm_i915_private *dev_priv = dev->dev_private;
3441
3442 if (!crtc->config.ips_enabled)
3443 return;
3444
3445 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003446 if (IS_BROADWELL(crtc->base.dev)) {
3447 mutex_lock(&dev_priv->rps.hw_lock);
3448 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3449 mutex_unlock(&dev_priv->rps.hw_lock);
3450 } else
3451 I915_WRITE(IPS_CTL, 0);
Paulo Zanonid77e4532013-09-24 13:52:55 -03003452 POSTING_READ(IPS_CTL);
3453
3454 /* We need to wait for a vblank before we can disable the plane. */
3455 intel_wait_for_vblank(dev, crtc->pipe);
3456}
3457
3458/** Loads the palette/gamma unit for the CRTC with the prepared values */
3459static void intel_crtc_load_lut(struct drm_crtc *crtc)
3460{
3461 struct drm_device *dev = crtc->dev;
3462 struct drm_i915_private *dev_priv = dev->dev_private;
3463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3464 enum pipe pipe = intel_crtc->pipe;
3465 int palreg = PALETTE(pipe);
3466 int i;
3467 bool reenable_ips = false;
3468
3469 /* The clocks have to be on to load the palette. */
3470 if (!crtc->enabled || !intel_crtc->active)
3471 return;
3472
3473 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3474 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3475 assert_dsi_pll_enabled(dev_priv);
3476 else
3477 assert_pll_enabled(dev_priv, pipe);
3478 }
3479
3480 /* use legacy palette for Ironlake */
3481 if (HAS_PCH_SPLIT(dev))
3482 palreg = LGC_PALETTE(pipe);
3483
3484 /* Workaround : Do not read or write the pipe palette/gamma data while
3485 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3486 */
3487 if (intel_crtc->config.ips_enabled &&
3488 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3489 GAMMA_MODE_MODE_SPLIT)) {
3490 hsw_disable_ips(intel_crtc);
3491 reenable_ips = true;
3492 }
3493
3494 for (i = 0; i < 256; i++) {
3495 I915_WRITE(palreg + 4 * i,
3496 (intel_crtc->lut_r[i] << 16) |
3497 (intel_crtc->lut_g[i] << 8) |
3498 intel_crtc->lut_b[i]);
3499 }
3500
3501 if (reenable_ips)
3502 hsw_enable_ips(intel_crtc);
3503}
3504
Jesse Barnesf67a5592011-01-05 10:31:48 -08003505static void ironlake_crtc_enable(struct drm_crtc *crtc)
3506{
3507 struct drm_device *dev = crtc->dev;
3508 struct drm_i915_private *dev_priv = dev->dev_private;
3509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003510 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003511 int pipe = intel_crtc->pipe;
3512 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003513
Daniel Vetter08a48462012-07-02 11:43:47 +02003514 WARN_ON(!crtc->enabled);
3515
Jesse Barnesf67a5592011-01-05 10:31:48 -08003516 if (intel_crtc->active)
3517 return;
3518
3519 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003520
3521 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3522 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3523
Daniel Vetterf6736a12013-06-05 13:34:30 +02003524 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003525 if (encoder->pre_enable)
3526 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003527
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003528 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003529 /* Note: FDI PLL enabling _must_ be done before we enable the
3530 * cpu pipes, hence this is separate from all the other fdi/pch
3531 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003532 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003533 } else {
3534 assert_fdi_tx_disabled(dev_priv, pipe);
3535 assert_fdi_rx_disabled(dev_priv, pipe);
3536 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003537
Jesse Barnesb074cec2013-04-25 12:55:02 -07003538 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003539
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003540 /*
3541 * On ILK+ LUT must be loaded before the pipe is running but with
3542 * clocks enabled
3543 */
3544 intel_crtc_load_lut(crtc);
3545
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003546 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003547 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003548 intel_crtc->config.has_pch_encoder, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003549 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003550 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003551 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003552
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003553 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003554 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003555
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003556 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003557 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003558 mutex_unlock(&dev->struct_mutex);
3559
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003560 for_each_encoder_on_crtc(dev, crtc, encoder)
3561 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003562
3563 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003564 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003565
3566 /*
3567 * There seems to be a race in PCH platform hw (at least on some
3568 * outputs) where an enabled pipe still completes any pageflip right
3569 * away (as if the pipe is off) instead of waiting for vblank. As soon
3570 * as the first vblank happend, everything works as expected. Hence just
3571 * wait for one vblank before returning to avoid strange things
3572 * happening.
3573 */
3574 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003575}
3576
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003577/* IPS only exists on ULT machines and is tied to pipe A. */
3578static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3579{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003580 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003581}
3582
Ville Syrjälädda9a662013-09-19 17:00:37 -03003583static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3584{
3585 struct drm_device *dev = crtc->dev;
3586 struct drm_i915_private *dev_priv = dev->dev_private;
3587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3588 int pipe = intel_crtc->pipe;
3589 int plane = intel_crtc->plane;
3590
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003591 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003592 intel_enable_planes(crtc);
3593 intel_crtc_update_cursor(crtc, true);
3594
3595 hsw_enable_ips(intel_crtc);
3596
3597 mutex_lock(&dev->struct_mutex);
3598 intel_update_fbc(dev);
3599 mutex_unlock(&dev->struct_mutex);
3600}
3601
3602static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3603{
3604 struct drm_device *dev = crtc->dev;
3605 struct drm_i915_private *dev_priv = dev->dev_private;
3606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3607 int pipe = intel_crtc->pipe;
3608 int plane = intel_crtc->plane;
3609
3610 intel_crtc_wait_for_pending_flips(crtc);
3611 drm_vblank_off(dev, pipe);
3612
3613 /* FBC must be disabled before disabling the plane on HSW. */
3614 if (dev_priv->fbc.plane == plane)
3615 intel_disable_fbc(dev);
3616
3617 hsw_disable_ips(intel_crtc);
3618
3619 intel_crtc_update_cursor(crtc, false);
3620 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003621 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003622}
3623
Paulo Zanonie4916942013-09-20 16:21:19 -03003624/*
3625 * This implements the workaround described in the "notes" section of the mode
3626 * set sequence documentation. When going from no pipes or single pipe to
3627 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3628 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3629 */
3630static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3631{
3632 struct drm_device *dev = crtc->base.dev;
3633 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3634
3635 /* We want to get the other_active_crtc only if there's only 1 other
3636 * active crtc. */
3637 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3638 if (!crtc_it->active || crtc_it == crtc)
3639 continue;
3640
3641 if (other_active_crtc)
3642 return;
3643
3644 other_active_crtc = crtc_it;
3645 }
3646 if (!other_active_crtc)
3647 return;
3648
3649 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3650 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3651}
3652
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003653static void haswell_crtc_enable(struct drm_crtc *crtc)
3654{
3655 struct drm_device *dev = crtc->dev;
3656 struct drm_i915_private *dev_priv = dev->dev_private;
3657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3658 struct intel_encoder *encoder;
3659 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003660
3661 WARN_ON(!crtc->enabled);
3662
3663 if (intel_crtc->active)
3664 return;
3665
3666 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003667
3668 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3669 if (intel_crtc->config.has_pch_encoder)
3670 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3671
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003672 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003673 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003674
3675 for_each_encoder_on_crtc(dev, crtc, encoder)
3676 if (encoder->pre_enable)
3677 encoder->pre_enable(encoder);
3678
Paulo Zanoni1f544382012-10-24 11:32:00 -02003679 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003680
Jesse Barnesb074cec2013-04-25 12:55:02 -07003681 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003682
3683 /*
3684 * On ILK+ LUT must be loaded before the pipe is running but with
3685 * clocks enabled
3686 */
3687 intel_crtc_load_lut(crtc);
3688
Paulo Zanoni1f544382012-10-24 11:32:00 -02003689 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003690 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003691
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003692 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003693 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003694 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003695
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003696 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003697 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003698
Jani Nikula8807e552013-08-30 19:40:32 +03003699 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003700 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003701 intel_opregion_notify_encoder(encoder, true);
3702 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003703
Paulo Zanonie4916942013-09-20 16:21:19 -03003704 /* If we change the relative order between pipe/planes enabling, we need
3705 * to change the workaround. */
3706 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003707 haswell_crtc_enable_planes(crtc);
3708
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003709 /*
3710 * There seems to be a race in PCH platform hw (at least on some
3711 * outputs) where an enabled pipe still completes any pageflip right
3712 * away (as if the pipe is off) instead of waiting for vblank. As soon
3713 * as the first vblank happend, everything works as expected. Hence just
3714 * wait for one vblank before returning to avoid strange things
3715 * happening.
3716 */
3717 intel_wait_for_vblank(dev, intel_crtc->pipe);
3718}
3719
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003720static void ironlake_pfit_disable(struct intel_crtc *crtc)
3721{
3722 struct drm_device *dev = crtc->base.dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724 int pipe = crtc->pipe;
3725
3726 /* To avoid upsetting the power well on haswell only disable the pfit if
3727 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003728 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003729 I915_WRITE(PF_CTL(pipe), 0);
3730 I915_WRITE(PF_WIN_POS(pipe), 0);
3731 I915_WRITE(PF_WIN_SZ(pipe), 0);
3732 }
3733}
3734
Jesse Barnes6be4a602010-09-10 10:26:01 -07003735static void ironlake_crtc_disable(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003740 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003741 int pipe = intel_crtc->pipe;
3742 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003743 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003744
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003745
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003746 if (!intel_crtc->active)
3747 return;
3748
Daniel Vetterea9d7582012-07-10 10:42:52 +02003749 for_each_encoder_on_crtc(dev, crtc, encoder)
3750 encoder->disable(encoder);
3751
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003752 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003753 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003754
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003755 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003756 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003757
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003758 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003759 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003760 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003761
Daniel Vetterd925c592013-06-05 13:34:04 +02003762 if (intel_crtc->config.has_pch_encoder)
3763 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3764
Jesse Barnesb24e7172011-01-04 15:09:30 -08003765 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003766
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003767 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003768
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003769 for_each_encoder_on_crtc(dev, crtc, encoder)
3770 if (encoder->post_disable)
3771 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003772
Daniel Vetterd925c592013-06-05 13:34:04 +02003773 if (intel_crtc->config.has_pch_encoder) {
3774 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003775
Daniel Vetterd925c592013-06-05 13:34:04 +02003776 ironlake_disable_pch_transcoder(dev_priv, pipe);
3777 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003778
Daniel Vetterd925c592013-06-05 13:34:04 +02003779 if (HAS_PCH_CPT(dev)) {
3780 /* disable TRANS_DP_CTL */
3781 reg = TRANS_DP_CTL(pipe);
3782 temp = I915_READ(reg);
3783 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3784 TRANS_DP_PORT_SEL_MASK);
3785 temp |= TRANS_DP_PORT_SEL_NONE;
3786 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003787
Daniel Vetterd925c592013-06-05 13:34:04 +02003788 /* disable DPLL_SEL */
3789 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003790 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003791 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003792 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003793
3794 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003795 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003796
3797 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003798 }
3799
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003800 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003801 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003802
3803 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003804 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003805 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003806}
3807
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003808static void haswell_crtc_disable(struct drm_crtc *crtc)
3809{
3810 struct drm_device *dev = crtc->dev;
3811 struct drm_i915_private *dev_priv = dev->dev_private;
3812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3813 struct intel_encoder *encoder;
3814 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003815 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003816
3817 if (!intel_crtc->active)
3818 return;
3819
Ville Syrjälädda9a662013-09-19 17:00:37 -03003820 haswell_crtc_disable_planes(crtc);
3821
Jani Nikula8807e552013-08-30 19:40:32 +03003822 for_each_encoder_on_crtc(dev, crtc, encoder) {
3823 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003824 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003825 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003826
Paulo Zanoni86642812013-04-12 17:57:57 -03003827 if (intel_crtc->config.has_pch_encoder)
3828 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003829 intel_disable_pipe(dev_priv, pipe);
3830
Paulo Zanoniad80a812012-10-24 16:06:19 -02003831 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003832
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003833 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003834
Paulo Zanoni1f544382012-10-24 11:32:00 -02003835 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003836
3837 for_each_encoder_on_crtc(dev, crtc, encoder)
3838 if (encoder->post_disable)
3839 encoder->post_disable(encoder);
3840
Daniel Vetter88adfff2013-03-28 10:42:01 +01003841 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003842 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003843 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003844 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003845 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003846
3847 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003848 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003849
3850 mutex_lock(&dev->struct_mutex);
3851 intel_update_fbc(dev);
3852 mutex_unlock(&dev->struct_mutex);
3853}
3854
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003855static void ironlake_crtc_off(struct drm_crtc *crtc)
3856{
3857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003858 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003859}
3860
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003861static void haswell_crtc_off(struct drm_crtc *crtc)
3862{
3863 intel_ddi_put_crtc_pll(crtc);
3864}
3865
Daniel Vetter02e792f2009-09-15 22:57:34 +02003866static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3867{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003868 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003869 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003870 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003871
Chris Wilson23f09ce2010-08-12 13:53:37 +01003872 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003873 dev_priv->mm.interruptible = false;
3874 (void) intel_overlay_switch_off(intel_crtc->overlay);
3875 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003876 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003877 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003878
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003879 /* Let userspace switch the overlay on again. In most cases userspace
3880 * has to recompute where to put it anyway.
3881 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003882}
3883
Egbert Eich61bc95c2013-03-04 09:24:38 -05003884/**
3885 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3886 * cursor plane briefly if not already running after enabling the display
3887 * plane.
3888 * This workaround avoids occasional blank screens when self refresh is
3889 * enabled.
3890 */
3891static void
3892g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3893{
3894 u32 cntl = I915_READ(CURCNTR(pipe));
3895
3896 if ((cntl & CURSOR_MODE) == 0) {
3897 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3898
3899 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3900 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3901 intel_wait_for_vblank(dev_priv->dev, pipe);
3902 I915_WRITE(CURCNTR(pipe), cntl);
3903 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3904 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3905 }
3906}
3907
Jesse Barnes2dd24552013-04-25 12:55:01 -07003908static void i9xx_pfit_enable(struct intel_crtc *crtc)
3909{
3910 struct drm_device *dev = crtc->base.dev;
3911 struct drm_i915_private *dev_priv = dev->dev_private;
3912 struct intel_crtc_config *pipe_config = &crtc->config;
3913
Daniel Vetter328d8e82013-05-08 10:36:31 +02003914 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003915 return;
3916
Daniel Vetterc0b03412013-05-28 12:05:54 +02003917 /*
3918 * The panel fitter should only be adjusted whilst the pipe is disabled,
3919 * according to register description and PRM.
3920 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003921 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3922 assert_pipe_disabled(dev_priv, crtc->pipe);
3923
Jesse Barnesb074cec2013-04-25 12:55:02 -07003924 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3925 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003926
3927 /* Border color in case we don't scale up to the full screen. Black by
3928 * default, change to something else for debugging. */
3929 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003930}
3931
Jesse Barnes586f49d2013-11-04 16:06:59 -08003932int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003933{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003934 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003935
Jesse Barnes586f49d2013-11-04 16:06:59 -08003936 /* Obtain SKU information */
3937 mutex_lock(&dev_priv->dpio_lock);
3938 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3939 CCK_FUSE_HPLL_FREQ_MASK;
3940 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003941
Jesse Barnes586f49d2013-11-04 16:06:59 -08003942 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003943}
3944
3945/* Adjust CDclk dividers to allow high res or save power if possible */
3946static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3947{
3948 struct drm_i915_private *dev_priv = dev->dev_private;
3949 u32 val, cmd;
3950
3951 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3952 cmd = 2;
3953 else if (cdclk == 266)
3954 cmd = 1;
3955 else
3956 cmd = 0;
3957
3958 mutex_lock(&dev_priv->rps.hw_lock);
3959 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3960 val &= ~DSPFREQGUAR_MASK;
3961 val |= (cmd << DSPFREQGUAR_SHIFT);
3962 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3963 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3964 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3965 50)) {
3966 DRM_ERROR("timed out waiting for CDclk change\n");
3967 }
3968 mutex_unlock(&dev_priv->rps.hw_lock);
3969
3970 if (cdclk == 400) {
3971 u32 divider, vco;
3972
3973 vco = valleyview_get_vco(dev_priv);
3974 divider = ((vco << 1) / cdclk) - 1;
3975
3976 mutex_lock(&dev_priv->dpio_lock);
3977 /* adjust cdclk divider */
3978 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3979 val &= ~0xf;
3980 val |= divider;
3981 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3982 mutex_unlock(&dev_priv->dpio_lock);
3983 }
3984
3985 mutex_lock(&dev_priv->dpio_lock);
3986 /* adjust self-refresh exit latency value */
3987 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
3988 val &= ~0x7f;
3989
3990 /*
3991 * For high bandwidth configs, we set a higher latency in the bunit
3992 * so that the core display fetch happens in time to avoid underruns.
3993 */
3994 if (cdclk == 400)
3995 val |= 4500 / 250; /* 4.5 usec */
3996 else
3997 val |= 3000 / 250; /* 3.0 usec */
3998 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
3999 mutex_unlock(&dev_priv->dpio_lock);
4000
4001 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4002 intel_i2c_reset(dev);
4003}
4004
4005static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4006{
4007 int cur_cdclk, vco;
4008 int divider;
4009
4010 vco = valleyview_get_vco(dev_priv);
4011
4012 mutex_lock(&dev_priv->dpio_lock);
4013 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4014 mutex_unlock(&dev_priv->dpio_lock);
4015
4016 divider &= 0xf;
4017
4018 cur_cdclk = (vco << 1) / (divider + 1);
4019
4020 return cur_cdclk;
4021}
4022
4023static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4024 int max_pixclk)
4025{
4026 int cur_cdclk;
4027
4028 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4029
4030 /*
4031 * Really only a few cases to deal with, as only 4 CDclks are supported:
4032 * 200MHz
4033 * 267MHz
4034 * 320MHz
4035 * 400MHz
4036 * So we check to see whether we're above 90% of the lower bin and
4037 * adjust if needed.
4038 */
4039 if (max_pixclk > 288000) {
4040 return 400;
4041 } else if (max_pixclk > 240000) {
4042 return 320;
4043 } else
4044 return 266;
4045 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4046}
4047
4048static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4049 unsigned modeset_pipes,
4050 struct intel_crtc_config *pipe_config)
4051{
4052 struct drm_device *dev = dev_priv->dev;
4053 struct intel_crtc *intel_crtc;
4054 int max_pixclk = 0;
4055
4056 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4057 base.head) {
4058 if (modeset_pipes & (1 << intel_crtc->pipe))
4059 max_pixclk = max(max_pixclk,
4060 pipe_config->adjusted_mode.crtc_clock);
4061 else if (intel_crtc->base.enabled)
4062 max_pixclk = max(max_pixclk,
4063 intel_crtc->config.adjusted_mode.crtc_clock);
4064 }
4065
4066 return max_pixclk;
4067}
4068
4069static void valleyview_modeset_global_pipes(struct drm_device *dev,
4070 unsigned *prepare_pipes,
4071 unsigned modeset_pipes,
4072 struct intel_crtc_config *pipe_config)
4073{
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 struct intel_crtc *intel_crtc;
4076 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4077 pipe_config);
4078 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4079
4080 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4081 return;
4082
4083 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4084 base.head)
4085 if (intel_crtc->base.enabled)
4086 *prepare_pipes |= (1 << intel_crtc->pipe);
4087}
4088
4089static void valleyview_modeset_global_resources(struct drm_device *dev)
4090{
4091 struct drm_i915_private *dev_priv = dev->dev_private;
4092 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4093 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4094 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4095
4096 if (req_cdclk != cur_cdclk)
4097 valleyview_set_cdclk(dev, req_cdclk);
4098}
4099
Jesse Barnes89b667f2013-04-18 14:51:36 -07004100static void valleyview_crtc_enable(struct drm_crtc *crtc)
4101{
4102 struct drm_device *dev = crtc->dev;
4103 struct drm_i915_private *dev_priv = dev->dev_private;
4104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4105 struct intel_encoder *encoder;
4106 int pipe = intel_crtc->pipe;
4107 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004108 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004109
4110 WARN_ON(!crtc->enabled);
4111
4112 if (intel_crtc->active)
4113 return;
4114
4115 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004116
Jesse Barnes89b667f2013-04-18 14:51:36 -07004117 for_each_encoder_on_crtc(dev, crtc, encoder)
4118 if (encoder->pre_pll_enable)
4119 encoder->pre_pll_enable(encoder);
4120
Jani Nikula23538ef2013-08-27 15:12:22 +03004121 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4122
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004123 if (!is_dsi)
4124 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004125
4126 for_each_encoder_on_crtc(dev, crtc, encoder)
4127 if (encoder->pre_enable)
4128 encoder->pre_enable(encoder);
4129
Jesse Barnes2dd24552013-04-25 12:55:01 -07004130 i9xx_pfit_enable(intel_crtc);
4131
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004132 intel_crtc_load_lut(crtc);
4133
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004134 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004135 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004136 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004137 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004138 intel_crtc_update_cursor(crtc, true);
4139
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004140 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004141
4142 for_each_encoder_on_crtc(dev, crtc, encoder)
4143 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004144}
4145
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004146static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004147{
4148 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004149 struct drm_i915_private *dev_priv = dev->dev_private;
4150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004151 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004152 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004153 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004154
Daniel Vetter08a48462012-07-02 11:43:47 +02004155 WARN_ON(!crtc->enabled);
4156
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004157 if (intel_crtc->active)
4158 return;
4159
4160 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004161
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004162 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004163 if (encoder->pre_enable)
4164 encoder->pre_enable(encoder);
4165
Daniel Vetterf6736a12013-06-05 13:34:30 +02004166 i9xx_enable_pll(intel_crtc);
4167
Jesse Barnes2dd24552013-04-25 12:55:01 -07004168 i9xx_pfit_enable(intel_crtc);
4169
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004170 intel_crtc_load_lut(crtc);
4171
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004172 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004173 intel_enable_pipe(dev_priv, pipe, false, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004174 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004175 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004176 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004177 if (IS_G4X(dev))
4178 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004179 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004180
4181 /* Give the overlay scaler a chance to enable if it's on this pipe */
4182 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004183
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004184 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004185
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004186 for_each_encoder_on_crtc(dev, crtc, encoder)
4187 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004188}
4189
Daniel Vetter87476d62013-04-11 16:29:06 +02004190static void i9xx_pfit_disable(struct intel_crtc *crtc)
4191{
4192 struct drm_device *dev = crtc->base.dev;
4193 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004194
4195 if (!crtc->config.gmch_pfit.control)
4196 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004197
4198 assert_pipe_disabled(dev_priv, crtc->pipe);
4199
Daniel Vetter328d8e82013-05-08 10:36:31 +02004200 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4201 I915_READ(PFIT_CONTROL));
4202 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004203}
4204
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004205static void i9xx_crtc_disable(struct drm_crtc *crtc)
4206{
4207 struct drm_device *dev = crtc->dev;
4208 struct drm_i915_private *dev_priv = dev->dev_private;
4209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004210 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004211 int pipe = intel_crtc->pipe;
4212 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004213
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004214 if (!intel_crtc->active)
4215 return;
4216
Daniel Vetterea9d7582012-07-10 10:42:52 +02004217 for_each_encoder_on_crtc(dev, crtc, encoder)
4218 encoder->disable(encoder);
4219
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004220 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004221 intel_crtc_wait_for_pending_flips(crtc);
4222 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004223
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004224 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004225 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004226
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004227 intel_crtc_dpms_overlay(intel_crtc, false);
4228 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004229 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004230 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004231
Jesse Barnesb24e7172011-01-04 15:09:30 -08004232 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004233
Daniel Vetter87476d62013-04-11 16:29:06 +02004234 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004235
Jesse Barnes89b667f2013-04-18 14:51:36 -07004236 for_each_encoder_on_crtc(dev, crtc, encoder)
4237 if (encoder->post_disable)
4238 encoder->post_disable(encoder);
4239
Jesse Barnesf6071162013-10-01 10:41:38 -07004240 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4241 vlv_disable_pll(dev_priv, pipe);
4242 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004243 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004244
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004245 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004246 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004247
Chris Wilson6b383a72010-09-13 13:54:26 +01004248 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004249}
4250
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004251static void i9xx_crtc_off(struct drm_crtc *crtc)
4252{
4253}
4254
Daniel Vetter976f8a22012-07-08 22:34:21 +02004255static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4256 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004257{
4258 struct drm_device *dev = crtc->dev;
4259 struct drm_i915_master_private *master_priv;
4260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4261 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004262
4263 if (!dev->primary->master)
4264 return;
4265
4266 master_priv = dev->primary->master->driver_priv;
4267 if (!master_priv->sarea_priv)
4268 return;
4269
Jesse Barnes79e53942008-11-07 14:24:08 -08004270 switch (pipe) {
4271 case 0:
4272 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4273 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4274 break;
4275 case 1:
4276 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4277 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4278 break;
4279 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004280 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004281 break;
4282 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004283}
4284
Daniel Vetter976f8a22012-07-08 22:34:21 +02004285/**
4286 * Sets the power management mode of the pipe and plane.
4287 */
4288void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004289{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004290 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004291 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004292 struct intel_encoder *intel_encoder;
4293 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004294
Daniel Vetter976f8a22012-07-08 22:34:21 +02004295 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4296 enable |= intel_encoder->connectors_active;
4297
4298 if (enable)
4299 dev_priv->display.crtc_enable(crtc);
4300 else
4301 dev_priv->display.crtc_disable(crtc);
4302
4303 intel_crtc_update_sarea(crtc, enable);
4304}
4305
Daniel Vetter976f8a22012-07-08 22:34:21 +02004306static void intel_crtc_disable(struct drm_crtc *crtc)
4307{
4308 struct drm_device *dev = crtc->dev;
4309 struct drm_connector *connector;
4310 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004312
4313 /* crtc should still be enabled when we disable it. */
4314 WARN_ON(!crtc->enabled);
4315
4316 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004317 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004318 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004319 dev_priv->display.off(crtc);
4320
Chris Wilson931872f2012-01-16 23:01:13 +00004321 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004322 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004323 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004324
4325 if (crtc->fb) {
4326 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004327 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004328 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004329 crtc->fb = NULL;
4330 }
4331
4332 /* Update computed state. */
4333 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4334 if (!connector->encoder || !connector->encoder->crtc)
4335 continue;
4336
4337 if (connector->encoder->crtc != crtc)
4338 continue;
4339
4340 connector->dpms = DRM_MODE_DPMS_OFF;
4341 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004342 }
4343}
4344
Chris Wilsonea5b2132010-08-04 13:50:23 +01004345void intel_encoder_destroy(struct drm_encoder *encoder)
4346{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004347 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004348
Chris Wilsonea5b2132010-08-04 13:50:23 +01004349 drm_encoder_cleanup(encoder);
4350 kfree(intel_encoder);
4351}
4352
Damien Lespiau92373292013-08-08 22:28:57 +01004353/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004354 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4355 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004356static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004357{
4358 if (mode == DRM_MODE_DPMS_ON) {
4359 encoder->connectors_active = true;
4360
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004361 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004362 } else {
4363 encoder->connectors_active = false;
4364
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004365 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004366 }
4367}
4368
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004369/* Cross check the actual hw state with our own modeset state tracking (and it's
4370 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004371static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004372{
4373 if (connector->get_hw_state(connector)) {
4374 struct intel_encoder *encoder = connector->encoder;
4375 struct drm_crtc *crtc;
4376 bool encoder_enabled;
4377 enum pipe pipe;
4378
4379 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4380 connector->base.base.id,
4381 drm_get_connector_name(&connector->base));
4382
4383 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4384 "wrong connector dpms state\n");
4385 WARN(connector->base.encoder != &encoder->base,
4386 "active connector not linked to encoder\n");
4387 WARN(!encoder->connectors_active,
4388 "encoder->connectors_active not set\n");
4389
4390 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4391 WARN(!encoder_enabled, "encoder not enabled\n");
4392 if (WARN_ON(!encoder->base.crtc))
4393 return;
4394
4395 crtc = encoder->base.crtc;
4396
4397 WARN(!crtc->enabled, "crtc not enabled\n");
4398 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4399 WARN(pipe != to_intel_crtc(crtc)->pipe,
4400 "encoder active on the wrong pipe\n");
4401 }
4402}
4403
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004404/* Even simpler default implementation, if there's really no special case to
4405 * consider. */
4406void intel_connector_dpms(struct drm_connector *connector, int mode)
4407{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004408 /* All the simple cases only support two dpms states. */
4409 if (mode != DRM_MODE_DPMS_ON)
4410 mode = DRM_MODE_DPMS_OFF;
4411
4412 if (mode == connector->dpms)
4413 return;
4414
4415 connector->dpms = mode;
4416
4417 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004418 if (connector->encoder)
4419 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004420
Daniel Vetterb9805142012-08-31 17:37:33 +02004421 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004422}
4423
Daniel Vetterf0947c32012-07-02 13:10:34 +02004424/* Simple connector->get_hw_state implementation for encoders that support only
4425 * one connector and no cloning and hence the encoder state determines the state
4426 * of the connector. */
4427bool intel_connector_get_hw_state(struct intel_connector *connector)
4428{
Daniel Vetter24929352012-07-02 20:28:59 +02004429 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004430 struct intel_encoder *encoder = connector->encoder;
4431
4432 return encoder->get_hw_state(encoder, &pipe);
4433}
4434
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004435static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4436 struct intel_crtc_config *pipe_config)
4437{
4438 struct drm_i915_private *dev_priv = dev->dev_private;
4439 struct intel_crtc *pipe_B_crtc =
4440 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4441
4442 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4443 pipe_name(pipe), pipe_config->fdi_lanes);
4444 if (pipe_config->fdi_lanes > 4) {
4445 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4446 pipe_name(pipe), pipe_config->fdi_lanes);
4447 return false;
4448 }
4449
Paulo Zanonibafb6552013-11-02 21:07:44 -07004450 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004451 if (pipe_config->fdi_lanes > 2) {
4452 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4453 pipe_config->fdi_lanes);
4454 return false;
4455 } else {
4456 return true;
4457 }
4458 }
4459
4460 if (INTEL_INFO(dev)->num_pipes == 2)
4461 return true;
4462
4463 /* Ivybridge 3 pipe is really complicated */
4464 switch (pipe) {
4465 case PIPE_A:
4466 return true;
4467 case PIPE_B:
4468 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4469 pipe_config->fdi_lanes > 2) {
4470 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4471 pipe_name(pipe), pipe_config->fdi_lanes);
4472 return false;
4473 }
4474 return true;
4475 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004476 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004477 pipe_B_crtc->config.fdi_lanes <= 2) {
4478 if (pipe_config->fdi_lanes > 2) {
4479 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4480 pipe_name(pipe), pipe_config->fdi_lanes);
4481 return false;
4482 }
4483 } else {
4484 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4485 return false;
4486 }
4487 return true;
4488 default:
4489 BUG();
4490 }
4491}
4492
Daniel Vettere29c22c2013-02-21 00:00:16 +01004493#define RETRY 1
4494static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4495 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004496{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004497 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004498 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004499 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004500 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004501
Daniel Vettere29c22c2013-02-21 00:00:16 +01004502retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004503 /* FDI is a binary signal running at ~2.7GHz, encoding
4504 * each output octet as 10 bits. The actual frequency
4505 * is stored as a divider into a 100MHz clock, and the
4506 * mode pixel clock is stored in units of 1KHz.
4507 * Hence the bw of each lane in terms of the mode signal
4508 * is:
4509 */
4510 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4511
Damien Lespiau241bfc32013-09-25 16:45:37 +01004512 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004513
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004514 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004515 pipe_config->pipe_bpp);
4516
4517 pipe_config->fdi_lanes = lane;
4518
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004519 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004520 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004521
Daniel Vettere29c22c2013-02-21 00:00:16 +01004522 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4523 intel_crtc->pipe, pipe_config);
4524 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4525 pipe_config->pipe_bpp -= 2*3;
4526 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4527 pipe_config->pipe_bpp);
4528 needs_recompute = true;
4529 pipe_config->bw_constrained = true;
4530
4531 goto retry;
4532 }
4533
4534 if (needs_recompute)
4535 return RETRY;
4536
4537 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004538}
4539
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004540static void hsw_compute_ips_config(struct intel_crtc *crtc,
4541 struct intel_crtc_config *pipe_config)
4542{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004543 pipe_config->ips_enabled = i915_enable_ips &&
4544 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004545 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004546}
4547
Daniel Vettera43f6e02013-06-07 23:10:32 +02004548static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004549 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004550{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004551 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004552 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004553
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004554 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004555 if (INTEL_INFO(dev)->gen < 4) {
4556 struct drm_i915_private *dev_priv = dev->dev_private;
4557 int clock_limit =
4558 dev_priv->display.get_display_clock_speed(dev);
4559
4560 /*
4561 * Enable pixel doubling when the dot clock
4562 * is > 90% of the (display) core speed.
4563 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004564 * GDG double wide on either pipe,
4565 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004566 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004567 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004568 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004569 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004570 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004571 }
4572
Damien Lespiau241bfc32013-09-25 16:45:37 +01004573 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004574 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004575 }
Chris Wilson89749352010-09-12 18:25:19 +01004576
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004577 /*
4578 * Pipe horizontal size must be even in:
4579 * - DVO ganged mode
4580 * - LVDS dual channel mode
4581 * - Double wide pipe
4582 */
4583 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4584 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4585 pipe_config->pipe_src_w &= ~1;
4586
Damien Lespiau8693a822013-05-03 18:48:11 +01004587 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4588 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004589 */
4590 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4591 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004592 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004593
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004594 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004595 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004596 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004597 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4598 * for lvds. */
4599 pipe_config->pipe_bpp = 8*3;
4600 }
4601
Damien Lespiauf5adf942013-06-24 18:29:34 +01004602 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004603 hsw_compute_ips_config(crtc, pipe_config);
4604
4605 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4606 * clock survives for now. */
4607 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4608 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004609
Daniel Vetter877d48d2013-04-19 11:24:43 +02004610 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004611 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004612
Daniel Vettere29c22c2013-02-21 00:00:16 +01004613 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004614}
4615
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004616static int valleyview_get_display_clock_speed(struct drm_device *dev)
4617{
4618 return 400000; /* FIXME */
4619}
4620
Jesse Barnese70236a2009-09-21 10:42:27 -07004621static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004622{
Jesse Barnese70236a2009-09-21 10:42:27 -07004623 return 400000;
4624}
Jesse Barnes79e53942008-11-07 14:24:08 -08004625
Jesse Barnese70236a2009-09-21 10:42:27 -07004626static int i915_get_display_clock_speed(struct drm_device *dev)
4627{
4628 return 333000;
4629}
Jesse Barnes79e53942008-11-07 14:24:08 -08004630
Jesse Barnese70236a2009-09-21 10:42:27 -07004631static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4632{
4633 return 200000;
4634}
Jesse Barnes79e53942008-11-07 14:24:08 -08004635
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004636static int pnv_get_display_clock_speed(struct drm_device *dev)
4637{
4638 u16 gcfgc = 0;
4639
4640 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4641
4642 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4643 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4644 return 267000;
4645 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4646 return 333000;
4647 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4648 return 444000;
4649 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4650 return 200000;
4651 default:
4652 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4653 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4654 return 133000;
4655 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4656 return 167000;
4657 }
4658}
4659
Jesse Barnese70236a2009-09-21 10:42:27 -07004660static int i915gm_get_display_clock_speed(struct drm_device *dev)
4661{
4662 u16 gcfgc = 0;
4663
4664 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4665
4666 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004667 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004668 else {
4669 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4670 case GC_DISPLAY_CLOCK_333_MHZ:
4671 return 333000;
4672 default:
4673 case GC_DISPLAY_CLOCK_190_200_MHZ:
4674 return 190000;
4675 }
4676 }
4677}
Jesse Barnes79e53942008-11-07 14:24:08 -08004678
Jesse Barnese70236a2009-09-21 10:42:27 -07004679static int i865_get_display_clock_speed(struct drm_device *dev)
4680{
4681 return 266000;
4682}
4683
4684static int i855_get_display_clock_speed(struct drm_device *dev)
4685{
4686 u16 hpllcc = 0;
4687 /* Assume that the hardware is in the high speed state. This
4688 * should be the default.
4689 */
4690 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4691 case GC_CLOCK_133_200:
4692 case GC_CLOCK_100_200:
4693 return 200000;
4694 case GC_CLOCK_166_250:
4695 return 250000;
4696 case GC_CLOCK_100_133:
4697 return 133000;
4698 }
4699
4700 /* Shouldn't happen */
4701 return 0;
4702}
4703
4704static int i830_get_display_clock_speed(struct drm_device *dev)
4705{
4706 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004707}
4708
Zhenyu Wang2c072452009-06-05 15:38:42 +08004709static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004710intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004711{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004712 while (*num > DATA_LINK_M_N_MASK ||
4713 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004714 *num >>= 1;
4715 *den >>= 1;
4716 }
4717}
4718
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004719static void compute_m_n(unsigned int m, unsigned int n,
4720 uint32_t *ret_m, uint32_t *ret_n)
4721{
4722 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4723 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4724 intel_reduce_m_n_ratio(ret_m, ret_n);
4725}
4726
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004727void
4728intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4729 int pixel_clock, int link_clock,
4730 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004731{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004732 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004733
4734 compute_m_n(bits_per_pixel * pixel_clock,
4735 link_clock * nlanes * 8,
4736 &m_n->gmch_m, &m_n->gmch_n);
4737
4738 compute_m_n(pixel_clock, link_clock,
4739 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004740}
4741
Chris Wilsona7615032011-01-12 17:04:08 +00004742static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4743{
Keith Packard72bbe582011-09-26 16:09:45 -07004744 if (i915_panel_use_ssc >= 0)
4745 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004746 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004747 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004748}
4749
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004750static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4751{
4752 struct drm_device *dev = crtc->dev;
4753 struct drm_i915_private *dev_priv = dev->dev_private;
4754 int refclk;
4755
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004756 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004757 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004758 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004759 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004760 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004761 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4762 refclk / 1000);
4763 } else if (!IS_GEN2(dev)) {
4764 refclk = 96000;
4765 } else {
4766 refclk = 48000;
4767 }
4768
4769 return refclk;
4770}
4771
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004772static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004773{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004774 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004775}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004776
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004777static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4778{
4779 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004780}
4781
Daniel Vetterf47709a2013-03-28 10:42:02 +01004782static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004783 intel_clock_t *reduced_clock)
4784{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004785 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004786 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004787 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004788 u32 fp, fp2 = 0;
4789
4790 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004791 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004792 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004793 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004794 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004795 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004796 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004797 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004798 }
4799
4800 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004801 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004802
Daniel Vetterf47709a2013-03-28 10:42:02 +01004803 crtc->lowfreq_avail = false;
4804 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004805 reduced_clock && i915_powersave) {
4806 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004807 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004808 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004809 } else {
4810 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004811 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004812 }
4813}
4814
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004815static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4816 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004817{
4818 u32 reg_val;
4819
4820 /*
4821 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4822 * and set it to a reasonable value instead.
4823 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004824 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004825 reg_val &= 0xffffff00;
4826 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004827 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004828
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004829 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004830 reg_val &= 0x8cffffff;
4831 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004832 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004833
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004834 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004835 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004836 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004837
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004838 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004839 reg_val &= 0x00ffffff;
4840 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004841 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004842}
4843
Daniel Vetterb5518422013-05-03 11:49:48 +02004844static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4845 struct intel_link_m_n *m_n)
4846{
4847 struct drm_device *dev = crtc->base.dev;
4848 struct drm_i915_private *dev_priv = dev->dev_private;
4849 int pipe = crtc->pipe;
4850
Daniel Vettere3b95f12013-05-03 11:49:49 +02004851 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4852 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4853 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4854 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004855}
4856
4857static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4858 struct intel_link_m_n *m_n)
4859{
4860 struct drm_device *dev = crtc->base.dev;
4861 struct drm_i915_private *dev_priv = dev->dev_private;
4862 int pipe = crtc->pipe;
4863 enum transcoder transcoder = crtc->config.cpu_transcoder;
4864
4865 if (INTEL_INFO(dev)->gen >= 5) {
4866 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4867 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4868 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4869 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4870 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004871 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4872 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4873 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4874 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004875 }
4876}
4877
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004878static void intel_dp_set_m_n(struct intel_crtc *crtc)
4879{
4880 if (crtc->config.has_pch_encoder)
4881 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4882 else
4883 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4884}
4885
Daniel Vetterf47709a2013-03-28 10:42:02 +01004886static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004887{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004888 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004889 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004890 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004891 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004892 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004893 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004894
Daniel Vetter09153002012-12-12 14:06:44 +01004895 mutex_lock(&dev_priv->dpio_lock);
4896
Daniel Vetterf47709a2013-03-28 10:42:02 +01004897 bestn = crtc->config.dpll.n;
4898 bestm1 = crtc->config.dpll.m1;
4899 bestm2 = crtc->config.dpll.m2;
4900 bestp1 = crtc->config.dpll.p1;
4901 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004902
Jesse Barnes89b667f2013-04-18 14:51:36 -07004903 /* See eDP HDMI DPIO driver vbios notes doc */
4904
4905 /* PLL B needs special handling */
4906 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004907 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004908
4909 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004910 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004911
4912 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004913 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004914 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004915 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004916
4917 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004918 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004919
4920 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004921 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4922 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4923 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004924 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004925
4926 /*
4927 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4928 * but we don't support that).
4929 * Note: don't use the DAC post divider as it seems unstable.
4930 */
4931 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004933
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004934 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004936
Jesse Barnes89b667f2013-04-18 14:51:36 -07004937 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004938 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004939 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004940 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004941 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004942 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004943 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004945 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004946
Jesse Barnes89b667f2013-04-18 14:51:36 -07004947 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4948 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4949 /* Use SSC source */
4950 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004951 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004952 0x0df40000);
4953 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004954 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004955 0x0df70000);
4956 } else { /* HDMI or VGA */
4957 /* Use bend source */
4958 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004959 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004960 0x0df70000);
4961 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004963 0x0df40000);
4964 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004965
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004966 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004967 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4968 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4969 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4970 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004972
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004974
Jesse Barnes89b667f2013-04-18 14:51:36 -07004975 /* Enable DPIO clock input */
4976 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4977 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004978 /* We should never disable this, set it here for state tracking */
4979 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004980 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004981 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004982 crtc->config.dpll_hw_state.dpll = dpll;
4983
Daniel Vetteref1b4602013-06-01 17:17:04 +02004984 dpll_md = (crtc->config.pixel_multiplier - 1)
4985 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004986 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4987
Daniel Vetterf47709a2013-03-28 10:42:02 +01004988 if (crtc->config.has_dp_encoder)
4989 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304990
Daniel Vetter09153002012-12-12 14:06:44 +01004991 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004992}
4993
Daniel Vetterf47709a2013-03-28 10:42:02 +01004994static void i9xx_update_pll(struct intel_crtc *crtc,
4995 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004996 int num_connectors)
4997{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004998 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004999 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005000 u32 dpll;
5001 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005002 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005003
Daniel Vetterf47709a2013-03-28 10:42:02 +01005004 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305005
Daniel Vetterf47709a2013-03-28 10:42:02 +01005006 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5007 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005008
5009 dpll = DPLL_VGA_MODE_DIS;
5010
Daniel Vetterf47709a2013-03-28 10:42:02 +01005011 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005012 dpll |= DPLLB_MODE_LVDS;
5013 else
5014 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005015
Daniel Vetteref1b4602013-06-01 17:17:04 +02005016 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005017 dpll |= (crtc->config.pixel_multiplier - 1)
5018 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005019 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005020
5021 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005022 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005023
Daniel Vetterf47709a2013-03-28 10:42:02 +01005024 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005025 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005026
5027 /* compute bitmask from p1 value */
5028 if (IS_PINEVIEW(dev))
5029 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5030 else {
5031 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5032 if (IS_G4X(dev) && reduced_clock)
5033 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5034 }
5035 switch (clock->p2) {
5036 case 5:
5037 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5038 break;
5039 case 7:
5040 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5041 break;
5042 case 10:
5043 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5044 break;
5045 case 14:
5046 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5047 break;
5048 }
5049 if (INTEL_INFO(dev)->gen >= 4)
5050 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5051
Daniel Vetter09ede542013-04-30 14:01:45 +02005052 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005053 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005054 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005055 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5056 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5057 else
5058 dpll |= PLL_REF_INPUT_DREFCLK;
5059
5060 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005061 crtc->config.dpll_hw_state.dpll = dpll;
5062
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005063 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005064 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5065 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005066 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005067 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005068
5069 if (crtc->config.has_dp_encoder)
5070 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005071}
5072
Daniel Vetterf47709a2013-03-28 10:42:02 +01005073static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005074 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005075 int num_connectors)
5076{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005077 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005078 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005079 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005080 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005081
Daniel Vetterf47709a2013-03-28 10:42:02 +01005082 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305083
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005084 dpll = DPLL_VGA_MODE_DIS;
5085
Daniel Vetterf47709a2013-03-28 10:42:02 +01005086 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005087 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5088 } else {
5089 if (clock->p1 == 2)
5090 dpll |= PLL_P1_DIVIDE_BY_TWO;
5091 else
5092 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5093 if (clock->p2 == 4)
5094 dpll |= PLL_P2_DIVIDE_BY_4;
5095 }
5096
Daniel Vetter4a33e482013-07-06 12:52:05 +02005097 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5098 dpll |= DPLL_DVO_2X_MODE;
5099
Daniel Vetterf47709a2013-03-28 10:42:02 +01005100 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005101 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5102 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5103 else
5104 dpll |= PLL_REF_INPUT_DREFCLK;
5105
5106 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005107 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005108}
5109
Daniel Vetter8a654f32013-06-01 17:16:22 +02005110static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005111{
5112 struct drm_device *dev = intel_crtc->base.dev;
5113 struct drm_i915_private *dev_priv = dev->dev_private;
5114 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005115 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005116 struct drm_display_mode *adjusted_mode =
5117 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005118 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5119
5120 /* We need to be careful not to changed the adjusted mode, for otherwise
5121 * the hw state checker will get angry at the mismatch. */
5122 crtc_vtotal = adjusted_mode->crtc_vtotal;
5123 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005124
5125 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5126 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005127 crtc_vtotal -= 1;
5128 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005129 vsyncshift = adjusted_mode->crtc_hsync_start
5130 - adjusted_mode->crtc_htotal / 2;
5131 } else {
5132 vsyncshift = 0;
5133 }
5134
5135 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005136 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005137
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005138 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005139 (adjusted_mode->crtc_hdisplay - 1) |
5140 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005141 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005142 (adjusted_mode->crtc_hblank_start - 1) |
5143 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005144 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005145 (adjusted_mode->crtc_hsync_start - 1) |
5146 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5147
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005148 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005149 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005150 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005151 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005152 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005153 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005154 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005155 (adjusted_mode->crtc_vsync_start - 1) |
5156 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5157
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005158 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5159 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5160 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5161 * bits. */
5162 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5163 (pipe == PIPE_B || pipe == PIPE_C))
5164 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5165
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005166 /* pipesrc controls the size that is scaled from, which should
5167 * always be the user's requested size.
5168 */
5169 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005170 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5171 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005172}
5173
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005174static void intel_get_pipe_timings(struct intel_crtc *crtc,
5175 struct intel_crtc_config *pipe_config)
5176{
5177 struct drm_device *dev = crtc->base.dev;
5178 struct drm_i915_private *dev_priv = dev->dev_private;
5179 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5180 uint32_t tmp;
5181
5182 tmp = I915_READ(HTOTAL(cpu_transcoder));
5183 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5184 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5185 tmp = I915_READ(HBLANK(cpu_transcoder));
5186 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5187 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5188 tmp = I915_READ(HSYNC(cpu_transcoder));
5189 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5190 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5191
5192 tmp = I915_READ(VTOTAL(cpu_transcoder));
5193 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5194 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5195 tmp = I915_READ(VBLANK(cpu_transcoder));
5196 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5197 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5198 tmp = I915_READ(VSYNC(cpu_transcoder));
5199 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5200 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5201
5202 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5203 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5204 pipe_config->adjusted_mode.crtc_vtotal += 1;
5205 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5206 }
5207
5208 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005209 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5210 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5211
5212 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5213 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005214}
5215
Jesse Barnesbabea612013-06-26 18:57:38 +03005216static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5217 struct intel_crtc_config *pipe_config)
5218{
5219 struct drm_crtc *crtc = &intel_crtc->base;
5220
5221 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5222 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5223 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5224 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5225
5226 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5227 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5228 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5229 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5230
5231 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5232
Damien Lespiau241bfc32013-09-25 16:45:37 +01005233 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005234 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5235}
5236
Daniel Vetter84b046f2013-02-19 18:48:54 +01005237static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5238{
5239 struct drm_device *dev = intel_crtc->base.dev;
5240 struct drm_i915_private *dev_priv = dev->dev_private;
5241 uint32_t pipeconf;
5242
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005243 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005244
Daniel Vetter67c72a12013-09-24 11:46:14 +02005245 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5246 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5247 pipeconf |= PIPECONF_ENABLE;
5248
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005249 if (intel_crtc->config.double_wide)
5250 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005251
Daniel Vetterff9ce462013-04-24 14:57:17 +02005252 /* only g4x and later have fancy bpc/dither controls */
5253 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005254 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5255 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5256 pipeconf |= PIPECONF_DITHER_EN |
5257 PIPECONF_DITHER_TYPE_SP;
5258
5259 switch (intel_crtc->config.pipe_bpp) {
5260 case 18:
5261 pipeconf |= PIPECONF_6BPC;
5262 break;
5263 case 24:
5264 pipeconf |= PIPECONF_8BPC;
5265 break;
5266 case 30:
5267 pipeconf |= PIPECONF_10BPC;
5268 break;
5269 default:
5270 /* Case prevented by intel_choose_pipe_bpp_dither. */
5271 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005272 }
5273 }
5274
5275 if (HAS_PIPE_CXSR(dev)) {
5276 if (intel_crtc->lowfreq_avail) {
5277 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5278 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5279 } else {
5280 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005281 }
5282 }
5283
Daniel Vetter84b046f2013-02-19 18:48:54 +01005284 if (!IS_GEN2(dev) &&
5285 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5286 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5287 else
5288 pipeconf |= PIPECONF_PROGRESSIVE;
5289
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005290 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5291 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005292
Daniel Vetter84b046f2013-02-19 18:48:54 +01005293 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5294 POSTING_READ(PIPECONF(intel_crtc->pipe));
5295}
5296
Eric Anholtf564048e2011-03-30 13:01:02 -07005297static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005298 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005299 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005300{
5301 struct drm_device *dev = crtc->dev;
5302 struct drm_i915_private *dev_priv = dev->dev_private;
5303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5304 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005305 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005306 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005307 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005308 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005309 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005310 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005311 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005312 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005313 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005314
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005315 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005316 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005317 case INTEL_OUTPUT_LVDS:
5318 is_lvds = true;
5319 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005320 case INTEL_OUTPUT_DSI:
5321 is_dsi = true;
5322 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005323 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005324
Eric Anholtc751ce42010-03-25 11:48:48 -07005325 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005326 }
5327
Jani Nikulaf2335332013-09-13 11:03:09 +03005328 if (is_dsi)
5329 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005330
Jani Nikulaf2335332013-09-13 11:03:09 +03005331 if (!intel_crtc->config.clock_set) {
5332 refclk = i9xx_get_refclk(crtc, num_connectors);
5333
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005334 /*
5335 * Returns a set of divisors for the desired target clock with
5336 * the given refclk, or FALSE. The returned values represent
5337 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5338 * 2) / p1 / p2.
5339 */
5340 limit = intel_limit(crtc, refclk);
5341 ok = dev_priv->display.find_dpll(limit, crtc,
5342 intel_crtc->config.port_clock,
5343 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005344 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005345 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5346 return -EINVAL;
5347 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005348
Jani Nikulaf2335332013-09-13 11:03:09 +03005349 if (is_lvds && dev_priv->lvds_downclock_avail) {
5350 /*
5351 * Ensure we match the reduced clock's P to the target
5352 * clock. If the clocks don't match, we can't switch
5353 * the display clock by using the FP0/FP1. In such case
5354 * we will disable the LVDS downclock feature.
5355 */
5356 has_reduced_clock =
5357 dev_priv->display.find_dpll(limit, crtc,
5358 dev_priv->lvds_downclock,
5359 refclk, &clock,
5360 &reduced_clock);
5361 }
5362 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005363 intel_crtc->config.dpll.n = clock.n;
5364 intel_crtc->config.dpll.m1 = clock.m1;
5365 intel_crtc->config.dpll.m2 = clock.m2;
5366 intel_crtc->config.dpll.p1 = clock.p1;
5367 intel_crtc->config.dpll.p2 = clock.p2;
5368 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005369
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005370 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005371 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305372 has_reduced_clock ? &reduced_clock : NULL,
5373 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005374 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005375 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005376 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005377 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005378 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005379 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005380 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005381
Jani Nikulaf2335332013-09-13 11:03:09 +03005382skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005383 /* Set up the display plane register */
5384 dspcntr = DISPPLANE_GAMMA_ENABLE;
5385
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005386 if (!IS_VALLEYVIEW(dev)) {
5387 if (pipe == 0)
5388 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5389 else
5390 dspcntr |= DISPPLANE_SEL_PIPE_B;
5391 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005392
Daniel Vetter8a654f32013-06-01 17:16:22 +02005393 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005394
5395 /* pipesrc and dspsize control the size that is scaled from,
5396 * which should always be the user's requested size.
5397 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005398 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005399 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5400 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005401 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005402
Daniel Vetter84b046f2013-02-19 18:48:54 +01005403 i9xx_set_pipeconf(intel_crtc);
5404
Eric Anholtf564048e2011-03-30 13:01:02 -07005405 I915_WRITE(DSPCNTR(plane), dspcntr);
5406 POSTING_READ(DSPCNTR(plane));
5407
Daniel Vetter94352cf2012-07-05 22:51:56 +02005408 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005409
Eric Anholtf564048e2011-03-30 13:01:02 -07005410 return ret;
5411}
5412
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005413static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5414 struct intel_crtc_config *pipe_config)
5415{
5416 struct drm_device *dev = crtc->base.dev;
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 uint32_t tmp;
5419
5420 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005421 if (!(tmp & PFIT_ENABLE))
5422 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005423
Daniel Vetter06922822013-07-11 13:35:40 +02005424 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005425 if (INTEL_INFO(dev)->gen < 4) {
5426 if (crtc->pipe != PIPE_B)
5427 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005428 } else {
5429 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5430 return;
5431 }
5432
Daniel Vetter06922822013-07-11 13:35:40 +02005433 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005434 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5435 if (INTEL_INFO(dev)->gen < 5)
5436 pipe_config->gmch_pfit.lvds_border_bits =
5437 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5438}
5439
Jesse Barnesacbec812013-09-20 11:29:32 -07005440static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5441 struct intel_crtc_config *pipe_config)
5442{
5443 struct drm_device *dev = crtc->base.dev;
5444 struct drm_i915_private *dev_priv = dev->dev_private;
5445 int pipe = pipe_config->cpu_transcoder;
5446 intel_clock_t clock;
5447 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005448 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005449
5450 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005451 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005452 mutex_unlock(&dev_priv->dpio_lock);
5453
5454 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5455 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5456 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5457 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5458 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5459
Ville Syrjäläf6466282013-10-14 14:50:31 +03005460 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005461
Ville Syrjäläf6466282013-10-14 14:50:31 +03005462 /* clock.dot is the fast clock */
5463 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005464}
5465
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005466static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5467 struct intel_crtc_config *pipe_config)
5468{
5469 struct drm_device *dev = crtc->base.dev;
5470 struct drm_i915_private *dev_priv = dev->dev_private;
5471 uint32_t tmp;
5472
Daniel Vettere143a212013-07-04 12:01:15 +02005473 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005474 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005475
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005476 tmp = I915_READ(PIPECONF(crtc->pipe));
5477 if (!(tmp & PIPECONF_ENABLE))
5478 return false;
5479
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005480 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5481 switch (tmp & PIPECONF_BPC_MASK) {
5482 case PIPECONF_6BPC:
5483 pipe_config->pipe_bpp = 18;
5484 break;
5485 case PIPECONF_8BPC:
5486 pipe_config->pipe_bpp = 24;
5487 break;
5488 case PIPECONF_10BPC:
5489 pipe_config->pipe_bpp = 30;
5490 break;
5491 default:
5492 break;
5493 }
5494 }
5495
Ville Syrjälä282740f2013-09-04 18:30:03 +03005496 if (INTEL_INFO(dev)->gen < 4)
5497 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5498
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005499 intel_get_pipe_timings(crtc, pipe_config);
5500
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005501 i9xx_get_pfit_config(crtc, pipe_config);
5502
Daniel Vetter6c49f242013-06-06 12:45:25 +02005503 if (INTEL_INFO(dev)->gen >= 4) {
5504 tmp = I915_READ(DPLL_MD(crtc->pipe));
5505 pipe_config->pixel_multiplier =
5506 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5507 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005508 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005509 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5510 tmp = I915_READ(DPLL(crtc->pipe));
5511 pipe_config->pixel_multiplier =
5512 ((tmp & SDVO_MULTIPLIER_MASK)
5513 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5514 } else {
5515 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5516 * port and will be fixed up in the encoder->get_config
5517 * function. */
5518 pipe_config->pixel_multiplier = 1;
5519 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005520 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5521 if (!IS_VALLEYVIEW(dev)) {
5522 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5523 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005524 } else {
5525 /* Mask out read-only status bits. */
5526 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5527 DPLL_PORTC_READY_MASK |
5528 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005529 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005530
Jesse Barnesacbec812013-09-20 11:29:32 -07005531 if (IS_VALLEYVIEW(dev))
5532 vlv_crtc_clock_get(crtc, pipe_config);
5533 else
5534 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005535
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005536 return true;
5537}
5538
Paulo Zanonidde86e22012-12-01 12:04:25 -02005539static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005540{
5541 struct drm_i915_private *dev_priv = dev->dev_private;
5542 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005543 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005544 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005545 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005546 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005547 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005548 bool has_ck505 = false;
5549 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005550
5551 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005552 list_for_each_entry(encoder, &mode_config->encoder_list,
5553 base.head) {
5554 switch (encoder->type) {
5555 case INTEL_OUTPUT_LVDS:
5556 has_panel = true;
5557 has_lvds = true;
5558 break;
5559 case INTEL_OUTPUT_EDP:
5560 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005561 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005562 has_cpu_edp = true;
5563 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005564 }
5565 }
5566
Keith Packard99eb6a02011-09-26 14:29:12 -07005567 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005568 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005569 can_ssc = has_ck505;
5570 } else {
5571 has_ck505 = false;
5572 can_ssc = true;
5573 }
5574
Imre Deak2de69052013-05-08 13:14:04 +03005575 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5576 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005577
5578 /* Ironlake: try to setup display ref clock before DPLL
5579 * enabling. This is only under driver's control after
5580 * PCH B stepping, previous chipset stepping should be
5581 * ignoring this setting.
5582 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005583 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005584
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005585 /* As we must carefully and slowly disable/enable each source in turn,
5586 * compute the final state we want first and check if we need to
5587 * make any changes at all.
5588 */
5589 final = val;
5590 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005591 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005592 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005593 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005594 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5595
5596 final &= ~DREF_SSC_SOURCE_MASK;
5597 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5598 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005599
Keith Packard199e5d72011-09-22 12:01:57 -07005600 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005601 final |= DREF_SSC_SOURCE_ENABLE;
5602
5603 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5604 final |= DREF_SSC1_ENABLE;
5605
5606 if (has_cpu_edp) {
5607 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5608 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5609 else
5610 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5611 } else
5612 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5613 } else {
5614 final |= DREF_SSC_SOURCE_DISABLE;
5615 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5616 }
5617
5618 if (final == val)
5619 return;
5620
5621 /* Always enable nonspread source */
5622 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5623
5624 if (has_ck505)
5625 val |= DREF_NONSPREAD_CK505_ENABLE;
5626 else
5627 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5628
5629 if (has_panel) {
5630 val &= ~DREF_SSC_SOURCE_MASK;
5631 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005632
Keith Packard199e5d72011-09-22 12:01:57 -07005633 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005634 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005635 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005636 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005637 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005638 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005639
5640 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005641 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005642 POSTING_READ(PCH_DREF_CONTROL);
5643 udelay(200);
5644
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005645 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005646
5647 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005648 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005649 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005650 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005651 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005652 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005653 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005654 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005655 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005656 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005657
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005658 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005659 POSTING_READ(PCH_DREF_CONTROL);
5660 udelay(200);
5661 } else {
5662 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5663
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005664 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005665
5666 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005667 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005668
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005669 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005670 POSTING_READ(PCH_DREF_CONTROL);
5671 udelay(200);
5672
5673 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005674 val &= ~DREF_SSC_SOURCE_MASK;
5675 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005676
5677 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005678 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005679
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005680 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005681 POSTING_READ(PCH_DREF_CONTROL);
5682 udelay(200);
5683 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005684
5685 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005686}
5687
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005688static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005689{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005690 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005691
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005692 tmp = I915_READ(SOUTH_CHICKEN2);
5693 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5694 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005695
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005696 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5697 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5698 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005699
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005700 tmp = I915_READ(SOUTH_CHICKEN2);
5701 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5702 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005703
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005704 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5705 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5706 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005707}
5708
5709/* WaMPhyProgramming:hsw */
5710static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5711{
5712 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005713
5714 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5715 tmp &= ~(0xFF << 24);
5716 tmp |= (0x12 << 24);
5717 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5718
Paulo Zanonidde86e22012-12-01 12:04:25 -02005719 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5720 tmp |= (1 << 11);
5721 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5722
5723 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5724 tmp |= (1 << 11);
5725 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5726
Paulo Zanonidde86e22012-12-01 12:04:25 -02005727 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5728 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5729 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5730
5731 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5732 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5733 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5734
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005735 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5736 tmp &= ~(7 << 13);
5737 tmp |= (5 << 13);
5738 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005739
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005740 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5741 tmp &= ~(7 << 13);
5742 tmp |= (5 << 13);
5743 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005744
5745 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5746 tmp &= ~0xFF;
5747 tmp |= 0x1C;
5748 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5749
5750 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5751 tmp &= ~0xFF;
5752 tmp |= 0x1C;
5753 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5754
5755 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5756 tmp &= ~(0xFF << 16);
5757 tmp |= (0x1C << 16);
5758 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5759
5760 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5761 tmp &= ~(0xFF << 16);
5762 tmp |= (0x1C << 16);
5763 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5764
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005765 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5766 tmp |= (1 << 27);
5767 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005768
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005769 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5770 tmp |= (1 << 27);
5771 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005772
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005773 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5774 tmp &= ~(0xF << 28);
5775 tmp |= (4 << 28);
5776 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005777
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005778 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5779 tmp &= ~(0xF << 28);
5780 tmp |= (4 << 28);
5781 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005782}
5783
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005784/* Implements 3 different sequences from BSpec chapter "Display iCLK
5785 * Programming" based on the parameters passed:
5786 * - Sequence to enable CLKOUT_DP
5787 * - Sequence to enable CLKOUT_DP without spread
5788 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5789 */
5790static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5791 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005792{
5793 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005794 uint32_t reg, tmp;
5795
5796 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5797 with_spread = true;
5798 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5799 with_fdi, "LP PCH doesn't have FDI\n"))
5800 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005801
5802 mutex_lock(&dev_priv->dpio_lock);
5803
5804 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5805 tmp &= ~SBI_SSCCTL_DISABLE;
5806 tmp |= SBI_SSCCTL_PATHALT;
5807 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5808
5809 udelay(24);
5810
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005811 if (with_spread) {
5812 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5813 tmp &= ~SBI_SSCCTL_PATHALT;
5814 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005815
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005816 if (with_fdi) {
5817 lpt_reset_fdi_mphy(dev_priv);
5818 lpt_program_fdi_mphy(dev_priv);
5819 }
5820 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005821
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005822 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5823 SBI_GEN0 : SBI_DBUFF0;
5824 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5825 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5826 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005827
5828 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005829}
5830
Paulo Zanoni47701c32013-07-23 11:19:25 -03005831/* Sequence to disable CLKOUT_DP */
5832static void lpt_disable_clkout_dp(struct drm_device *dev)
5833{
5834 struct drm_i915_private *dev_priv = dev->dev_private;
5835 uint32_t reg, tmp;
5836
5837 mutex_lock(&dev_priv->dpio_lock);
5838
5839 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5840 SBI_GEN0 : SBI_DBUFF0;
5841 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5842 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5843 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5844
5845 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5846 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5847 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5848 tmp |= SBI_SSCCTL_PATHALT;
5849 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5850 udelay(32);
5851 }
5852 tmp |= SBI_SSCCTL_DISABLE;
5853 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5854 }
5855
5856 mutex_unlock(&dev_priv->dpio_lock);
5857}
5858
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005859static void lpt_init_pch_refclk(struct drm_device *dev)
5860{
5861 struct drm_mode_config *mode_config = &dev->mode_config;
5862 struct intel_encoder *encoder;
5863 bool has_vga = false;
5864
5865 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5866 switch (encoder->type) {
5867 case INTEL_OUTPUT_ANALOG:
5868 has_vga = true;
5869 break;
5870 }
5871 }
5872
Paulo Zanoni47701c32013-07-23 11:19:25 -03005873 if (has_vga)
5874 lpt_enable_clkout_dp(dev, true, true);
5875 else
5876 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005877}
5878
Paulo Zanonidde86e22012-12-01 12:04:25 -02005879/*
5880 * Initialize reference clocks when the driver loads
5881 */
5882void intel_init_pch_refclk(struct drm_device *dev)
5883{
5884 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5885 ironlake_init_pch_refclk(dev);
5886 else if (HAS_PCH_LPT(dev))
5887 lpt_init_pch_refclk(dev);
5888}
5889
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005890static int ironlake_get_refclk(struct drm_crtc *crtc)
5891{
5892 struct drm_device *dev = crtc->dev;
5893 struct drm_i915_private *dev_priv = dev->dev_private;
5894 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005895 int num_connectors = 0;
5896 bool is_lvds = false;
5897
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005898 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005899 switch (encoder->type) {
5900 case INTEL_OUTPUT_LVDS:
5901 is_lvds = true;
5902 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005903 }
5904 num_connectors++;
5905 }
5906
5907 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5908 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005909 dev_priv->vbt.lvds_ssc_freq);
5910 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005911 }
5912
5913 return 120000;
5914}
5915
Daniel Vetter6ff93602013-04-19 11:24:36 +02005916static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005917{
5918 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5920 int pipe = intel_crtc->pipe;
5921 uint32_t val;
5922
Daniel Vetter78114072013-06-13 00:54:57 +02005923 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005924
Daniel Vetter965e0c42013-03-27 00:44:57 +01005925 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005926 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005927 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005928 break;
5929 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005930 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005931 break;
5932 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005933 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005934 break;
5935 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005936 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005937 break;
5938 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005939 /* Case prevented by intel_choose_pipe_bpp_dither. */
5940 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005941 }
5942
Daniel Vetterd8b32242013-04-25 17:54:44 +02005943 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005944 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5945
Daniel Vetter6ff93602013-04-19 11:24:36 +02005946 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005947 val |= PIPECONF_INTERLACED_ILK;
5948 else
5949 val |= PIPECONF_PROGRESSIVE;
5950
Daniel Vetter50f3b012013-03-27 00:44:56 +01005951 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005952 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005953
Paulo Zanonic8203562012-09-12 10:06:29 -03005954 I915_WRITE(PIPECONF(pipe), val);
5955 POSTING_READ(PIPECONF(pipe));
5956}
5957
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005958/*
5959 * Set up the pipe CSC unit.
5960 *
5961 * Currently only full range RGB to limited range RGB conversion
5962 * is supported, but eventually this should handle various
5963 * RGB<->YCbCr scenarios as well.
5964 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005965static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005966{
5967 struct drm_device *dev = crtc->dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
5969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5970 int pipe = intel_crtc->pipe;
5971 uint16_t coeff = 0x7800; /* 1.0 */
5972
5973 /*
5974 * TODO: Check what kind of values actually come out of the pipe
5975 * with these coeff/postoff values and adjust to get the best
5976 * accuracy. Perhaps we even need to take the bpc value into
5977 * consideration.
5978 */
5979
Daniel Vetter50f3b012013-03-27 00:44:56 +01005980 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005981 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5982
5983 /*
5984 * GY/GU and RY/RU should be the other way around according
5985 * to BSpec, but reality doesn't agree. Just set them up in
5986 * a way that results in the correct picture.
5987 */
5988 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5989 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5990
5991 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5992 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5993
5994 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5995 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5996
5997 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5998 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5999 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6000
6001 if (INTEL_INFO(dev)->gen > 6) {
6002 uint16_t postoff = 0;
6003
Daniel Vetter50f3b012013-03-27 00:44:56 +01006004 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006005 postoff = (16 * (1 << 13) / 255) & 0x1fff;
6006
6007 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6008 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6009 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6010
6011 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6012 } else {
6013 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6014
Daniel Vetter50f3b012013-03-27 00:44:56 +01006015 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006016 mode |= CSC_BLACK_SCREEN_OFFSET;
6017
6018 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6019 }
6020}
6021
Daniel Vetter6ff93602013-04-19 11:24:36 +02006022static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006023{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006024 struct drm_device *dev = crtc->dev;
6025 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006027 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006028 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006029 uint32_t val;
6030
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006031 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006032
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006033 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006034 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6035
Daniel Vetter6ff93602013-04-19 11:24:36 +02006036 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006037 val |= PIPECONF_INTERLACED_ILK;
6038 else
6039 val |= PIPECONF_PROGRESSIVE;
6040
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006041 I915_WRITE(PIPECONF(cpu_transcoder), val);
6042 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006043
6044 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6045 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006046
6047 if (IS_BROADWELL(dev)) {
6048 val = 0;
6049
6050 switch (intel_crtc->config.pipe_bpp) {
6051 case 18:
6052 val |= PIPEMISC_DITHER_6_BPC;
6053 break;
6054 case 24:
6055 val |= PIPEMISC_DITHER_8_BPC;
6056 break;
6057 case 30:
6058 val |= PIPEMISC_DITHER_10_BPC;
6059 break;
6060 case 36:
6061 val |= PIPEMISC_DITHER_12_BPC;
6062 break;
6063 default:
6064 /* Case prevented by pipe_config_set_bpp. */
6065 BUG();
6066 }
6067
6068 if (intel_crtc->config.dither)
6069 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6070
6071 I915_WRITE(PIPEMISC(pipe), val);
6072 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006073}
6074
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006075static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006076 intel_clock_t *clock,
6077 bool *has_reduced_clock,
6078 intel_clock_t *reduced_clock)
6079{
6080 struct drm_device *dev = crtc->dev;
6081 struct drm_i915_private *dev_priv = dev->dev_private;
6082 struct intel_encoder *intel_encoder;
6083 int refclk;
6084 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006085 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006086
6087 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6088 switch (intel_encoder->type) {
6089 case INTEL_OUTPUT_LVDS:
6090 is_lvds = true;
6091 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006092 }
6093 }
6094
6095 refclk = ironlake_get_refclk(crtc);
6096
6097 /*
6098 * Returns a set of divisors for the desired target clock with the given
6099 * refclk, or FALSE. The returned values represent the clock equation:
6100 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6101 */
6102 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006103 ret = dev_priv->display.find_dpll(limit, crtc,
6104 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006105 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006106 if (!ret)
6107 return false;
6108
6109 if (is_lvds && dev_priv->lvds_downclock_avail) {
6110 /*
6111 * Ensure we match the reduced clock's P to the target clock.
6112 * If the clocks don't match, we can't switch the display clock
6113 * by using the FP0/FP1. In such case we will disable the LVDS
6114 * downclock feature.
6115 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006116 *has_reduced_clock =
6117 dev_priv->display.find_dpll(limit, crtc,
6118 dev_priv->lvds_downclock,
6119 refclk, clock,
6120 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006121 }
6122
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006123 return true;
6124}
6125
Paulo Zanonid4b19312012-11-29 11:29:32 -02006126int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6127{
6128 /*
6129 * Account for spread spectrum to avoid
6130 * oversubscribing the link. Max center spread
6131 * is 2.5%; use 5% for safety's sake.
6132 */
6133 u32 bps = target_clock * bpp * 21 / 20;
6134 return bps / (link_bw * 8) + 1;
6135}
6136
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006137static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006138{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006139 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006140}
6141
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006142static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006143 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006144 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006145{
6146 struct drm_crtc *crtc = &intel_crtc->base;
6147 struct drm_device *dev = crtc->dev;
6148 struct drm_i915_private *dev_priv = dev->dev_private;
6149 struct intel_encoder *intel_encoder;
6150 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006151 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006152 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006153
6154 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6155 switch (intel_encoder->type) {
6156 case INTEL_OUTPUT_LVDS:
6157 is_lvds = true;
6158 break;
6159 case INTEL_OUTPUT_SDVO:
6160 case INTEL_OUTPUT_HDMI:
6161 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006162 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006163 }
6164
6165 num_connectors++;
6166 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006167
Chris Wilsonc1858122010-12-03 21:35:48 +00006168 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006169 factor = 21;
6170 if (is_lvds) {
6171 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006172 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006173 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006174 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006175 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006176 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006177
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006178 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006179 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006180
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006181 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6182 *fp2 |= FP_CB_TUNE;
6183
Chris Wilson5eddb702010-09-11 13:48:45 +01006184 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006185
Eric Anholta07d6782011-03-30 13:01:08 -07006186 if (is_lvds)
6187 dpll |= DPLLB_MODE_LVDS;
6188 else
6189 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006190
Daniel Vetteref1b4602013-06-01 17:17:04 +02006191 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6192 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006193
6194 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006195 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006196 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006197 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006198
Eric Anholta07d6782011-03-30 13:01:08 -07006199 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006200 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006201 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006202 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006203
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006204 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006205 case 5:
6206 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6207 break;
6208 case 7:
6209 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6210 break;
6211 case 10:
6212 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6213 break;
6214 case 14:
6215 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6216 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006217 }
6218
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006219 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006220 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006221 else
6222 dpll |= PLL_REF_INPUT_DREFCLK;
6223
Daniel Vetter959e16d2013-06-05 13:34:21 +02006224 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006225}
6226
Jesse Barnes79e53942008-11-07 14:24:08 -08006227static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006228 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006229 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006230{
6231 struct drm_device *dev = crtc->dev;
6232 struct drm_i915_private *dev_priv = dev->dev_private;
6233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6234 int pipe = intel_crtc->pipe;
6235 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006236 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006237 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006238 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006239 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006240 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006241 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006242 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006243 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006244
6245 for_each_encoder_on_crtc(dev, crtc, encoder) {
6246 switch (encoder->type) {
6247 case INTEL_OUTPUT_LVDS:
6248 is_lvds = true;
6249 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006250 }
6251
6252 num_connectors++;
6253 }
6254
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006255 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6256 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6257
Daniel Vetterff9a6752013-06-01 17:16:21 +02006258 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006259 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006260 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006261 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6262 return -EINVAL;
6263 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006264 /* Compat-code for transition, will disappear. */
6265 if (!intel_crtc->config.clock_set) {
6266 intel_crtc->config.dpll.n = clock.n;
6267 intel_crtc->config.dpll.m1 = clock.m1;
6268 intel_crtc->config.dpll.m2 = clock.m2;
6269 intel_crtc->config.dpll.p1 = clock.p1;
6270 intel_crtc->config.dpll.p2 = clock.p2;
6271 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006272
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006273 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006274 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006275 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006276 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006277 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006278
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006279 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006280 &fp, &reduced_clock,
6281 has_reduced_clock ? &fp2 : NULL);
6282
Daniel Vetter959e16d2013-06-05 13:34:21 +02006283 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006284 intel_crtc->config.dpll_hw_state.fp0 = fp;
6285 if (has_reduced_clock)
6286 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6287 else
6288 intel_crtc->config.dpll_hw_state.fp1 = fp;
6289
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006290 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006291 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006292 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6293 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006294 return -EINVAL;
6295 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006296 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006297 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006298
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006299 if (intel_crtc->config.has_dp_encoder)
6300 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006301
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006302 if (is_lvds && has_reduced_clock && i915_powersave)
6303 intel_crtc->lowfreq_avail = true;
6304 else
6305 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006306
Daniel Vetter8a654f32013-06-01 17:16:22 +02006307 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006308
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006309 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006310 intel_cpu_transcoder_set_m_n(intel_crtc,
6311 &intel_crtc->config.fdi_m_n);
6312 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006313
Daniel Vetter6ff93602013-04-19 11:24:36 +02006314 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006315
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006316 /* Set up the display plane register */
6317 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006318 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006319
Daniel Vetter94352cf2012-07-05 22:51:56 +02006320 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006321
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006322 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006323}
6324
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006325static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6326 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006327{
6328 struct drm_device *dev = crtc->base.dev;
6329 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006330 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006331
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006332 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6333 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6334 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6335 & ~TU_SIZE_MASK;
6336 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6337 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6338 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6339}
6340
6341static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6342 enum transcoder transcoder,
6343 struct intel_link_m_n *m_n)
6344{
6345 struct drm_device *dev = crtc->base.dev;
6346 struct drm_i915_private *dev_priv = dev->dev_private;
6347 enum pipe pipe = crtc->pipe;
6348
6349 if (INTEL_INFO(dev)->gen >= 5) {
6350 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6351 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6352 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6353 & ~TU_SIZE_MASK;
6354 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6355 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6356 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6357 } else {
6358 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6359 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6360 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6361 & ~TU_SIZE_MASK;
6362 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6363 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6364 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6365 }
6366}
6367
6368void intel_dp_get_m_n(struct intel_crtc *crtc,
6369 struct intel_crtc_config *pipe_config)
6370{
6371 if (crtc->config.has_pch_encoder)
6372 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6373 else
6374 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6375 &pipe_config->dp_m_n);
6376}
6377
Daniel Vetter72419202013-04-04 13:28:53 +02006378static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6379 struct intel_crtc_config *pipe_config)
6380{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006381 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6382 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006383}
6384
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006385static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6386 struct intel_crtc_config *pipe_config)
6387{
6388 struct drm_device *dev = crtc->base.dev;
6389 struct drm_i915_private *dev_priv = dev->dev_private;
6390 uint32_t tmp;
6391
6392 tmp = I915_READ(PF_CTL(crtc->pipe));
6393
6394 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006395 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006396 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6397 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006398
6399 /* We currently do not free assignements of panel fitters on
6400 * ivb/hsw (since we don't use the higher upscaling modes which
6401 * differentiates them) so just WARN about this case for now. */
6402 if (IS_GEN7(dev)) {
6403 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6404 PF_PIPE_SEL_IVB(crtc->pipe));
6405 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006406 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006407}
6408
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006409static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6410 struct intel_crtc_config *pipe_config)
6411{
6412 struct drm_device *dev = crtc->base.dev;
6413 struct drm_i915_private *dev_priv = dev->dev_private;
6414 uint32_t tmp;
6415
Daniel Vettere143a212013-07-04 12:01:15 +02006416 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006417 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006418
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006419 tmp = I915_READ(PIPECONF(crtc->pipe));
6420 if (!(tmp & PIPECONF_ENABLE))
6421 return false;
6422
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006423 switch (tmp & PIPECONF_BPC_MASK) {
6424 case PIPECONF_6BPC:
6425 pipe_config->pipe_bpp = 18;
6426 break;
6427 case PIPECONF_8BPC:
6428 pipe_config->pipe_bpp = 24;
6429 break;
6430 case PIPECONF_10BPC:
6431 pipe_config->pipe_bpp = 30;
6432 break;
6433 case PIPECONF_12BPC:
6434 pipe_config->pipe_bpp = 36;
6435 break;
6436 default:
6437 break;
6438 }
6439
Daniel Vetterab9412b2013-05-03 11:49:46 +02006440 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006441 struct intel_shared_dpll *pll;
6442
Daniel Vetter88adfff2013-03-28 10:42:01 +01006443 pipe_config->has_pch_encoder = true;
6444
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006445 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6446 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6447 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006448
6449 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006450
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006451 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006452 pipe_config->shared_dpll =
6453 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006454 } else {
6455 tmp = I915_READ(PCH_DPLL_SEL);
6456 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6457 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6458 else
6459 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6460 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006461
6462 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6463
6464 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6465 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006466
6467 tmp = pipe_config->dpll_hw_state.dpll;
6468 pipe_config->pixel_multiplier =
6469 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6470 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006471
6472 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006473 } else {
6474 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006475 }
6476
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006477 intel_get_pipe_timings(crtc, pipe_config);
6478
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006479 ironlake_get_pfit_config(crtc, pipe_config);
6480
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006481 return true;
6482}
6483
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006484static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6485{
6486 struct drm_device *dev = dev_priv->dev;
6487 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6488 struct intel_crtc *crtc;
6489 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006490 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006491
6492 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
Paulo Zanoni798183c2013-12-06 20:29:01 -02006493 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006494 pipe_name(crtc->pipe));
6495
6496 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6497 WARN(plls->spll_refcount, "SPLL enabled\n");
6498 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6499 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6500 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6501 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6502 "CPU PWM1 enabled\n");
6503 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6504 "CPU PWM2 enabled\n");
6505 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6506 "PCH PWM1 enabled\n");
6507 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6508 "Utility pin enabled\n");
6509 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6510
6511 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6512 val = I915_READ(DEIMR);
6513 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6514 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6515 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006516 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006517 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6518 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6519}
6520
6521/*
6522 * This function implements pieces of two sequences from BSpec:
6523 * - Sequence for display software to disable LCPLL
6524 * - Sequence for display software to allow package C8+
6525 * The steps implemented here are just the steps that actually touch the LCPLL
6526 * register. Callers should take care of disabling all the display engine
6527 * functions, doing the mode unset, fixing interrupts, etc.
6528 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006529static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6530 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006531{
6532 uint32_t val;
6533
6534 assert_can_disable_lcpll(dev_priv);
6535
6536 val = I915_READ(LCPLL_CTL);
6537
6538 if (switch_to_fclk) {
6539 val |= LCPLL_CD_SOURCE_FCLK;
6540 I915_WRITE(LCPLL_CTL, val);
6541
6542 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6543 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6544 DRM_ERROR("Switching to FCLK failed\n");
6545
6546 val = I915_READ(LCPLL_CTL);
6547 }
6548
6549 val |= LCPLL_PLL_DISABLE;
6550 I915_WRITE(LCPLL_CTL, val);
6551 POSTING_READ(LCPLL_CTL);
6552
6553 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6554 DRM_ERROR("LCPLL still locked\n");
6555
6556 val = I915_READ(D_COMP);
6557 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006558 mutex_lock(&dev_priv->rps.hw_lock);
6559 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6560 DRM_ERROR("Failed to disable D_COMP\n");
6561 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006562 POSTING_READ(D_COMP);
6563 ndelay(100);
6564
6565 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6566 DRM_ERROR("D_COMP RCOMP still in progress\n");
6567
6568 if (allow_power_down) {
6569 val = I915_READ(LCPLL_CTL);
6570 val |= LCPLL_POWER_DOWN_ALLOW;
6571 I915_WRITE(LCPLL_CTL, val);
6572 POSTING_READ(LCPLL_CTL);
6573 }
6574}
6575
6576/*
6577 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6578 * source.
6579 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006580static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006581{
6582 uint32_t val;
6583
6584 val = I915_READ(LCPLL_CTL);
6585
6586 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6587 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6588 return;
6589
Paulo Zanoni215733f2013-08-19 13:18:07 -03006590 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6591 * we'll hang the machine! */
Deepak Sc8d9a592013-11-23 14:55:42 +05306592 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03006593
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006594 if (val & LCPLL_POWER_DOWN_ALLOW) {
6595 val &= ~LCPLL_POWER_DOWN_ALLOW;
6596 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006597 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006598 }
6599
6600 val = I915_READ(D_COMP);
6601 val |= D_COMP_COMP_FORCE;
6602 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006603 mutex_lock(&dev_priv->rps.hw_lock);
6604 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6605 DRM_ERROR("Failed to enable D_COMP\n");
6606 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006607 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006608
6609 val = I915_READ(LCPLL_CTL);
6610 val &= ~LCPLL_PLL_DISABLE;
6611 I915_WRITE(LCPLL_CTL, val);
6612
6613 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6614 DRM_ERROR("LCPLL not locked yet\n");
6615
6616 if (val & LCPLL_CD_SOURCE_FCLK) {
6617 val = I915_READ(LCPLL_CTL);
6618 val &= ~LCPLL_CD_SOURCE_FCLK;
6619 I915_WRITE(LCPLL_CTL, val);
6620
6621 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6622 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6623 DRM_ERROR("Switching back to LCPLL failed\n");
6624 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006625
Deepak Sc8d9a592013-11-23 14:55:42 +05306626 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006627}
6628
Paulo Zanonic67a4702013-08-19 13:18:09 -03006629void hsw_enable_pc8_work(struct work_struct *__work)
6630{
6631 struct drm_i915_private *dev_priv =
6632 container_of(to_delayed_work(__work), struct drm_i915_private,
6633 pc8.enable_work);
6634 struct drm_device *dev = dev_priv->dev;
6635 uint32_t val;
6636
6637 if (dev_priv->pc8.enabled)
6638 return;
6639
6640 DRM_DEBUG_KMS("Enabling package C8+\n");
6641
6642 dev_priv->pc8.enabled = true;
6643
6644 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6645 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6646 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6647 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6648 }
6649
6650 lpt_disable_clkout_dp(dev);
6651 hsw_pc8_disable_interrupts(dev);
6652 hsw_disable_lcpll(dev_priv, true, true);
6653}
6654
6655static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6656{
6657 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6658 WARN(dev_priv->pc8.disable_count < 1,
6659 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6660
6661 dev_priv->pc8.disable_count--;
6662 if (dev_priv->pc8.disable_count != 0)
6663 return;
6664
6665 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006666 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006667}
6668
6669static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6670{
6671 struct drm_device *dev = dev_priv->dev;
6672 uint32_t val;
6673
6674 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6675 WARN(dev_priv->pc8.disable_count < 0,
6676 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6677
6678 dev_priv->pc8.disable_count++;
6679 if (dev_priv->pc8.disable_count != 1)
6680 return;
6681
6682 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6683 if (!dev_priv->pc8.enabled)
6684 return;
6685
6686 DRM_DEBUG_KMS("Disabling package C8+\n");
6687
6688 hsw_restore_lcpll(dev_priv);
6689 hsw_pc8_restore_interrupts(dev);
6690 lpt_init_pch_refclk(dev);
6691
6692 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6693 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6694 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6695 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6696 }
6697
6698 intel_prepare_ddi(dev);
6699 i915_gem_init_swizzling(dev);
6700 mutex_lock(&dev_priv->rps.hw_lock);
6701 gen6_update_ring_freq(dev);
6702 mutex_unlock(&dev_priv->rps.hw_lock);
6703 dev_priv->pc8.enabled = false;
6704}
6705
6706void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6707{
6708 mutex_lock(&dev_priv->pc8.lock);
6709 __hsw_enable_package_c8(dev_priv);
6710 mutex_unlock(&dev_priv->pc8.lock);
6711}
6712
6713void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6714{
6715 mutex_lock(&dev_priv->pc8.lock);
6716 __hsw_disable_package_c8(dev_priv);
6717 mutex_unlock(&dev_priv->pc8.lock);
6718}
6719
6720static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6721{
6722 struct drm_device *dev = dev_priv->dev;
6723 struct intel_crtc *crtc;
6724 uint32_t val;
6725
6726 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6727 if (crtc->base.enabled)
6728 return false;
6729
6730 /* This case is still possible since we have the i915.disable_power_well
6731 * parameter and also the KVMr or something else might be requesting the
6732 * power well. */
6733 val = I915_READ(HSW_PWR_WELL_DRIVER);
6734 if (val != 0) {
6735 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6736 return false;
6737 }
6738
6739 return true;
6740}
6741
6742/* Since we're called from modeset_global_resources there's no way to
6743 * symmetrically increase and decrease the refcount, so we use
6744 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6745 * or not.
6746 */
6747static void hsw_update_package_c8(struct drm_device *dev)
6748{
6749 struct drm_i915_private *dev_priv = dev->dev_private;
6750 bool allow;
6751
6752 if (!i915_enable_pc8)
6753 return;
6754
6755 mutex_lock(&dev_priv->pc8.lock);
6756
6757 allow = hsw_can_enable_package_c8(dev_priv);
6758
6759 if (allow == dev_priv->pc8.requirements_met)
6760 goto done;
6761
6762 dev_priv->pc8.requirements_met = allow;
6763
6764 if (allow)
6765 __hsw_enable_package_c8(dev_priv);
6766 else
6767 __hsw_disable_package_c8(dev_priv);
6768
6769done:
6770 mutex_unlock(&dev_priv->pc8.lock);
6771}
6772
6773static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6774{
Chris Wilson34581222013-11-18 18:32:36 -08006775 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006776 if (!dev_priv->pc8.gpu_idle) {
6777 dev_priv->pc8.gpu_idle = true;
Chris Wilson34581222013-11-18 18:32:36 -08006778 __hsw_enable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006779 }
Chris Wilson34581222013-11-18 18:32:36 -08006780 mutex_unlock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006781}
6782
6783static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6784{
Chris Wilson34581222013-11-18 18:32:36 -08006785 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006786 if (dev_priv->pc8.gpu_idle) {
6787 dev_priv->pc8.gpu_idle = false;
Chris Wilson34581222013-11-18 18:32:36 -08006788 __hsw_disable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006789 }
Chris Wilson34581222013-11-18 18:32:36 -08006790 mutex_unlock(&dev_priv->pc8.lock);
Daniel Vetter94352cf2012-07-05 22:51:56 +02006791}
Eric Anholtf564048e2011-03-30 13:01:02 -07006792
Imre Deak6efdf352013-10-16 17:25:52 +03006793#define for_each_power_domain(domain, mask) \
6794 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6795 if ((1 << (domain)) & (mask))
6796
6797static unsigned long get_pipe_power_domains(struct drm_device *dev,
6798 enum pipe pipe, bool pfit_enabled)
6799{
6800 unsigned long mask;
6801 enum transcoder transcoder;
6802
6803 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6804
6805 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6806 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6807 if (pfit_enabled)
6808 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6809
6810 return mask;
6811}
6812
Imre Deakbaa70702013-10-25 17:36:48 +03006813void intel_display_set_init_power(struct drm_device *dev, bool enable)
6814{
6815 struct drm_i915_private *dev_priv = dev->dev_private;
6816
6817 if (dev_priv->power_domains.init_power_on == enable)
6818 return;
6819
6820 if (enable)
6821 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6822 else
6823 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6824
6825 dev_priv->power_domains.init_power_on = enable;
6826}
6827
Imre Deak4f074122013-10-16 17:25:51 +03006828static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006829{
Imre Deak6efdf352013-10-16 17:25:52 +03006830 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006831 struct intel_crtc *crtc;
6832
Imre Deak6efdf352013-10-16 17:25:52 +03006833 /*
6834 * First get all needed power domains, then put all unneeded, to avoid
6835 * any unnecessary toggling of the power wells.
6836 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006837 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006838 enum intel_display_power_domain domain;
6839
Jesse Barnes79e53942008-11-07 14:24:08 -08006840 if (!crtc->base.enabled)
6841 continue;
6842
Imre Deak6efdf352013-10-16 17:25:52 +03006843 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6844 crtc->pipe,
6845 crtc->config.pch_pfit.enabled);
6846
6847 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6848 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006849 }
6850
Imre Deak6efdf352013-10-16 17:25:52 +03006851 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6852 enum intel_display_power_domain domain;
6853
6854 for_each_power_domain(domain, crtc->enabled_power_domains)
6855 intel_display_power_put(dev, domain);
6856
6857 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6858 }
Imre Deakbaa70702013-10-25 17:36:48 +03006859
6860 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006861}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006862
Imre Deak4f074122013-10-16 17:25:51 +03006863static void haswell_modeset_global_resources(struct drm_device *dev)
6864{
6865 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006866 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006867}
6868
6869static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6870 int x, int y,
6871 struct drm_framebuffer *fb)
6872{
6873 struct drm_device *dev = crtc->dev;
6874 struct drm_i915_private *dev_priv = dev->dev_private;
6875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6876 int plane = intel_crtc->plane;
6877 int ret;
6878
6879 if (!intel_ddi_pll_mode_set(crtc))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006880 return -EINVAL;
Eric Anholtbad720f2009-10-22 16:11:14 -07006881
Chris Wilson560b85b2010-08-07 11:01:38 +01006882 if (intel_crtc->config.has_dp_encoder)
6883 intel_dp_set_m_n(intel_crtc);
6884
6885 intel_crtc->lowfreq_avail = false;
6886
6887 intel_set_pipe_timings(intel_crtc);
6888
6889 if (intel_crtc->config.has_pch_encoder) {
6890 intel_cpu_transcoder_set_m_n(intel_crtc,
6891 &intel_crtc->config.fdi_m_n);
6892 }
6893
6894 haswell_set_pipeconf(crtc);
6895
6896 intel_set_pipe_csc(crtc);
6897
6898 /* Set up the display plane register */
6899 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6900 POSTING_READ(DSPCNTR(plane));
6901
6902 ret = intel_pipe_set_base(crtc, x, y, fb);
6903
Chris Wilson560b85b2010-08-07 11:01:38 +01006904 return ret;
6905}
6906
6907static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6908 struct intel_crtc_config *pipe_config)
6909{
6910 struct drm_device *dev = crtc->base.dev;
6911 struct drm_i915_private *dev_priv = dev->dev_private;
6912 enum intel_display_power_domain pfit_domain;
6913 uint32_t tmp;
6914
6915 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6916 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6917
6918 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6919 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6920 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006921 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01006922 default:
6923 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006924 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6925 case TRANS_DDI_EDP_INPUT_A_ON:
6926 trans_edp_pipe = PIPE_A;
6927 break;
6928 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6929 trans_edp_pipe = PIPE_B;
6930 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01006931 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006932 trans_edp_pipe = PIPE_C;
6933 break;
6934 }
6935
Chris Wilson6b383a72010-09-13 13:54:26 +01006936 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006937 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6938 }
6939
6940 if (!intel_display_power_enabled(dev,
6941 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6942 return false;
6943
6944 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6945 if (!(tmp & PIPECONF_ENABLE))
6946 return false;
6947
6948 /*
6949 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6950 * DDI E. So just check whether this pipe is wired to DDI E and whether
6951 * the PCH transcoder is on.
6952 */
6953 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6954 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6955 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6956 pipe_config->has_pch_encoder = true;
6957
6958 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6959 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6960 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6961
6962 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6963 }
6964
Chris Wilson560b85b2010-08-07 11:01:38 +01006965 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006966
6967 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6968 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01006969 ironlake_get_pfit_config(crtc, pipe_config);
6970
6971 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6972 (I915_READ(IPS_CTL) & IPS_ENABLE);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006973
6974 pipe_config->pixel_multiplier = 1;
6975
6976 return true;
6977}
Jesse Barnes79e53942008-11-07 14:24:08 -08006978
Chris Wilson05394f32010-11-08 19:18:58 +00006979static int intel_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006980 int x, int y,
6981 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006982{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006983 struct drm_device *dev = crtc->dev;
6984 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006985 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006987 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006988 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006989 int ret;
6990
Eric Anholt0b701d22011-03-30 13:01:03 -07006991 drm_vblank_pre_modeset(dev, pipe);
6992
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006993 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6994
Jesse Barnes79e53942008-11-07 14:24:08 -08006995 drm_vblank_post_modeset(dev, pipe);
6996
Daniel Vetter9256aa12012-10-31 19:26:13 +01006997 if (ret != 0)
6998 return ret;
6999
7000 for_each_encoder_on_crtc(dev, crtc, encoder) {
7001 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7002 encoder->base.base.id,
7003 drm_get_encoder_name(&encoder->base),
7004 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02007005 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007006 }
7007
7008 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007009}
7010
Jani Nikula1a915102013-10-16 12:34:48 +03007011static struct {
7012 int clock;
7013 u32 config;
7014} hdmi_audio_clock[] = {
7015 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7016 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7017 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7018 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7019 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7020 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7021 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7022 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7023 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7024 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7025};
7026
7027/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7028static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7029{
7030 int i;
7031
7032 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7033 if (mode->clock == hdmi_audio_clock[i].clock)
7034 break;
7035 }
7036
7037 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7038 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7039 i = 1;
7040 }
7041
7042 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7043 hdmi_audio_clock[i].clock,
7044 hdmi_audio_clock[i].config);
7045
7046 return hdmi_audio_clock[i].config;
7047}
7048
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007049static bool intel_eld_uptodate(struct drm_connector *connector,
7050 int reg_eldv, uint32_t bits_eldv,
7051 int reg_elda, uint32_t bits_elda,
7052 int reg_edid)
7053{
7054 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7055 uint8_t *eld = connector->eld;
7056 uint32_t i;
7057
7058 i = I915_READ(reg_eldv);
7059 i &= bits_eldv;
7060
7061 if (!eld[0])
7062 return !i;
7063
7064 if (!i)
7065 return false;
7066
7067 i = I915_READ(reg_elda);
7068 i &= ~bits_elda;
7069 I915_WRITE(reg_elda, i);
7070
7071 for (i = 0; i < eld[2]; i++)
7072 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7073 return false;
7074
7075 return true;
7076}
7077
Wu Fengguange0dac652011-09-05 14:25:34 +08007078static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007079 struct drm_crtc *crtc,
7080 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007081{
7082 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7083 uint8_t *eld = connector->eld;
7084 uint32_t eldv;
7085 uint32_t len;
7086 uint32_t i;
7087
7088 i = I915_READ(G4X_AUD_VID_DID);
7089
7090 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7091 eldv = G4X_ELDV_DEVCL_DEVBLC;
7092 else
7093 eldv = G4X_ELDV_DEVCTG;
7094
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007095 if (intel_eld_uptodate(connector,
7096 G4X_AUD_CNTL_ST, eldv,
7097 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7098 G4X_HDMIW_HDMIEDID))
7099 return;
7100
Wu Fengguange0dac652011-09-05 14:25:34 +08007101 i = I915_READ(G4X_AUD_CNTL_ST);
7102 i &= ~(eldv | G4X_ELD_ADDR);
7103 len = (i >> 9) & 0x1f; /* ELD buffer size */
7104 I915_WRITE(G4X_AUD_CNTL_ST, i);
7105
7106 if (!eld[0])
7107 return;
7108
7109 len = min_t(uint8_t, eld[2], len);
7110 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7111 for (i = 0; i < len; i++)
7112 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7113
7114 i = I915_READ(G4X_AUD_CNTL_ST);
7115 i |= eldv;
7116 I915_WRITE(G4X_AUD_CNTL_ST, i);
7117}
7118
Wang Xingchao83358c852012-08-16 22:43:37 +08007119static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007120 struct drm_crtc *crtc,
7121 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007122{
7123 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7124 uint8_t *eld = connector->eld;
7125 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007127 uint32_t eldv;
7128 uint32_t i;
7129 int len;
7130 int pipe = to_intel_crtc(crtc)->pipe;
7131 int tmp;
7132
7133 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7134 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7135 int aud_config = HSW_AUD_CFG(pipe);
7136 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7137
7138
7139 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7140
7141 /* Audio output enable */
7142 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7143 tmp = I915_READ(aud_cntrl_st2);
7144 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7145 I915_WRITE(aud_cntrl_st2, tmp);
7146
7147 /* Wait for 1 vertical blank */
7148 intel_wait_for_vblank(dev, pipe);
7149
7150 /* Set ELD valid state */
7151 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007152 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007153 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7154 I915_WRITE(aud_cntrl_st2, tmp);
7155 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007156 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007157
7158 /* Enable HDMI mode */
7159 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007160 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007161 /* clear N_programing_enable and N_value_index */
7162 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7163 I915_WRITE(aud_config, tmp);
7164
7165 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7166
7167 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007168 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007169
7170 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7171 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7172 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7173 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007174 } else {
7175 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7176 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007177
7178 if (intel_eld_uptodate(connector,
7179 aud_cntrl_st2, eldv,
7180 aud_cntl_st, IBX_ELD_ADDRESS,
7181 hdmiw_hdmiedid))
7182 return;
7183
7184 i = I915_READ(aud_cntrl_st2);
7185 i &= ~eldv;
7186 I915_WRITE(aud_cntrl_st2, i);
7187
7188 if (!eld[0])
7189 return;
7190
7191 i = I915_READ(aud_cntl_st);
7192 i &= ~IBX_ELD_ADDRESS;
7193 I915_WRITE(aud_cntl_st, i);
7194 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7195 DRM_DEBUG_DRIVER("port num:%d\n", i);
7196
7197 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7198 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7199 for (i = 0; i < len; i++)
7200 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7201
7202 i = I915_READ(aud_cntrl_st2);
7203 i |= eldv;
7204 I915_WRITE(aud_cntrl_st2, i);
7205
7206}
7207
Wu Fengguange0dac652011-09-05 14:25:34 +08007208static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007209 struct drm_crtc *crtc,
7210 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007211{
7212 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7213 uint8_t *eld = connector->eld;
7214 uint32_t eldv;
7215 uint32_t i;
7216 int len;
7217 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007218 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007219 int aud_cntl_st;
7220 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007221 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007222
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007223 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007224 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7225 aud_config = IBX_AUD_CFG(pipe);
7226 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007227 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007228 } else if (IS_VALLEYVIEW(connector->dev)) {
7229 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7230 aud_config = VLV_AUD_CFG(pipe);
7231 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7232 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007233 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007234 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7235 aud_config = CPT_AUD_CFG(pipe);
7236 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007237 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007238 }
7239
Wang Xingchao9b138a82012-08-09 16:52:18 +08007240 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007241
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007242 if (IS_VALLEYVIEW(connector->dev)) {
7243 struct intel_encoder *intel_encoder;
7244 struct intel_digital_port *intel_dig_port;
7245
7246 intel_encoder = intel_attached_encoder(connector);
7247 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7248 i = intel_dig_port->port;
7249 } else {
7250 i = I915_READ(aud_cntl_st);
7251 i = (i >> 29) & DIP_PORT_SEL_MASK;
7252 /* DIP_Port_Select, 0x1 = PortB */
7253 }
7254
Wu Fengguange0dac652011-09-05 14:25:34 +08007255 if (!i) {
7256 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7257 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007258 eldv = IBX_ELD_VALIDB;
7259 eldv |= IBX_ELD_VALIDB << 4;
7260 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007261 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007262 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007263 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007264 }
7265
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007266 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7267 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7268 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007269 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007270 } else {
7271 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7272 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007273
7274 if (intel_eld_uptodate(connector,
7275 aud_cntrl_st2, eldv,
7276 aud_cntl_st, IBX_ELD_ADDRESS,
7277 hdmiw_hdmiedid))
7278 return;
7279
Wu Fengguange0dac652011-09-05 14:25:34 +08007280 i = I915_READ(aud_cntrl_st2);
7281 i &= ~eldv;
7282 I915_WRITE(aud_cntrl_st2, i);
7283
7284 if (!eld[0])
7285 return;
7286
Wu Fengguange0dac652011-09-05 14:25:34 +08007287 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007288 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007289 I915_WRITE(aud_cntl_st, i);
7290
7291 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7292 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7293 for (i = 0; i < len; i++)
7294 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7295
7296 i = I915_READ(aud_cntrl_st2);
7297 i |= eldv;
7298 I915_WRITE(aud_cntrl_st2, i);
7299}
7300
7301void intel_write_eld(struct drm_encoder *encoder,
7302 struct drm_display_mode *mode)
7303{
7304 struct drm_crtc *crtc = encoder->crtc;
7305 struct drm_connector *connector;
7306 struct drm_device *dev = encoder->dev;
7307 struct drm_i915_private *dev_priv = dev->dev_private;
7308
7309 connector = drm_select_eld(encoder, mode);
7310 if (!connector)
7311 return;
7312
7313 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7314 connector->base.id,
7315 drm_get_connector_name(connector),
7316 connector->encoder->base.id,
7317 drm_get_encoder_name(connector->encoder));
7318
7319 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7320
7321 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007322 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007323}
7324
Jesse Barnes79e53942008-11-07 14:24:08 -08007325static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7326{
7327 struct drm_device *dev = crtc->dev;
7328 struct drm_i915_private *dev_priv = dev->dev_private;
7329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7330 bool visible = base != 0;
7331 u32 cntl;
7332
7333 if (intel_crtc->cursor_visible == visible)
7334 return;
7335
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007336 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007337 if (visible) {
7338 /* On these chipsets we can only modify the base whilst
7339 * the cursor is disabled.
7340 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007341 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007342
7343 cntl &= ~(CURSOR_FORMAT_MASK);
7344 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7345 cntl |= CURSOR_ENABLE |
7346 CURSOR_GAMMA_ENABLE |
7347 CURSOR_FORMAT_ARGB;
7348 } else
7349 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007350 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007351
7352 intel_crtc->cursor_visible = visible;
7353}
7354
7355static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7356{
7357 struct drm_device *dev = crtc->dev;
7358 struct drm_i915_private *dev_priv = dev->dev_private;
7359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7360 int pipe = intel_crtc->pipe;
7361 bool visible = base != 0;
7362
7363 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007364 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007365 if (base) {
7366 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7367 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7368 cntl |= pipe << 28; /* Connect to correct pipe */
7369 } else {
7370 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7371 cntl |= CURSOR_MODE_DISABLE;
7372 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007373 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007374
7375 intel_crtc->cursor_visible = visible;
7376 }
7377 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007378 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007379}
7380
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007381static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7382{
7383 struct drm_device *dev = crtc->dev;
7384 struct drm_i915_private *dev_priv = dev->dev_private;
7385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7386 int pipe = intel_crtc->pipe;
7387 bool visible = base != 0;
7388
7389 if (intel_crtc->cursor_visible != visible) {
7390 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7391 if (base) {
7392 cntl &= ~CURSOR_MODE;
7393 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7394 } else {
7395 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7396 cntl |= CURSOR_MODE_DISABLE;
7397 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007398 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007399 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007400 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7401 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007402 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7403
7404 intel_crtc->cursor_visible = visible;
7405 }
7406 /* and commit changes on next vblank */
7407 I915_WRITE(CURBASE_IVB(pipe), base);
7408}
7409
Jesse Barnes79e53942008-11-07 14:24:08 -08007410/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7411static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7412 bool on)
7413{
7414 struct drm_device *dev = crtc->dev;
7415 struct drm_i915_private *dev_priv = dev->dev_private;
7416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7417 int pipe = intel_crtc->pipe;
7418 int x = intel_crtc->cursor_x;
7419 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007420 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007421 bool visible;
7422
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007423 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007424 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007425
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007426 if (x >= intel_crtc->config.pipe_src_w)
7427 base = 0;
7428
7429 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007430 base = 0;
7431
7432 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007433 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007434 base = 0;
7435
7436 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7437 x = -x;
7438 }
7439 pos |= x << CURSOR_X_SHIFT;
7440
7441 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007442 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007443 base = 0;
7444
7445 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7446 y = -y;
7447 }
7448 pos |= y << CURSOR_Y_SHIFT;
7449
7450 visible = base != 0;
7451 if (!visible && !intel_crtc->cursor_visible)
7452 return;
7453
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007454 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007455 I915_WRITE(CURPOS_IVB(pipe), pos);
7456 ivb_update_cursor(crtc, base);
7457 } else {
7458 I915_WRITE(CURPOS(pipe), pos);
7459 if (IS_845G(dev) || IS_I865G(dev))
7460 i845_update_cursor(crtc, base);
7461 else
7462 i9xx_update_cursor(crtc, base);
7463 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007464}
7465
7466static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7467 struct drm_file *file,
7468 uint32_t handle,
7469 uint32_t width, uint32_t height)
7470{
7471 struct drm_device *dev = crtc->dev;
7472 struct drm_i915_private *dev_priv = dev->dev_private;
7473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007474 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007475 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007476 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007477
Jesse Barnes79e53942008-11-07 14:24:08 -08007478 /* if we want to turn off the cursor ignore width and height */
7479 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007480 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007481 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007482 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007483 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007484 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007485 }
7486
7487 /* Currently we only support 64x64 cursors */
7488 if (width != 64 || height != 64) {
7489 DRM_ERROR("we currently only support 64x64 cursors\n");
7490 return -EINVAL;
7491 }
7492
Chris Wilson05394f32010-11-08 19:18:58 +00007493 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007494 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007495 return -ENOENT;
7496
Chris Wilson05394f32010-11-08 19:18:58 +00007497 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007498 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007499 ret = -ENOMEM;
7500 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007501 }
7502
Dave Airlie71acb5e2008-12-30 20:31:46 +10007503 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007504 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007505 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007506 unsigned alignment;
7507
Chris Wilsond9e86c02010-11-10 16:40:20 +00007508 if (obj->tiling_mode) {
7509 DRM_ERROR("cursor cannot be tiled\n");
7510 ret = -EINVAL;
7511 goto fail_locked;
7512 }
7513
Chris Wilson693db182013-03-05 14:52:39 +00007514 /* Note that the w/a also requires 2 PTE of padding following
7515 * the bo. We currently fill all unused PTE with the shadow
7516 * page and so we should always have valid PTE following the
7517 * cursor preventing the VT-d warning.
7518 */
7519 alignment = 0;
7520 if (need_vtd_wa(dev))
7521 alignment = 64*1024;
7522
7523 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007524 if (ret) {
7525 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007526 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007527 }
7528
Chris Wilsond9e86c02010-11-10 16:40:20 +00007529 ret = i915_gem_object_put_fence(obj);
7530 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007531 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007532 goto fail_unpin;
7533 }
7534
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007535 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007536 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007537 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007538 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007539 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7540 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007541 if (ret) {
7542 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007543 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007544 }
Chris Wilson05394f32010-11-08 19:18:58 +00007545 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007546 }
7547
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007548 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007549 I915_WRITE(CURSIZE, (height << 12) | width);
7550
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007551 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007552 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007553 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007554 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007555 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7556 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007557 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007558 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007559 }
Jesse Barnes80824002009-09-10 15:28:06 -07007560
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007561 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007562
7563 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007564 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007565 intel_crtc->cursor_width = width;
7566 intel_crtc->cursor_height = height;
7567
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007568 if (intel_crtc->active)
7569 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007570
Jesse Barnes79e53942008-11-07 14:24:08 -08007571 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007572fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007573 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007574fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007575 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007576fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007577 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007578 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007579}
7580
7581static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7582{
Jesse Barnes79e53942008-11-07 14:24:08 -08007583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007584
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007585 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7586 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007587
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007588 if (intel_crtc->active)
7589 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007590
7591 return 0;
7592}
7593
Jesse Barnes79e53942008-11-07 14:24:08 -08007594static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007595 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007596{
James Simmons72034252010-08-03 01:33:19 +01007597 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007599
James Simmons72034252010-08-03 01:33:19 +01007600 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007601 intel_crtc->lut_r[i] = red[i] >> 8;
7602 intel_crtc->lut_g[i] = green[i] >> 8;
7603 intel_crtc->lut_b[i] = blue[i] >> 8;
7604 }
7605
7606 intel_crtc_load_lut(crtc);
7607}
7608
Jesse Barnes79e53942008-11-07 14:24:08 -08007609/* VESA 640x480x72Hz mode to set on the pipe */
7610static struct drm_display_mode load_detect_mode = {
7611 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7612 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7613};
7614
Chris Wilsond2dff872011-04-19 08:36:26 +01007615static struct drm_framebuffer *
7616intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007617 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007618 struct drm_i915_gem_object *obj)
7619{
7620 struct intel_framebuffer *intel_fb;
7621 int ret;
7622
7623 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7624 if (!intel_fb) {
7625 drm_gem_object_unreference_unlocked(&obj->base);
7626 return ERR_PTR(-ENOMEM);
7627 }
7628
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007629 ret = i915_mutex_lock_interruptible(dev);
7630 if (ret)
7631 goto err;
7632
Chris Wilsond2dff872011-04-19 08:36:26 +01007633 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007634 mutex_unlock(&dev->struct_mutex);
7635 if (ret)
7636 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007637
7638 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007639err:
7640 drm_gem_object_unreference_unlocked(&obj->base);
7641 kfree(intel_fb);
7642
7643 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007644}
7645
7646static u32
7647intel_framebuffer_pitch_for_width(int width, int bpp)
7648{
7649 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7650 return ALIGN(pitch, 64);
7651}
7652
7653static u32
7654intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7655{
7656 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7657 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7658}
7659
7660static struct drm_framebuffer *
7661intel_framebuffer_create_for_mode(struct drm_device *dev,
7662 struct drm_display_mode *mode,
7663 int depth, int bpp)
7664{
7665 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007666 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007667
7668 obj = i915_gem_alloc_object(dev,
7669 intel_framebuffer_size_for_mode(mode, bpp));
7670 if (obj == NULL)
7671 return ERR_PTR(-ENOMEM);
7672
7673 mode_cmd.width = mode->hdisplay;
7674 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007675 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7676 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007677 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007678
7679 return intel_framebuffer_create(dev, &mode_cmd, obj);
7680}
7681
7682static struct drm_framebuffer *
7683mode_fits_in_fbdev(struct drm_device *dev,
7684 struct drm_display_mode *mode)
7685{
Daniel Vetter4520f532013-10-09 09:18:51 +02007686#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007687 struct drm_i915_private *dev_priv = dev->dev_private;
7688 struct drm_i915_gem_object *obj;
7689 struct drm_framebuffer *fb;
7690
7691 if (dev_priv->fbdev == NULL)
7692 return NULL;
7693
7694 obj = dev_priv->fbdev->ifb.obj;
7695 if (obj == NULL)
7696 return NULL;
7697
7698 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7700 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007701 return NULL;
7702
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007704 return NULL;
7705
7706 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007707#else
7708 return NULL;
7709#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007710}
7711
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007712bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007713 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007714 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007715{
7716 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007717 struct intel_encoder *intel_encoder =
7718 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007719 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007720 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007721 struct drm_crtc *crtc = NULL;
7722 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007723 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007724 int i = -1;
7725
Chris Wilsond2dff872011-04-19 08:36:26 +01007726 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7727 connector->base.id, drm_get_connector_name(connector),
7728 encoder->base.id, drm_get_encoder_name(encoder));
7729
Jesse Barnes79e53942008-11-07 14:24:08 -08007730 /*
7731 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007732 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007733 * - if the connector already has an assigned crtc, use it (but make
7734 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007735 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007736 * - try to find the first unused crtc that can drive this connector,
7737 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007738 */
7739
7740 /* See if we already have a CRTC for this connector */
7741 if (encoder->crtc) {
7742 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007743
Daniel Vetter7b240562012-12-12 00:35:33 +01007744 mutex_lock(&crtc->mutex);
7745
Daniel Vetter24218aa2012-08-12 19:27:11 +02007746 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007747 old->load_detect_temp = false;
7748
7749 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007750 if (connector->dpms != DRM_MODE_DPMS_ON)
7751 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007752
Chris Wilson71731882011-04-19 23:10:58 +01007753 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007754 }
7755
7756 /* Find an unused one (if possible) */
7757 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7758 i++;
7759 if (!(encoder->possible_crtcs & (1 << i)))
7760 continue;
7761 if (!possible_crtc->enabled) {
7762 crtc = possible_crtc;
7763 break;
7764 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007765 }
7766
7767 /*
7768 * If we didn't find an unused CRTC, don't use any.
7769 */
7770 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007771 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7772 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007773 }
7774
Daniel Vetter7b240562012-12-12 00:35:33 +01007775 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007776 intel_encoder->new_crtc = to_intel_crtc(crtc);
7777 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007778
7779 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007780 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007781 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007782 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007783
Chris Wilson64927112011-04-20 07:25:26 +01007784 if (!mode)
7785 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007786
Chris Wilsond2dff872011-04-19 08:36:26 +01007787 /* We need a framebuffer large enough to accommodate all accesses
7788 * that the plane may generate whilst we perform load detection.
7789 * We can not rely on the fbcon either being present (we get called
7790 * during its initialisation to detect all boot displays, or it may
7791 * not even exist) or that it is large enough to satisfy the
7792 * requested mode.
7793 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007794 fb = mode_fits_in_fbdev(dev, mode);
7795 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007796 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007797 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7798 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007799 } else
7800 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007801 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007802 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007803 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007804 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007805 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007806
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007807 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007808 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007809 if (old->release_fb)
7810 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007811 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007812 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007813 }
Chris Wilson71731882011-04-19 23:10:58 +01007814
Jesse Barnes79e53942008-11-07 14:24:08 -08007815 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007816 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007817 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007818}
7819
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007820void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007821 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007822{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007823 struct intel_encoder *intel_encoder =
7824 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007825 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007826 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007827
Chris Wilsond2dff872011-04-19 08:36:26 +01007828 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7829 connector->base.id, drm_get_connector_name(connector),
7830 encoder->base.id, drm_get_encoder_name(encoder));
7831
Chris Wilson8261b192011-04-19 23:18:09 +01007832 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007833 to_intel_connector(connector)->new_encoder = NULL;
7834 intel_encoder->new_crtc = NULL;
7835 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007836
Daniel Vetter36206362012-12-10 20:42:17 +01007837 if (old->release_fb) {
7838 drm_framebuffer_unregister_private(old->release_fb);
7839 drm_framebuffer_unreference(old->release_fb);
7840 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007841
Daniel Vetter67c96402013-01-23 16:25:09 +00007842 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007843 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007844 }
7845
Eric Anholtc751ce42010-03-25 11:48:48 -07007846 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007847 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7848 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007849
7850 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007851}
7852
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007853static int i9xx_pll_refclk(struct drm_device *dev,
7854 const struct intel_crtc_config *pipe_config)
7855{
7856 struct drm_i915_private *dev_priv = dev->dev_private;
7857 u32 dpll = pipe_config->dpll_hw_state.dpll;
7858
7859 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7860 return dev_priv->vbt.lvds_ssc_freq * 1000;
7861 else if (HAS_PCH_SPLIT(dev))
7862 return 120000;
7863 else if (!IS_GEN2(dev))
7864 return 96000;
7865 else
7866 return 48000;
7867}
7868
Jesse Barnes79e53942008-11-07 14:24:08 -08007869/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007870static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7871 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007872{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007873 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007874 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007875 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007876 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007877 u32 fp;
7878 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007879 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007880
7881 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007882 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007883 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007884 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007885
7886 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007887 if (IS_PINEVIEW(dev)) {
7888 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7889 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007890 } else {
7891 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7892 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7893 }
7894
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007895 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007896 if (IS_PINEVIEW(dev))
7897 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7898 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007899 else
7900 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007901 DPLL_FPA01_P1_POST_DIV_SHIFT);
7902
7903 switch (dpll & DPLL_MODE_MASK) {
7904 case DPLLB_MODE_DAC_SERIAL:
7905 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7906 5 : 10;
7907 break;
7908 case DPLLB_MODE_LVDS:
7909 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7910 7 : 14;
7911 break;
7912 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007913 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007914 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007915 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007916 }
7917
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007918 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007919 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007920 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007921 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007922 } else {
7923 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7924
7925 if (is_lvds) {
7926 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7927 DPLL_FPA01_P1_POST_DIV_SHIFT);
7928 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007929 } else {
7930 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7931 clock.p1 = 2;
7932 else {
7933 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7934 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7935 }
7936 if (dpll & PLL_P2_DIVIDE_BY_4)
7937 clock.p2 = 4;
7938 else
7939 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007940 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007941
7942 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007943 }
7944
Ville Syrjälä18442d02013-09-13 16:00:08 +03007945 /*
7946 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007947 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007948 * encoder's get_config() function.
7949 */
7950 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007951}
7952
Ville Syrjälä6878da02013-09-13 15:59:11 +03007953int intel_dotclock_calculate(int link_freq,
7954 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007955{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007956 /*
7957 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007958 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007959 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007960 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007961 *
7962 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007963 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007964 */
7965
Ville Syrjälä6878da02013-09-13 15:59:11 +03007966 if (!m_n->link_n)
7967 return 0;
7968
7969 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7970}
7971
Ville Syrjälä18442d02013-09-13 16:00:08 +03007972static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7973 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007974{
7975 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007976
7977 /* read out port_clock from the DPLL */
7978 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007979
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007980 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007981 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007982 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007983 * agree once we know their relationship in the encoder's
7984 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007985 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007986 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007987 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7988 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007989}
7990
7991/** Returns the currently programmed mode of the given pipe. */
7992struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7993 struct drm_crtc *crtc)
7994{
Jesse Barnes548f2452011-02-17 10:40:53 -08007995 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007997 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007998 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007999 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008000 int htot = I915_READ(HTOTAL(cpu_transcoder));
8001 int hsync = I915_READ(HSYNC(cpu_transcoder));
8002 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8003 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008004 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008005
8006 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8007 if (!mode)
8008 return NULL;
8009
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008010 /*
8011 * Construct a pipe_config sufficient for getting the clock info
8012 * back out of crtc_clock_get.
8013 *
8014 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8015 * to use a real value here instead.
8016 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008017 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008018 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008019 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8020 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8021 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008022 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8023
Ville Syrjälä773ae032013-09-23 17:48:20 +03008024 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008025 mode->hdisplay = (htot & 0xffff) + 1;
8026 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8027 mode->hsync_start = (hsync & 0xffff) + 1;
8028 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8029 mode->vdisplay = (vtot & 0xffff) + 1;
8030 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8031 mode->vsync_start = (vsync & 0xffff) + 1;
8032 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8033
8034 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008035
8036 return mode;
8037}
8038
Daniel Vetter3dec0092010-08-20 21:40:52 +02008039static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008040{
8041 struct drm_device *dev = crtc->dev;
8042 drm_i915_private_t *dev_priv = dev->dev_private;
8043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8044 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008045 int dpll_reg = DPLL(pipe);
8046 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008047
Eric Anholtbad720f2009-10-22 16:11:14 -07008048 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008049 return;
8050
8051 if (!dev_priv->lvds_downclock_avail)
8052 return;
8053
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008054 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008055 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008056 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008057
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008058 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008059
8060 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8061 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008062 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008063
Jesse Barnes652c3932009-08-17 13:31:43 -07008064 dpll = I915_READ(dpll_reg);
8065 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008066 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008067 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008068}
8069
8070static void intel_decrease_pllclock(struct drm_crtc *crtc)
8071{
8072 struct drm_device *dev = crtc->dev;
8073 drm_i915_private_t *dev_priv = dev->dev_private;
8074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008075
Eric Anholtbad720f2009-10-22 16:11:14 -07008076 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008077 return;
8078
8079 if (!dev_priv->lvds_downclock_avail)
8080 return;
8081
8082 /*
8083 * Since this is called by a timer, we should never get here in
8084 * the manual case.
8085 */
8086 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008087 int pipe = intel_crtc->pipe;
8088 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008089 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008090
Zhao Yakui44d98a62009-10-09 11:39:40 +08008091 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008092
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008093 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008094
Chris Wilson074b5e12012-05-02 12:07:06 +01008095 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008096 dpll |= DISPLAY_RATE_SELECT_FPA1;
8097 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008098 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008099 dpll = I915_READ(dpll_reg);
8100 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008101 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008102 }
8103
8104}
8105
Chris Wilsonf047e392012-07-21 12:31:41 +01008106void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008107{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008108 struct drm_i915_private *dev_priv = dev->dev_private;
8109
8110 hsw_package_c8_gpu_busy(dev_priv);
8111 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008112}
8113
8114void intel_mark_idle(struct drm_device *dev)
8115{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008116 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008117 struct drm_crtc *crtc;
8118
Paulo Zanonic67a4702013-08-19 13:18:09 -03008119 hsw_package_c8_gpu_idle(dev_priv);
8120
Chris Wilson725a5b52013-01-08 11:02:57 +00008121 if (!i915_powersave)
8122 return;
8123
8124 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8125 if (!crtc->fb)
8126 continue;
8127
8128 intel_decrease_pllclock(crtc);
8129 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008130
8131 if (dev_priv->info->gen >= 6)
8132 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008133}
8134
Chris Wilsonc65355b2013-06-06 16:53:41 -03008135void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8136 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008137{
8138 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008139 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008140
8141 if (!i915_powersave)
8142 return;
8143
Jesse Barnes652c3932009-08-17 13:31:43 -07008144 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008145 if (!crtc->fb)
8146 continue;
8147
Chris Wilsonc65355b2013-06-06 16:53:41 -03008148 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8149 continue;
8150
8151 intel_increase_pllclock(crtc);
8152 if (ring && intel_fbc_enabled(dev))
8153 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008154 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008155}
8156
Jesse Barnes79e53942008-11-07 14:24:08 -08008157static void intel_crtc_destroy(struct drm_crtc *crtc)
8158{
8159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008160 struct drm_device *dev = crtc->dev;
8161 struct intel_unpin_work *work;
8162 unsigned long flags;
8163
8164 spin_lock_irqsave(&dev->event_lock, flags);
8165 work = intel_crtc->unpin_work;
8166 intel_crtc->unpin_work = NULL;
8167 spin_unlock_irqrestore(&dev->event_lock, flags);
8168
8169 if (work) {
8170 cancel_work_sync(&work->work);
8171 kfree(work);
8172 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008173
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008174 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8175
Jesse Barnes79e53942008-11-07 14:24:08 -08008176 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008177
Jesse Barnes79e53942008-11-07 14:24:08 -08008178 kfree(intel_crtc);
8179}
8180
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008181static void intel_unpin_work_fn(struct work_struct *__work)
8182{
8183 struct intel_unpin_work *work =
8184 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008185 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008186
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008187 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008188 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008189 drm_gem_object_unreference(&work->pending_flip_obj->base);
8190 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008191
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008192 intel_update_fbc(dev);
8193 mutex_unlock(&dev->struct_mutex);
8194
8195 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8196 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8197
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008198 kfree(work);
8199}
8200
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008201static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008202 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008203{
8204 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8206 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008207 unsigned long flags;
8208
8209 /* Ignore early vblank irqs */
8210 if (intel_crtc == NULL)
8211 return;
8212
8213 spin_lock_irqsave(&dev->event_lock, flags);
8214 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008215
8216 /* Ensure we don't miss a work->pending update ... */
8217 smp_rmb();
8218
8219 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008220 spin_unlock_irqrestore(&dev->event_lock, flags);
8221 return;
8222 }
8223
Chris Wilsone7d841c2012-12-03 11:36:30 +00008224 /* and that the unpin work is consistent wrt ->pending. */
8225 smp_rmb();
8226
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008227 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008228
Rob Clark45a066e2012-10-08 14:50:40 -05008229 if (work->event)
8230 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008231
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008232 drm_vblank_put(dev, intel_crtc->pipe);
8233
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008234 spin_unlock_irqrestore(&dev->event_lock, flags);
8235
Daniel Vetter2c10d572012-12-20 21:24:07 +01008236 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008237
8238 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008239
8240 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008241}
8242
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008243void intel_finish_page_flip(struct drm_device *dev, int pipe)
8244{
8245 drm_i915_private_t *dev_priv = dev->dev_private;
8246 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8247
Mario Kleiner49b14a52010-12-09 07:00:07 +01008248 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008249}
8250
8251void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8252{
8253 drm_i915_private_t *dev_priv = dev->dev_private;
8254 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8255
Mario Kleiner49b14a52010-12-09 07:00:07 +01008256 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008257}
8258
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008259void intel_prepare_page_flip(struct drm_device *dev, int plane)
8260{
8261 drm_i915_private_t *dev_priv = dev->dev_private;
8262 struct intel_crtc *intel_crtc =
8263 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8264 unsigned long flags;
8265
Chris Wilsone7d841c2012-12-03 11:36:30 +00008266 /* NB: An MMIO update of the plane base pointer will also
8267 * generate a page-flip completion irq, i.e. every modeset
8268 * is also accompanied by a spurious intel_prepare_page_flip().
8269 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008270 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008271 if (intel_crtc->unpin_work)
8272 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008273 spin_unlock_irqrestore(&dev->event_lock, flags);
8274}
8275
Chris Wilsone7d841c2012-12-03 11:36:30 +00008276inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8277{
8278 /* Ensure that the work item is consistent when activating it ... */
8279 smp_wmb();
8280 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8281 /* and that it is marked active as soon as the irq could fire. */
8282 smp_wmb();
8283}
8284
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008285static int intel_gen2_queue_flip(struct drm_device *dev,
8286 struct drm_crtc *crtc,
8287 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008288 struct drm_i915_gem_object *obj,
8289 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008290{
8291 struct drm_i915_private *dev_priv = dev->dev_private;
8292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008293 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008294 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008295 int ret;
8296
Daniel Vetter6d90c952012-04-26 23:28:05 +02008297 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008298 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008299 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008300
Daniel Vetter6d90c952012-04-26 23:28:05 +02008301 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008302 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008303 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008304
8305 /* Can't queue multiple flips, so wait for the previous
8306 * one to finish before executing the next.
8307 */
8308 if (intel_crtc->plane)
8309 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8310 else
8311 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008312 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8313 intel_ring_emit(ring, MI_NOOP);
8314 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8315 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8316 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008317 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008318 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008319
8320 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008321 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008322 return 0;
8323
8324err_unpin:
8325 intel_unpin_fb_obj(obj);
8326err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008327 return ret;
8328}
8329
8330static int intel_gen3_queue_flip(struct drm_device *dev,
8331 struct drm_crtc *crtc,
8332 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008333 struct drm_i915_gem_object *obj,
8334 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008335{
8336 struct drm_i915_private *dev_priv = dev->dev_private;
8337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008338 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008339 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008340 int ret;
8341
Daniel Vetter6d90c952012-04-26 23:28:05 +02008342 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008343 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008344 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008345
Daniel Vetter6d90c952012-04-26 23:28:05 +02008346 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008347 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008348 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008349
8350 if (intel_crtc->plane)
8351 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8352 else
8353 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008354 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8355 intel_ring_emit(ring, MI_NOOP);
8356 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8357 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8358 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008359 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008360 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008361
Chris Wilsone7d841c2012-12-03 11:36:30 +00008362 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008363 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008364 return 0;
8365
8366err_unpin:
8367 intel_unpin_fb_obj(obj);
8368err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008369 return ret;
8370}
8371
8372static int intel_gen4_queue_flip(struct drm_device *dev,
8373 struct drm_crtc *crtc,
8374 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008375 struct drm_i915_gem_object *obj,
8376 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008377{
8378 struct drm_i915_private *dev_priv = dev->dev_private;
8379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8380 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008381 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008382 int ret;
8383
Daniel Vetter6d90c952012-04-26 23:28:05 +02008384 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008385 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008386 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008387
Daniel Vetter6d90c952012-04-26 23:28:05 +02008388 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008389 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008390 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008391
8392 /* i965+ uses the linear or tiled offsets from the
8393 * Display Registers (which do not change across a page-flip)
8394 * so we need only reprogram the base address.
8395 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008396 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8397 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8398 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008399 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008400 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008401 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008402
8403 /* XXX Enabling the panel-fitter across page-flip is so far
8404 * untested on non-native modes, so ignore it for now.
8405 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8406 */
8407 pf = 0;
8408 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008409 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008410
8411 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008412 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008413 return 0;
8414
8415err_unpin:
8416 intel_unpin_fb_obj(obj);
8417err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008418 return ret;
8419}
8420
8421static int intel_gen6_queue_flip(struct drm_device *dev,
8422 struct drm_crtc *crtc,
8423 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008424 struct drm_i915_gem_object *obj,
8425 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008426{
8427 struct drm_i915_private *dev_priv = dev->dev_private;
8428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008429 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008430 uint32_t pf, pipesrc;
8431 int ret;
8432
Daniel Vetter6d90c952012-04-26 23:28:05 +02008433 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008434 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008435 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008436
Daniel Vetter6d90c952012-04-26 23:28:05 +02008437 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008438 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008439 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008440
Daniel Vetter6d90c952012-04-26 23:28:05 +02008441 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8442 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8443 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008444 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008445
Chris Wilson99d9acd2012-04-17 20:37:00 +01008446 /* Contrary to the suggestions in the documentation,
8447 * "Enable Panel Fitter" does not seem to be required when page
8448 * flipping with a non-native mode, and worse causes a normal
8449 * modeset to fail.
8450 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8451 */
8452 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008453 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008454 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008455
8456 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008457 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008458 return 0;
8459
8460err_unpin:
8461 intel_unpin_fb_obj(obj);
8462err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008463 return ret;
8464}
8465
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008466static int intel_gen7_queue_flip(struct drm_device *dev,
8467 struct drm_crtc *crtc,
8468 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008469 struct drm_i915_gem_object *obj,
8470 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008471{
8472 struct drm_i915_private *dev_priv = dev->dev_private;
8473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008474 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008475 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008476 int len, ret;
8477
8478 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008479 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008480 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008481
8482 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8483 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008484 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008485
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008486 switch(intel_crtc->plane) {
8487 case PLANE_A:
8488 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8489 break;
8490 case PLANE_B:
8491 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8492 break;
8493 case PLANE_C:
8494 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8495 break;
8496 default:
8497 WARN_ONCE(1, "unknown plane in flip command\n");
8498 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008499 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008500 }
8501
Chris Wilsonffe74d72013-08-26 20:58:12 +01008502 len = 4;
8503 if (ring->id == RCS)
8504 len += 6;
8505
8506 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008507 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008508 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008509
Chris Wilsonffe74d72013-08-26 20:58:12 +01008510 /* Unmask the flip-done completion message. Note that the bspec says that
8511 * we should do this for both the BCS and RCS, and that we must not unmask
8512 * more than one flip event at any time (or ensure that one flip message
8513 * can be sent by waiting for flip-done prior to queueing new flips).
8514 * Experimentation says that BCS works despite DERRMR masking all
8515 * flip-done completion events and that unmasking all planes at once
8516 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8517 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8518 */
8519 if (ring->id == RCS) {
8520 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8521 intel_ring_emit(ring, DERRMR);
8522 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8523 DERRMR_PIPEB_PRI_FLIP_DONE |
8524 DERRMR_PIPEC_PRI_FLIP_DONE));
8525 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8526 intel_ring_emit(ring, DERRMR);
8527 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8528 }
8529
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008530 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008531 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008532 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008533 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008534
8535 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008536 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008537 return 0;
8538
8539err_unpin:
8540 intel_unpin_fb_obj(obj);
8541err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008542 return ret;
8543}
8544
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008545static int intel_default_queue_flip(struct drm_device *dev,
8546 struct drm_crtc *crtc,
8547 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008548 struct drm_i915_gem_object *obj,
8549 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008550{
8551 return -ENODEV;
8552}
8553
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008554static int intel_crtc_page_flip(struct drm_crtc *crtc,
8555 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008556 struct drm_pending_vblank_event *event,
8557 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008558{
8559 struct drm_device *dev = crtc->dev;
8560 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008561 struct drm_framebuffer *old_fb = crtc->fb;
8562 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8564 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008565 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008566 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008567
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008568 /* Can't change pixel format via MI display flips. */
8569 if (fb->pixel_format != crtc->fb->pixel_format)
8570 return -EINVAL;
8571
8572 /*
8573 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8574 * Note that pitch changes could also affect these register.
8575 */
8576 if (INTEL_INFO(dev)->gen > 3 &&
8577 (fb->offsets[0] != crtc->fb->offsets[0] ||
8578 fb->pitches[0] != crtc->fb->pitches[0]))
8579 return -EINVAL;
8580
Daniel Vetterb14c5672013-09-19 12:18:32 +02008581 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008582 if (work == NULL)
8583 return -ENOMEM;
8584
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008585 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008586 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008587 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008588 INIT_WORK(&work->work, intel_unpin_work_fn);
8589
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008590 ret = drm_vblank_get(dev, intel_crtc->pipe);
8591 if (ret)
8592 goto free_work;
8593
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008594 /* We borrow the event spin lock for protecting unpin_work */
8595 spin_lock_irqsave(&dev->event_lock, flags);
8596 if (intel_crtc->unpin_work) {
8597 spin_unlock_irqrestore(&dev->event_lock, flags);
8598 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008599 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008600
8601 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008602 return -EBUSY;
8603 }
8604 intel_crtc->unpin_work = work;
8605 spin_unlock_irqrestore(&dev->event_lock, flags);
8606
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008607 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8608 flush_workqueue(dev_priv->wq);
8609
Chris Wilson79158102012-05-23 11:13:58 +01008610 ret = i915_mutex_lock_interruptible(dev);
8611 if (ret)
8612 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008613
Jesse Barnes75dfca82010-02-10 15:09:44 -08008614 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008615 drm_gem_object_reference(&work->old_fb_obj->base);
8616 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008617
8618 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008619
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008620 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008621
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008622 work->enable_stall_check = true;
8623
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008624 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008625 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008626
Keith Packarded8d1972013-07-22 18:49:58 -07008627 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008628 if (ret)
8629 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008630
Chris Wilson7782de32011-07-08 12:22:41 +01008631 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008632 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008633 mutex_unlock(&dev->struct_mutex);
8634
Jesse Barnese5510fa2010-07-01 16:48:37 -07008635 trace_i915_flip_request(intel_crtc->plane, obj);
8636
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008637 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008638
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008639cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008640 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008641 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008642 drm_gem_object_unreference(&work->old_fb_obj->base);
8643 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008644 mutex_unlock(&dev->struct_mutex);
8645
Chris Wilson79158102012-05-23 11:13:58 +01008646cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008647 spin_lock_irqsave(&dev->event_lock, flags);
8648 intel_crtc->unpin_work = NULL;
8649 spin_unlock_irqrestore(&dev->event_lock, flags);
8650
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008651 drm_vblank_put(dev, intel_crtc->pipe);
8652free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008653 kfree(work);
8654
8655 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008656}
8657
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008658static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008659 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8660 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008661};
8662
Daniel Vetter50f56112012-07-02 09:35:43 +02008663static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8664 struct drm_crtc *crtc)
8665{
8666 struct drm_device *dev;
8667 struct drm_crtc *tmp;
8668 int crtc_mask = 1;
8669
8670 WARN(!crtc, "checking null crtc?\n");
8671
8672 dev = crtc->dev;
8673
8674 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8675 if (tmp == crtc)
8676 break;
8677 crtc_mask <<= 1;
8678 }
8679
8680 if (encoder->possible_crtcs & crtc_mask)
8681 return true;
8682 return false;
8683}
8684
Daniel Vetter9a935852012-07-05 22:34:27 +02008685/**
8686 * intel_modeset_update_staged_output_state
8687 *
8688 * Updates the staged output configuration state, e.g. after we've read out the
8689 * current hw state.
8690 */
8691static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8692{
8693 struct intel_encoder *encoder;
8694 struct intel_connector *connector;
8695
8696 list_for_each_entry(connector, &dev->mode_config.connector_list,
8697 base.head) {
8698 connector->new_encoder =
8699 to_intel_encoder(connector->base.encoder);
8700 }
8701
8702 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8703 base.head) {
8704 encoder->new_crtc =
8705 to_intel_crtc(encoder->base.crtc);
8706 }
8707}
8708
8709/**
8710 * intel_modeset_commit_output_state
8711 *
8712 * This function copies the stage display pipe configuration to the real one.
8713 */
8714static void intel_modeset_commit_output_state(struct drm_device *dev)
8715{
8716 struct intel_encoder *encoder;
8717 struct intel_connector *connector;
8718
8719 list_for_each_entry(connector, &dev->mode_config.connector_list,
8720 base.head) {
8721 connector->base.encoder = &connector->new_encoder->base;
8722 }
8723
8724 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8725 base.head) {
8726 encoder->base.crtc = &encoder->new_crtc->base;
8727 }
8728}
8729
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008730static void
8731connected_sink_compute_bpp(struct intel_connector * connector,
8732 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008733{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008734 int bpp = pipe_config->pipe_bpp;
8735
8736 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8737 connector->base.base.id,
8738 drm_get_connector_name(&connector->base));
8739
8740 /* Don't use an invalid EDID bpc value */
8741 if (connector->base.display_info.bpc &&
8742 connector->base.display_info.bpc * 3 < bpp) {
8743 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8744 bpp, connector->base.display_info.bpc*3);
8745 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8746 }
8747
8748 /* Clamp bpp to 8 on screens without EDID 1.4 */
8749 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8750 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8751 bpp);
8752 pipe_config->pipe_bpp = 24;
8753 }
8754}
8755
8756static int
8757compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8758 struct drm_framebuffer *fb,
8759 struct intel_crtc_config *pipe_config)
8760{
8761 struct drm_device *dev = crtc->base.dev;
8762 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008763 int bpp;
8764
Daniel Vetterd42264b2013-03-28 16:38:08 +01008765 switch (fb->pixel_format) {
8766 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008767 bpp = 8*3; /* since we go through a colormap */
8768 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008769 case DRM_FORMAT_XRGB1555:
8770 case DRM_FORMAT_ARGB1555:
8771 /* checked in intel_framebuffer_init already */
8772 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8773 return -EINVAL;
8774 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008775 bpp = 6*3; /* min is 18bpp */
8776 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008777 case DRM_FORMAT_XBGR8888:
8778 case DRM_FORMAT_ABGR8888:
8779 /* checked in intel_framebuffer_init already */
8780 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8781 return -EINVAL;
8782 case DRM_FORMAT_XRGB8888:
8783 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008784 bpp = 8*3;
8785 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008786 case DRM_FORMAT_XRGB2101010:
8787 case DRM_FORMAT_ARGB2101010:
8788 case DRM_FORMAT_XBGR2101010:
8789 case DRM_FORMAT_ABGR2101010:
8790 /* checked in intel_framebuffer_init already */
8791 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008792 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008793 bpp = 10*3;
8794 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008795 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008796 default:
8797 DRM_DEBUG_KMS("unsupported depth\n");
8798 return -EINVAL;
8799 }
8800
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008801 pipe_config->pipe_bpp = bpp;
8802
8803 /* Clamp display bpp to EDID value */
8804 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008805 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008806 if (!connector->new_encoder ||
8807 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008808 continue;
8809
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008810 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008811 }
8812
8813 return bpp;
8814}
8815
Daniel Vetter644db712013-09-19 14:53:58 +02008816static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8817{
8818 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8819 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008820 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008821 mode->crtc_hdisplay, mode->crtc_hsync_start,
8822 mode->crtc_hsync_end, mode->crtc_htotal,
8823 mode->crtc_vdisplay, mode->crtc_vsync_start,
8824 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8825}
8826
Daniel Vetterc0b03412013-05-28 12:05:54 +02008827static void intel_dump_pipe_config(struct intel_crtc *crtc,
8828 struct intel_crtc_config *pipe_config,
8829 const char *context)
8830{
8831 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8832 context, pipe_name(crtc->pipe));
8833
8834 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8835 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8836 pipe_config->pipe_bpp, pipe_config->dither);
8837 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8838 pipe_config->has_pch_encoder,
8839 pipe_config->fdi_lanes,
8840 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8841 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8842 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008843 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8844 pipe_config->has_dp_encoder,
8845 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8846 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8847 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008848 DRM_DEBUG_KMS("requested mode:\n");
8849 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8850 DRM_DEBUG_KMS("adjusted mode:\n");
8851 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008852 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008853 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008854 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8855 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008856 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8857 pipe_config->gmch_pfit.control,
8858 pipe_config->gmch_pfit.pgm_ratios,
8859 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008860 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008861 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008862 pipe_config->pch_pfit.size,
8863 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008864 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008865 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008866}
8867
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008868static bool check_encoder_cloning(struct drm_crtc *crtc)
8869{
8870 int num_encoders = 0;
8871 bool uncloneable_encoders = false;
8872 struct intel_encoder *encoder;
8873
8874 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8875 base.head) {
8876 if (&encoder->new_crtc->base != crtc)
8877 continue;
8878
8879 num_encoders++;
8880 if (!encoder->cloneable)
8881 uncloneable_encoders = true;
8882 }
8883
8884 return !(num_encoders > 1 && uncloneable_encoders);
8885}
8886
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008887static struct intel_crtc_config *
8888intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008889 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008890 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008891{
8892 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008893 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008894 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008895 int plane_bpp, ret = -EINVAL;
8896 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008897
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008898 if (!check_encoder_cloning(crtc)) {
8899 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8900 return ERR_PTR(-EINVAL);
8901 }
8902
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008903 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8904 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008905 return ERR_PTR(-ENOMEM);
8906
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008907 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8908 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008909
Daniel Vettere143a212013-07-04 12:01:15 +02008910 pipe_config->cpu_transcoder =
8911 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008912 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008913
Imre Deak2960bc92013-07-30 13:36:32 +03008914 /*
8915 * Sanitize sync polarity flags based on requested ones. If neither
8916 * positive or negative polarity is requested, treat this as meaning
8917 * negative polarity.
8918 */
8919 if (!(pipe_config->adjusted_mode.flags &
8920 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8921 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8922
8923 if (!(pipe_config->adjusted_mode.flags &
8924 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8925 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8926
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008927 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8928 * plane pixel format and any sink constraints into account. Returns the
8929 * source plane bpp so that dithering can be selected on mismatches
8930 * after encoders and crtc also have had their say. */
8931 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8932 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008933 if (plane_bpp < 0)
8934 goto fail;
8935
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008936 /*
8937 * Determine the real pipe dimensions. Note that stereo modes can
8938 * increase the actual pipe size due to the frame doubling and
8939 * insertion of additional space for blanks between the frame. This
8940 * is stored in the crtc timings. We use the requested mode to do this
8941 * computation to clearly distinguish it from the adjusted mode, which
8942 * can be changed by the connectors in the below retry loop.
8943 */
8944 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8945 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8946 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8947
Daniel Vettere29c22c2013-02-21 00:00:16 +01008948encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008949 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008950 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008951 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008952
Daniel Vetter135c81b2013-07-21 21:37:09 +02008953 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008954 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008955
Daniel Vetter7758a112012-07-08 19:40:39 +02008956 /* Pass our mode to the connectors and the CRTC to give them a chance to
8957 * adjust it according to limitations or connector properties, and also
8958 * a chance to reject the mode entirely.
8959 */
8960 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8961 base.head) {
8962
8963 if (&encoder->new_crtc->base != crtc)
8964 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008965
Daniel Vetterefea6e82013-07-21 21:36:59 +02008966 if (!(encoder->compute_config(encoder, pipe_config))) {
8967 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008968 goto fail;
8969 }
8970 }
8971
Daniel Vetterff9a6752013-06-01 17:16:21 +02008972 /* Set default port clock if not overwritten by the encoder. Needs to be
8973 * done afterwards in case the encoder adjusts the mode. */
8974 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008975 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8976 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008977
Daniel Vettera43f6e02013-06-07 23:10:32 +02008978 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008979 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008980 DRM_DEBUG_KMS("CRTC fixup failed\n");
8981 goto fail;
8982 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008983
8984 if (ret == RETRY) {
8985 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8986 ret = -EINVAL;
8987 goto fail;
8988 }
8989
8990 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8991 retry = false;
8992 goto encoder_retry;
8993 }
8994
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008995 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8996 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8997 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8998
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008999 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009000fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009001 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009002 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009003}
9004
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009005/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9006 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9007static void
9008intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9009 unsigned *prepare_pipes, unsigned *disable_pipes)
9010{
9011 struct intel_crtc *intel_crtc;
9012 struct drm_device *dev = crtc->dev;
9013 struct intel_encoder *encoder;
9014 struct intel_connector *connector;
9015 struct drm_crtc *tmp_crtc;
9016
9017 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9018
9019 /* Check which crtcs have changed outputs connected to them, these need
9020 * to be part of the prepare_pipes mask. We don't (yet) support global
9021 * modeset across multiple crtcs, so modeset_pipes will only have one
9022 * bit set at most. */
9023 list_for_each_entry(connector, &dev->mode_config.connector_list,
9024 base.head) {
9025 if (connector->base.encoder == &connector->new_encoder->base)
9026 continue;
9027
9028 if (connector->base.encoder) {
9029 tmp_crtc = connector->base.encoder->crtc;
9030
9031 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9032 }
9033
9034 if (connector->new_encoder)
9035 *prepare_pipes |=
9036 1 << connector->new_encoder->new_crtc->pipe;
9037 }
9038
9039 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9040 base.head) {
9041 if (encoder->base.crtc == &encoder->new_crtc->base)
9042 continue;
9043
9044 if (encoder->base.crtc) {
9045 tmp_crtc = encoder->base.crtc;
9046
9047 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9048 }
9049
9050 if (encoder->new_crtc)
9051 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9052 }
9053
9054 /* Check for any pipes that will be fully disabled ... */
9055 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9056 base.head) {
9057 bool used = false;
9058
9059 /* Don't try to disable disabled crtcs. */
9060 if (!intel_crtc->base.enabled)
9061 continue;
9062
9063 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9064 base.head) {
9065 if (encoder->new_crtc == intel_crtc)
9066 used = true;
9067 }
9068
9069 if (!used)
9070 *disable_pipes |= 1 << intel_crtc->pipe;
9071 }
9072
9073
9074 /* set_mode is also used to update properties on life display pipes. */
9075 intel_crtc = to_intel_crtc(crtc);
9076 if (crtc->enabled)
9077 *prepare_pipes |= 1 << intel_crtc->pipe;
9078
Daniel Vetterb6c51642013-04-12 18:48:43 +02009079 /*
9080 * For simplicity do a full modeset on any pipe where the output routing
9081 * changed. We could be more clever, but that would require us to be
9082 * more careful with calling the relevant encoder->mode_set functions.
9083 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009084 if (*prepare_pipes)
9085 *modeset_pipes = *prepare_pipes;
9086
9087 /* ... and mask these out. */
9088 *modeset_pipes &= ~(*disable_pipes);
9089 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009090
9091 /*
9092 * HACK: We don't (yet) fully support global modesets. intel_set_config
9093 * obies this rule, but the modeset restore mode of
9094 * intel_modeset_setup_hw_state does not.
9095 */
9096 *modeset_pipes &= 1 << intel_crtc->pipe;
9097 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009098
9099 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9100 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009101}
9102
Daniel Vetterea9d7582012-07-10 10:42:52 +02009103static bool intel_crtc_in_use(struct drm_crtc *crtc)
9104{
9105 struct drm_encoder *encoder;
9106 struct drm_device *dev = crtc->dev;
9107
9108 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9109 if (encoder->crtc == crtc)
9110 return true;
9111
9112 return false;
9113}
9114
9115static void
9116intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9117{
9118 struct intel_encoder *intel_encoder;
9119 struct intel_crtc *intel_crtc;
9120 struct drm_connector *connector;
9121
9122 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9123 base.head) {
9124 if (!intel_encoder->base.crtc)
9125 continue;
9126
9127 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9128
9129 if (prepare_pipes & (1 << intel_crtc->pipe))
9130 intel_encoder->connectors_active = false;
9131 }
9132
9133 intel_modeset_commit_output_state(dev);
9134
9135 /* Update computed state. */
9136 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9137 base.head) {
9138 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9139 }
9140
9141 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9142 if (!connector->encoder || !connector->encoder->crtc)
9143 continue;
9144
9145 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9146
9147 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009148 struct drm_property *dpms_property =
9149 dev->mode_config.dpms_property;
9150
Daniel Vetterea9d7582012-07-10 10:42:52 +02009151 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009152 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009153 dpms_property,
9154 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009155
9156 intel_encoder = to_intel_encoder(connector->encoder);
9157 intel_encoder->connectors_active = true;
9158 }
9159 }
9160
9161}
9162
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009163static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009164{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009165 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009166
9167 if (clock1 == clock2)
9168 return true;
9169
9170 if (!clock1 || !clock2)
9171 return false;
9172
9173 diff = abs(clock1 - clock2);
9174
9175 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9176 return true;
9177
9178 return false;
9179}
9180
Daniel Vetter25c5b262012-07-08 22:08:04 +02009181#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9182 list_for_each_entry((intel_crtc), \
9183 &(dev)->mode_config.crtc_list, \
9184 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009185 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009186
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009187static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009188intel_pipe_config_compare(struct drm_device *dev,
9189 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009190 struct intel_crtc_config *pipe_config)
9191{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009192#define PIPE_CONF_CHECK_X(name) \
9193 if (current_config->name != pipe_config->name) { \
9194 DRM_ERROR("mismatch in " #name " " \
9195 "(expected 0x%08x, found 0x%08x)\n", \
9196 current_config->name, \
9197 pipe_config->name); \
9198 return false; \
9199 }
9200
Daniel Vetter08a24032013-04-19 11:25:34 +02009201#define PIPE_CONF_CHECK_I(name) \
9202 if (current_config->name != pipe_config->name) { \
9203 DRM_ERROR("mismatch in " #name " " \
9204 "(expected %i, found %i)\n", \
9205 current_config->name, \
9206 pipe_config->name); \
9207 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009208 }
9209
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009210#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9211 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009212 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009213 "(expected %i, found %i)\n", \
9214 current_config->name & (mask), \
9215 pipe_config->name & (mask)); \
9216 return false; \
9217 }
9218
Ville Syrjälä5e550652013-09-06 23:29:07 +03009219#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9220 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9221 DRM_ERROR("mismatch in " #name " " \
9222 "(expected %i, found %i)\n", \
9223 current_config->name, \
9224 pipe_config->name); \
9225 return false; \
9226 }
9227
Daniel Vetterbb760062013-06-06 14:55:52 +02009228#define PIPE_CONF_QUIRK(quirk) \
9229 ((current_config->quirks | pipe_config->quirks) & (quirk))
9230
Daniel Vettereccb1402013-05-22 00:50:22 +02009231 PIPE_CONF_CHECK_I(cpu_transcoder);
9232
Daniel Vetter08a24032013-04-19 11:25:34 +02009233 PIPE_CONF_CHECK_I(has_pch_encoder);
9234 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009235 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9236 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9237 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9238 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9239 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009240
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009241 PIPE_CONF_CHECK_I(has_dp_encoder);
9242 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9243 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9244 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9245 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9246 PIPE_CONF_CHECK_I(dp_m_n.tu);
9247
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009248 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9249 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9250 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9251 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9252 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9253 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9254
9255 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9256 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9257 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9258 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9259 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9260 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9261
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009262 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009263
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009264 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9265 DRM_MODE_FLAG_INTERLACE);
9266
Daniel Vetterbb760062013-06-06 14:55:52 +02009267 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9268 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9269 DRM_MODE_FLAG_PHSYNC);
9270 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9271 DRM_MODE_FLAG_NHSYNC);
9272 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9273 DRM_MODE_FLAG_PVSYNC);
9274 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9275 DRM_MODE_FLAG_NVSYNC);
9276 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009277
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009278 PIPE_CONF_CHECK_I(pipe_src_w);
9279 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009280
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009281 PIPE_CONF_CHECK_I(gmch_pfit.control);
9282 /* pfit ratios are autocomputed by the hw on gen4+ */
9283 if (INTEL_INFO(dev)->gen < 4)
9284 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9285 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009286 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9287 if (current_config->pch_pfit.enabled) {
9288 PIPE_CONF_CHECK_I(pch_pfit.pos);
9289 PIPE_CONF_CHECK_I(pch_pfit.size);
9290 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009291
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009292 PIPE_CONF_CHECK_I(ips_enabled);
9293
Ville Syrjälä282740f2013-09-04 18:30:03 +03009294 PIPE_CONF_CHECK_I(double_wide);
9295
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009296 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009297 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009298 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009299 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9300 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009301
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009302 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9303 PIPE_CONF_CHECK_I(pipe_bpp);
9304
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009305 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01009306 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009307 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9308 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03009309
Daniel Vetter66e985c2013-06-05 13:34:20 +02009310#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009311#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009312#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009313#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009314#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009315
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009316 return true;
9317}
9318
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009319static void
9320check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009321{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009322 struct intel_connector *connector;
9323
9324 list_for_each_entry(connector, &dev->mode_config.connector_list,
9325 base.head) {
9326 /* This also checks the encoder/connector hw state with the
9327 * ->get_hw_state callbacks. */
9328 intel_connector_check_state(connector);
9329
9330 WARN(&connector->new_encoder->base != connector->base.encoder,
9331 "connector's staged encoder doesn't match current encoder\n");
9332 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009333}
9334
9335static void
9336check_encoder_state(struct drm_device *dev)
9337{
9338 struct intel_encoder *encoder;
9339 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009340
9341 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9342 base.head) {
9343 bool enabled = false;
9344 bool active = false;
9345 enum pipe pipe, tracked_pipe;
9346
9347 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9348 encoder->base.base.id,
9349 drm_get_encoder_name(&encoder->base));
9350
9351 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9352 "encoder's stage crtc doesn't match current crtc\n");
9353 WARN(encoder->connectors_active && !encoder->base.crtc,
9354 "encoder's active_connectors set, but no crtc\n");
9355
9356 list_for_each_entry(connector, &dev->mode_config.connector_list,
9357 base.head) {
9358 if (connector->base.encoder != &encoder->base)
9359 continue;
9360 enabled = true;
9361 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9362 active = true;
9363 }
9364 WARN(!!encoder->base.crtc != enabled,
9365 "encoder's enabled state mismatch "
9366 "(expected %i, found %i)\n",
9367 !!encoder->base.crtc, enabled);
9368 WARN(active && !encoder->base.crtc,
9369 "active encoder with no crtc\n");
9370
9371 WARN(encoder->connectors_active != active,
9372 "encoder's computed active state doesn't match tracked active state "
9373 "(expected %i, found %i)\n", active, encoder->connectors_active);
9374
9375 active = encoder->get_hw_state(encoder, &pipe);
9376 WARN(active != encoder->connectors_active,
9377 "encoder's hw state doesn't match sw tracking "
9378 "(expected %i, found %i)\n",
9379 encoder->connectors_active, active);
9380
9381 if (!encoder->base.crtc)
9382 continue;
9383
9384 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9385 WARN(active && pipe != tracked_pipe,
9386 "active encoder's pipe doesn't match"
9387 "(expected %i, found %i)\n",
9388 tracked_pipe, pipe);
9389
9390 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009391}
9392
9393static void
9394check_crtc_state(struct drm_device *dev)
9395{
9396 drm_i915_private_t *dev_priv = dev->dev_private;
9397 struct intel_crtc *crtc;
9398 struct intel_encoder *encoder;
9399 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009400
9401 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9402 base.head) {
9403 bool enabled = false;
9404 bool active = false;
9405
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009406 memset(&pipe_config, 0, sizeof(pipe_config));
9407
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009408 DRM_DEBUG_KMS("[CRTC:%d]\n",
9409 crtc->base.base.id);
9410
9411 WARN(crtc->active && !crtc->base.enabled,
9412 "active crtc, but not enabled in sw tracking\n");
9413
9414 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9415 base.head) {
9416 if (encoder->base.crtc != &crtc->base)
9417 continue;
9418 enabled = true;
9419 if (encoder->connectors_active)
9420 active = true;
9421 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009422
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009423 WARN(active != crtc->active,
9424 "crtc's computed active state doesn't match tracked active state "
9425 "(expected %i, found %i)\n", active, crtc->active);
9426 WARN(enabled != crtc->base.enabled,
9427 "crtc's computed enabled state doesn't match tracked enabled state "
9428 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9429
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009430 active = dev_priv->display.get_pipe_config(crtc,
9431 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009432
9433 /* hw state is inconsistent with the pipe A quirk */
9434 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9435 active = crtc->active;
9436
Daniel Vetter6c49f242013-06-06 12:45:25 +02009437 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9438 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009439 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009440 if (encoder->base.crtc != &crtc->base)
9441 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009442 if (encoder->get_config &&
9443 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009444 encoder->get_config(encoder, &pipe_config);
9445 }
9446
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009447 WARN(crtc->active != active,
9448 "crtc active state doesn't match with hw state "
9449 "(expected %i, found %i)\n", crtc->active, active);
9450
Daniel Vetterc0b03412013-05-28 12:05:54 +02009451 if (active &&
9452 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9453 WARN(1, "pipe state doesn't match!\n");
9454 intel_dump_pipe_config(crtc, &pipe_config,
9455 "[hw state]");
9456 intel_dump_pipe_config(crtc, &crtc->config,
9457 "[sw state]");
9458 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009459 }
9460}
9461
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009462static void
9463check_shared_dpll_state(struct drm_device *dev)
9464{
9465 drm_i915_private_t *dev_priv = dev->dev_private;
9466 struct intel_crtc *crtc;
9467 struct intel_dpll_hw_state dpll_hw_state;
9468 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009469
9470 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9471 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9472 int enabled_crtcs = 0, active_crtcs = 0;
9473 bool active;
9474
9475 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9476
9477 DRM_DEBUG_KMS("%s\n", pll->name);
9478
9479 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9480
9481 WARN(pll->active > pll->refcount,
9482 "more active pll users than references: %i vs %i\n",
9483 pll->active, pll->refcount);
9484 WARN(pll->active && !pll->on,
9485 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009486 WARN(pll->on && !pll->active,
9487 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009488 WARN(pll->on != active,
9489 "pll on state mismatch (expected %i, found %i)\n",
9490 pll->on, active);
9491
9492 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9493 base.head) {
9494 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9495 enabled_crtcs++;
9496 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9497 active_crtcs++;
9498 }
9499 WARN(pll->active != active_crtcs,
9500 "pll active crtcs mismatch (expected %i, found %i)\n",
9501 pll->active, active_crtcs);
9502 WARN(pll->refcount != enabled_crtcs,
9503 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9504 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009505
9506 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9507 sizeof(dpll_hw_state)),
9508 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009509 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009510}
9511
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009512void
9513intel_modeset_check_state(struct drm_device *dev)
9514{
9515 check_connector_state(dev);
9516 check_encoder_state(dev);
9517 check_crtc_state(dev);
9518 check_shared_dpll_state(dev);
9519}
9520
Ville Syrjälä18442d02013-09-13 16:00:08 +03009521void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9522 int dotclock)
9523{
9524 /*
9525 * FDI already provided one idea for the dotclock.
9526 * Yell if the encoder disagrees.
9527 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009528 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009529 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009530 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009531}
9532
Daniel Vetterf30da182013-04-11 20:22:50 +02009533static int __intel_set_mode(struct drm_crtc *crtc,
9534 struct drm_display_mode *mode,
9535 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009536{
9537 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009538 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009539 struct drm_display_mode *saved_mode, *saved_hwmode;
9540 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009541 struct intel_crtc *intel_crtc;
9542 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009543 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009544
Daniel Vettera1e22652013-09-21 00:35:38 +02009545 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009546 if (!saved_mode)
9547 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009548 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009549
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009550 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009551 &prepare_pipes, &disable_pipes);
9552
Tim Gardner3ac18232012-12-07 07:54:26 -07009553 *saved_hwmode = crtc->hwmode;
9554 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009555
Daniel Vetter25c5b262012-07-08 22:08:04 +02009556 /* Hack: Because we don't (yet) support global modeset on multiple
9557 * crtcs, we don't keep track of the new mode for more than one crtc.
9558 * Hence simply check whether any bit is set in modeset_pipes in all the
9559 * pieces of code that are not yet converted to deal with mutliple crtcs
9560 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009561 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009562 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009563 if (IS_ERR(pipe_config)) {
9564 ret = PTR_ERR(pipe_config);
9565 pipe_config = NULL;
9566
Tim Gardner3ac18232012-12-07 07:54:26 -07009567 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009568 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009569 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9570 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009571 }
9572
Jesse Barnes30a970c2013-11-04 13:48:12 -08009573 /*
9574 * See if the config requires any additional preparation, e.g.
9575 * to adjust global state with pipes off. We need to do this
9576 * here so we can get the modeset_pipe updated config for the new
9577 * mode set on this crtc. For other crtcs we need to use the
9578 * adjusted_mode bits in the crtc directly.
9579 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009580 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08009581 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9582 modeset_pipes, pipe_config);
9583
Ville Syrjäläc164f832013-11-05 22:34:12 +02009584 /* may have added more to prepare_pipes than we should */
9585 prepare_pipes &= ~disable_pipes;
9586 }
9587
Daniel Vetter460da9162013-03-27 00:44:51 +01009588 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9589 intel_crtc_disable(&intel_crtc->base);
9590
Daniel Vetterea9d7582012-07-10 10:42:52 +02009591 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9592 if (intel_crtc->base.enabled)
9593 dev_priv->display.crtc_disable(&intel_crtc->base);
9594 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009595
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009596 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9597 * to set it here already despite that we pass it down the callchain.
9598 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009599 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009600 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009601 /* mode_set/enable/disable functions rely on a correct pipe
9602 * config. */
9603 to_intel_crtc(crtc)->config = *pipe_config;
9604 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009605
Daniel Vetterea9d7582012-07-10 10:42:52 +02009606 /* Only after disabling all output pipelines that will be changed can we
9607 * update the the output configuration. */
9608 intel_modeset_update_state(dev, prepare_pipes);
9609
Daniel Vetter47fab732012-10-26 10:58:18 +02009610 if (dev_priv->display.modeset_global_resources)
9611 dev_priv->display.modeset_global_resources(dev);
9612
Daniel Vettera6778b32012-07-02 09:56:42 +02009613 /* Set up the DPLL and any encoders state that needs to adjust or depend
9614 * on the DPLL.
9615 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009616 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009617 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009618 x, y, fb);
9619 if (ret)
9620 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009621 }
9622
9623 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009624 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9625 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009626
Daniel Vetter25c5b262012-07-08 22:08:04 +02009627 if (modeset_pipes) {
9628 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009629 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009630
Daniel Vetter25c5b262012-07-08 22:08:04 +02009631 /* Calculate and store various constants which
9632 * are later needed by vblank and swap-completion
9633 * timestamping. They are derived from true hwmode.
9634 */
9635 drm_calc_timestamping_constants(crtc);
9636 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009637
9638 /* FIXME: add subpixel order */
9639done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009640 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009641 crtc->hwmode = *saved_hwmode;
9642 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009643 }
9644
Tim Gardner3ac18232012-12-07 07:54:26 -07009645out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009646 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009647 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009648 return ret;
9649}
9650
Damien Lespiaue7457a92013-08-08 22:28:59 +01009651static int intel_set_mode(struct drm_crtc *crtc,
9652 struct drm_display_mode *mode,
9653 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009654{
9655 int ret;
9656
9657 ret = __intel_set_mode(crtc, mode, x, y, fb);
9658
9659 if (ret == 0)
9660 intel_modeset_check_state(crtc->dev);
9661
9662 return ret;
9663}
9664
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009665void intel_crtc_restore_mode(struct drm_crtc *crtc)
9666{
9667 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9668}
9669
Daniel Vetter25c5b262012-07-08 22:08:04 +02009670#undef for_each_intel_crtc_masked
9671
Daniel Vetterd9e55602012-07-04 22:16:09 +02009672static void intel_set_config_free(struct intel_set_config *config)
9673{
9674 if (!config)
9675 return;
9676
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009677 kfree(config->save_connector_encoders);
9678 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009679 kfree(config);
9680}
9681
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009682static int intel_set_config_save_state(struct drm_device *dev,
9683 struct intel_set_config *config)
9684{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009685 struct drm_encoder *encoder;
9686 struct drm_connector *connector;
9687 int count;
9688
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009689 config->save_encoder_crtcs =
9690 kcalloc(dev->mode_config.num_encoder,
9691 sizeof(struct drm_crtc *), GFP_KERNEL);
9692 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009693 return -ENOMEM;
9694
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009695 config->save_connector_encoders =
9696 kcalloc(dev->mode_config.num_connector,
9697 sizeof(struct drm_encoder *), GFP_KERNEL);
9698 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009699 return -ENOMEM;
9700
9701 /* Copy data. Note that driver private data is not affected.
9702 * Should anything bad happen only the expected state is
9703 * restored, not the drivers personal bookkeeping.
9704 */
9705 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009706 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009707 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009708 }
9709
9710 count = 0;
9711 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009712 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009713 }
9714
9715 return 0;
9716}
9717
9718static void intel_set_config_restore_state(struct drm_device *dev,
9719 struct intel_set_config *config)
9720{
Daniel Vetter9a935852012-07-05 22:34:27 +02009721 struct intel_encoder *encoder;
9722 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009723 int count;
9724
9725 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009726 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9727 encoder->new_crtc =
9728 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009729 }
9730
9731 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009732 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9733 connector->new_encoder =
9734 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009735 }
9736}
9737
Imre Deake3de42b2013-05-03 19:44:07 +02009738static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009739is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009740{
9741 int i;
9742
Chris Wilson2e57f472013-07-17 12:14:40 +01009743 if (set->num_connectors == 0)
9744 return false;
9745
9746 if (WARN_ON(set->connectors == NULL))
9747 return false;
9748
9749 for (i = 0; i < set->num_connectors; i++)
9750 if (set->connectors[i]->encoder &&
9751 set->connectors[i]->encoder->crtc == set->crtc &&
9752 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009753 return true;
9754
9755 return false;
9756}
9757
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009758static void
9759intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9760 struct intel_set_config *config)
9761{
9762
9763 /* We should be able to check here if the fb has the same properties
9764 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009765 if (is_crtc_connector_off(set)) {
9766 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009767 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009768 /* If we have no fb then treat it as a full mode set */
9769 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009770 struct intel_crtc *intel_crtc =
9771 to_intel_crtc(set->crtc);
9772
9773 if (intel_crtc->active && i915_fastboot) {
9774 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9775 config->fb_changed = true;
9776 } else {
9777 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9778 config->mode_changed = true;
9779 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009780 } else if (set->fb == NULL) {
9781 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009782 } else if (set->fb->pixel_format !=
9783 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009784 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009785 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009786 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009787 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009788 }
9789
Daniel Vetter835c5872012-07-10 18:11:08 +02009790 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009791 config->fb_changed = true;
9792
9793 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9794 DRM_DEBUG_KMS("modes are different, full mode set\n");
9795 drm_mode_debug_printmodeline(&set->crtc->mode);
9796 drm_mode_debug_printmodeline(set->mode);
9797 config->mode_changed = true;
9798 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009799
9800 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9801 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009802}
9803
Daniel Vetter2e431052012-07-04 22:42:15 +02009804static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009805intel_modeset_stage_output_state(struct drm_device *dev,
9806 struct drm_mode_set *set,
9807 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009808{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009809 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009810 struct intel_connector *connector;
9811 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009812 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009813
Damien Lespiau9abdda72013-02-13 13:29:23 +00009814 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009815 * of connectors. For paranoia, double-check this. */
9816 WARN_ON(!set->fb && (set->num_connectors != 0));
9817 WARN_ON(set->fb && (set->num_connectors == 0));
9818
Daniel Vetter9a935852012-07-05 22:34:27 +02009819 list_for_each_entry(connector, &dev->mode_config.connector_list,
9820 base.head) {
9821 /* Otherwise traverse passed in connector list and get encoders
9822 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009823 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009824 if (set->connectors[ro] == &connector->base) {
9825 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009826 break;
9827 }
9828 }
9829
Daniel Vetter9a935852012-07-05 22:34:27 +02009830 /* If we disable the crtc, disable all its connectors. Also, if
9831 * the connector is on the changing crtc but not on the new
9832 * connector list, disable it. */
9833 if ((!set->fb || ro == set->num_connectors) &&
9834 connector->base.encoder &&
9835 connector->base.encoder->crtc == set->crtc) {
9836 connector->new_encoder = NULL;
9837
9838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9839 connector->base.base.id,
9840 drm_get_connector_name(&connector->base));
9841 }
9842
9843
9844 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009845 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009846 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009847 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009848 }
9849 /* connector->new_encoder is now updated for all connectors. */
9850
9851 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009852 list_for_each_entry(connector, &dev->mode_config.connector_list,
9853 base.head) {
9854 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009855 continue;
9856
Daniel Vetter9a935852012-07-05 22:34:27 +02009857 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009858
9859 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009860 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009861 new_crtc = set->crtc;
9862 }
9863
9864 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009865 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9866 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009867 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009868 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009869 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9870
9871 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9872 connector->base.base.id,
9873 drm_get_connector_name(&connector->base),
9874 new_crtc->base.id);
9875 }
9876
9877 /* Check for any encoders that needs to be disabled. */
9878 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9879 base.head) {
9880 list_for_each_entry(connector,
9881 &dev->mode_config.connector_list,
9882 base.head) {
9883 if (connector->new_encoder == encoder) {
9884 WARN_ON(!connector->new_encoder->new_crtc);
9885
9886 goto next_encoder;
9887 }
9888 }
9889 encoder->new_crtc = NULL;
9890next_encoder:
9891 /* Only now check for crtc changes so we don't miss encoders
9892 * that will be disabled. */
9893 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009894 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009895 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009896 }
9897 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009898 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009899
Daniel Vetter2e431052012-07-04 22:42:15 +02009900 return 0;
9901}
9902
9903static int intel_crtc_set_config(struct drm_mode_set *set)
9904{
9905 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009906 struct drm_mode_set save_set;
9907 struct intel_set_config *config;
9908 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009909
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009910 BUG_ON(!set);
9911 BUG_ON(!set->crtc);
9912 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009913
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009914 /* Enforce sane interface api - has been abused by the fb helper. */
9915 BUG_ON(!set->mode && set->fb);
9916 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009917
Daniel Vetter2e431052012-07-04 22:42:15 +02009918 if (set->fb) {
9919 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9920 set->crtc->base.id, set->fb->base.id,
9921 (int)set->num_connectors, set->x, set->y);
9922 } else {
9923 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009924 }
9925
9926 dev = set->crtc->dev;
9927
9928 ret = -ENOMEM;
9929 config = kzalloc(sizeof(*config), GFP_KERNEL);
9930 if (!config)
9931 goto out_config;
9932
9933 ret = intel_set_config_save_state(dev, config);
9934 if (ret)
9935 goto out_config;
9936
9937 save_set.crtc = set->crtc;
9938 save_set.mode = &set->crtc->mode;
9939 save_set.x = set->crtc->x;
9940 save_set.y = set->crtc->y;
9941 save_set.fb = set->crtc->fb;
9942
9943 /* Compute whether we need a full modeset, only an fb base update or no
9944 * change at all. In the future we might also check whether only the
9945 * mode changed, e.g. for LVDS where we only change the panel fitter in
9946 * such cases. */
9947 intel_set_config_compute_mode_changes(set, config);
9948
Daniel Vetter9a935852012-07-05 22:34:27 +02009949 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009950 if (ret)
9951 goto fail;
9952
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009953 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009954 ret = intel_set_mode(set->crtc, set->mode,
9955 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009956 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009957 intel_crtc_wait_for_pending_flips(set->crtc);
9958
Daniel Vetter4f660f42012-07-02 09:47:37 +02009959 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009960 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009961 }
9962
Chris Wilson2d05eae2013-05-03 17:36:25 +01009963 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009964 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9965 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009966fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009967 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009968
Chris Wilson2d05eae2013-05-03 17:36:25 +01009969 /* Try to restore the config */
9970 if (config->mode_changed &&
9971 intel_set_mode(save_set.crtc, save_set.mode,
9972 save_set.x, save_set.y, save_set.fb))
9973 DRM_ERROR("failed to restore config after modeset failure\n");
9974 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009975
Daniel Vetterd9e55602012-07-04 22:16:09 +02009976out_config:
9977 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009978 return ret;
9979}
9980
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009981static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009982 .cursor_set = intel_crtc_cursor_set,
9983 .cursor_move = intel_crtc_cursor_move,
9984 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009985 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009986 .destroy = intel_crtc_destroy,
9987 .page_flip = intel_crtc_page_flip,
9988};
9989
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009990static void intel_cpu_pll_init(struct drm_device *dev)
9991{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009992 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009993 intel_ddi_pll_init(dev);
9994}
9995
Daniel Vetter53589012013-06-05 13:34:16 +02009996static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9997 struct intel_shared_dpll *pll,
9998 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009999{
Daniel Vetter53589012013-06-05 13:34:16 +020010000 uint32_t val;
10001
10002 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010003 hw_state->dpll = val;
10004 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10005 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010006
10007 return val & DPLL_VCO_ENABLE;
10008}
10009
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010010static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10011 struct intel_shared_dpll *pll)
10012{
10013 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10014 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10015}
10016
Daniel Vettere7b903d2013-06-05 13:34:14 +020010017static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10018 struct intel_shared_dpll *pll)
10019{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010020 /* PCH refclock must be enabled first */
10021 assert_pch_refclk_enabled(dev_priv);
10022
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010023 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10024
10025 /* Wait for the clocks to stabilize. */
10026 POSTING_READ(PCH_DPLL(pll->id));
10027 udelay(150);
10028
10029 /* The pixel multiplier can only be updated once the
10030 * DPLL is enabled and the clocks are stable.
10031 *
10032 * So write it again.
10033 */
10034 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10035 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010036 udelay(200);
10037}
10038
10039static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10040 struct intel_shared_dpll *pll)
10041{
10042 struct drm_device *dev = dev_priv->dev;
10043 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010044
10045 /* Make sure no transcoder isn't still depending on us. */
10046 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10047 if (intel_crtc_to_shared_dpll(crtc) == pll)
10048 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10049 }
10050
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010051 I915_WRITE(PCH_DPLL(pll->id), 0);
10052 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010053 udelay(200);
10054}
10055
Daniel Vetter46edb022013-06-05 13:34:12 +020010056static char *ibx_pch_dpll_names[] = {
10057 "PCH DPLL A",
10058 "PCH DPLL B",
10059};
10060
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010061static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010062{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010063 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010064 int i;
10065
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010066 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010067
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010068 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010069 dev_priv->shared_dplls[i].id = i;
10070 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010071 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010072 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10073 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010074 dev_priv->shared_dplls[i].get_hw_state =
10075 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010076 }
10077}
10078
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010079static void intel_shared_dpll_init(struct drm_device *dev)
10080{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010081 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010082
10083 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10084 ibx_pch_dpll_init(dev);
10085 else
10086 dev_priv->num_shared_dpll = 0;
10087
10088 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10089 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
10090 dev_priv->num_shared_dpll);
10091}
10092
Hannes Ederb358d0a2008-12-18 21:18:47 +010010093static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010094{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010095 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010096 struct intel_crtc *intel_crtc;
10097 int i;
10098
Daniel Vetter955382f2013-09-19 14:05:45 +020010099 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010100 if (intel_crtc == NULL)
10101 return;
10102
10103 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10104
10105 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010106 for (i = 0; i < 256; i++) {
10107 intel_crtc->lut_r[i] = i;
10108 intel_crtc->lut_g[i] = i;
10109 intel_crtc->lut_b[i] = i;
10110 }
10111
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010112 /*
10113 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10114 * is hooked to plane B. Hence we want plane A feeding pipe B.
10115 */
Jesse Barnes80824002009-09-10 15:28:06 -070010116 intel_crtc->pipe = pipe;
10117 intel_crtc->plane = pipe;
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010118 if (IS_MOBILE(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010119 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010120 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010121 }
10122
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010123 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10124 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10125 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10126 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10127
Jesse Barnes79e53942008-11-07 14:24:08 -080010128 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010129}
10130
Jesse Barnes752aa882013-10-31 18:55:49 +020010131enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10132{
10133 struct drm_encoder *encoder = connector->base.encoder;
10134
10135 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10136
10137 if (!encoder)
10138 return INVALID_PIPE;
10139
10140 return to_intel_crtc(encoder->crtc)->pipe;
10141}
10142
Carl Worth08d7b3d2009-04-29 14:43:54 -070010143int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010144 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010145{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010146 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010147 struct drm_mode_object *drmmode_obj;
10148 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010149
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010150 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10151 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010152
Daniel Vetterc05422d2009-08-11 16:05:30 +020010153 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10154 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010155
Daniel Vetterc05422d2009-08-11 16:05:30 +020010156 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010157 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010158 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010159 }
10160
Daniel Vetterc05422d2009-08-11 16:05:30 +020010161 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10162 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010163
Daniel Vetterc05422d2009-08-11 16:05:30 +020010164 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010165}
10166
Daniel Vetter66a92782012-07-12 20:08:18 +020010167static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010168{
Daniel Vetter66a92782012-07-12 20:08:18 +020010169 struct drm_device *dev = encoder->base.dev;
10170 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010171 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010172 int entry = 0;
10173
Daniel Vetter66a92782012-07-12 20:08:18 +020010174 list_for_each_entry(source_encoder,
10175 &dev->mode_config.encoder_list, base.head) {
10176
10177 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010178 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010179
10180 /* Intel hw has only one MUX where enocoders could be cloned. */
10181 if (encoder->cloneable && source_encoder->cloneable)
10182 index_mask |= (1 << entry);
10183
Jesse Barnes79e53942008-11-07 14:24:08 -080010184 entry++;
10185 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010186
Jesse Barnes79e53942008-11-07 14:24:08 -080010187 return index_mask;
10188}
10189
Chris Wilson4d302442010-12-14 19:21:29 +000010190static bool has_edp_a(struct drm_device *dev)
10191{
10192 struct drm_i915_private *dev_priv = dev->dev_private;
10193
10194 if (!IS_MOBILE(dev))
10195 return false;
10196
10197 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10198 return false;
10199
10200 if (IS_GEN5(dev) &&
10201 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10202 return false;
10203
10204 return true;
10205}
10206
Jesse Barnes79e53942008-11-07 14:24:08 -080010207static void intel_setup_outputs(struct drm_device *dev)
10208{
Eric Anholt725e30a2009-01-22 13:01:02 -080010209 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010210 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010211 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010212
Daniel Vetterc9093352013-06-06 22:22:47 +020010213 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010214
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010215 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010216 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010217
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010218 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010219 int found;
10220
10221 /* Haswell uses DDI functions to detect digital outputs */
10222 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10223 /* DDI A only supports eDP */
10224 if (found)
10225 intel_ddi_init(dev, PORT_A);
10226
10227 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10228 * register */
10229 found = I915_READ(SFUSE_STRAP);
10230
10231 if (found & SFUSE_STRAP_DDIB_DETECTED)
10232 intel_ddi_init(dev, PORT_B);
10233 if (found & SFUSE_STRAP_DDIC_DETECTED)
10234 intel_ddi_init(dev, PORT_C);
10235 if (found & SFUSE_STRAP_DDID_DETECTED)
10236 intel_ddi_init(dev, PORT_D);
10237 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010238 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +020010239 dpd_is_edp = intel_dpd_is_edp(dev);
10240
10241 if (has_edp_a(dev))
10242 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010243
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010244 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010245 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010246 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010247 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010248 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010249 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010250 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010251 }
10252
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010253 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010254 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010255
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010256 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010257 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010258
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010259 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010260 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010261
Daniel Vetter270b3042012-10-27 15:52:05 +020010262 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010263 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010264 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010265 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10266 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10267 PORT_B);
10268 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10269 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10270 }
10271
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010272 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10273 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10274 PORT_C);
10275 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10276 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10277 PORT_C);
10278 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010279
Jani Nikula3cfca972013-08-27 15:12:26 +030010280 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010281 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010282 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010283
Paulo Zanonie2debe92013-02-18 19:00:27 -030010284 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010285 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010286 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010287 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10288 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010289 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010290 }
Ma Ling27185ae2009-08-24 13:50:23 +080010291
Imre Deake7281ea2013-05-08 13:14:08 +030010292 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010293 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010294 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010295
10296 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010297
Paulo Zanonie2debe92013-02-18 19:00:27 -030010298 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010299 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010300 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010301 }
Ma Ling27185ae2009-08-24 13:50:23 +080010302
Paulo Zanonie2debe92013-02-18 19:00:27 -030010303 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010304
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010305 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10306 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010307 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010308 }
Imre Deake7281ea2013-05-08 13:14:08 +030010309 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010310 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010311 }
Ma Ling27185ae2009-08-24 13:50:23 +080010312
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010313 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010314 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010315 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010316 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010317 intel_dvo_init(dev);
10318
Zhenyu Wang103a1962009-11-27 11:44:36 +080010319 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010320 intel_tv_init(dev);
10321
Chris Wilson4ef69c72010-09-09 15:14:28 +010010322 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10323 encoder->base.possible_crtcs = encoder->crtc_mask;
10324 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010325 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010326 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010327
Paulo Zanonidde86e22012-12-01 12:04:25 -020010328 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010329
10330 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010331}
10332
Chris Wilsonddfe1562013-08-06 17:43:07 +010010333void intel_framebuffer_fini(struct intel_framebuffer *fb)
10334{
10335 drm_framebuffer_cleanup(&fb->base);
Daniel Vetter80075d42013-10-09 21:23:52 +020010336 WARN_ON(!fb->obj->framebuffer_references--);
Chris Wilsonddfe1562013-08-06 17:43:07 +010010337 drm_gem_object_unreference_unlocked(&fb->obj->base);
10338}
10339
Jesse Barnes79e53942008-11-07 14:24:08 -080010340static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10341{
10342 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010343
Chris Wilsonddfe1562013-08-06 17:43:07 +010010344 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010345 kfree(intel_fb);
10346}
10347
10348static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010349 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010350 unsigned int *handle)
10351{
10352 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010353 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010354
Chris Wilson05394f32010-11-08 19:18:58 +000010355 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010356}
10357
10358static const struct drm_framebuffer_funcs intel_fb_funcs = {
10359 .destroy = intel_user_framebuffer_destroy,
10360 .create_handle = intel_user_framebuffer_create_handle,
10361};
10362
Dave Airlie38651672010-03-30 05:34:13 +000010363int intel_framebuffer_init(struct drm_device *dev,
10364 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010365 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010366 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010367{
Daniel Vetter53155c02013-10-09 21:55:33 +020010368 int aligned_height, tile_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010369 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010370 int ret;
10371
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010372 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10373
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010374 if (obj->tiling_mode == I915_TILING_Y) {
10375 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010376 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010377 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010378
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010379 if (mode_cmd->pitches[0] & 63) {
10380 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10381 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010382 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010383 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010384
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010385 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10386 pitch_limit = 32*1024;
10387 } else if (INTEL_INFO(dev)->gen >= 4) {
10388 if (obj->tiling_mode)
10389 pitch_limit = 16*1024;
10390 else
10391 pitch_limit = 32*1024;
10392 } else if (INTEL_INFO(dev)->gen >= 3) {
10393 if (obj->tiling_mode)
10394 pitch_limit = 8*1024;
10395 else
10396 pitch_limit = 16*1024;
10397 } else
10398 /* XXX DSPC is limited to 4k tiled */
10399 pitch_limit = 8*1024;
10400
10401 if (mode_cmd->pitches[0] > pitch_limit) {
10402 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10403 obj->tiling_mode ? "tiled" : "linear",
10404 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010405 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010406 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010407
10408 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010409 mode_cmd->pitches[0] != obj->stride) {
10410 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10411 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010412 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010413 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010414
Ville Syrjälä57779d02012-10-31 17:50:14 +020010415 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010416 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010417 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010418 case DRM_FORMAT_RGB565:
10419 case DRM_FORMAT_XRGB8888:
10420 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010421 break;
10422 case DRM_FORMAT_XRGB1555:
10423 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010424 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010425 DRM_DEBUG("unsupported pixel format: %s\n",
10426 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010427 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010428 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010429 break;
10430 case DRM_FORMAT_XBGR8888:
10431 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010432 case DRM_FORMAT_XRGB2101010:
10433 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010434 case DRM_FORMAT_XBGR2101010:
10435 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010436 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010437 DRM_DEBUG("unsupported pixel format: %s\n",
10438 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010439 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010440 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010441 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010442 case DRM_FORMAT_YUYV:
10443 case DRM_FORMAT_UYVY:
10444 case DRM_FORMAT_YVYU:
10445 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010446 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010447 DRM_DEBUG("unsupported pixel format: %s\n",
10448 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010449 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010450 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010451 break;
10452 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010453 DRM_DEBUG("unsupported pixel format: %s\n",
10454 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010455 return -EINVAL;
10456 }
10457
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010458 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10459 if (mode_cmd->offsets[0] != 0)
10460 return -EINVAL;
10461
Daniel Vetter53155c02013-10-09 21:55:33 +020010462 tile_height = IS_GEN2(dev) ? 16 : 8;
10463 aligned_height = ALIGN(mode_cmd->height,
10464 obj->tiling_mode ? tile_height : 1);
10465 /* FIXME drm helper for size checks (especially planar formats)? */
10466 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10467 return -EINVAL;
10468
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010469 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10470 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010471 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010472
Jesse Barnes79e53942008-11-07 14:24:08 -080010473 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10474 if (ret) {
10475 DRM_ERROR("framebuffer init failed %d\n", ret);
10476 return ret;
10477 }
10478
Jesse Barnes79e53942008-11-07 14:24:08 -080010479 return 0;
10480}
10481
Jesse Barnes79e53942008-11-07 14:24:08 -080010482static struct drm_framebuffer *
10483intel_user_framebuffer_create(struct drm_device *dev,
10484 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010485 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010486{
Chris Wilson05394f32010-11-08 19:18:58 +000010487 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010488
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010489 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10490 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010491 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010492 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010493
Chris Wilsond2dff872011-04-19 08:36:26 +010010494 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010495}
10496
Daniel Vetter4520f532013-10-09 09:18:51 +020010497#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010498static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010499{
10500}
10501#endif
10502
Jesse Barnes79e53942008-11-07 14:24:08 -080010503static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010504 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010505 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010506};
10507
Jesse Barnese70236a2009-09-21 10:42:27 -070010508/* Set up chip specific display functions */
10509static void intel_init_display(struct drm_device *dev)
10510{
10511 struct drm_i915_private *dev_priv = dev->dev_private;
10512
Daniel Vetteree9300b2013-06-03 22:40:22 +020010513 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10514 dev_priv->display.find_dpll = g4x_find_best_dpll;
10515 else if (IS_VALLEYVIEW(dev))
10516 dev_priv->display.find_dpll = vlv_find_best_dpll;
10517 else if (IS_PINEVIEW(dev))
10518 dev_priv->display.find_dpll = pnv_find_best_dpll;
10519 else
10520 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10521
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010522 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010523 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010524 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010525 dev_priv->display.crtc_enable = haswell_crtc_enable;
10526 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010527 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010528 dev_priv->display.update_plane = ironlake_update_plane;
10529 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010530 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010531 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010532 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10533 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010534 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010535 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010536 } else if (IS_VALLEYVIEW(dev)) {
10537 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10538 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10539 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10540 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10541 dev_priv->display.off = i9xx_crtc_off;
10542 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010543 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010544 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010545 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010546 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10547 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010548 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010549 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010550 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010551
Jesse Barnese70236a2009-09-21 10:42:27 -070010552 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010553 if (IS_VALLEYVIEW(dev))
10554 dev_priv->display.get_display_clock_speed =
10555 valleyview_get_display_clock_speed;
10556 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010557 dev_priv->display.get_display_clock_speed =
10558 i945_get_display_clock_speed;
10559 else if (IS_I915G(dev))
10560 dev_priv->display.get_display_clock_speed =
10561 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010562 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010563 dev_priv->display.get_display_clock_speed =
10564 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010565 else if (IS_PINEVIEW(dev))
10566 dev_priv->display.get_display_clock_speed =
10567 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010568 else if (IS_I915GM(dev))
10569 dev_priv->display.get_display_clock_speed =
10570 i915gm_get_display_clock_speed;
10571 else if (IS_I865G(dev))
10572 dev_priv->display.get_display_clock_speed =
10573 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010574 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010575 dev_priv->display.get_display_clock_speed =
10576 i855_get_display_clock_speed;
10577 else /* 852, 830 */
10578 dev_priv->display.get_display_clock_speed =
10579 i830_get_display_clock_speed;
10580
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010581 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010582 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010583 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010584 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010585 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010586 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010587 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010588 } else if (IS_IVYBRIDGE(dev)) {
10589 /* FIXME: detect B0+ stepping and use auto training */
10590 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010591 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010592 dev_priv->display.modeset_global_resources =
10593 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010594 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010595 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010596 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010597 dev_priv->display.modeset_global_resources =
10598 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010599 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010600 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010601 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010602 } else if (IS_VALLEYVIEW(dev)) {
10603 dev_priv->display.modeset_global_resources =
10604 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040010605 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010606 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010607
10608 /* Default just returns -ENODEV to indicate unsupported */
10609 dev_priv->display.queue_flip = intel_default_queue_flip;
10610
10611 switch (INTEL_INFO(dev)->gen) {
10612 case 2:
10613 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10614 break;
10615
10616 case 3:
10617 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10618 break;
10619
10620 case 4:
10621 case 5:
10622 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10623 break;
10624
10625 case 6:
10626 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10627 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010628 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010629 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010630 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10631 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010632 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020010633
10634 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010635}
10636
Jesse Barnesb690e962010-07-19 13:53:12 -070010637/*
10638 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10639 * resume, or other times. This quirk makes sure that's the case for
10640 * affected systems.
10641 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010642static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010643{
10644 struct drm_i915_private *dev_priv = dev->dev_private;
10645
10646 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010647 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010648}
10649
Keith Packard435793d2011-07-12 14:56:22 -070010650/*
10651 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10652 */
10653static void quirk_ssc_force_disable(struct drm_device *dev)
10654{
10655 struct drm_i915_private *dev_priv = dev->dev_private;
10656 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010657 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010658}
10659
Carsten Emde4dca20e2012-03-15 15:56:26 +010010660/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010661 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10662 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010663 */
10664static void quirk_invert_brightness(struct drm_device *dev)
10665{
10666 struct drm_i915_private *dev_priv = dev->dev_private;
10667 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010668 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010669}
10670
10671struct intel_quirk {
10672 int device;
10673 int subsystem_vendor;
10674 int subsystem_device;
10675 void (*hook)(struct drm_device *dev);
10676};
10677
Egbert Eich5f85f1762012-10-14 15:46:38 +020010678/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10679struct intel_dmi_quirk {
10680 void (*hook)(struct drm_device *dev);
10681 const struct dmi_system_id (*dmi_id_list)[];
10682};
10683
10684static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10685{
10686 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10687 return 1;
10688}
10689
10690static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10691 {
10692 .dmi_id_list = &(const struct dmi_system_id[]) {
10693 {
10694 .callback = intel_dmi_reverse_brightness,
10695 .ident = "NCR Corporation",
10696 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10697 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10698 },
10699 },
10700 { } /* terminating entry */
10701 },
10702 .hook = quirk_invert_brightness,
10703 },
10704};
10705
Ben Widawskyc43b5632012-04-16 14:07:40 -070010706static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010707 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010708 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010709
Jesse Barnesb690e962010-07-19 13:53:12 -070010710 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10711 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10712
Jesse Barnesb690e962010-07-19 13:53:12 -070010713 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10714 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10715
Chris Wilsona4945f92013-10-08 11:16:59 +010010716 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010717 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010718
10719 /* Lenovo U160 cannot use SSC on LVDS */
10720 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010721
10722 /* Sony Vaio Y cannot use SSC on LVDS */
10723 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010724
Jani Nikulaee1452d2013-09-20 15:05:30 +030010725 /*
10726 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10727 * seem to use inverted backlight PWM.
10728 */
10729 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070010730};
10731
10732static void intel_init_quirks(struct drm_device *dev)
10733{
10734 struct pci_dev *d = dev->pdev;
10735 int i;
10736
10737 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10738 struct intel_quirk *q = &intel_quirks[i];
10739
10740 if (d->device == q->device &&
10741 (d->subsystem_vendor == q->subsystem_vendor ||
10742 q->subsystem_vendor == PCI_ANY_ID) &&
10743 (d->subsystem_device == q->subsystem_device ||
10744 q->subsystem_device == PCI_ANY_ID))
10745 q->hook(dev);
10746 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010747 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10748 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10749 intel_dmi_quirks[i].hook(dev);
10750 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010751}
10752
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010753/* Disable the VGA plane that we never use */
10754static void i915_disable_vga(struct drm_device *dev)
10755{
10756 struct drm_i915_private *dev_priv = dev->dev_private;
10757 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010758 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010759
10760 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010761 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010762 sr1 = inb(VGA_SR_DATA);
10763 outb(sr1 | 1<<5, VGA_SR_DATA);
10764 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10765 udelay(300);
10766
10767 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10768 POSTING_READ(vga_reg);
10769}
10770
Daniel Vetterf8175862012-04-10 15:50:11 +020010771void intel_modeset_init_hw(struct drm_device *dev)
10772{
Jesse Barnesf6071162013-10-01 10:41:38 -070010773 struct drm_i915_private *dev_priv = dev->dev_private;
10774
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010775 intel_prepare_ddi(dev);
10776
Daniel Vetterf8175862012-04-10 15:50:11 +020010777 intel_init_clock_gating(dev);
10778
Jesse Barnesf6071162013-10-01 10:41:38 -070010779 /* Enable the CRI clock source so we can get at the display */
10780 if (IS_VALLEYVIEW(dev))
10781 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10782 DPLL_INTEGRATED_CRI_CLK_VLV);
10783
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010784 intel_init_dpio(dev);
10785
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010786 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010787 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010788 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010789}
10790
Imre Deak7d708ee2013-04-17 14:04:50 +030010791void intel_modeset_suspend_hw(struct drm_device *dev)
10792{
10793 intel_suspend_hw(dev);
10794}
10795
Jesse Barnes79e53942008-11-07 14:24:08 -080010796void intel_modeset_init(struct drm_device *dev)
10797{
Jesse Barnes652c3932009-08-17 13:31:43 -070010798 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010799 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010800
10801 drm_mode_config_init(dev);
10802
10803 dev->mode_config.min_width = 0;
10804 dev->mode_config.min_height = 0;
10805
Dave Airlie019d96c2011-09-29 16:20:42 +010010806 dev->mode_config.preferred_depth = 24;
10807 dev->mode_config.prefer_shadow = 1;
10808
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010809 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010810
Jesse Barnesb690e962010-07-19 13:53:12 -070010811 intel_init_quirks(dev);
10812
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010813 intel_init_pm(dev);
10814
Ben Widawskye3c74752013-04-05 13:12:39 -070010815 if (INTEL_INFO(dev)->num_pipes == 0)
10816 return;
10817
Jesse Barnese70236a2009-09-21 10:42:27 -070010818 intel_init_display(dev);
10819
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010820 if (IS_GEN2(dev)) {
10821 dev->mode_config.max_width = 2048;
10822 dev->mode_config.max_height = 2048;
10823 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010824 dev->mode_config.max_width = 4096;
10825 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010826 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010827 dev->mode_config.max_width = 8192;
10828 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010829 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010830 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010831
Zhao Yakui28c97732009-10-09 11:39:41 +080010832 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010833 INTEL_INFO(dev)->num_pipes,
10834 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010835
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010836 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010837 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010838 for (j = 0; j < dev_priv->num_plane; j++) {
10839 ret = intel_plane_init(dev, i, j);
10840 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010841 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10842 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010843 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010844 }
10845
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010846 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010847 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010848
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010849 /* Just disable it once at startup */
10850 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010851 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010852
10853 /* Just in case the BIOS is doing something questionable. */
10854 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010855}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010856
Daniel Vetter24929352012-07-02 20:28:59 +020010857static void
10858intel_connector_break_all_links(struct intel_connector *connector)
10859{
10860 connector->base.dpms = DRM_MODE_DPMS_OFF;
10861 connector->base.encoder = NULL;
10862 connector->encoder->connectors_active = false;
10863 connector->encoder->base.crtc = NULL;
10864}
10865
Daniel Vetter7fad7982012-07-04 17:51:47 +020010866static void intel_enable_pipe_a(struct drm_device *dev)
10867{
10868 struct intel_connector *connector;
10869 struct drm_connector *crt = NULL;
10870 struct intel_load_detect_pipe load_detect_temp;
10871
10872 /* We can't just switch on the pipe A, we need to set things up with a
10873 * proper mode and output configuration. As a gross hack, enable pipe A
10874 * by enabling the load detect pipe once. */
10875 list_for_each_entry(connector,
10876 &dev->mode_config.connector_list,
10877 base.head) {
10878 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10879 crt = &connector->base;
10880 break;
10881 }
10882 }
10883
10884 if (!crt)
10885 return;
10886
10887 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10888 intel_release_load_detect_pipe(crt, &load_detect_temp);
10889
10890
10891}
10892
Daniel Vetterfa555832012-10-10 23:14:00 +020010893static bool
10894intel_check_plane_mapping(struct intel_crtc *crtc)
10895{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010896 struct drm_device *dev = crtc->base.dev;
10897 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010898 u32 reg, val;
10899
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010900 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010901 return true;
10902
10903 reg = DSPCNTR(!crtc->plane);
10904 val = I915_READ(reg);
10905
10906 if ((val & DISPLAY_PLANE_ENABLE) &&
10907 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10908 return false;
10909
10910 return true;
10911}
10912
Daniel Vetter24929352012-07-02 20:28:59 +020010913static void intel_sanitize_crtc(struct intel_crtc *crtc)
10914{
10915 struct drm_device *dev = crtc->base.dev;
10916 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010917 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010918
Daniel Vetter24929352012-07-02 20:28:59 +020010919 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010920 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010921 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10922
10923 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010924 * disable the crtc (and hence change the state) if it is wrong. Note
10925 * that gen4+ has a fixed plane -> pipe mapping. */
10926 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010927 struct intel_connector *connector;
10928 bool plane;
10929
Daniel Vetter24929352012-07-02 20:28:59 +020010930 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10931 crtc->base.base.id);
10932
10933 /* Pipe has the wrong plane attached and the plane is active.
10934 * Temporarily change the plane mapping and disable everything
10935 * ... */
10936 plane = crtc->plane;
10937 crtc->plane = !plane;
10938 dev_priv->display.crtc_disable(&crtc->base);
10939 crtc->plane = plane;
10940
10941 /* ... and break all links. */
10942 list_for_each_entry(connector, &dev->mode_config.connector_list,
10943 base.head) {
10944 if (connector->encoder->base.crtc != &crtc->base)
10945 continue;
10946
10947 intel_connector_break_all_links(connector);
10948 }
10949
10950 WARN_ON(crtc->active);
10951 crtc->base.enabled = false;
10952 }
Daniel Vetter24929352012-07-02 20:28:59 +020010953
Daniel Vetter7fad7982012-07-04 17:51:47 +020010954 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10955 crtc->pipe == PIPE_A && !crtc->active) {
10956 /* BIOS forgot to enable pipe A, this mostly happens after
10957 * resume. Force-enable the pipe to fix this, the update_dpms
10958 * call below we restore the pipe to the right state, but leave
10959 * the required bits on. */
10960 intel_enable_pipe_a(dev);
10961 }
10962
Daniel Vetter24929352012-07-02 20:28:59 +020010963 /* Adjust the state of the output pipe according to whether we
10964 * have active connectors/encoders. */
10965 intel_crtc_update_dpms(&crtc->base);
10966
10967 if (crtc->active != crtc->base.enabled) {
10968 struct intel_encoder *encoder;
10969
10970 /* This can happen either due to bugs in the get_hw_state
10971 * functions or because the pipe is force-enabled due to the
10972 * pipe A quirk. */
10973 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10974 crtc->base.base.id,
10975 crtc->base.enabled ? "enabled" : "disabled",
10976 crtc->active ? "enabled" : "disabled");
10977
10978 crtc->base.enabled = crtc->active;
10979
10980 /* Because we only establish the connector -> encoder ->
10981 * crtc links if something is active, this means the
10982 * crtc is now deactivated. Break the links. connector
10983 * -> encoder links are only establish when things are
10984 * actually up, hence no need to break them. */
10985 WARN_ON(crtc->active);
10986
10987 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10988 WARN_ON(encoder->connectors_active);
10989 encoder->base.crtc = NULL;
10990 }
10991 }
10992}
10993
10994static void intel_sanitize_encoder(struct intel_encoder *encoder)
10995{
10996 struct intel_connector *connector;
10997 struct drm_device *dev = encoder->base.dev;
10998
10999 /* We need to check both for a crtc link (meaning that the
11000 * encoder is active and trying to read from a pipe) and the
11001 * pipe itself being active. */
11002 bool has_active_crtc = encoder->base.crtc &&
11003 to_intel_crtc(encoder->base.crtc)->active;
11004
11005 if (encoder->connectors_active && !has_active_crtc) {
11006 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11007 encoder->base.base.id,
11008 drm_get_encoder_name(&encoder->base));
11009
11010 /* Connector is active, but has no active pipe. This is
11011 * fallout from our resume register restoring. Disable
11012 * the encoder manually again. */
11013 if (encoder->base.crtc) {
11014 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11015 encoder->base.base.id,
11016 drm_get_encoder_name(&encoder->base));
11017 encoder->disable(encoder);
11018 }
11019
11020 /* Inconsistent output/port/pipe state happens presumably due to
11021 * a bug in one of the get_hw_state functions. Or someplace else
11022 * in our code, like the register restore mess on resume. Clamp
11023 * things to off as a safer default. */
11024 list_for_each_entry(connector,
11025 &dev->mode_config.connector_list,
11026 base.head) {
11027 if (connector->encoder != encoder)
11028 continue;
11029
11030 intel_connector_break_all_links(connector);
11031 }
11032 }
11033 /* Enabled encoders without active connectors will be fixed in
11034 * the crtc fixup. */
11035}
11036
Daniel Vetter44cec742013-01-25 17:53:21 +010011037void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011038{
11039 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011040 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011041
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011042 /* This function can be called both from intel_modeset_setup_hw_state or
11043 * at a very early point in our resume sequence, where the power well
11044 * structures are not yet restored. Since this function is at a very
11045 * paranoid "someone might have enabled VGA while we were not looking"
11046 * level, just check if the power well is enabled instead of trying to
11047 * follow the "don't touch the power well if we don't need it" policy
11048 * the rest of the driver uses. */
Jesse Barnesf9e711e2013-11-25 17:15:32 +020011049 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030011050 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011051 return;
11052
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030011053 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011054 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020011055 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011056 }
11057}
11058
Daniel Vetter30e984d2013-06-05 13:34:17 +020011059static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011060{
11061 struct drm_i915_private *dev_priv = dev->dev_private;
11062 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011063 struct intel_crtc *crtc;
11064 struct intel_encoder *encoder;
11065 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011066 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011067
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011068 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11069 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011070 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011071
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011072 crtc->active = dev_priv->display.get_pipe_config(crtc,
11073 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011074
11075 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030011076 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020011077
11078 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11079 crtc->base.base.id,
11080 crtc->active ? "enabled" : "disabled");
11081 }
11082
Daniel Vetter53589012013-06-05 13:34:16 +020011083 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011084 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011085 intel_ddi_setup_hw_pll_state(dev);
11086
Daniel Vetter53589012013-06-05 13:34:16 +020011087 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11088 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11089
11090 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11091 pll->active = 0;
11092 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11093 base.head) {
11094 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11095 pll->active++;
11096 }
11097 pll->refcount = pll->active;
11098
Daniel Vetter35c95372013-07-17 06:55:04 +020011099 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11100 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011101 }
11102
Daniel Vetter24929352012-07-02 20:28:59 +020011103 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11104 base.head) {
11105 pipe = 0;
11106
11107 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011108 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11109 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070011110 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011111 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011112 } else {
11113 encoder->base.crtc = NULL;
11114 }
11115
11116 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011117 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011118 encoder->base.base.id,
11119 drm_get_encoder_name(&encoder->base),
11120 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011121 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011122 }
11123
11124 list_for_each_entry(connector, &dev->mode_config.connector_list,
11125 base.head) {
11126 if (connector->get_hw_state(connector)) {
11127 connector->base.dpms = DRM_MODE_DPMS_ON;
11128 connector->encoder->connectors_active = true;
11129 connector->base.encoder = &connector->encoder->base;
11130 } else {
11131 connector->base.dpms = DRM_MODE_DPMS_OFF;
11132 connector->base.encoder = NULL;
11133 }
11134 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11135 connector->base.base.id,
11136 drm_get_connector_name(&connector->base),
11137 connector->base.encoder ? "enabled" : "disabled");
11138 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011139}
11140
11141/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11142 * and i915 state tracking structures. */
11143void intel_modeset_setup_hw_state(struct drm_device *dev,
11144 bool force_restore)
11145{
11146 struct drm_i915_private *dev_priv = dev->dev_private;
11147 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011148 struct intel_crtc *crtc;
11149 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011150 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011151
11152 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011153
Jesse Barnesbabea612013-06-26 18:57:38 +030011154 /*
11155 * Now that we have the config, copy it to each CRTC struct
11156 * Note that this could go away if we move to using crtc_config
11157 * checking everywhere.
11158 */
11159 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11160 base.head) {
11161 if (crtc->active && i915_fastboot) {
11162 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11163
11164 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11165 crtc->base.base.id);
11166 drm_mode_debug_printmodeline(&crtc->base.mode);
11167 }
11168 }
11169
Daniel Vetter24929352012-07-02 20:28:59 +020011170 /* HW state is read out, now we need to sanitize this mess. */
11171 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11172 base.head) {
11173 intel_sanitize_encoder(encoder);
11174 }
11175
11176 for_each_pipe(pipe) {
11177 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11178 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011179 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011180 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011181
Daniel Vetter35c95372013-07-17 06:55:04 +020011182 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11183 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11184
11185 if (!pll->on || pll->active)
11186 continue;
11187
11188 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11189
11190 pll->disable(dev_priv, pll);
11191 pll->on = false;
11192 }
11193
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011194 if (IS_HASWELL(dev))
11195 ilk_wm_get_hw_state(dev);
11196
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011197 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011198 i915_redisable_vga(dev);
11199
Daniel Vetterf30da182013-04-11 20:22:50 +020011200 /*
11201 * We need to use raw interfaces for restoring state to avoid
11202 * checking (bogus) intermediate states.
11203 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011204 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011205 struct drm_crtc *crtc =
11206 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011207
11208 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11209 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011210 }
11211 } else {
11212 intel_modeset_update_staged_output_state(dev);
11213 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011214
11215 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020011216
11217 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011218}
11219
11220void intel_modeset_gem_init(struct drm_device *dev)
11221{
Chris Wilson1833b132012-05-09 11:56:28 +010011222 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011223
11224 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011225
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011226 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080011227}
11228
11229void intel_modeset_cleanup(struct drm_device *dev)
11230{
Jesse Barnes652c3932009-08-17 13:31:43 -070011231 struct drm_i915_private *dev_priv = dev->dev_private;
11232 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011233 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011234
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011235 /*
11236 * Interrupts and polling as the first thing to avoid creating havoc.
11237 * Too much stuff here (turning of rps, connectors, ...) would
11238 * experience fancy races otherwise.
11239 */
11240 drm_irq_uninstall(dev);
11241 cancel_work_sync(&dev_priv->hotplug_work);
11242 /*
11243 * Due to the hpd irq storm handling the hotplug work can re-arm the
11244 * poll handlers. Hence disable polling after hpd handling is shut down.
11245 */
Keith Packardf87ea762010-10-03 19:36:26 -070011246 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011247
Jesse Barnes652c3932009-08-17 13:31:43 -070011248 mutex_lock(&dev->struct_mutex);
11249
Jesse Barnes723bfd72010-10-07 16:01:13 -070011250 intel_unregister_dsm_handler();
11251
Jesse Barnes652c3932009-08-17 13:31:43 -070011252 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11253 /* Skip inactive CRTCs */
11254 if (!crtc->fb)
11255 continue;
11256
Daniel Vetter3dec0092010-08-20 21:40:52 +020011257 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011258 }
11259
Chris Wilson973d04f2011-07-08 12:22:37 +010011260 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011261
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011262 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011263
Daniel Vetter930ebb42012-06-29 23:32:16 +020011264 ironlake_teardown_rc6(dev);
11265
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011266 mutex_unlock(&dev->struct_mutex);
11267
Chris Wilson1630fe72011-07-08 12:22:42 +010011268 /* flush any delayed tasks or pending work */
11269 flush_scheduled_work();
11270
Jani Nikuladb31af12013-11-08 16:48:53 +020011271 /* destroy the backlight and sysfs files before encoders/connectors */
11272 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11273 intel_panel_destroy_backlight(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030011274 drm_sysfs_connector_remove(connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011275 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011276
Jesse Barnes79e53942008-11-07 14:24:08 -080011277 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011278
11279 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011280}
11281
Dave Airlie28d52042009-09-21 14:33:58 +100011282/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011283 * Return which encoder is currently attached for connector.
11284 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011285struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011286{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011287 return &intel_attached_encoder(connector)->base;
11288}
Jesse Barnes79e53942008-11-07 14:24:08 -080011289
Chris Wilsondf0e9242010-09-09 16:20:55 +010011290void intel_connector_attach_encoder(struct intel_connector *connector,
11291 struct intel_encoder *encoder)
11292{
11293 connector->encoder = encoder;
11294 drm_mode_connector_attach_encoder(&connector->base,
11295 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011296}
Dave Airlie28d52042009-09-21 14:33:58 +100011297
11298/*
11299 * set vga decode state - true == enable VGA decode
11300 */
11301int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11302{
11303 struct drm_i915_private *dev_priv = dev->dev_private;
11304 u16 gmch_ctrl;
11305
11306 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11307 if (state)
11308 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11309 else
11310 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11311 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11312 return 0;
11313}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011314
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011315struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011316
11317 u32 power_well_driver;
11318
Chris Wilson63b66e52013-08-08 15:12:06 +020011319 int num_transcoders;
11320
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011321 struct intel_cursor_error_state {
11322 u32 control;
11323 u32 position;
11324 u32 base;
11325 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011326 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011327
11328 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011329 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011330 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011331 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011332
11333 struct intel_plane_error_state {
11334 u32 control;
11335 u32 stride;
11336 u32 size;
11337 u32 pos;
11338 u32 addr;
11339 u32 surface;
11340 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011341 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011342
11343 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011344 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020011345 enum transcoder cpu_transcoder;
11346
11347 u32 conf;
11348
11349 u32 htotal;
11350 u32 hblank;
11351 u32 hsync;
11352 u32 vtotal;
11353 u32 vblank;
11354 u32 vsync;
11355 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011356};
11357
11358struct intel_display_error_state *
11359intel_display_capture_error_state(struct drm_device *dev)
11360{
Akshay Joshi0206e352011-08-16 15:34:10 -040011361 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011362 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011363 int transcoders[] = {
11364 TRANSCODER_A,
11365 TRANSCODER_B,
11366 TRANSCODER_C,
11367 TRANSCODER_EDP,
11368 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011369 int i;
11370
Chris Wilson63b66e52013-08-08 15:12:06 +020011371 if (INTEL_INFO(dev)->num_pipes == 0)
11372 return NULL;
11373
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011374 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011375 if (error == NULL)
11376 return NULL;
11377
Imre Deak190be112013-11-25 17:15:31 +020011378 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011379 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11380
Damien Lespiau52331302012-08-15 19:23:25 +010011381 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020011382 error->pipe[i].power_domain_on =
11383 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11384 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011385 continue;
11386
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011387 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11388 error->cursor[i].control = I915_READ(CURCNTR(i));
11389 error->cursor[i].position = I915_READ(CURPOS(i));
11390 error->cursor[i].base = I915_READ(CURBASE(i));
11391 } else {
11392 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11393 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11394 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11395 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011396
11397 error->plane[i].control = I915_READ(DSPCNTR(i));
11398 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011399 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011400 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011401 error->plane[i].pos = I915_READ(DSPPOS(i));
11402 }
Paulo Zanonica291362013-03-06 20:03:14 -030011403 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11404 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011405 if (INTEL_INFO(dev)->gen >= 4) {
11406 error->plane[i].surface = I915_READ(DSPSURF(i));
11407 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11408 }
11409
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011410 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011411 }
11412
11413 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11414 if (HAS_DDI(dev_priv->dev))
11415 error->num_transcoders++; /* Account for eDP. */
11416
11417 for (i = 0; i < error->num_transcoders; i++) {
11418 enum transcoder cpu_transcoder = transcoders[i];
11419
Imre Deakddf9c532013-11-27 22:02:02 +020011420 error->transcoder[i].power_domain_on =
11421 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11422 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011423 continue;
11424
Chris Wilson63b66e52013-08-08 15:12:06 +020011425 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11426
11427 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11428 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11429 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11430 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11431 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11432 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11433 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011434 }
11435
11436 return error;
11437}
11438
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011439#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11440
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011441void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011442intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011443 struct drm_device *dev,
11444 struct intel_display_error_state *error)
11445{
11446 int i;
11447
Chris Wilson63b66e52013-08-08 15:12:06 +020011448 if (!error)
11449 return;
11450
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011451 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020011452 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011453 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011454 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011455 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011456 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020011457 err_printf(m, " Power: %s\n",
11458 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011459 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011460
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011461 err_printf(m, "Plane [%d]:\n", i);
11462 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11463 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011464 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011465 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11466 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011467 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011468 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011469 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011470 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011471 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11472 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011473 }
11474
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011475 err_printf(m, "Cursor [%d]:\n", i);
11476 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11477 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11478 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011479 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011480
11481 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011482 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011483 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011484 err_printf(m, " Power: %s\n",
11485 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020011486 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11487 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11488 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11489 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11490 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11491 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11492 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11493 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011494}