blob: 9405340369d176c49c4344f4f7a2ddce4c1c5c2d [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 },
Ville Syrjälä9c333712013-12-09 18:54:17 +020093 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +020094 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -040095 .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 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200106 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200107 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200108 .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 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200119 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200120 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 .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
Ville Syrjälä8212d562013-12-10 14:06:45 +02001370 /* Enable the CRI clock source so we can get at the display */
1371 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
1372 DPLL_INTEGRATED_CRI_CLK_VLV);
1373
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001374 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001375 /*
1376 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1377 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1378 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1379 * b. The other bits such as sfr settings / modesel may all be set
1380 * to 0.
1381 *
1382 * This should only be done on init and resume from S3 with both
1383 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1384 */
1385 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1386}
1387
Daniel Vetter426115c2013-07-11 22:13:42 +02001388static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001389{
Daniel Vetter426115c2013-07-11 22:13:42 +02001390 struct drm_device *dev = crtc->base.dev;
1391 struct drm_i915_private *dev_priv = dev->dev_private;
1392 int reg = DPLL(crtc->pipe);
1393 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001394
Daniel Vetter426115c2013-07-11 22:13:42 +02001395 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001396
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001397 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001398 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1399
1400 /* PLL is protected by panel, make sure we can write it */
1401 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001402 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001403
Daniel Vetter426115c2013-07-11 22:13:42 +02001404 I915_WRITE(reg, dpll);
1405 POSTING_READ(reg);
1406 udelay(150);
1407
1408 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1409 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1410
1411 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1412 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001413
1414 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001415 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001416 POSTING_READ(reg);
1417 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001418 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001419 POSTING_READ(reg);
1420 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001421 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001422 POSTING_READ(reg);
1423 udelay(150); /* wait for warmup */
1424}
1425
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001426static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001427{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001428 struct drm_device *dev = crtc->base.dev;
1429 struct drm_i915_private *dev_priv = dev->dev_private;
1430 int reg = DPLL(crtc->pipe);
1431 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001432
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001433 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001434
1435 /* No really, not for ILK+ */
1436 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001437
1438 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001439 if (IS_MOBILE(dev) && !IS_I830(dev))
1440 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001441
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001442 I915_WRITE(reg, dpll);
1443
1444 /* Wait for the clocks to stabilize. */
1445 POSTING_READ(reg);
1446 udelay(150);
1447
1448 if (INTEL_INFO(dev)->gen >= 4) {
1449 I915_WRITE(DPLL_MD(crtc->pipe),
1450 crtc->config.dpll_hw_state.dpll_md);
1451 } else {
1452 /* The pixel multiplier can only be updated once the
1453 * DPLL is enabled and the clocks are stable.
1454 *
1455 * So write it again.
1456 */
1457 I915_WRITE(reg, dpll);
1458 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001459
1460 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001461 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001462 POSTING_READ(reg);
1463 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001464 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001465 POSTING_READ(reg);
1466 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001467 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001468 POSTING_READ(reg);
1469 udelay(150); /* wait for warmup */
1470}
1471
1472/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001473 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001474 * @dev_priv: i915 private structure
1475 * @pipe: pipe PLL to disable
1476 *
1477 * Disable the PLL for @pipe, making sure the pipe is off first.
1478 *
1479 * Note! This is for pre-ILK only.
1480 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001481static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001482{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001483 /* Don't disable pipe A or pipe A PLLs if needed */
1484 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1485 return;
1486
1487 /* Make sure the pipe isn't still relying on us */
1488 assert_pipe_disabled(dev_priv, pipe);
1489
Daniel Vetter50b44a42013-06-05 13:34:33 +02001490 I915_WRITE(DPLL(pipe), 0);
1491 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001492}
1493
Jesse Barnesf6071162013-10-01 10:41:38 -07001494static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1495{
1496 u32 val = 0;
1497
1498 /* Make sure the pipe isn't still relying on us */
1499 assert_pipe_disabled(dev_priv, pipe);
1500
1501 /* Leave integrated clock source enabled */
1502 if (pipe == PIPE_B)
1503 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1504 I915_WRITE(DPLL(pipe), val);
1505 POSTING_READ(DPLL(pipe));
1506}
1507
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001508void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1509 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001510{
1511 u32 port_mask;
1512
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001513 switch (dport->port) {
1514 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001515 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001516 break;
1517 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001518 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001519 break;
1520 default:
1521 BUG();
1522 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001523
1524 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1525 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Ville Syrjäläbe46ffd2013-11-29 13:21:49 +02001526 port_name(dport->port), I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001527}
1528
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001529/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001530 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001531 * @dev_priv: i915 private structure
1532 * @pipe: pipe PLL to enable
1533 *
1534 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1535 * drives the transcoder clock.
1536 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001537static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001538{
Daniel Vettere2b78262013-06-07 23:10:03 +02001539 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1540 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001541
Chris Wilson48da64a2012-05-13 20:16:12 +01001542 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001543 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001544 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001545 return;
1546
1547 if (WARN_ON(pll->refcount == 0))
1548 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001549
Daniel Vetter46edb022013-06-05 13:34:12 +02001550 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1551 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001552 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001553
Daniel Vettercdbd2312013-06-05 13:34:03 +02001554 if (pll->active++) {
1555 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001556 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001557 return;
1558 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001559 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001560
Daniel Vetter46edb022013-06-05 13:34:12 +02001561 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001562 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001563 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001564}
1565
Daniel Vettere2b78262013-06-07 23:10:03 +02001566static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001567{
Daniel Vettere2b78262013-06-07 23:10:03 +02001568 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1569 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001570
Jesse Barnes92f25842011-01-04 15:09:34 -08001571 /* PCH only available on ILK+ */
1572 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001573 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001574 return;
1575
Chris Wilson48da64a2012-05-13 20:16:12 +01001576 if (WARN_ON(pll->refcount == 0))
1577 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001578
Daniel Vetter46edb022013-06-05 13:34:12 +02001579 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1580 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001581 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001582
Chris Wilson48da64a2012-05-13 20:16:12 +01001583 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001584 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001585 return;
1586 }
1587
Daniel Vettere9d69442013-06-05 13:34:15 +02001588 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001589 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001590 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001591 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001592
Daniel Vetter46edb022013-06-05 13:34:12 +02001593 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001594 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001595 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001596}
1597
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001598static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1599 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001600{
Daniel Vetter23670b322012-11-01 09:15:30 +01001601 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001602 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001604 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001605
1606 /* PCH only available on ILK+ */
1607 BUG_ON(dev_priv->info->gen < 5);
1608
1609 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001610 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001611 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001612
1613 /* FDI must be feeding us bits for PCH ports */
1614 assert_fdi_tx_enabled(dev_priv, pipe);
1615 assert_fdi_rx_enabled(dev_priv, pipe);
1616
Daniel Vetter23670b322012-11-01 09:15:30 +01001617 if (HAS_PCH_CPT(dev)) {
1618 /* Workaround: Set the timing override bit before enabling the
1619 * pch transcoder. */
1620 reg = TRANS_CHICKEN2(pipe);
1621 val = I915_READ(reg);
1622 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1623 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001624 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001625
Daniel Vetterab9412b2013-05-03 11:49:46 +02001626 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001627 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001628 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001629
1630 if (HAS_PCH_IBX(dev_priv->dev)) {
1631 /*
1632 * make the BPC in transcoder be consistent with
1633 * that in pipeconf reg.
1634 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001635 val &= ~PIPECONF_BPC_MASK;
1636 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001637 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001638
1639 val &= ~TRANS_INTERLACE_MASK;
1640 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001641 if (HAS_PCH_IBX(dev_priv->dev) &&
1642 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1643 val |= TRANS_LEGACY_INTERLACED_ILK;
1644 else
1645 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001646 else
1647 val |= TRANS_PROGRESSIVE;
1648
Jesse Barnes040484a2011-01-03 12:14:26 -08001649 I915_WRITE(reg, val | TRANS_ENABLE);
1650 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001651 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001652}
1653
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001654static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001655 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001656{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001657 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001658
1659 /* PCH only available on ILK+ */
1660 BUG_ON(dev_priv->info->gen < 5);
1661
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001662 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001663 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001664 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001665
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001666 /* Workaround: set timing override bit. */
1667 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001668 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001669 I915_WRITE(_TRANSA_CHICKEN2, val);
1670
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001671 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001672 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001673
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001674 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1675 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001676 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001677 else
1678 val |= TRANS_PROGRESSIVE;
1679
Daniel Vetterab9412b2013-05-03 11:49:46 +02001680 I915_WRITE(LPT_TRANSCONF, val);
1681 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001682 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001683}
1684
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001685static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1686 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001687{
Daniel Vetter23670b322012-11-01 09:15:30 +01001688 struct drm_device *dev = dev_priv->dev;
1689 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001690
1691 /* FDI relies on the transcoder */
1692 assert_fdi_tx_disabled(dev_priv, pipe);
1693 assert_fdi_rx_disabled(dev_priv, pipe);
1694
Jesse Barnes291906f2011-02-02 12:28:03 -08001695 /* Ports must be off as well */
1696 assert_pch_ports_disabled(dev_priv, pipe);
1697
Daniel Vetterab9412b2013-05-03 11:49:46 +02001698 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001699 val = I915_READ(reg);
1700 val &= ~TRANS_ENABLE;
1701 I915_WRITE(reg, val);
1702 /* wait for PCH transcoder off, transcoder state */
1703 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001704 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001705
1706 if (!HAS_PCH_IBX(dev)) {
1707 /* Workaround: Clear the timing override chicken bit again. */
1708 reg = TRANS_CHICKEN2(pipe);
1709 val = I915_READ(reg);
1710 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1711 I915_WRITE(reg, val);
1712 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001713}
1714
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001715static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001716{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001717 u32 val;
1718
Daniel Vetterab9412b2013-05-03 11:49:46 +02001719 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001720 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001721 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001722 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001723 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001724 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001725
1726 /* Workaround: clear timing override bit. */
1727 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001728 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001729 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001730}
1731
1732/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001733 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001734 * @dev_priv: i915 private structure
1735 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001736 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001737 *
1738 * Enable @pipe, making sure that various hardware specific requirements
1739 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1740 *
1741 * @pipe should be %PIPE_A or %PIPE_B.
1742 *
1743 * Will wait until the pipe is actually running (i.e. first vblank) before
1744 * returning.
1745 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001746static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001747 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001748{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001749 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1750 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001751 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001752 int reg;
1753 u32 val;
1754
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001755 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001756 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001757 assert_sprites_disabled(dev_priv, pipe);
1758
Paulo Zanoni681e5812012-12-06 11:12:38 -02001759 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001760 pch_transcoder = TRANSCODER_A;
1761 else
1762 pch_transcoder = pipe;
1763
Jesse Barnesb24e7172011-01-04 15:09:30 -08001764 /*
1765 * A pipe without a PLL won't actually be able to drive bits from
1766 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1767 * need the check.
1768 */
1769 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001770 if (dsi)
1771 assert_dsi_pll_enabled(dev_priv);
1772 else
1773 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001774 else {
1775 if (pch_port) {
1776 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001777 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001778 assert_fdi_tx_pll_enabled(dev_priv,
1779 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001780 }
1781 /* FIXME: assert CPU port conditions for SNB+ */
1782 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001783
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001784 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001786 if (val & PIPECONF_ENABLE)
1787 return;
1788
1789 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001790 intel_wait_for_vblank(dev_priv->dev, pipe);
1791}
1792
1793/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001794 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001795 * @dev_priv: i915 private structure
1796 * @pipe: pipe to disable
1797 *
1798 * Disable @pipe, making sure that various hardware specific requirements
1799 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1800 *
1801 * @pipe should be %PIPE_A or %PIPE_B.
1802 *
1803 * Will wait until the pipe has shut down before returning.
1804 */
1805static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1806 enum pipe pipe)
1807{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001808 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1809 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810 int reg;
1811 u32 val;
1812
1813 /*
1814 * Make sure planes won't keep trying to pump pixels to us,
1815 * or we might hang the display.
1816 */
1817 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001818 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001819 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001820
1821 /* Don't disable pipe A or pipe A PLLs if needed */
1822 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1823 return;
1824
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001825 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001826 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001827 if ((val & PIPECONF_ENABLE) == 0)
1828 return;
1829
1830 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001831 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1832}
1833
Keith Packardd74362c2011-07-28 14:47:14 -07001834/*
1835 * Plane regs are double buffered, going from enabled->disabled needs a
1836 * trigger in order to latch. The display address reg provides this.
1837 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001838void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1839 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001840{
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001841 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1842
1843 I915_WRITE(reg, I915_READ(reg));
1844 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001845}
1846
Jesse Barnesb24e7172011-01-04 15:09:30 -08001847/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001848 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001849 * @dev_priv: i915 private structure
1850 * @plane: plane to enable
1851 * @pipe: pipe being fed
1852 *
1853 * Enable @plane on @pipe, making sure that @pipe is running first.
1854 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001855static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1856 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001857{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001858 struct intel_crtc *intel_crtc =
1859 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001860 int reg;
1861 u32 val;
1862
1863 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1864 assert_pipe_enabled(dev_priv, pipe);
1865
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001866 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001867
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001868 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001869
Jesse Barnesb24e7172011-01-04 15:09:30 -08001870 reg = DSPCNTR(plane);
1871 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001872 if (val & DISPLAY_PLANE_ENABLE)
1873 return;
1874
1875 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001876 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001877 intel_wait_for_vblank(dev_priv->dev, pipe);
1878}
1879
Jesse Barnesb24e7172011-01-04 15:09:30 -08001880/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001881 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001882 * @dev_priv: i915 private structure
1883 * @plane: plane to disable
1884 * @pipe: pipe consuming the data
1885 *
1886 * Disable @plane; should be an independent operation.
1887 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001888static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1889 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001890{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001891 struct intel_crtc *intel_crtc =
1892 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001893 int reg;
1894 u32 val;
1895
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001896 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001897
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001898 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001899
Jesse Barnesb24e7172011-01-04 15:09:30 -08001900 reg = DSPCNTR(plane);
1901 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001902 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1903 return;
1904
1905 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001906 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001907 intel_wait_for_vblank(dev_priv->dev, pipe);
1908}
1909
Chris Wilson693db182013-03-05 14:52:39 +00001910static bool need_vtd_wa(struct drm_device *dev)
1911{
1912#ifdef CONFIG_INTEL_IOMMU
1913 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1914 return true;
1915#endif
1916 return false;
1917}
1918
Chris Wilson127bd2a2010-07-23 23:32:05 +01001919int
Chris Wilson48b956c2010-09-14 12:50:34 +01001920intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001921 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001922 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001923{
Chris Wilsonce453d82011-02-21 14:43:56 +00001924 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001925 u32 alignment;
1926 int ret;
1927
Chris Wilson05394f32010-11-08 19:18:58 +00001928 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001929 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001930 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1931 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001932 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001933 alignment = 4 * 1024;
1934 else
1935 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001936 break;
1937 case I915_TILING_X:
1938 /* pin() will align the object as required by fence */
1939 alignment = 0;
1940 break;
1941 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001942 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001943 return -EINVAL;
1944 default:
1945 BUG();
1946 }
1947
Chris Wilson693db182013-03-05 14:52:39 +00001948 /* Note that the w/a also requires 64 PTE of padding following the
1949 * bo. We currently fill all unused PTE with the shadow page and so
1950 * we should always have valid PTE following the scanout preventing
1951 * the VT-d warning.
1952 */
1953 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1954 alignment = 256 * 1024;
1955
Chris Wilsonce453d82011-02-21 14:43:56 +00001956 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001957 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001958 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001959 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001960
1961 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1962 * fence, whereas 965+ only requires a fence if using
1963 * framebuffer compression. For simplicity, we always install
1964 * a fence as the cost is not that onerous.
1965 */
Chris Wilson06d98132012-04-17 15:31:24 +01001966 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001967 if (ret)
1968 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001969
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001970 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001971
Chris Wilsonce453d82011-02-21 14:43:56 +00001972 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001973 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001974
1975err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001976 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001977err_interruptible:
1978 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001979 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001980}
1981
Chris Wilson1690e1e2011-12-14 13:57:08 +01001982void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1983{
1984 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001985 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001986}
1987
Daniel Vetterc2c75132012-07-05 12:17:30 +02001988/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1989 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001990unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1991 unsigned int tiling_mode,
1992 unsigned int cpp,
1993 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001994{
Chris Wilsonbc752862013-02-21 20:04:31 +00001995 if (tiling_mode != I915_TILING_NONE) {
1996 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001997
Chris Wilsonbc752862013-02-21 20:04:31 +00001998 tile_rows = *y / 8;
1999 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002000
Chris Wilsonbc752862013-02-21 20:04:31 +00002001 tiles = *x / (512/cpp);
2002 *x %= 512/cpp;
2003
2004 return tile_rows * pitch * 8 + tiles * 4096;
2005 } else {
2006 unsigned int offset;
2007
2008 offset = *y * pitch + *x * cpp;
2009 *y = 0;
2010 *x = (offset & 4095) / cpp;
2011 return offset & -4096;
2012 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002013}
2014
Jesse Barnes17638cd2011-06-24 12:19:23 -07002015static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2016 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002017{
2018 struct drm_device *dev = crtc->dev;
2019 struct drm_i915_private *dev_priv = dev->dev_private;
2020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2021 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002022 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002023 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002024 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002025 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002026 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002027
2028 switch (plane) {
2029 case 0:
2030 case 1:
2031 break;
2032 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002033 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002034 return -EINVAL;
2035 }
2036
2037 intel_fb = to_intel_framebuffer(fb);
2038 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002039
Chris Wilson5eddb702010-09-11 13:48:45 +01002040 reg = DSPCNTR(plane);
2041 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002042 /* Mask out pixel format bits in case we change it */
2043 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002044 switch (fb->pixel_format) {
2045 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002046 dspcntr |= DISPPLANE_8BPP;
2047 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002048 case DRM_FORMAT_XRGB1555:
2049 case DRM_FORMAT_ARGB1555:
2050 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002051 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002052 case DRM_FORMAT_RGB565:
2053 dspcntr |= DISPPLANE_BGRX565;
2054 break;
2055 case DRM_FORMAT_XRGB8888:
2056 case DRM_FORMAT_ARGB8888:
2057 dspcntr |= DISPPLANE_BGRX888;
2058 break;
2059 case DRM_FORMAT_XBGR8888:
2060 case DRM_FORMAT_ABGR8888:
2061 dspcntr |= DISPPLANE_RGBX888;
2062 break;
2063 case DRM_FORMAT_XRGB2101010:
2064 case DRM_FORMAT_ARGB2101010:
2065 dspcntr |= DISPPLANE_BGRX101010;
2066 break;
2067 case DRM_FORMAT_XBGR2101010:
2068 case DRM_FORMAT_ABGR2101010:
2069 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002070 break;
2071 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002072 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002073 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002074
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002075 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002076 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002077 dspcntr |= DISPPLANE_TILED;
2078 else
2079 dspcntr &= ~DISPPLANE_TILED;
2080 }
2081
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002082 if (IS_G4X(dev))
2083 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2084
Chris Wilson5eddb702010-09-11 13:48:45 +01002085 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002086
Daniel Vettere506a0c2012-07-05 12:17:29 +02002087 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002088
Daniel Vetterc2c75132012-07-05 12:17:30 +02002089 if (INTEL_INFO(dev)->gen >= 4) {
2090 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002091 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2092 fb->bits_per_pixel / 8,
2093 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002094 linear_offset -= intel_crtc->dspaddr_offset;
2095 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002096 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002097 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002098
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002099 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2100 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2101 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002102 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002103 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002104 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002105 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002106 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002107 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002108 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002109 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002110 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002111
Jesse Barnes17638cd2011-06-24 12:19:23 -07002112 return 0;
2113}
2114
2115static int ironlake_update_plane(struct drm_crtc *crtc,
2116 struct drm_framebuffer *fb, int x, int y)
2117{
2118 struct drm_device *dev = crtc->dev;
2119 struct drm_i915_private *dev_priv = dev->dev_private;
2120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2121 struct intel_framebuffer *intel_fb;
2122 struct drm_i915_gem_object *obj;
2123 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002124 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002125 u32 dspcntr;
2126 u32 reg;
2127
2128 switch (plane) {
2129 case 0:
2130 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002131 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002132 break;
2133 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002134 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002135 return -EINVAL;
2136 }
2137
2138 intel_fb = to_intel_framebuffer(fb);
2139 obj = intel_fb->obj;
2140
2141 reg = DSPCNTR(plane);
2142 dspcntr = I915_READ(reg);
2143 /* Mask out pixel format bits in case we change it */
2144 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002145 switch (fb->pixel_format) {
2146 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002147 dspcntr |= DISPPLANE_8BPP;
2148 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002149 case DRM_FORMAT_RGB565:
2150 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002151 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002152 case DRM_FORMAT_XRGB8888:
2153 case DRM_FORMAT_ARGB8888:
2154 dspcntr |= DISPPLANE_BGRX888;
2155 break;
2156 case DRM_FORMAT_XBGR8888:
2157 case DRM_FORMAT_ABGR8888:
2158 dspcntr |= DISPPLANE_RGBX888;
2159 break;
2160 case DRM_FORMAT_XRGB2101010:
2161 case DRM_FORMAT_ARGB2101010:
2162 dspcntr |= DISPPLANE_BGRX101010;
2163 break;
2164 case DRM_FORMAT_XBGR2101010:
2165 case DRM_FORMAT_ABGR2101010:
2166 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002167 break;
2168 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002169 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002170 }
2171
2172 if (obj->tiling_mode != I915_TILING_NONE)
2173 dspcntr |= DISPPLANE_TILED;
2174 else
2175 dspcntr &= ~DISPPLANE_TILED;
2176
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002177 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002178 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2179 else
2180 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002181
2182 I915_WRITE(reg, dspcntr);
2183
Daniel Vettere506a0c2012-07-05 12:17:29 +02002184 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002185 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002186 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2187 fb->bits_per_pixel / 8,
2188 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002189 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002190
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002191 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2192 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2193 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002194 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002195 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002196 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002197 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002198 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2199 } else {
2200 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2201 I915_WRITE(DSPLINOFF(plane), linear_offset);
2202 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002203 POSTING_READ(reg);
2204
2205 return 0;
2206}
2207
2208/* Assume fb object is pinned & idle & fenced and just update base pointers */
2209static int
2210intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2211 int x, int y, enum mode_set_atomic state)
2212{
2213 struct drm_device *dev = crtc->dev;
2214 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002215
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002216 if (dev_priv->display.disable_fbc)
2217 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002218 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002219
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002220 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002221}
2222
Ville Syrjälä96a02912013-02-18 19:08:49 +02002223void intel_display_handle_reset(struct drm_device *dev)
2224{
2225 struct drm_i915_private *dev_priv = dev->dev_private;
2226 struct drm_crtc *crtc;
2227
2228 /*
2229 * Flips in the rings have been nuked by the reset,
2230 * so complete all pending flips so that user space
2231 * will get its events and not get stuck.
2232 *
2233 * Also update the base address of all primary
2234 * planes to the the last fb to make sure we're
2235 * showing the correct fb after a reset.
2236 *
2237 * Need to make two loops over the crtcs so that we
2238 * don't try to grab a crtc mutex before the
2239 * pending_flip_queue really got woken up.
2240 */
2241
2242 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2244 enum plane plane = intel_crtc->plane;
2245
2246 intel_prepare_page_flip(dev, plane);
2247 intel_finish_page_flip_plane(dev, plane);
2248 }
2249
2250 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2252
2253 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002254 /*
2255 * FIXME: Once we have proper support for primary planes (and
2256 * disabling them without disabling the entire crtc) allow again
2257 * a NULL crtc->fb.
2258 */
2259 if (intel_crtc->active && crtc->fb)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002260 dev_priv->display.update_plane(crtc, crtc->fb,
2261 crtc->x, crtc->y);
2262 mutex_unlock(&crtc->mutex);
2263 }
2264}
2265
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002266static int
Chris Wilson14667a42012-04-03 17:58:35 +01002267intel_finish_fb(struct drm_framebuffer *old_fb)
2268{
2269 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2270 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2271 bool was_interruptible = dev_priv->mm.interruptible;
2272 int ret;
2273
Chris Wilson14667a42012-04-03 17:58:35 +01002274 /* Big Hammer, we also need to ensure that any pending
2275 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2276 * current scanout is retired before unpinning the old
2277 * framebuffer.
2278 *
2279 * This should only fail upon a hung GPU, in which case we
2280 * can safely continue.
2281 */
2282 dev_priv->mm.interruptible = false;
2283 ret = i915_gem_object_finish_gpu(obj);
2284 dev_priv->mm.interruptible = was_interruptible;
2285
2286 return ret;
2287}
2288
Ville Syrjälä198598d2012-10-31 17:50:24 +02002289static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2290{
2291 struct drm_device *dev = crtc->dev;
2292 struct drm_i915_master_private *master_priv;
2293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2294
2295 if (!dev->primary->master)
2296 return;
2297
2298 master_priv = dev->primary->master->driver_priv;
2299 if (!master_priv->sarea_priv)
2300 return;
2301
2302 switch (intel_crtc->pipe) {
2303 case 0:
2304 master_priv->sarea_priv->pipeA_x = x;
2305 master_priv->sarea_priv->pipeA_y = y;
2306 break;
2307 case 1:
2308 master_priv->sarea_priv->pipeB_x = x;
2309 master_priv->sarea_priv->pipeB_y = y;
2310 break;
2311 default:
2312 break;
2313 }
2314}
2315
Chris Wilson14667a42012-04-03 17:58:35 +01002316static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002317intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002318 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002319{
2320 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002321 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002323 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002324 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002325
2326 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002327 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002328 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002329 return 0;
2330 }
2331
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002332 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002333 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2334 plane_name(intel_crtc->plane),
2335 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002336 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002337 }
2338
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002339 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002340 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002341 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002342 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002343 if (ret != 0) {
2344 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002345 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002346 return ret;
2347 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002348
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002349 /*
2350 * Update pipe size and adjust fitter if needed: the reason for this is
2351 * that in compute_mode_changes we check the native mode (not the pfit
2352 * mode) to see if we can flip rather than do a full mode set. In the
2353 * fastboot case, we'll flip, but if we don't update the pipesrc and
2354 * pfit state, we'll end up with a big fb scanned out into the wrong
2355 * sized surface.
2356 *
2357 * To fix this properly, we need to hoist the checks up into
2358 * compute_mode_changes (or above), check the actual pfit state and
2359 * whether the platform allows pfit disable with pipe active, and only
2360 * then update the pipesrc and pfit state, even on the flip path.
2361 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002362 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002363 const struct drm_display_mode *adjusted_mode =
2364 &intel_crtc->config.adjusted_mode;
2365
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002366 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002367 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2368 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002369 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002370 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2371 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2372 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2373 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2374 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2375 }
2376 }
2377
Daniel Vetter94352cf2012-07-05 22:51:56 +02002378 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002379 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002380 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002381 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002382 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002383 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002384 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002385
Daniel Vetter94352cf2012-07-05 22:51:56 +02002386 old_fb = crtc->fb;
2387 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002388 crtc->x = x;
2389 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002390
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002391 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002392 if (intel_crtc->active && old_fb != fb)
2393 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002394 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002395 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002396
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002397 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002398 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002399 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002400
Ville Syrjälä198598d2012-10-31 17:50:24 +02002401 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002402
2403 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002404}
2405
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002406static void intel_fdi_normal_train(struct drm_crtc *crtc)
2407{
2408 struct drm_device *dev = crtc->dev;
2409 struct drm_i915_private *dev_priv = dev->dev_private;
2410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2411 int pipe = intel_crtc->pipe;
2412 u32 reg, temp;
2413
2414 /* enable normal train */
2415 reg = FDI_TX_CTL(pipe);
2416 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002417 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002418 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2419 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002420 } else {
2421 temp &= ~FDI_LINK_TRAIN_NONE;
2422 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002423 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002424 I915_WRITE(reg, temp);
2425
2426 reg = FDI_RX_CTL(pipe);
2427 temp = I915_READ(reg);
2428 if (HAS_PCH_CPT(dev)) {
2429 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2430 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2431 } else {
2432 temp &= ~FDI_LINK_TRAIN_NONE;
2433 temp |= FDI_LINK_TRAIN_NONE;
2434 }
2435 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2436
2437 /* wait one idle pattern time */
2438 POSTING_READ(reg);
2439 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002440
2441 /* IVB wants error correction enabled */
2442 if (IS_IVYBRIDGE(dev))
2443 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2444 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002445}
2446
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002447static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002448{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002449 return crtc->base.enabled && crtc->active &&
2450 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002451}
2452
Daniel Vetter01a415f2012-10-27 15:58:40 +02002453static void ivb_modeset_global_resources(struct drm_device *dev)
2454{
2455 struct drm_i915_private *dev_priv = dev->dev_private;
2456 struct intel_crtc *pipe_B_crtc =
2457 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2458 struct intel_crtc *pipe_C_crtc =
2459 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2460 uint32_t temp;
2461
Daniel Vetter1e833f42013-02-19 22:31:57 +01002462 /*
2463 * When everything is off disable fdi C so that we could enable fdi B
2464 * with all lanes. Note that we don't care about enabled pipes without
2465 * an enabled pch encoder.
2466 */
2467 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2468 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002469 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2470 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2471
2472 temp = I915_READ(SOUTH_CHICKEN1);
2473 temp &= ~FDI_BC_BIFURCATION_SELECT;
2474 DRM_DEBUG_KMS("disabling fdi C rx\n");
2475 I915_WRITE(SOUTH_CHICKEN1, temp);
2476 }
2477}
2478
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002479/* The FDI link training functions for ILK/Ibexpeak. */
2480static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2481{
2482 struct drm_device *dev = crtc->dev;
2483 struct drm_i915_private *dev_priv = dev->dev_private;
2484 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2485 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002486 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002489 /* FDI needs bits from pipe & plane first */
2490 assert_pipe_enabled(dev_priv, pipe);
2491 assert_plane_enabled(dev_priv, plane);
2492
Adam Jacksone1a44742010-06-25 15:32:14 -04002493 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2494 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002495 reg = FDI_RX_IMR(pipe);
2496 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002497 temp &= ~FDI_RX_SYMBOL_LOCK;
2498 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002499 I915_WRITE(reg, temp);
2500 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002501 udelay(150);
2502
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002503 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002504 reg = FDI_TX_CTL(pipe);
2505 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002506 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2507 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002508 temp &= ~FDI_LINK_TRAIN_NONE;
2509 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002511
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 reg = FDI_RX_CTL(pipe);
2513 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002514 temp &= ~FDI_LINK_TRAIN_NONE;
2515 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002516 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2517
2518 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519 udelay(150);
2520
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002521 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002522 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2523 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2524 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002525
Chris Wilson5eddb702010-09-11 13:48:45 +01002526 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002527 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002528 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002529 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2530
2531 if ((temp & FDI_RX_BIT_LOCK)) {
2532 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002533 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002534 break;
2535 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002537 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539
2540 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = FDI_TX_CTL(pipe);
2542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002543 temp &= ~FDI_LINK_TRAIN_NONE;
2544 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002546
Chris Wilson5eddb702010-09-11 13:48:45 +01002547 reg = FDI_RX_CTL(pipe);
2548 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002549 temp &= ~FDI_LINK_TRAIN_NONE;
2550 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002551 I915_WRITE(reg, temp);
2552
2553 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002554 udelay(150);
2555
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002557 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002558 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2560
2561 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002562 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002563 DRM_DEBUG_KMS("FDI train 2 done.\n");
2564 break;
2565 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002566 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002567 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002568 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569
2570 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002571
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002572}
2573
Akshay Joshi0206e352011-08-16 15:34:10 -04002574static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002575 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2576 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2577 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2578 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2579};
2580
2581/* The FDI link training functions for SNB/Cougarpoint. */
2582static void gen6_fdi_link_train(struct drm_crtc *crtc)
2583{
2584 struct drm_device *dev = crtc->dev;
2585 struct drm_i915_private *dev_priv = dev->dev_private;
2586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2587 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002588 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002589
Adam Jacksone1a44742010-06-25 15:32:14 -04002590 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2591 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 reg = FDI_RX_IMR(pipe);
2593 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002594 temp &= ~FDI_RX_SYMBOL_LOCK;
2595 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 I915_WRITE(reg, temp);
2597
2598 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002599 udelay(150);
2600
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002601 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002602 reg = FDI_TX_CTL(pipe);
2603 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002604 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2605 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002606 temp &= ~FDI_LINK_TRAIN_NONE;
2607 temp |= FDI_LINK_TRAIN_PATTERN_1;
2608 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2609 /* SNB-B */
2610 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002611 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002612
Daniel Vetterd74cf322012-10-26 10:58:13 +02002613 I915_WRITE(FDI_RX_MISC(pipe),
2614 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2615
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 reg = FDI_RX_CTL(pipe);
2617 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002618 if (HAS_PCH_CPT(dev)) {
2619 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2620 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2621 } else {
2622 temp &= ~FDI_LINK_TRAIN_NONE;
2623 temp |= FDI_LINK_TRAIN_PATTERN_1;
2624 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002625 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2626
2627 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002628 udelay(150);
2629
Akshay Joshi0206e352011-08-16 15:34:10 -04002630 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002631 reg = FDI_TX_CTL(pipe);
2632 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002633 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2634 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002635 I915_WRITE(reg, temp);
2636
2637 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002638 udelay(500);
2639
Sean Paulfa37d392012-03-02 12:53:39 -05002640 for (retry = 0; retry < 5; retry++) {
2641 reg = FDI_RX_IIR(pipe);
2642 temp = I915_READ(reg);
2643 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2644 if (temp & FDI_RX_BIT_LOCK) {
2645 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2646 DRM_DEBUG_KMS("FDI train 1 done.\n");
2647 break;
2648 }
2649 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002650 }
Sean Paulfa37d392012-03-02 12:53:39 -05002651 if (retry < 5)
2652 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002653 }
2654 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002655 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002656
2657 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002658 reg = FDI_TX_CTL(pipe);
2659 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002660 temp &= ~FDI_LINK_TRAIN_NONE;
2661 temp |= FDI_LINK_TRAIN_PATTERN_2;
2662 if (IS_GEN6(dev)) {
2663 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2664 /* SNB-B */
2665 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2666 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002667 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002668
Chris Wilson5eddb702010-09-11 13:48:45 +01002669 reg = FDI_RX_CTL(pipe);
2670 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002671 if (HAS_PCH_CPT(dev)) {
2672 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2673 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2674 } else {
2675 temp &= ~FDI_LINK_TRAIN_NONE;
2676 temp |= FDI_LINK_TRAIN_PATTERN_2;
2677 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002678 I915_WRITE(reg, temp);
2679
2680 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002681 udelay(150);
2682
Akshay Joshi0206e352011-08-16 15:34:10 -04002683 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002684 reg = FDI_TX_CTL(pipe);
2685 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002686 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2687 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002688 I915_WRITE(reg, temp);
2689
2690 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002691 udelay(500);
2692
Sean Paulfa37d392012-03-02 12:53:39 -05002693 for (retry = 0; retry < 5; retry++) {
2694 reg = FDI_RX_IIR(pipe);
2695 temp = I915_READ(reg);
2696 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2697 if (temp & FDI_RX_SYMBOL_LOCK) {
2698 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2699 DRM_DEBUG_KMS("FDI train 2 done.\n");
2700 break;
2701 }
2702 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002703 }
Sean Paulfa37d392012-03-02 12:53:39 -05002704 if (retry < 5)
2705 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002706 }
2707 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002708 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002709
2710 DRM_DEBUG_KMS("FDI train done.\n");
2711}
2712
Jesse Barnes357555c2011-04-28 15:09:55 -07002713/* Manual link training for Ivy Bridge A0 parts */
2714static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2715{
2716 struct drm_device *dev = crtc->dev;
2717 struct drm_i915_private *dev_priv = dev->dev_private;
2718 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2719 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002720 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002721
2722 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2723 for train result */
2724 reg = FDI_RX_IMR(pipe);
2725 temp = I915_READ(reg);
2726 temp &= ~FDI_RX_SYMBOL_LOCK;
2727 temp &= ~FDI_RX_BIT_LOCK;
2728 I915_WRITE(reg, temp);
2729
2730 POSTING_READ(reg);
2731 udelay(150);
2732
Daniel Vetter01a415f2012-10-27 15:58:40 +02002733 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2734 I915_READ(FDI_RX_IIR(pipe)));
2735
Jesse Barnes139ccd32013-08-19 11:04:55 -07002736 /* Try each vswing and preemphasis setting twice before moving on */
2737 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2738 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002739 reg = FDI_TX_CTL(pipe);
2740 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002741 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2742 temp &= ~FDI_TX_ENABLE;
2743 I915_WRITE(reg, temp);
2744
2745 reg = FDI_RX_CTL(pipe);
2746 temp = I915_READ(reg);
2747 temp &= ~FDI_LINK_TRAIN_AUTO;
2748 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2749 temp &= ~FDI_RX_ENABLE;
2750 I915_WRITE(reg, temp);
2751
2752 /* enable CPU FDI TX and PCH FDI RX */
2753 reg = FDI_TX_CTL(pipe);
2754 temp = I915_READ(reg);
2755 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2756 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2757 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002758 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002759 temp |= snb_b_fdi_train_param[j/2];
2760 temp |= FDI_COMPOSITE_SYNC;
2761 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2762
2763 I915_WRITE(FDI_RX_MISC(pipe),
2764 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2765
2766 reg = FDI_RX_CTL(pipe);
2767 temp = I915_READ(reg);
2768 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2769 temp |= FDI_COMPOSITE_SYNC;
2770 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2771
2772 POSTING_READ(reg);
2773 udelay(1); /* should be 0.5us */
2774
2775 for (i = 0; i < 4; i++) {
2776 reg = FDI_RX_IIR(pipe);
2777 temp = I915_READ(reg);
2778 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2779
2780 if (temp & FDI_RX_BIT_LOCK ||
2781 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2782 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2783 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2784 i);
2785 break;
2786 }
2787 udelay(1); /* should be 0.5us */
2788 }
2789 if (i == 4) {
2790 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2791 continue;
2792 }
2793
2794 /* Train 2 */
2795 reg = FDI_TX_CTL(pipe);
2796 temp = I915_READ(reg);
2797 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2798 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2799 I915_WRITE(reg, temp);
2800
2801 reg = FDI_RX_CTL(pipe);
2802 temp = I915_READ(reg);
2803 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2804 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002805 I915_WRITE(reg, temp);
2806
2807 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002808 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002809
Jesse Barnes139ccd32013-08-19 11:04:55 -07002810 for (i = 0; i < 4; i++) {
2811 reg = FDI_RX_IIR(pipe);
2812 temp = I915_READ(reg);
2813 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002814
Jesse Barnes139ccd32013-08-19 11:04:55 -07002815 if (temp & FDI_RX_SYMBOL_LOCK ||
2816 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2817 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2818 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2819 i);
2820 goto train_done;
2821 }
2822 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002823 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002824 if (i == 4)
2825 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002826 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002827
Jesse Barnes139ccd32013-08-19 11:04:55 -07002828train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002829 DRM_DEBUG_KMS("FDI train done.\n");
2830}
2831
Daniel Vetter88cefb62012-08-12 19:27:14 +02002832static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002833{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002834 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002835 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002836 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002837 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002838
Jesse Barnesc64e3112010-09-10 11:27:03 -07002839
Jesse Barnes0e23b992010-09-10 11:10:00 -07002840 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002841 reg = FDI_RX_CTL(pipe);
2842 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002843 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2844 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002845 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002846 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2847
2848 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002849 udelay(200);
2850
2851 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002852 temp = I915_READ(reg);
2853 I915_WRITE(reg, temp | FDI_PCDCLK);
2854
2855 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002856 udelay(200);
2857
Paulo Zanoni20749732012-11-23 15:30:38 -02002858 /* Enable CPU FDI TX PLL, always on for Ironlake */
2859 reg = FDI_TX_CTL(pipe);
2860 temp = I915_READ(reg);
2861 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2862 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002863
Paulo Zanoni20749732012-11-23 15:30:38 -02002864 POSTING_READ(reg);
2865 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002866 }
2867}
2868
Daniel Vetter88cefb62012-08-12 19:27:14 +02002869static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2870{
2871 struct drm_device *dev = intel_crtc->base.dev;
2872 struct drm_i915_private *dev_priv = dev->dev_private;
2873 int pipe = intel_crtc->pipe;
2874 u32 reg, temp;
2875
2876 /* Switch from PCDclk to Rawclk */
2877 reg = FDI_RX_CTL(pipe);
2878 temp = I915_READ(reg);
2879 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2880
2881 /* Disable CPU FDI TX PLL */
2882 reg = FDI_TX_CTL(pipe);
2883 temp = I915_READ(reg);
2884 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2885
2886 POSTING_READ(reg);
2887 udelay(100);
2888
2889 reg = FDI_RX_CTL(pipe);
2890 temp = I915_READ(reg);
2891 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2892
2893 /* Wait for the clocks to turn off. */
2894 POSTING_READ(reg);
2895 udelay(100);
2896}
2897
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002898static void ironlake_fdi_disable(struct drm_crtc *crtc)
2899{
2900 struct drm_device *dev = crtc->dev;
2901 struct drm_i915_private *dev_priv = dev->dev_private;
2902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2903 int pipe = intel_crtc->pipe;
2904 u32 reg, temp;
2905
2906 /* disable CPU FDI tx and PCH FDI rx */
2907 reg = FDI_TX_CTL(pipe);
2908 temp = I915_READ(reg);
2909 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2910 POSTING_READ(reg);
2911
2912 reg = FDI_RX_CTL(pipe);
2913 temp = I915_READ(reg);
2914 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002915 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002916 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2917
2918 POSTING_READ(reg);
2919 udelay(100);
2920
2921 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002922 if (HAS_PCH_IBX(dev)) {
2923 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002924 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002925
2926 /* still set train pattern 1 */
2927 reg = FDI_TX_CTL(pipe);
2928 temp = I915_READ(reg);
2929 temp &= ~FDI_LINK_TRAIN_NONE;
2930 temp |= FDI_LINK_TRAIN_PATTERN_1;
2931 I915_WRITE(reg, temp);
2932
2933 reg = FDI_RX_CTL(pipe);
2934 temp = I915_READ(reg);
2935 if (HAS_PCH_CPT(dev)) {
2936 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2937 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2938 } else {
2939 temp &= ~FDI_LINK_TRAIN_NONE;
2940 temp |= FDI_LINK_TRAIN_PATTERN_1;
2941 }
2942 /* BPC in FDI rx is consistent with that in PIPECONF */
2943 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002944 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002945 I915_WRITE(reg, temp);
2946
2947 POSTING_READ(reg);
2948 udelay(100);
2949}
2950
Chris Wilson5bb61642012-09-27 21:25:58 +01002951static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2952{
2953 struct drm_device *dev = crtc->dev;
2954 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002956 unsigned long flags;
2957 bool pending;
2958
Ville Syrjälä10d83732013-01-29 18:13:34 +02002959 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2960 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002961 return false;
2962
2963 spin_lock_irqsave(&dev->event_lock, flags);
2964 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2965 spin_unlock_irqrestore(&dev->event_lock, flags);
2966
2967 return pending;
2968}
2969
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002970static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2971{
Chris Wilson0f911282012-04-17 10:05:38 +01002972 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002973 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002974
2975 if (crtc->fb == NULL)
2976 return;
2977
Daniel Vetter2c10d572012-12-20 21:24:07 +01002978 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2979
Chris Wilson5bb61642012-09-27 21:25:58 +01002980 wait_event(dev_priv->pending_flip_queue,
2981 !intel_crtc_has_pending_flip(crtc));
2982
Chris Wilson0f911282012-04-17 10:05:38 +01002983 mutex_lock(&dev->struct_mutex);
2984 intel_finish_fb(crtc->fb);
2985 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002986}
2987
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002988/* Program iCLKIP clock to the desired frequency */
2989static void lpt_program_iclkip(struct drm_crtc *crtc)
2990{
2991 struct drm_device *dev = crtc->dev;
2992 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002993 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002994 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2995 u32 temp;
2996
Daniel Vetter09153002012-12-12 14:06:44 +01002997 mutex_lock(&dev_priv->dpio_lock);
2998
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002999 /* It is necessary to ungate the pixclk gate prior to programming
3000 * the divisors, and gate it back when it is done.
3001 */
3002 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3003
3004 /* Disable SSCCTL */
3005 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003006 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3007 SBI_SSCCTL_DISABLE,
3008 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003009
3010 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003011 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003012 auxdiv = 1;
3013 divsel = 0x41;
3014 phaseinc = 0x20;
3015 } else {
3016 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003017 * but the adjusted_mode->crtc_clock in in KHz. To get the
3018 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003019 * convert the virtual clock precision to KHz here for higher
3020 * precision.
3021 */
3022 u32 iclk_virtual_root_freq = 172800 * 1000;
3023 u32 iclk_pi_range = 64;
3024 u32 desired_divisor, msb_divisor_value, pi_value;
3025
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003026 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003027 msb_divisor_value = desired_divisor / iclk_pi_range;
3028 pi_value = desired_divisor % iclk_pi_range;
3029
3030 auxdiv = 0;
3031 divsel = msb_divisor_value - 2;
3032 phaseinc = pi_value;
3033 }
3034
3035 /* This should not happen with any sane values */
3036 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3037 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3038 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3039 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3040
3041 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 +03003042 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003043 auxdiv,
3044 divsel,
3045 phasedir,
3046 phaseinc);
3047
3048 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003049 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003050 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3051 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3052 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3053 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3054 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3055 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003056 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003057
3058 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003059 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003060 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3061 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003062 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003063
3064 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003065 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003066 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003067 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003068
3069 /* Wait for initialization time */
3070 udelay(24);
3071
3072 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003073
3074 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003075}
3076
Daniel Vetter275f01b22013-05-03 11:49:47 +02003077static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3078 enum pipe pch_transcoder)
3079{
3080 struct drm_device *dev = crtc->base.dev;
3081 struct drm_i915_private *dev_priv = dev->dev_private;
3082 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3083
3084 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3085 I915_READ(HTOTAL(cpu_transcoder)));
3086 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3087 I915_READ(HBLANK(cpu_transcoder)));
3088 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3089 I915_READ(HSYNC(cpu_transcoder)));
3090
3091 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3092 I915_READ(VTOTAL(cpu_transcoder)));
3093 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3094 I915_READ(VBLANK(cpu_transcoder)));
3095 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3096 I915_READ(VSYNC(cpu_transcoder)));
3097 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3098 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3099}
3100
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003101static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3102{
3103 struct drm_i915_private *dev_priv = dev->dev_private;
3104 uint32_t temp;
3105
3106 temp = I915_READ(SOUTH_CHICKEN1);
3107 if (temp & FDI_BC_BIFURCATION_SELECT)
3108 return;
3109
3110 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3111 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3112
3113 temp |= FDI_BC_BIFURCATION_SELECT;
3114 DRM_DEBUG_KMS("enabling fdi C rx\n");
3115 I915_WRITE(SOUTH_CHICKEN1, temp);
3116 POSTING_READ(SOUTH_CHICKEN1);
3117}
3118
3119static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3120{
3121 struct drm_device *dev = intel_crtc->base.dev;
3122 struct drm_i915_private *dev_priv = dev->dev_private;
3123
3124 switch (intel_crtc->pipe) {
3125 case PIPE_A:
3126 break;
3127 case PIPE_B:
3128 if (intel_crtc->config.fdi_lanes > 2)
3129 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3130 else
3131 cpt_enable_fdi_bc_bifurcation(dev);
3132
3133 break;
3134 case PIPE_C:
3135 cpt_enable_fdi_bc_bifurcation(dev);
3136
3137 break;
3138 default:
3139 BUG();
3140 }
3141}
3142
Jesse Barnesf67a5592011-01-05 10:31:48 -08003143/*
3144 * Enable PCH resources required for PCH ports:
3145 * - PCH PLLs
3146 * - FDI training & RX/TX
3147 * - update transcoder timings
3148 * - DP transcoding bits
3149 * - transcoder
3150 */
3151static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003152{
3153 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003154 struct drm_i915_private *dev_priv = dev->dev_private;
3155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3156 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003157 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003158
Daniel Vetterab9412b2013-05-03 11:49:46 +02003159 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003160
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003161 if (IS_IVYBRIDGE(dev))
3162 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3163
Daniel Vettercd986ab2012-10-26 10:58:12 +02003164 /* Write the TU size bits before fdi link training, so that error
3165 * detection works. */
3166 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3167 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3168
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003169 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003170 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003171
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003172 /* We need to program the right clock selection before writing the pixel
3173 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003174 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003175 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003176
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003177 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003178 temp |= TRANS_DPLL_ENABLE(pipe);
3179 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003180 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003181 temp |= sel;
3182 else
3183 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003184 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003185 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003186
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003187 /* XXX: pch pll's can be enabled any time before we enable the PCH
3188 * transcoder, and we actually should do this to not upset any PCH
3189 * transcoder that already use the clock when we share it.
3190 *
3191 * Note that enable_shared_dpll tries to do the right thing, but
3192 * get_shared_dpll unconditionally resets the pll - we need that to have
3193 * the right LVDS enable sequence. */
3194 ironlake_enable_shared_dpll(intel_crtc);
3195
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003196 /* set transcoder timing, panel must allow it */
3197 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003198 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003199
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003200 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003201
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003202 /* For PCH DP, enable TRANS_DP_CTL */
3203 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003204 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3205 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003206 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003207 reg = TRANS_DP_CTL(pipe);
3208 temp = I915_READ(reg);
3209 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003210 TRANS_DP_SYNC_MASK |
3211 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003212 temp |= (TRANS_DP_OUTPUT_ENABLE |
3213 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003214 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003215
3216 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003217 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003218 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003219 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003220
3221 switch (intel_trans_dp_port_sel(crtc)) {
3222 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003223 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003224 break;
3225 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003226 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003227 break;
3228 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003229 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003230 break;
3231 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003232 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003233 }
3234
Chris Wilson5eddb702010-09-11 13:48:45 +01003235 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003236 }
3237
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003238 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003239}
3240
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003241static void lpt_pch_enable(struct drm_crtc *crtc)
3242{
3243 struct drm_device *dev = crtc->dev;
3244 struct drm_i915_private *dev_priv = dev->dev_private;
3245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003246 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003247
Daniel Vetterab9412b2013-05-03 11:49:46 +02003248 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003249
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003250 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003251
Paulo Zanoni0540e482012-10-31 18:12:40 -02003252 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003253 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003254
Paulo Zanoni937bb612012-10-31 18:12:47 -02003255 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003256}
3257
Daniel Vettere2b78262013-06-07 23:10:03 +02003258static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003259{
Daniel Vettere2b78262013-06-07 23:10:03 +02003260 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003261
3262 if (pll == NULL)
3263 return;
3264
3265 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003266 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003267 return;
3268 }
3269
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003270 if (--pll->refcount == 0) {
3271 WARN_ON(pll->on);
3272 WARN_ON(pll->active);
3273 }
3274
Daniel Vettera43f6e02013-06-07 23:10:32 +02003275 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003276}
3277
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003278static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003279{
Daniel Vettere2b78262013-06-07 23:10:03 +02003280 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3281 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3282 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003283
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003284 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003285 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3286 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003287 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003288 }
3289
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003290 if (HAS_PCH_IBX(dev_priv->dev)) {
3291 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003292 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003293 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003294
Daniel Vetter46edb022013-06-05 13:34:12 +02003295 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3296 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003297
3298 goto found;
3299 }
3300
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003301 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3302 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003303
3304 /* Only want to check enabled timings first */
3305 if (pll->refcount == 0)
3306 continue;
3307
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003308 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3309 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003310 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003311 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003312 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003313
3314 goto found;
3315 }
3316 }
3317
3318 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003319 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3320 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003321 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003322 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3323 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003324 goto found;
3325 }
3326 }
3327
3328 return NULL;
3329
3330found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003331 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003332 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3333 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003334
Daniel Vettercdbd2312013-06-05 13:34:03 +02003335 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003336 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3337 sizeof(pll->hw_state));
3338
Daniel Vetter46edb022013-06-05 13:34:12 +02003339 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003340 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003341 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003342
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003343 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003344 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003345 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003346
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003347 return pll;
3348}
3349
Daniel Vettera1520312013-05-03 11:49:50 +02003350static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003351{
3352 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003353 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003354 u32 temp;
3355
3356 temp = I915_READ(dslreg);
3357 udelay(500);
3358 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003359 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003360 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003361 }
3362}
3363
Jesse Barnesb074cec2013-04-25 12:55:02 -07003364static void ironlake_pfit_enable(struct intel_crtc *crtc)
3365{
3366 struct drm_device *dev = crtc->base.dev;
3367 struct drm_i915_private *dev_priv = dev->dev_private;
3368 int pipe = crtc->pipe;
3369
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003370 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003371 /* Force use of hard-coded filter coefficients
3372 * as some pre-programmed values are broken,
3373 * e.g. x201.
3374 */
3375 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3376 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3377 PF_PIPE_SEL_IVB(pipe));
3378 else
3379 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3380 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3381 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003382 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003383}
3384
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003385static void intel_enable_planes(struct drm_crtc *crtc)
3386{
3387 struct drm_device *dev = crtc->dev;
3388 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3389 struct intel_plane *intel_plane;
3390
3391 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3392 if (intel_plane->pipe == pipe)
3393 intel_plane_restore(&intel_plane->base);
3394}
3395
3396static void intel_disable_planes(struct drm_crtc *crtc)
3397{
3398 struct drm_device *dev = crtc->dev;
3399 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3400 struct intel_plane *intel_plane;
3401
3402 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3403 if (intel_plane->pipe == pipe)
3404 intel_plane_disable(&intel_plane->base);
3405}
3406
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003407void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003408{
3409 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3410
3411 if (!crtc->config.ips_enabled)
3412 return;
3413
3414 /* We can only enable IPS after we enable a plane and wait for a vblank.
3415 * We guarantee that the plane is enabled by calling intel_enable_ips
3416 * only after intel_enable_plane. And intel_enable_plane already waits
3417 * for a vblank, so all we need to do here is to enable the IPS bit. */
3418 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003419 if (IS_BROADWELL(crtc->base.dev)) {
3420 mutex_lock(&dev_priv->rps.hw_lock);
3421 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3422 mutex_unlock(&dev_priv->rps.hw_lock);
3423 /* Quoting Art Runyan: "its not safe to expect any particular
3424 * value in IPS_CTL bit 31 after enabling IPS through the
3425 * mailbox." Therefore we need to defer waiting on the state
3426 * change.
3427 * TODO: need to fix this for state checker
3428 */
3429 } else {
3430 I915_WRITE(IPS_CTL, IPS_ENABLE);
3431 /* The bit only becomes 1 in the next vblank, so this wait here
3432 * is essentially intel_wait_for_vblank. If we don't have this
3433 * and don't wait for vblanks until the end of crtc_enable, then
3434 * the HW state readout code will complain that the expected
3435 * IPS_CTL value is not the one we read. */
3436 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3437 DRM_ERROR("Timed out waiting for IPS enable\n");
3438 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003439}
3440
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003441void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003442{
3443 struct drm_device *dev = crtc->base.dev;
3444 struct drm_i915_private *dev_priv = dev->dev_private;
3445
3446 if (!crtc->config.ips_enabled)
3447 return;
3448
3449 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003450 if (IS_BROADWELL(crtc->base.dev)) {
3451 mutex_lock(&dev_priv->rps.hw_lock);
3452 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3453 mutex_unlock(&dev_priv->rps.hw_lock);
3454 } else
3455 I915_WRITE(IPS_CTL, 0);
Paulo Zanonid77e4532013-09-24 13:52:55 -03003456 POSTING_READ(IPS_CTL);
3457
3458 /* We need to wait for a vblank before we can disable the plane. */
3459 intel_wait_for_vblank(dev, crtc->pipe);
3460}
3461
3462/** Loads the palette/gamma unit for the CRTC with the prepared values */
3463static void intel_crtc_load_lut(struct drm_crtc *crtc)
3464{
3465 struct drm_device *dev = crtc->dev;
3466 struct drm_i915_private *dev_priv = dev->dev_private;
3467 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3468 enum pipe pipe = intel_crtc->pipe;
3469 int palreg = PALETTE(pipe);
3470 int i;
3471 bool reenable_ips = false;
3472
3473 /* The clocks have to be on to load the palette. */
3474 if (!crtc->enabled || !intel_crtc->active)
3475 return;
3476
3477 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3478 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3479 assert_dsi_pll_enabled(dev_priv);
3480 else
3481 assert_pll_enabled(dev_priv, pipe);
3482 }
3483
3484 /* use legacy palette for Ironlake */
3485 if (HAS_PCH_SPLIT(dev))
3486 palreg = LGC_PALETTE(pipe);
3487
3488 /* Workaround : Do not read or write the pipe palette/gamma data while
3489 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3490 */
3491 if (intel_crtc->config.ips_enabled &&
3492 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3493 GAMMA_MODE_MODE_SPLIT)) {
3494 hsw_disable_ips(intel_crtc);
3495 reenable_ips = true;
3496 }
3497
3498 for (i = 0; i < 256; i++) {
3499 I915_WRITE(palreg + 4 * i,
3500 (intel_crtc->lut_r[i] << 16) |
3501 (intel_crtc->lut_g[i] << 8) |
3502 intel_crtc->lut_b[i]);
3503 }
3504
3505 if (reenable_ips)
3506 hsw_enable_ips(intel_crtc);
3507}
3508
Jesse Barnesf67a5592011-01-05 10:31:48 -08003509static void ironlake_crtc_enable(struct drm_crtc *crtc)
3510{
3511 struct drm_device *dev = crtc->dev;
3512 struct drm_i915_private *dev_priv = dev->dev_private;
3513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003514 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003515 int pipe = intel_crtc->pipe;
3516 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003517
Daniel Vetter08a48462012-07-02 11:43:47 +02003518 WARN_ON(!crtc->enabled);
3519
Jesse Barnesf67a5592011-01-05 10:31:48 -08003520 if (intel_crtc->active)
3521 return;
3522
3523 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003524
3525 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3526 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3527
Daniel Vetterf6736a12013-06-05 13:34:30 +02003528 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003529 if (encoder->pre_enable)
3530 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003531
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003532 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003533 /* Note: FDI PLL enabling _must_ be done before we enable the
3534 * cpu pipes, hence this is separate from all the other fdi/pch
3535 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003536 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003537 } else {
3538 assert_fdi_tx_disabled(dev_priv, pipe);
3539 assert_fdi_rx_disabled(dev_priv, pipe);
3540 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003541
Jesse Barnesb074cec2013-04-25 12:55:02 -07003542 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003543
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003544 /*
3545 * On ILK+ LUT must be loaded before the pipe is running but with
3546 * clocks enabled
3547 */
3548 intel_crtc_load_lut(crtc);
3549
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003550 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003551 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003552 intel_crtc->config.has_pch_encoder, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003553 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003554 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003555 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003556
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003557 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003558 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003559
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003560 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003561 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003562 mutex_unlock(&dev->struct_mutex);
3563
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003564 for_each_encoder_on_crtc(dev, crtc, encoder)
3565 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003566
3567 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003568 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003569
3570 /*
3571 * There seems to be a race in PCH platform hw (at least on some
3572 * outputs) where an enabled pipe still completes any pageflip right
3573 * away (as if the pipe is off) instead of waiting for vblank. As soon
3574 * as the first vblank happend, everything works as expected. Hence just
3575 * wait for one vblank before returning to avoid strange things
3576 * happening.
3577 */
3578 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003579}
3580
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003581/* IPS only exists on ULT machines and is tied to pipe A. */
3582static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3583{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003584 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003585}
3586
Ville Syrjälädda9a662013-09-19 17:00:37 -03003587static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3588{
3589 struct drm_device *dev = crtc->dev;
3590 struct drm_i915_private *dev_priv = dev->dev_private;
3591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3592 int pipe = intel_crtc->pipe;
3593 int plane = intel_crtc->plane;
3594
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003595 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003596 intel_enable_planes(crtc);
3597 intel_crtc_update_cursor(crtc, true);
3598
3599 hsw_enable_ips(intel_crtc);
3600
3601 mutex_lock(&dev->struct_mutex);
3602 intel_update_fbc(dev);
3603 mutex_unlock(&dev->struct_mutex);
3604}
3605
3606static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3607{
3608 struct drm_device *dev = crtc->dev;
3609 struct drm_i915_private *dev_priv = dev->dev_private;
3610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3611 int pipe = intel_crtc->pipe;
3612 int plane = intel_crtc->plane;
3613
3614 intel_crtc_wait_for_pending_flips(crtc);
3615 drm_vblank_off(dev, pipe);
3616
3617 /* FBC must be disabled before disabling the plane on HSW. */
3618 if (dev_priv->fbc.plane == plane)
3619 intel_disable_fbc(dev);
3620
3621 hsw_disable_ips(intel_crtc);
3622
3623 intel_crtc_update_cursor(crtc, false);
3624 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003625 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003626}
3627
Paulo Zanonie4916942013-09-20 16:21:19 -03003628/*
3629 * This implements the workaround described in the "notes" section of the mode
3630 * set sequence documentation. When going from no pipes or single pipe to
3631 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3632 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3633 */
3634static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3635{
3636 struct drm_device *dev = crtc->base.dev;
3637 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3638
3639 /* We want to get the other_active_crtc only if there's only 1 other
3640 * active crtc. */
3641 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3642 if (!crtc_it->active || crtc_it == crtc)
3643 continue;
3644
3645 if (other_active_crtc)
3646 return;
3647
3648 other_active_crtc = crtc_it;
3649 }
3650 if (!other_active_crtc)
3651 return;
3652
3653 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3654 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3655}
3656
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003657static void haswell_crtc_enable(struct drm_crtc *crtc)
3658{
3659 struct drm_device *dev = crtc->dev;
3660 struct drm_i915_private *dev_priv = dev->dev_private;
3661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3662 struct intel_encoder *encoder;
3663 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003664
3665 WARN_ON(!crtc->enabled);
3666
3667 if (intel_crtc->active)
3668 return;
3669
3670 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003671
3672 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3673 if (intel_crtc->config.has_pch_encoder)
3674 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3675
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003676 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003677 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003678
3679 for_each_encoder_on_crtc(dev, crtc, encoder)
3680 if (encoder->pre_enable)
3681 encoder->pre_enable(encoder);
3682
Paulo Zanoni1f544382012-10-24 11:32:00 -02003683 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003684
Jesse Barnesb074cec2013-04-25 12:55:02 -07003685 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003686
3687 /*
3688 * On ILK+ LUT must be loaded before the pipe is running but with
3689 * clocks enabled
3690 */
3691 intel_crtc_load_lut(crtc);
3692
Paulo Zanoni1f544382012-10-24 11:32:00 -02003693 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003694 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003695
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003696 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003697 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003698 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003699
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003700 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003701 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003702
Jani Nikula8807e552013-08-30 19:40:32 +03003703 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003704 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003705 intel_opregion_notify_encoder(encoder, true);
3706 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003707
Paulo Zanonie4916942013-09-20 16:21:19 -03003708 /* If we change the relative order between pipe/planes enabling, we need
3709 * to change the workaround. */
3710 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003711 haswell_crtc_enable_planes(crtc);
3712
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003713 /*
3714 * There seems to be a race in PCH platform hw (at least on some
3715 * outputs) where an enabled pipe still completes any pageflip right
3716 * away (as if the pipe is off) instead of waiting for vblank. As soon
3717 * as the first vblank happend, everything works as expected. Hence just
3718 * wait for one vblank before returning to avoid strange things
3719 * happening.
3720 */
3721 intel_wait_for_vblank(dev, intel_crtc->pipe);
3722}
3723
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003724static void ironlake_pfit_disable(struct intel_crtc *crtc)
3725{
3726 struct drm_device *dev = crtc->base.dev;
3727 struct drm_i915_private *dev_priv = dev->dev_private;
3728 int pipe = crtc->pipe;
3729
3730 /* To avoid upsetting the power well on haswell only disable the pfit if
3731 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003732 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003733 I915_WRITE(PF_CTL(pipe), 0);
3734 I915_WRITE(PF_WIN_POS(pipe), 0);
3735 I915_WRITE(PF_WIN_SZ(pipe), 0);
3736 }
3737}
3738
Jesse Barnes6be4a602010-09-10 10:26:01 -07003739static void ironlake_crtc_disable(struct drm_crtc *crtc)
3740{
3741 struct drm_device *dev = crtc->dev;
3742 struct drm_i915_private *dev_priv = dev->dev_private;
3743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003744 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003745 int pipe = intel_crtc->pipe;
3746 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003747 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003748
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003749
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003750 if (!intel_crtc->active)
3751 return;
3752
Daniel Vetterea9d7582012-07-10 10:42:52 +02003753 for_each_encoder_on_crtc(dev, crtc, encoder)
3754 encoder->disable(encoder);
3755
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003756 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003757 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003758
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003759 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003760 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003761
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003762 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003763 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003764 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003765
Daniel Vetterd925c592013-06-05 13:34:04 +02003766 if (intel_crtc->config.has_pch_encoder)
3767 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3768
Jesse Barnesb24e7172011-01-04 15:09:30 -08003769 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003770
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003771 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003772
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003773 for_each_encoder_on_crtc(dev, crtc, encoder)
3774 if (encoder->post_disable)
3775 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003776
Daniel Vetterd925c592013-06-05 13:34:04 +02003777 if (intel_crtc->config.has_pch_encoder) {
3778 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003779
Daniel Vetterd925c592013-06-05 13:34:04 +02003780 ironlake_disable_pch_transcoder(dev_priv, pipe);
3781 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003782
Daniel Vetterd925c592013-06-05 13:34:04 +02003783 if (HAS_PCH_CPT(dev)) {
3784 /* disable TRANS_DP_CTL */
3785 reg = TRANS_DP_CTL(pipe);
3786 temp = I915_READ(reg);
3787 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3788 TRANS_DP_PORT_SEL_MASK);
3789 temp |= TRANS_DP_PORT_SEL_NONE;
3790 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003791
Daniel Vetterd925c592013-06-05 13:34:04 +02003792 /* disable DPLL_SEL */
3793 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003794 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003795 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003796 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003797
3798 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003799 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003800
3801 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003802 }
3803
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003804 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003805 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003806
3807 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003808 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003809 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003810}
3811
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003812static void haswell_crtc_disable(struct drm_crtc *crtc)
3813{
3814 struct drm_device *dev = crtc->dev;
3815 struct drm_i915_private *dev_priv = dev->dev_private;
3816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3817 struct intel_encoder *encoder;
3818 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003819 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003820
3821 if (!intel_crtc->active)
3822 return;
3823
Ville Syrjälädda9a662013-09-19 17:00:37 -03003824 haswell_crtc_disable_planes(crtc);
3825
Jani Nikula8807e552013-08-30 19:40:32 +03003826 for_each_encoder_on_crtc(dev, crtc, encoder) {
3827 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003828 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003829 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003830
Paulo Zanoni86642812013-04-12 17:57:57 -03003831 if (intel_crtc->config.has_pch_encoder)
3832 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003833 intel_disable_pipe(dev_priv, pipe);
3834
Paulo Zanoniad80a812012-10-24 16:06:19 -02003835 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003836
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003837 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003838
Paulo Zanoni1f544382012-10-24 11:32:00 -02003839 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003840
3841 for_each_encoder_on_crtc(dev, crtc, encoder)
3842 if (encoder->post_disable)
3843 encoder->post_disable(encoder);
3844
Daniel Vetter88adfff2013-03-28 10:42:01 +01003845 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003846 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003847 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003848 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003849 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003850
3851 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003852 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003853
3854 mutex_lock(&dev->struct_mutex);
3855 intel_update_fbc(dev);
3856 mutex_unlock(&dev->struct_mutex);
3857}
3858
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003859static void ironlake_crtc_off(struct drm_crtc *crtc)
3860{
3861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003862 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003863}
3864
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003865static void haswell_crtc_off(struct drm_crtc *crtc)
3866{
3867 intel_ddi_put_crtc_pll(crtc);
3868}
3869
Daniel Vetter02e792f2009-09-15 22:57:34 +02003870static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3871{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003872 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003873 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003874 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003875
Chris Wilson23f09ce2010-08-12 13:53:37 +01003876 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003877 dev_priv->mm.interruptible = false;
3878 (void) intel_overlay_switch_off(intel_crtc->overlay);
3879 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003880 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003881 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003882
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003883 /* Let userspace switch the overlay on again. In most cases userspace
3884 * has to recompute where to put it anyway.
3885 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003886}
3887
Egbert Eich61bc95c2013-03-04 09:24:38 -05003888/**
3889 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3890 * cursor plane briefly if not already running after enabling the display
3891 * plane.
3892 * This workaround avoids occasional blank screens when self refresh is
3893 * enabled.
3894 */
3895static void
3896g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3897{
3898 u32 cntl = I915_READ(CURCNTR(pipe));
3899
3900 if ((cntl & CURSOR_MODE) == 0) {
3901 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3902
3903 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3904 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3905 intel_wait_for_vblank(dev_priv->dev, pipe);
3906 I915_WRITE(CURCNTR(pipe), cntl);
3907 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3908 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3909 }
3910}
3911
Jesse Barnes2dd24552013-04-25 12:55:01 -07003912static void i9xx_pfit_enable(struct intel_crtc *crtc)
3913{
3914 struct drm_device *dev = crtc->base.dev;
3915 struct drm_i915_private *dev_priv = dev->dev_private;
3916 struct intel_crtc_config *pipe_config = &crtc->config;
3917
Daniel Vetter328d8e82013-05-08 10:36:31 +02003918 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003919 return;
3920
Daniel Vetterc0b03412013-05-28 12:05:54 +02003921 /*
3922 * The panel fitter should only be adjusted whilst the pipe is disabled,
3923 * according to register description and PRM.
3924 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003925 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3926 assert_pipe_disabled(dev_priv, crtc->pipe);
3927
Jesse Barnesb074cec2013-04-25 12:55:02 -07003928 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3929 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003930
3931 /* Border color in case we don't scale up to the full screen. Black by
3932 * default, change to something else for debugging. */
3933 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003934}
3935
Jesse Barnes586f49d2013-11-04 16:06:59 -08003936int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003937{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003938 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003939
Jesse Barnes586f49d2013-11-04 16:06:59 -08003940 /* Obtain SKU information */
3941 mutex_lock(&dev_priv->dpio_lock);
3942 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3943 CCK_FUSE_HPLL_FREQ_MASK;
3944 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003945
Jesse Barnes586f49d2013-11-04 16:06:59 -08003946 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003947}
3948
3949/* Adjust CDclk dividers to allow high res or save power if possible */
3950static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3951{
3952 struct drm_i915_private *dev_priv = dev->dev_private;
3953 u32 val, cmd;
3954
3955 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3956 cmd = 2;
3957 else if (cdclk == 266)
3958 cmd = 1;
3959 else
3960 cmd = 0;
3961
3962 mutex_lock(&dev_priv->rps.hw_lock);
3963 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3964 val &= ~DSPFREQGUAR_MASK;
3965 val |= (cmd << DSPFREQGUAR_SHIFT);
3966 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3967 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3968 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3969 50)) {
3970 DRM_ERROR("timed out waiting for CDclk change\n");
3971 }
3972 mutex_unlock(&dev_priv->rps.hw_lock);
3973
3974 if (cdclk == 400) {
3975 u32 divider, vco;
3976
3977 vco = valleyview_get_vco(dev_priv);
3978 divider = ((vco << 1) / cdclk) - 1;
3979
3980 mutex_lock(&dev_priv->dpio_lock);
3981 /* adjust cdclk divider */
3982 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3983 val &= ~0xf;
3984 val |= divider;
3985 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3986 mutex_unlock(&dev_priv->dpio_lock);
3987 }
3988
3989 mutex_lock(&dev_priv->dpio_lock);
3990 /* adjust self-refresh exit latency value */
3991 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
3992 val &= ~0x7f;
3993
3994 /*
3995 * For high bandwidth configs, we set a higher latency in the bunit
3996 * so that the core display fetch happens in time to avoid underruns.
3997 */
3998 if (cdclk == 400)
3999 val |= 4500 / 250; /* 4.5 usec */
4000 else
4001 val |= 3000 / 250; /* 3.0 usec */
4002 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4003 mutex_unlock(&dev_priv->dpio_lock);
4004
4005 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4006 intel_i2c_reset(dev);
4007}
4008
4009static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4010{
4011 int cur_cdclk, vco;
4012 int divider;
4013
4014 vco = valleyview_get_vco(dev_priv);
4015
4016 mutex_lock(&dev_priv->dpio_lock);
4017 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4018 mutex_unlock(&dev_priv->dpio_lock);
4019
4020 divider &= 0xf;
4021
4022 cur_cdclk = (vco << 1) / (divider + 1);
4023
4024 return cur_cdclk;
4025}
4026
4027static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4028 int max_pixclk)
4029{
4030 int cur_cdclk;
4031
4032 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4033
4034 /*
4035 * Really only a few cases to deal with, as only 4 CDclks are supported:
4036 * 200MHz
4037 * 267MHz
4038 * 320MHz
4039 * 400MHz
4040 * So we check to see whether we're above 90% of the lower bin and
4041 * adjust if needed.
4042 */
4043 if (max_pixclk > 288000) {
4044 return 400;
4045 } else if (max_pixclk > 240000) {
4046 return 320;
4047 } else
4048 return 266;
4049 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4050}
4051
4052static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4053 unsigned modeset_pipes,
4054 struct intel_crtc_config *pipe_config)
4055{
4056 struct drm_device *dev = dev_priv->dev;
4057 struct intel_crtc *intel_crtc;
4058 int max_pixclk = 0;
4059
4060 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4061 base.head) {
4062 if (modeset_pipes & (1 << intel_crtc->pipe))
4063 max_pixclk = max(max_pixclk,
4064 pipe_config->adjusted_mode.crtc_clock);
4065 else if (intel_crtc->base.enabled)
4066 max_pixclk = max(max_pixclk,
4067 intel_crtc->config.adjusted_mode.crtc_clock);
4068 }
4069
4070 return max_pixclk;
4071}
4072
4073static void valleyview_modeset_global_pipes(struct drm_device *dev,
4074 unsigned *prepare_pipes,
4075 unsigned modeset_pipes,
4076 struct intel_crtc_config *pipe_config)
4077{
4078 struct drm_i915_private *dev_priv = dev->dev_private;
4079 struct intel_crtc *intel_crtc;
4080 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4081 pipe_config);
4082 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4083
4084 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4085 return;
4086
4087 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4088 base.head)
4089 if (intel_crtc->base.enabled)
4090 *prepare_pipes |= (1 << intel_crtc->pipe);
4091}
4092
4093static void valleyview_modeset_global_resources(struct drm_device *dev)
4094{
4095 struct drm_i915_private *dev_priv = dev->dev_private;
4096 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4097 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4098 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4099
4100 if (req_cdclk != cur_cdclk)
4101 valleyview_set_cdclk(dev, req_cdclk);
4102}
4103
Jesse Barnes89b667f2013-04-18 14:51:36 -07004104static void valleyview_crtc_enable(struct drm_crtc *crtc)
4105{
4106 struct drm_device *dev = crtc->dev;
4107 struct drm_i915_private *dev_priv = dev->dev_private;
4108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4109 struct intel_encoder *encoder;
4110 int pipe = intel_crtc->pipe;
4111 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004112 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004113
4114 WARN_ON(!crtc->enabled);
4115
4116 if (intel_crtc->active)
4117 return;
4118
4119 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004120
Jesse Barnes89b667f2013-04-18 14:51:36 -07004121 for_each_encoder_on_crtc(dev, crtc, encoder)
4122 if (encoder->pre_pll_enable)
4123 encoder->pre_pll_enable(encoder);
4124
Jani Nikula23538ef2013-08-27 15:12:22 +03004125 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4126
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004127 if (!is_dsi)
4128 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004129
4130 for_each_encoder_on_crtc(dev, crtc, encoder)
4131 if (encoder->pre_enable)
4132 encoder->pre_enable(encoder);
4133
Jesse Barnes2dd24552013-04-25 12:55:01 -07004134 i9xx_pfit_enable(intel_crtc);
4135
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004136 intel_crtc_load_lut(crtc);
4137
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004138 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004139 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004140 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004141 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004142 intel_crtc_update_cursor(crtc, true);
4143
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004144 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004145
4146 for_each_encoder_on_crtc(dev, crtc, encoder)
4147 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004148}
4149
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004150static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004151{
4152 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004153 struct drm_i915_private *dev_priv = dev->dev_private;
4154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004155 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004156 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004157 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004158
Daniel Vetter08a48462012-07-02 11:43:47 +02004159 WARN_ON(!crtc->enabled);
4160
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004161 if (intel_crtc->active)
4162 return;
4163
4164 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004165
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004166 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004167 if (encoder->pre_enable)
4168 encoder->pre_enable(encoder);
4169
Daniel Vetterf6736a12013-06-05 13:34:30 +02004170 i9xx_enable_pll(intel_crtc);
4171
Jesse Barnes2dd24552013-04-25 12:55:01 -07004172 i9xx_pfit_enable(intel_crtc);
4173
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004174 intel_crtc_load_lut(crtc);
4175
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004176 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004177 intel_enable_pipe(dev_priv, pipe, false, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004178 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004179 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004180 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004181 if (IS_G4X(dev))
4182 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004183 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004184
4185 /* Give the overlay scaler a chance to enable if it's on this pipe */
4186 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004187
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004188 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004189
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004190 for_each_encoder_on_crtc(dev, crtc, encoder)
4191 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004192}
4193
Daniel Vetter87476d62013-04-11 16:29:06 +02004194static void i9xx_pfit_disable(struct intel_crtc *crtc)
4195{
4196 struct drm_device *dev = crtc->base.dev;
4197 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004198
4199 if (!crtc->config.gmch_pfit.control)
4200 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004201
4202 assert_pipe_disabled(dev_priv, crtc->pipe);
4203
Daniel Vetter328d8e82013-05-08 10:36:31 +02004204 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4205 I915_READ(PFIT_CONTROL));
4206 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004207}
4208
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004209static void i9xx_crtc_disable(struct drm_crtc *crtc)
4210{
4211 struct drm_device *dev = crtc->dev;
4212 struct drm_i915_private *dev_priv = dev->dev_private;
4213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004214 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004215 int pipe = intel_crtc->pipe;
4216 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004217
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004218 if (!intel_crtc->active)
4219 return;
4220
Daniel Vetterea9d7582012-07-10 10:42:52 +02004221 for_each_encoder_on_crtc(dev, crtc, encoder)
4222 encoder->disable(encoder);
4223
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004224 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004225 intel_crtc_wait_for_pending_flips(crtc);
4226 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004227
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004228 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004229 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004230
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004231 intel_crtc_dpms_overlay(intel_crtc, false);
4232 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004233 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004234 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004235
Jesse Barnesb24e7172011-01-04 15:09:30 -08004236 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004237
Daniel Vetter87476d62013-04-11 16:29:06 +02004238 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004239
Jesse Barnes89b667f2013-04-18 14:51:36 -07004240 for_each_encoder_on_crtc(dev, crtc, encoder)
4241 if (encoder->post_disable)
4242 encoder->post_disable(encoder);
4243
Jesse Barnesf6071162013-10-01 10:41:38 -07004244 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4245 vlv_disable_pll(dev_priv, pipe);
4246 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004247 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004248
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004249 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004250 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004251
Chris Wilson6b383a72010-09-13 13:54:26 +01004252 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004253}
4254
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004255static void i9xx_crtc_off(struct drm_crtc *crtc)
4256{
4257}
4258
Daniel Vetter976f8a22012-07-08 22:34:21 +02004259static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4260 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004261{
4262 struct drm_device *dev = crtc->dev;
4263 struct drm_i915_master_private *master_priv;
4264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4265 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004266
4267 if (!dev->primary->master)
4268 return;
4269
4270 master_priv = dev->primary->master->driver_priv;
4271 if (!master_priv->sarea_priv)
4272 return;
4273
Jesse Barnes79e53942008-11-07 14:24:08 -08004274 switch (pipe) {
4275 case 0:
4276 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4277 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4278 break;
4279 case 1:
4280 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4281 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4282 break;
4283 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004284 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004285 break;
4286 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004287}
4288
Daniel Vetter976f8a22012-07-08 22:34:21 +02004289/**
4290 * Sets the power management mode of the pipe and plane.
4291 */
4292void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004293{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004294 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004295 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004296 struct intel_encoder *intel_encoder;
4297 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004298
Daniel Vetter976f8a22012-07-08 22:34:21 +02004299 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4300 enable |= intel_encoder->connectors_active;
4301
4302 if (enable)
4303 dev_priv->display.crtc_enable(crtc);
4304 else
4305 dev_priv->display.crtc_disable(crtc);
4306
4307 intel_crtc_update_sarea(crtc, enable);
4308}
4309
Daniel Vetter976f8a22012-07-08 22:34:21 +02004310static void intel_crtc_disable(struct drm_crtc *crtc)
4311{
4312 struct drm_device *dev = crtc->dev;
4313 struct drm_connector *connector;
4314 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004316
4317 /* crtc should still be enabled when we disable it. */
4318 WARN_ON(!crtc->enabled);
4319
4320 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004321 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004322 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004323 dev_priv->display.off(crtc);
4324
Chris Wilson931872f2012-01-16 23:01:13 +00004325 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004326 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004327 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004328
4329 if (crtc->fb) {
4330 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004331 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004332 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004333 crtc->fb = NULL;
4334 }
4335
4336 /* Update computed state. */
4337 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4338 if (!connector->encoder || !connector->encoder->crtc)
4339 continue;
4340
4341 if (connector->encoder->crtc != crtc)
4342 continue;
4343
4344 connector->dpms = DRM_MODE_DPMS_OFF;
4345 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004346 }
4347}
4348
Chris Wilsonea5b2132010-08-04 13:50:23 +01004349void intel_encoder_destroy(struct drm_encoder *encoder)
4350{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004351 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004352
Chris Wilsonea5b2132010-08-04 13:50:23 +01004353 drm_encoder_cleanup(encoder);
4354 kfree(intel_encoder);
4355}
4356
Damien Lespiau92373292013-08-08 22:28:57 +01004357/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004358 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4359 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004360static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004361{
4362 if (mode == DRM_MODE_DPMS_ON) {
4363 encoder->connectors_active = true;
4364
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004365 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004366 } else {
4367 encoder->connectors_active = false;
4368
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004369 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004370 }
4371}
4372
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004373/* Cross check the actual hw state with our own modeset state tracking (and it's
4374 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004375static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004376{
4377 if (connector->get_hw_state(connector)) {
4378 struct intel_encoder *encoder = connector->encoder;
4379 struct drm_crtc *crtc;
4380 bool encoder_enabled;
4381 enum pipe pipe;
4382
4383 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4384 connector->base.base.id,
4385 drm_get_connector_name(&connector->base));
4386
4387 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4388 "wrong connector dpms state\n");
4389 WARN(connector->base.encoder != &encoder->base,
4390 "active connector not linked to encoder\n");
4391 WARN(!encoder->connectors_active,
4392 "encoder->connectors_active not set\n");
4393
4394 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4395 WARN(!encoder_enabled, "encoder not enabled\n");
4396 if (WARN_ON(!encoder->base.crtc))
4397 return;
4398
4399 crtc = encoder->base.crtc;
4400
4401 WARN(!crtc->enabled, "crtc not enabled\n");
4402 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4403 WARN(pipe != to_intel_crtc(crtc)->pipe,
4404 "encoder active on the wrong pipe\n");
4405 }
4406}
4407
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004408/* Even simpler default implementation, if there's really no special case to
4409 * consider. */
4410void intel_connector_dpms(struct drm_connector *connector, int mode)
4411{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004412 /* All the simple cases only support two dpms states. */
4413 if (mode != DRM_MODE_DPMS_ON)
4414 mode = DRM_MODE_DPMS_OFF;
4415
4416 if (mode == connector->dpms)
4417 return;
4418
4419 connector->dpms = mode;
4420
4421 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004422 if (connector->encoder)
4423 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004424
Daniel Vetterb9805142012-08-31 17:37:33 +02004425 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004426}
4427
Daniel Vetterf0947c32012-07-02 13:10:34 +02004428/* Simple connector->get_hw_state implementation for encoders that support only
4429 * one connector and no cloning and hence the encoder state determines the state
4430 * of the connector. */
4431bool intel_connector_get_hw_state(struct intel_connector *connector)
4432{
Daniel Vetter24929352012-07-02 20:28:59 +02004433 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004434 struct intel_encoder *encoder = connector->encoder;
4435
4436 return encoder->get_hw_state(encoder, &pipe);
4437}
4438
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004439static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4440 struct intel_crtc_config *pipe_config)
4441{
4442 struct drm_i915_private *dev_priv = dev->dev_private;
4443 struct intel_crtc *pipe_B_crtc =
4444 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4445
4446 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4447 pipe_name(pipe), pipe_config->fdi_lanes);
4448 if (pipe_config->fdi_lanes > 4) {
4449 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4450 pipe_name(pipe), pipe_config->fdi_lanes);
4451 return false;
4452 }
4453
Paulo Zanonibafb6552013-11-02 21:07:44 -07004454 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004455 if (pipe_config->fdi_lanes > 2) {
4456 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4457 pipe_config->fdi_lanes);
4458 return false;
4459 } else {
4460 return true;
4461 }
4462 }
4463
4464 if (INTEL_INFO(dev)->num_pipes == 2)
4465 return true;
4466
4467 /* Ivybridge 3 pipe is really complicated */
4468 switch (pipe) {
4469 case PIPE_A:
4470 return true;
4471 case PIPE_B:
4472 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4473 pipe_config->fdi_lanes > 2) {
4474 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4475 pipe_name(pipe), pipe_config->fdi_lanes);
4476 return false;
4477 }
4478 return true;
4479 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004480 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004481 pipe_B_crtc->config.fdi_lanes <= 2) {
4482 if (pipe_config->fdi_lanes > 2) {
4483 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4484 pipe_name(pipe), pipe_config->fdi_lanes);
4485 return false;
4486 }
4487 } else {
4488 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4489 return false;
4490 }
4491 return true;
4492 default:
4493 BUG();
4494 }
4495}
4496
Daniel Vettere29c22c2013-02-21 00:00:16 +01004497#define RETRY 1
4498static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4499 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004500{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004501 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004502 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004503 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004504 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004505
Daniel Vettere29c22c2013-02-21 00:00:16 +01004506retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004507 /* FDI is a binary signal running at ~2.7GHz, encoding
4508 * each output octet as 10 bits. The actual frequency
4509 * is stored as a divider into a 100MHz clock, and the
4510 * mode pixel clock is stored in units of 1KHz.
4511 * Hence the bw of each lane in terms of the mode signal
4512 * is:
4513 */
4514 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4515
Damien Lespiau241bfc32013-09-25 16:45:37 +01004516 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004517
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004518 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004519 pipe_config->pipe_bpp);
4520
4521 pipe_config->fdi_lanes = lane;
4522
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004523 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004524 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004525
Daniel Vettere29c22c2013-02-21 00:00:16 +01004526 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4527 intel_crtc->pipe, pipe_config);
4528 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4529 pipe_config->pipe_bpp -= 2*3;
4530 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4531 pipe_config->pipe_bpp);
4532 needs_recompute = true;
4533 pipe_config->bw_constrained = true;
4534
4535 goto retry;
4536 }
4537
4538 if (needs_recompute)
4539 return RETRY;
4540
4541 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004542}
4543
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004544static void hsw_compute_ips_config(struct intel_crtc *crtc,
4545 struct intel_crtc_config *pipe_config)
4546{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004547 pipe_config->ips_enabled = i915_enable_ips &&
4548 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004549 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004550}
4551
Daniel Vettera43f6e02013-06-07 23:10:32 +02004552static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004553 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004554{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004555 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004556 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004557
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004558 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004559 if (INTEL_INFO(dev)->gen < 4) {
4560 struct drm_i915_private *dev_priv = dev->dev_private;
4561 int clock_limit =
4562 dev_priv->display.get_display_clock_speed(dev);
4563
4564 /*
4565 * Enable pixel doubling when the dot clock
4566 * is > 90% of the (display) core speed.
4567 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004568 * GDG double wide on either pipe,
4569 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004570 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004571 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004572 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004573 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004574 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004575 }
4576
Damien Lespiau241bfc32013-09-25 16:45:37 +01004577 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004578 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004579 }
Chris Wilson89749352010-09-12 18:25:19 +01004580
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004581 /*
4582 * Pipe horizontal size must be even in:
4583 * - DVO ganged mode
4584 * - LVDS dual channel mode
4585 * - Double wide pipe
4586 */
4587 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4588 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4589 pipe_config->pipe_src_w &= ~1;
4590
Damien Lespiau8693a822013-05-03 18:48:11 +01004591 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4592 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004593 */
4594 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4595 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004596 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004597
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004598 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004599 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004600 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004601 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4602 * for lvds. */
4603 pipe_config->pipe_bpp = 8*3;
4604 }
4605
Damien Lespiauf5adf942013-06-24 18:29:34 +01004606 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004607 hsw_compute_ips_config(crtc, pipe_config);
4608
4609 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4610 * clock survives for now. */
4611 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4612 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004613
Daniel Vetter877d48d2013-04-19 11:24:43 +02004614 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004615 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004616
Daniel Vettere29c22c2013-02-21 00:00:16 +01004617 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004618}
4619
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004620static int valleyview_get_display_clock_speed(struct drm_device *dev)
4621{
4622 return 400000; /* FIXME */
4623}
4624
Jesse Barnese70236a2009-09-21 10:42:27 -07004625static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004626{
Jesse Barnese70236a2009-09-21 10:42:27 -07004627 return 400000;
4628}
Jesse Barnes79e53942008-11-07 14:24:08 -08004629
Jesse Barnese70236a2009-09-21 10:42:27 -07004630static int i915_get_display_clock_speed(struct drm_device *dev)
4631{
4632 return 333000;
4633}
Jesse Barnes79e53942008-11-07 14:24:08 -08004634
Jesse Barnese70236a2009-09-21 10:42:27 -07004635static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4636{
4637 return 200000;
4638}
Jesse Barnes79e53942008-11-07 14:24:08 -08004639
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004640static int pnv_get_display_clock_speed(struct drm_device *dev)
4641{
4642 u16 gcfgc = 0;
4643
4644 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4645
4646 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4647 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4648 return 267000;
4649 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4650 return 333000;
4651 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4652 return 444000;
4653 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4654 return 200000;
4655 default:
4656 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4657 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4658 return 133000;
4659 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4660 return 167000;
4661 }
4662}
4663
Jesse Barnese70236a2009-09-21 10:42:27 -07004664static int i915gm_get_display_clock_speed(struct drm_device *dev)
4665{
4666 u16 gcfgc = 0;
4667
4668 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4669
4670 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004671 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004672 else {
4673 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4674 case GC_DISPLAY_CLOCK_333_MHZ:
4675 return 333000;
4676 default:
4677 case GC_DISPLAY_CLOCK_190_200_MHZ:
4678 return 190000;
4679 }
4680 }
4681}
Jesse Barnes79e53942008-11-07 14:24:08 -08004682
Jesse Barnese70236a2009-09-21 10:42:27 -07004683static int i865_get_display_clock_speed(struct drm_device *dev)
4684{
4685 return 266000;
4686}
4687
4688static int i855_get_display_clock_speed(struct drm_device *dev)
4689{
4690 u16 hpllcc = 0;
4691 /* Assume that the hardware is in the high speed state. This
4692 * should be the default.
4693 */
4694 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4695 case GC_CLOCK_133_200:
4696 case GC_CLOCK_100_200:
4697 return 200000;
4698 case GC_CLOCK_166_250:
4699 return 250000;
4700 case GC_CLOCK_100_133:
4701 return 133000;
4702 }
4703
4704 /* Shouldn't happen */
4705 return 0;
4706}
4707
4708static int i830_get_display_clock_speed(struct drm_device *dev)
4709{
4710 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004711}
4712
Zhenyu Wang2c072452009-06-05 15:38:42 +08004713static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004714intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004715{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004716 while (*num > DATA_LINK_M_N_MASK ||
4717 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004718 *num >>= 1;
4719 *den >>= 1;
4720 }
4721}
4722
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004723static void compute_m_n(unsigned int m, unsigned int n,
4724 uint32_t *ret_m, uint32_t *ret_n)
4725{
4726 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4727 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4728 intel_reduce_m_n_ratio(ret_m, ret_n);
4729}
4730
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004731void
4732intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4733 int pixel_clock, int link_clock,
4734 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004735{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004736 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004737
4738 compute_m_n(bits_per_pixel * pixel_clock,
4739 link_clock * nlanes * 8,
4740 &m_n->gmch_m, &m_n->gmch_n);
4741
4742 compute_m_n(pixel_clock, link_clock,
4743 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004744}
4745
Chris Wilsona7615032011-01-12 17:04:08 +00004746static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4747{
Keith Packard72bbe582011-09-26 16:09:45 -07004748 if (i915_panel_use_ssc >= 0)
4749 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004750 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004751 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004752}
4753
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004754static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4755{
4756 struct drm_device *dev = crtc->dev;
4757 struct drm_i915_private *dev_priv = dev->dev_private;
4758 int refclk;
4759
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004760 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004761 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004762 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004763 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02004764 refclk = dev_priv->vbt.lvds_ssc_freq;
4765 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004766 } else if (!IS_GEN2(dev)) {
4767 refclk = 96000;
4768 } else {
4769 refclk = 48000;
4770 }
4771
4772 return refclk;
4773}
4774
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004775static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004776{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004777 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004778}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004779
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004780static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4781{
4782 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004783}
4784
Daniel Vetterf47709a2013-03-28 10:42:02 +01004785static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004786 intel_clock_t *reduced_clock)
4787{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004788 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004789 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004790 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004791 u32 fp, fp2 = 0;
4792
4793 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004794 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004795 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004796 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004797 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004798 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004799 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004800 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004801 }
4802
4803 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004804 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004805
Daniel Vetterf47709a2013-03-28 10:42:02 +01004806 crtc->lowfreq_avail = false;
4807 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004808 reduced_clock && i915_powersave) {
4809 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004810 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004811 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004812 } else {
4813 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004814 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004815 }
4816}
4817
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004818static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4819 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004820{
4821 u32 reg_val;
4822
4823 /*
4824 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4825 * and set it to a reasonable value instead.
4826 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004827 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004828 reg_val &= 0xffffff00;
4829 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004830 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004831
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004832 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004833 reg_val &= 0x8cffffff;
4834 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004835 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004836
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004837 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004838 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004839 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004840
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004841 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004842 reg_val &= 0x00ffffff;
4843 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004844 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004845}
4846
Daniel Vetterb5518422013-05-03 11:49:48 +02004847static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4848 struct intel_link_m_n *m_n)
4849{
4850 struct drm_device *dev = crtc->base.dev;
4851 struct drm_i915_private *dev_priv = dev->dev_private;
4852 int pipe = crtc->pipe;
4853
Daniel Vettere3b95f12013-05-03 11:49:49 +02004854 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4855 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4856 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4857 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004858}
4859
4860static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4861 struct intel_link_m_n *m_n)
4862{
4863 struct drm_device *dev = crtc->base.dev;
4864 struct drm_i915_private *dev_priv = dev->dev_private;
4865 int pipe = crtc->pipe;
4866 enum transcoder transcoder = crtc->config.cpu_transcoder;
4867
4868 if (INTEL_INFO(dev)->gen >= 5) {
4869 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4870 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4871 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4872 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4873 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004874 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4875 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4876 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4877 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004878 }
4879}
4880
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004881static void intel_dp_set_m_n(struct intel_crtc *crtc)
4882{
4883 if (crtc->config.has_pch_encoder)
4884 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4885 else
4886 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4887}
4888
Daniel Vetterf47709a2013-03-28 10:42:02 +01004889static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004890{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004891 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004892 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004893 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004894 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004895 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004896 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004897
Daniel Vetter09153002012-12-12 14:06:44 +01004898 mutex_lock(&dev_priv->dpio_lock);
4899
Daniel Vetterf47709a2013-03-28 10:42:02 +01004900 bestn = crtc->config.dpll.n;
4901 bestm1 = crtc->config.dpll.m1;
4902 bestm2 = crtc->config.dpll.m2;
4903 bestp1 = crtc->config.dpll.p1;
4904 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004905
Jesse Barnes89b667f2013-04-18 14:51:36 -07004906 /* See eDP HDMI DPIO driver vbios notes doc */
4907
4908 /* PLL B needs special handling */
4909 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004910 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004911
4912 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004913 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004914
4915 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004916 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004917 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004918 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004919
4920 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004921 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004922
4923 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004924 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4925 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4926 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004927 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004928
4929 /*
4930 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4931 * but we don't support that).
4932 * Note: don't use the DAC post divider as it seems unstable.
4933 */
4934 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004936
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004937 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004938 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004939
Jesse Barnes89b667f2013-04-18 14:51:36 -07004940 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004941 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004942 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004943 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004945 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004946 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004947 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004948 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004949
Jesse Barnes89b667f2013-04-18 14:51:36 -07004950 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4951 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4952 /* Use SSC source */
4953 if (!pipe)
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 0x0df40000);
4956 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004958 0x0df70000);
4959 } else { /* HDMI or VGA */
4960 /* Use bend source */
4961 if (!pipe)
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 0x0df70000);
4964 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004965 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004966 0x0df40000);
4967 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004968
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004969 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004970 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4971 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4972 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4973 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004974 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004975
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004976 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004977
Jesse Barnes89b667f2013-04-18 14:51:36 -07004978 /* Enable DPIO clock input */
4979 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4980 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004981 /* We should never disable this, set it here for state tracking */
4982 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004983 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004984 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004985 crtc->config.dpll_hw_state.dpll = dpll;
4986
Daniel Vetteref1b4602013-06-01 17:17:04 +02004987 dpll_md = (crtc->config.pixel_multiplier - 1)
4988 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004989 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4990
Daniel Vetterf47709a2013-03-28 10:42:02 +01004991 if (crtc->config.has_dp_encoder)
4992 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304993
Daniel Vetter09153002012-12-12 14:06:44 +01004994 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004995}
4996
Daniel Vetterf47709a2013-03-28 10:42:02 +01004997static void i9xx_update_pll(struct intel_crtc *crtc,
4998 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004999 int num_connectors)
5000{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005001 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005002 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005003 u32 dpll;
5004 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005005 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005006
Daniel Vetterf47709a2013-03-28 10:42:02 +01005007 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305008
Daniel Vetterf47709a2013-03-28 10:42:02 +01005009 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5010 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005011
5012 dpll = DPLL_VGA_MODE_DIS;
5013
Daniel Vetterf47709a2013-03-28 10:42:02 +01005014 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005015 dpll |= DPLLB_MODE_LVDS;
5016 else
5017 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005018
Daniel Vetteref1b4602013-06-01 17:17:04 +02005019 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005020 dpll |= (crtc->config.pixel_multiplier - 1)
5021 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005022 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005023
5024 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005025 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005026
Daniel Vetterf47709a2013-03-28 10:42:02 +01005027 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005028 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005029
5030 /* compute bitmask from p1 value */
5031 if (IS_PINEVIEW(dev))
5032 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5033 else {
5034 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5035 if (IS_G4X(dev) && reduced_clock)
5036 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5037 }
5038 switch (clock->p2) {
5039 case 5:
5040 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5041 break;
5042 case 7:
5043 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5044 break;
5045 case 10:
5046 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5047 break;
5048 case 14:
5049 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5050 break;
5051 }
5052 if (INTEL_INFO(dev)->gen >= 4)
5053 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5054
Daniel Vetter09ede542013-04-30 14:01:45 +02005055 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005056 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005057 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005058 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5059 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5060 else
5061 dpll |= PLL_REF_INPUT_DREFCLK;
5062
5063 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005064 crtc->config.dpll_hw_state.dpll = dpll;
5065
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005066 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005067 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5068 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005069 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005070 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005071
5072 if (crtc->config.has_dp_encoder)
5073 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005074}
5075
Daniel Vetterf47709a2013-03-28 10:42:02 +01005076static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005077 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005078 int num_connectors)
5079{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005080 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005081 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005082 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005083 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005084
Daniel Vetterf47709a2013-03-28 10:42:02 +01005085 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305086
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005087 dpll = DPLL_VGA_MODE_DIS;
5088
Daniel Vetterf47709a2013-03-28 10:42:02 +01005089 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005090 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5091 } else {
5092 if (clock->p1 == 2)
5093 dpll |= PLL_P1_DIVIDE_BY_TWO;
5094 else
5095 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5096 if (clock->p2 == 4)
5097 dpll |= PLL_P2_DIVIDE_BY_4;
5098 }
5099
Daniel Vetter4a33e482013-07-06 12:52:05 +02005100 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5101 dpll |= DPLL_DVO_2X_MODE;
5102
Daniel Vetterf47709a2013-03-28 10:42:02 +01005103 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005104 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5105 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5106 else
5107 dpll |= PLL_REF_INPUT_DREFCLK;
5108
5109 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005110 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005111}
5112
Daniel Vetter8a654f32013-06-01 17:16:22 +02005113static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005114{
5115 struct drm_device *dev = intel_crtc->base.dev;
5116 struct drm_i915_private *dev_priv = dev->dev_private;
5117 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005118 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005119 struct drm_display_mode *adjusted_mode =
5120 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005121 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5122
5123 /* We need to be careful not to changed the adjusted mode, for otherwise
5124 * the hw state checker will get angry at the mismatch. */
5125 crtc_vtotal = adjusted_mode->crtc_vtotal;
5126 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005127
5128 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5129 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005130 crtc_vtotal -= 1;
5131 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005132 vsyncshift = adjusted_mode->crtc_hsync_start
5133 - adjusted_mode->crtc_htotal / 2;
5134 } else {
5135 vsyncshift = 0;
5136 }
5137
5138 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005139 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005140
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005141 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005142 (adjusted_mode->crtc_hdisplay - 1) |
5143 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005144 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005145 (adjusted_mode->crtc_hblank_start - 1) |
5146 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005147 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005148 (adjusted_mode->crtc_hsync_start - 1) |
5149 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5150
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005151 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005152 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005153 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005154 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005155 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005156 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005157 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005158 (adjusted_mode->crtc_vsync_start - 1) |
5159 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5160
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005161 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5162 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5163 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5164 * bits. */
5165 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5166 (pipe == PIPE_B || pipe == PIPE_C))
5167 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5168
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005169 /* pipesrc controls the size that is scaled from, which should
5170 * always be the user's requested size.
5171 */
5172 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005173 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5174 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005175}
5176
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005177static void intel_get_pipe_timings(struct intel_crtc *crtc,
5178 struct intel_crtc_config *pipe_config)
5179{
5180 struct drm_device *dev = crtc->base.dev;
5181 struct drm_i915_private *dev_priv = dev->dev_private;
5182 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5183 uint32_t tmp;
5184
5185 tmp = I915_READ(HTOTAL(cpu_transcoder));
5186 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5187 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5188 tmp = I915_READ(HBLANK(cpu_transcoder));
5189 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5190 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5191 tmp = I915_READ(HSYNC(cpu_transcoder));
5192 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5193 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5194
5195 tmp = I915_READ(VTOTAL(cpu_transcoder));
5196 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5197 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5198 tmp = I915_READ(VBLANK(cpu_transcoder));
5199 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5200 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5201 tmp = I915_READ(VSYNC(cpu_transcoder));
5202 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5203 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5204
5205 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5206 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5207 pipe_config->adjusted_mode.crtc_vtotal += 1;
5208 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5209 }
5210
5211 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005212 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5213 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5214
5215 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5216 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005217}
5218
Jesse Barnesbabea612013-06-26 18:57:38 +03005219static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5220 struct intel_crtc_config *pipe_config)
5221{
5222 struct drm_crtc *crtc = &intel_crtc->base;
5223
5224 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5225 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5226 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5227 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5228
5229 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5230 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5231 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5232 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5233
5234 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5235
Damien Lespiau241bfc32013-09-25 16:45:37 +01005236 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005237 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5238}
5239
Daniel Vetter84b046f2013-02-19 18:48:54 +01005240static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5241{
5242 struct drm_device *dev = intel_crtc->base.dev;
5243 struct drm_i915_private *dev_priv = dev->dev_private;
5244 uint32_t pipeconf;
5245
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005246 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005247
Daniel Vetter67c72a12013-09-24 11:46:14 +02005248 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5249 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5250 pipeconf |= PIPECONF_ENABLE;
5251
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005252 if (intel_crtc->config.double_wide)
5253 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005254
Daniel Vetterff9ce462013-04-24 14:57:17 +02005255 /* only g4x and later have fancy bpc/dither controls */
5256 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005257 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5258 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5259 pipeconf |= PIPECONF_DITHER_EN |
5260 PIPECONF_DITHER_TYPE_SP;
5261
5262 switch (intel_crtc->config.pipe_bpp) {
5263 case 18:
5264 pipeconf |= PIPECONF_6BPC;
5265 break;
5266 case 24:
5267 pipeconf |= PIPECONF_8BPC;
5268 break;
5269 case 30:
5270 pipeconf |= PIPECONF_10BPC;
5271 break;
5272 default:
5273 /* Case prevented by intel_choose_pipe_bpp_dither. */
5274 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005275 }
5276 }
5277
5278 if (HAS_PIPE_CXSR(dev)) {
5279 if (intel_crtc->lowfreq_avail) {
5280 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5281 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5282 } else {
5283 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005284 }
5285 }
5286
Daniel Vetter84b046f2013-02-19 18:48:54 +01005287 if (!IS_GEN2(dev) &&
5288 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5289 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5290 else
5291 pipeconf |= PIPECONF_PROGRESSIVE;
5292
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005293 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5294 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005295
Daniel Vetter84b046f2013-02-19 18:48:54 +01005296 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5297 POSTING_READ(PIPECONF(intel_crtc->pipe));
5298}
5299
Eric Anholtf564048e2011-03-30 13:01:02 -07005300static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005301 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005302 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005303{
5304 struct drm_device *dev = crtc->dev;
5305 struct drm_i915_private *dev_priv = dev->dev_private;
5306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5307 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005308 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005309 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005310 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005311 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005312 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005313 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005314 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005315 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005316 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005317
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005318 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005319 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005320 case INTEL_OUTPUT_LVDS:
5321 is_lvds = true;
5322 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005323 case INTEL_OUTPUT_DSI:
5324 is_dsi = true;
5325 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005326 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005327
Eric Anholtc751ce42010-03-25 11:48:48 -07005328 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005329 }
5330
Jani Nikulaf2335332013-09-13 11:03:09 +03005331 if (is_dsi)
5332 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005333
Jani Nikulaf2335332013-09-13 11:03:09 +03005334 if (!intel_crtc->config.clock_set) {
5335 refclk = i9xx_get_refclk(crtc, num_connectors);
5336
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005337 /*
5338 * Returns a set of divisors for the desired target clock with
5339 * the given refclk, or FALSE. The returned values represent
5340 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5341 * 2) / p1 / p2.
5342 */
5343 limit = intel_limit(crtc, refclk);
5344 ok = dev_priv->display.find_dpll(limit, crtc,
5345 intel_crtc->config.port_clock,
5346 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005347 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005348 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5349 return -EINVAL;
5350 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005351
Jani Nikulaf2335332013-09-13 11:03:09 +03005352 if (is_lvds && dev_priv->lvds_downclock_avail) {
5353 /*
5354 * Ensure we match the reduced clock's P to the target
5355 * clock. If the clocks don't match, we can't switch
5356 * the display clock by using the FP0/FP1. In such case
5357 * we will disable the LVDS downclock feature.
5358 */
5359 has_reduced_clock =
5360 dev_priv->display.find_dpll(limit, crtc,
5361 dev_priv->lvds_downclock,
5362 refclk, &clock,
5363 &reduced_clock);
5364 }
5365 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005366 intel_crtc->config.dpll.n = clock.n;
5367 intel_crtc->config.dpll.m1 = clock.m1;
5368 intel_crtc->config.dpll.m2 = clock.m2;
5369 intel_crtc->config.dpll.p1 = clock.p1;
5370 intel_crtc->config.dpll.p2 = clock.p2;
5371 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005372
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005373 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005374 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305375 has_reduced_clock ? &reduced_clock : NULL,
5376 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005377 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005378 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005379 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005380 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005381 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005382 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005383 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005384
Jani Nikulaf2335332013-09-13 11:03:09 +03005385skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005386 /* Set up the display plane register */
5387 dspcntr = DISPPLANE_GAMMA_ENABLE;
5388
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005389 if (!IS_VALLEYVIEW(dev)) {
5390 if (pipe == 0)
5391 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5392 else
5393 dspcntr |= DISPPLANE_SEL_PIPE_B;
5394 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005395
Daniel Vetter8a654f32013-06-01 17:16:22 +02005396 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005397
5398 /* pipesrc and dspsize control the size that is scaled from,
5399 * which should always be the user's requested size.
5400 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005401 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005402 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5403 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005404 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005405
Daniel Vetter84b046f2013-02-19 18:48:54 +01005406 i9xx_set_pipeconf(intel_crtc);
5407
Eric Anholtf564048e2011-03-30 13:01:02 -07005408 I915_WRITE(DSPCNTR(plane), dspcntr);
5409 POSTING_READ(DSPCNTR(plane));
5410
Daniel Vetter94352cf2012-07-05 22:51:56 +02005411 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005412
Eric Anholtf564048e2011-03-30 13:01:02 -07005413 return ret;
5414}
5415
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005416static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5417 struct intel_crtc_config *pipe_config)
5418{
5419 struct drm_device *dev = crtc->base.dev;
5420 struct drm_i915_private *dev_priv = dev->dev_private;
5421 uint32_t tmp;
5422
5423 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005424 if (!(tmp & PFIT_ENABLE))
5425 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005426
Daniel Vetter06922822013-07-11 13:35:40 +02005427 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005428 if (INTEL_INFO(dev)->gen < 4) {
5429 if (crtc->pipe != PIPE_B)
5430 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005431 } else {
5432 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5433 return;
5434 }
5435
Daniel Vetter06922822013-07-11 13:35:40 +02005436 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005437 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5438 if (INTEL_INFO(dev)->gen < 5)
5439 pipe_config->gmch_pfit.lvds_border_bits =
5440 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5441}
5442
Jesse Barnesacbec812013-09-20 11:29:32 -07005443static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5444 struct intel_crtc_config *pipe_config)
5445{
5446 struct drm_device *dev = crtc->base.dev;
5447 struct drm_i915_private *dev_priv = dev->dev_private;
5448 int pipe = pipe_config->cpu_transcoder;
5449 intel_clock_t clock;
5450 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005451 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005452
5453 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005454 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005455 mutex_unlock(&dev_priv->dpio_lock);
5456
5457 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5458 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5459 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5460 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5461 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5462
Ville Syrjäläf6466282013-10-14 14:50:31 +03005463 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005464
Ville Syrjäläf6466282013-10-14 14:50:31 +03005465 /* clock.dot is the fast clock */
5466 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005467}
5468
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005469static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5470 struct intel_crtc_config *pipe_config)
5471{
5472 struct drm_device *dev = crtc->base.dev;
5473 struct drm_i915_private *dev_priv = dev->dev_private;
5474 uint32_t tmp;
5475
Daniel Vettere143a212013-07-04 12:01:15 +02005476 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005477 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005478
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005479 tmp = I915_READ(PIPECONF(crtc->pipe));
5480 if (!(tmp & PIPECONF_ENABLE))
5481 return false;
5482
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005483 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5484 switch (tmp & PIPECONF_BPC_MASK) {
5485 case PIPECONF_6BPC:
5486 pipe_config->pipe_bpp = 18;
5487 break;
5488 case PIPECONF_8BPC:
5489 pipe_config->pipe_bpp = 24;
5490 break;
5491 case PIPECONF_10BPC:
5492 pipe_config->pipe_bpp = 30;
5493 break;
5494 default:
5495 break;
5496 }
5497 }
5498
Ville Syrjälä282740f2013-09-04 18:30:03 +03005499 if (INTEL_INFO(dev)->gen < 4)
5500 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5501
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005502 intel_get_pipe_timings(crtc, pipe_config);
5503
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005504 i9xx_get_pfit_config(crtc, pipe_config);
5505
Daniel Vetter6c49f242013-06-06 12:45:25 +02005506 if (INTEL_INFO(dev)->gen >= 4) {
5507 tmp = I915_READ(DPLL_MD(crtc->pipe));
5508 pipe_config->pixel_multiplier =
5509 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5510 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005511 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005512 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5513 tmp = I915_READ(DPLL(crtc->pipe));
5514 pipe_config->pixel_multiplier =
5515 ((tmp & SDVO_MULTIPLIER_MASK)
5516 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5517 } else {
5518 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5519 * port and will be fixed up in the encoder->get_config
5520 * function. */
5521 pipe_config->pixel_multiplier = 1;
5522 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005523 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5524 if (!IS_VALLEYVIEW(dev)) {
5525 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5526 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005527 } else {
5528 /* Mask out read-only status bits. */
5529 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5530 DPLL_PORTC_READY_MASK |
5531 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005532 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005533
Jesse Barnesacbec812013-09-20 11:29:32 -07005534 if (IS_VALLEYVIEW(dev))
5535 vlv_crtc_clock_get(crtc, pipe_config);
5536 else
5537 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005538
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005539 return true;
5540}
5541
Paulo Zanonidde86e22012-12-01 12:04:25 -02005542static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005543{
5544 struct drm_i915_private *dev_priv = dev->dev_private;
5545 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005546 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005547 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005548 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005549 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005550 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005551 bool has_ck505 = false;
5552 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005553
5554 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005555 list_for_each_entry(encoder, &mode_config->encoder_list,
5556 base.head) {
5557 switch (encoder->type) {
5558 case INTEL_OUTPUT_LVDS:
5559 has_panel = true;
5560 has_lvds = true;
5561 break;
5562 case INTEL_OUTPUT_EDP:
5563 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005564 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005565 has_cpu_edp = true;
5566 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005567 }
5568 }
5569
Keith Packard99eb6a02011-09-26 14:29:12 -07005570 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005571 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005572 can_ssc = has_ck505;
5573 } else {
5574 has_ck505 = false;
5575 can_ssc = true;
5576 }
5577
Imre Deak2de69052013-05-08 13:14:04 +03005578 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5579 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005580
5581 /* Ironlake: try to setup display ref clock before DPLL
5582 * enabling. This is only under driver's control after
5583 * PCH B stepping, previous chipset stepping should be
5584 * ignoring this setting.
5585 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005586 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005587
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005588 /* As we must carefully and slowly disable/enable each source in turn,
5589 * compute the final state we want first and check if we need to
5590 * make any changes at all.
5591 */
5592 final = val;
5593 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005594 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005595 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005596 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005597 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5598
5599 final &= ~DREF_SSC_SOURCE_MASK;
5600 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5601 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005602
Keith Packard199e5d72011-09-22 12:01:57 -07005603 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005604 final |= DREF_SSC_SOURCE_ENABLE;
5605
5606 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5607 final |= DREF_SSC1_ENABLE;
5608
5609 if (has_cpu_edp) {
5610 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5611 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5612 else
5613 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5614 } else
5615 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5616 } else {
5617 final |= DREF_SSC_SOURCE_DISABLE;
5618 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5619 }
5620
5621 if (final == val)
5622 return;
5623
5624 /* Always enable nonspread source */
5625 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5626
5627 if (has_ck505)
5628 val |= DREF_NONSPREAD_CK505_ENABLE;
5629 else
5630 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5631
5632 if (has_panel) {
5633 val &= ~DREF_SSC_SOURCE_MASK;
5634 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005635
Keith Packard199e5d72011-09-22 12:01:57 -07005636 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005637 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005638 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005639 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005640 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005641 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005642
5643 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005644 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005645 POSTING_READ(PCH_DREF_CONTROL);
5646 udelay(200);
5647
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005648 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005649
5650 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005651 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005652 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005653 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005654 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005655 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005656 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005657 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005658 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005659 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005660
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005661 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005662 POSTING_READ(PCH_DREF_CONTROL);
5663 udelay(200);
5664 } else {
5665 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5666
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005667 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005668
5669 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005670 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005671
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005672 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005673 POSTING_READ(PCH_DREF_CONTROL);
5674 udelay(200);
5675
5676 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005677 val &= ~DREF_SSC_SOURCE_MASK;
5678 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005679
5680 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005681 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005682
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005683 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005684 POSTING_READ(PCH_DREF_CONTROL);
5685 udelay(200);
5686 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005687
5688 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005689}
5690
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005691static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005692{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005693 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005694
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005695 tmp = I915_READ(SOUTH_CHICKEN2);
5696 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5697 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005698
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005699 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5700 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5701 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005702
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005703 tmp = I915_READ(SOUTH_CHICKEN2);
5704 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5705 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005706
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005707 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5708 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5709 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005710}
5711
5712/* WaMPhyProgramming:hsw */
5713static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5714{
5715 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005716
5717 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5718 tmp &= ~(0xFF << 24);
5719 tmp |= (0x12 << 24);
5720 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5721
Paulo Zanonidde86e22012-12-01 12:04:25 -02005722 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5723 tmp |= (1 << 11);
5724 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5725
5726 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5727 tmp |= (1 << 11);
5728 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5729
Paulo Zanonidde86e22012-12-01 12:04:25 -02005730 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5731 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5732 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5733
5734 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5735 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5736 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5737
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005738 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5739 tmp &= ~(7 << 13);
5740 tmp |= (5 << 13);
5741 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005742
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005743 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5744 tmp &= ~(7 << 13);
5745 tmp |= (5 << 13);
5746 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005747
5748 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5749 tmp &= ~0xFF;
5750 tmp |= 0x1C;
5751 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5752
5753 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5754 tmp &= ~0xFF;
5755 tmp |= 0x1C;
5756 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5757
5758 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5759 tmp &= ~(0xFF << 16);
5760 tmp |= (0x1C << 16);
5761 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5762
5763 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5764 tmp &= ~(0xFF << 16);
5765 tmp |= (0x1C << 16);
5766 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5767
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005768 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5769 tmp |= (1 << 27);
5770 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005771
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005772 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5773 tmp |= (1 << 27);
5774 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005775
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005776 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5777 tmp &= ~(0xF << 28);
5778 tmp |= (4 << 28);
5779 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005780
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005781 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5782 tmp &= ~(0xF << 28);
5783 tmp |= (4 << 28);
5784 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005785}
5786
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005787/* Implements 3 different sequences from BSpec chapter "Display iCLK
5788 * Programming" based on the parameters passed:
5789 * - Sequence to enable CLKOUT_DP
5790 * - Sequence to enable CLKOUT_DP without spread
5791 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5792 */
5793static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5794 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005795{
5796 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005797 uint32_t reg, tmp;
5798
5799 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5800 with_spread = true;
5801 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5802 with_fdi, "LP PCH doesn't have FDI\n"))
5803 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005804
5805 mutex_lock(&dev_priv->dpio_lock);
5806
5807 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5808 tmp &= ~SBI_SSCCTL_DISABLE;
5809 tmp |= SBI_SSCCTL_PATHALT;
5810 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5811
5812 udelay(24);
5813
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005814 if (with_spread) {
5815 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5816 tmp &= ~SBI_SSCCTL_PATHALT;
5817 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005818
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005819 if (with_fdi) {
5820 lpt_reset_fdi_mphy(dev_priv);
5821 lpt_program_fdi_mphy(dev_priv);
5822 }
5823 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005824
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005825 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5826 SBI_GEN0 : SBI_DBUFF0;
5827 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5828 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5829 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005830
5831 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005832}
5833
Paulo Zanoni47701c32013-07-23 11:19:25 -03005834/* Sequence to disable CLKOUT_DP */
5835static void lpt_disable_clkout_dp(struct drm_device *dev)
5836{
5837 struct drm_i915_private *dev_priv = dev->dev_private;
5838 uint32_t reg, tmp;
5839
5840 mutex_lock(&dev_priv->dpio_lock);
5841
5842 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5843 SBI_GEN0 : SBI_DBUFF0;
5844 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5845 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5846 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5847
5848 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5849 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5850 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5851 tmp |= SBI_SSCCTL_PATHALT;
5852 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5853 udelay(32);
5854 }
5855 tmp |= SBI_SSCCTL_DISABLE;
5856 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5857 }
5858
5859 mutex_unlock(&dev_priv->dpio_lock);
5860}
5861
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005862static void lpt_init_pch_refclk(struct drm_device *dev)
5863{
5864 struct drm_mode_config *mode_config = &dev->mode_config;
5865 struct intel_encoder *encoder;
5866 bool has_vga = false;
5867
5868 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5869 switch (encoder->type) {
5870 case INTEL_OUTPUT_ANALOG:
5871 has_vga = true;
5872 break;
5873 }
5874 }
5875
Paulo Zanoni47701c32013-07-23 11:19:25 -03005876 if (has_vga)
5877 lpt_enable_clkout_dp(dev, true, true);
5878 else
5879 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005880}
5881
Paulo Zanonidde86e22012-12-01 12:04:25 -02005882/*
5883 * Initialize reference clocks when the driver loads
5884 */
5885void intel_init_pch_refclk(struct drm_device *dev)
5886{
5887 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5888 ironlake_init_pch_refclk(dev);
5889 else if (HAS_PCH_LPT(dev))
5890 lpt_init_pch_refclk(dev);
5891}
5892
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005893static int ironlake_get_refclk(struct drm_crtc *crtc)
5894{
5895 struct drm_device *dev = crtc->dev;
5896 struct drm_i915_private *dev_priv = dev->dev_private;
5897 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005898 int num_connectors = 0;
5899 bool is_lvds = false;
5900
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005901 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005902 switch (encoder->type) {
5903 case INTEL_OUTPUT_LVDS:
5904 is_lvds = true;
5905 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005906 }
5907 num_connectors++;
5908 }
5909
5910 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005911 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005912 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005913 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005914 }
5915
5916 return 120000;
5917}
5918
Daniel Vetter6ff93602013-04-19 11:24:36 +02005919static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005920{
5921 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5923 int pipe = intel_crtc->pipe;
5924 uint32_t val;
5925
Daniel Vetter78114072013-06-13 00:54:57 +02005926 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005927
Daniel Vetter965e0c42013-03-27 00:44:57 +01005928 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005929 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005930 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005931 break;
5932 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005933 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005934 break;
5935 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005936 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005937 break;
5938 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005939 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005940 break;
5941 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005942 /* Case prevented by intel_choose_pipe_bpp_dither. */
5943 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005944 }
5945
Daniel Vetterd8b32242013-04-25 17:54:44 +02005946 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005947 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5948
Daniel Vetter6ff93602013-04-19 11:24:36 +02005949 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005950 val |= PIPECONF_INTERLACED_ILK;
5951 else
5952 val |= PIPECONF_PROGRESSIVE;
5953
Daniel Vetter50f3b012013-03-27 00:44:56 +01005954 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005955 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005956
Paulo Zanonic8203562012-09-12 10:06:29 -03005957 I915_WRITE(PIPECONF(pipe), val);
5958 POSTING_READ(PIPECONF(pipe));
5959}
5960
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005961/*
5962 * Set up the pipe CSC unit.
5963 *
5964 * Currently only full range RGB to limited range RGB conversion
5965 * is supported, but eventually this should handle various
5966 * RGB<->YCbCr scenarios as well.
5967 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005968static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005969{
5970 struct drm_device *dev = crtc->dev;
5971 struct drm_i915_private *dev_priv = dev->dev_private;
5972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5973 int pipe = intel_crtc->pipe;
5974 uint16_t coeff = 0x7800; /* 1.0 */
5975
5976 /*
5977 * TODO: Check what kind of values actually come out of the pipe
5978 * with these coeff/postoff values and adjust to get the best
5979 * accuracy. Perhaps we even need to take the bpc value into
5980 * consideration.
5981 */
5982
Daniel Vetter50f3b012013-03-27 00:44:56 +01005983 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005984 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5985
5986 /*
5987 * GY/GU and RY/RU should be the other way around according
5988 * to BSpec, but reality doesn't agree. Just set them up in
5989 * a way that results in the correct picture.
5990 */
5991 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5992 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5993
5994 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5995 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5996
5997 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5998 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5999
6000 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6001 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6002 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6003
6004 if (INTEL_INFO(dev)->gen > 6) {
6005 uint16_t postoff = 0;
6006
Daniel Vetter50f3b012013-03-27 00:44:56 +01006007 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006008 postoff = (16 * (1 << 13) / 255) & 0x1fff;
6009
6010 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6011 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6012 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6013
6014 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6015 } else {
6016 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6017
Daniel Vetter50f3b012013-03-27 00:44:56 +01006018 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006019 mode |= CSC_BLACK_SCREEN_OFFSET;
6020
6021 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6022 }
6023}
6024
Daniel Vetter6ff93602013-04-19 11:24:36 +02006025static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006026{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006027 struct drm_device *dev = crtc->dev;
6028 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006030 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006031 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006032 uint32_t val;
6033
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006034 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006035
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006036 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006037 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6038
Daniel Vetter6ff93602013-04-19 11:24:36 +02006039 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006040 val |= PIPECONF_INTERLACED_ILK;
6041 else
6042 val |= PIPECONF_PROGRESSIVE;
6043
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006044 I915_WRITE(PIPECONF(cpu_transcoder), val);
6045 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006046
6047 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6048 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006049
6050 if (IS_BROADWELL(dev)) {
6051 val = 0;
6052
6053 switch (intel_crtc->config.pipe_bpp) {
6054 case 18:
6055 val |= PIPEMISC_DITHER_6_BPC;
6056 break;
6057 case 24:
6058 val |= PIPEMISC_DITHER_8_BPC;
6059 break;
6060 case 30:
6061 val |= PIPEMISC_DITHER_10_BPC;
6062 break;
6063 case 36:
6064 val |= PIPEMISC_DITHER_12_BPC;
6065 break;
6066 default:
6067 /* Case prevented by pipe_config_set_bpp. */
6068 BUG();
6069 }
6070
6071 if (intel_crtc->config.dither)
6072 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6073
6074 I915_WRITE(PIPEMISC(pipe), val);
6075 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006076}
6077
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006078static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006079 intel_clock_t *clock,
6080 bool *has_reduced_clock,
6081 intel_clock_t *reduced_clock)
6082{
6083 struct drm_device *dev = crtc->dev;
6084 struct drm_i915_private *dev_priv = dev->dev_private;
6085 struct intel_encoder *intel_encoder;
6086 int refclk;
6087 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006088 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006089
6090 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6091 switch (intel_encoder->type) {
6092 case INTEL_OUTPUT_LVDS:
6093 is_lvds = true;
6094 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006095 }
6096 }
6097
6098 refclk = ironlake_get_refclk(crtc);
6099
6100 /*
6101 * Returns a set of divisors for the desired target clock with the given
6102 * refclk, or FALSE. The returned values represent the clock equation:
6103 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6104 */
6105 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006106 ret = dev_priv->display.find_dpll(limit, crtc,
6107 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006108 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006109 if (!ret)
6110 return false;
6111
6112 if (is_lvds && dev_priv->lvds_downclock_avail) {
6113 /*
6114 * Ensure we match the reduced clock's P to the target clock.
6115 * If the clocks don't match, we can't switch the display clock
6116 * by using the FP0/FP1. In such case we will disable the LVDS
6117 * downclock feature.
6118 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006119 *has_reduced_clock =
6120 dev_priv->display.find_dpll(limit, crtc,
6121 dev_priv->lvds_downclock,
6122 refclk, clock,
6123 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006124 }
6125
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006126 return true;
6127}
6128
Paulo Zanonid4b19312012-11-29 11:29:32 -02006129int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6130{
6131 /*
6132 * Account for spread spectrum to avoid
6133 * oversubscribing the link. Max center spread
6134 * is 2.5%; use 5% for safety's sake.
6135 */
6136 u32 bps = target_clock * bpp * 21 / 20;
6137 return bps / (link_bw * 8) + 1;
6138}
6139
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006140static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006141{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006142 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006143}
6144
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006145static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006146 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006147 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006148{
6149 struct drm_crtc *crtc = &intel_crtc->base;
6150 struct drm_device *dev = crtc->dev;
6151 struct drm_i915_private *dev_priv = dev->dev_private;
6152 struct intel_encoder *intel_encoder;
6153 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006154 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006155 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006156
6157 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6158 switch (intel_encoder->type) {
6159 case INTEL_OUTPUT_LVDS:
6160 is_lvds = true;
6161 break;
6162 case INTEL_OUTPUT_SDVO:
6163 case INTEL_OUTPUT_HDMI:
6164 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006165 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006166 }
6167
6168 num_connectors++;
6169 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006170
Chris Wilsonc1858122010-12-03 21:35:48 +00006171 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006172 factor = 21;
6173 if (is_lvds) {
6174 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006175 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006176 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006177 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006178 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006179 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006180
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006181 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006182 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006183
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006184 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6185 *fp2 |= FP_CB_TUNE;
6186
Chris Wilson5eddb702010-09-11 13:48:45 +01006187 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006188
Eric Anholta07d6782011-03-30 13:01:08 -07006189 if (is_lvds)
6190 dpll |= DPLLB_MODE_LVDS;
6191 else
6192 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006193
Daniel Vetteref1b4602013-06-01 17:17:04 +02006194 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6195 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006196
6197 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006198 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006199 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006200 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006201
Eric Anholta07d6782011-03-30 13:01:08 -07006202 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006203 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006204 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006205 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006206
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006207 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006208 case 5:
6209 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6210 break;
6211 case 7:
6212 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6213 break;
6214 case 10:
6215 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6216 break;
6217 case 14:
6218 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6219 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006220 }
6221
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006222 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006223 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006224 else
6225 dpll |= PLL_REF_INPUT_DREFCLK;
6226
Daniel Vetter959e16d2013-06-05 13:34:21 +02006227 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006228}
6229
Jesse Barnes79e53942008-11-07 14:24:08 -08006230static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006231 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006232 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006233{
6234 struct drm_device *dev = crtc->dev;
6235 struct drm_i915_private *dev_priv = dev->dev_private;
6236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6237 int pipe = intel_crtc->pipe;
6238 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006239 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006240 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006241 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006242 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006243 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006244 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006245 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006246 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006247
6248 for_each_encoder_on_crtc(dev, crtc, encoder) {
6249 switch (encoder->type) {
6250 case INTEL_OUTPUT_LVDS:
6251 is_lvds = true;
6252 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006253 }
6254
6255 num_connectors++;
6256 }
6257
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006258 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6259 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6260
Daniel Vetterff9a6752013-06-01 17:16:21 +02006261 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006262 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006263 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006264 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6265 return -EINVAL;
6266 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006267 /* Compat-code for transition, will disappear. */
6268 if (!intel_crtc->config.clock_set) {
6269 intel_crtc->config.dpll.n = clock.n;
6270 intel_crtc->config.dpll.m1 = clock.m1;
6271 intel_crtc->config.dpll.m2 = clock.m2;
6272 intel_crtc->config.dpll.p1 = clock.p1;
6273 intel_crtc->config.dpll.p2 = clock.p2;
6274 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006275
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006276 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006277 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006278 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006279 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006280 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006281
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006282 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006283 &fp, &reduced_clock,
6284 has_reduced_clock ? &fp2 : NULL);
6285
Daniel Vetter959e16d2013-06-05 13:34:21 +02006286 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006287 intel_crtc->config.dpll_hw_state.fp0 = fp;
6288 if (has_reduced_clock)
6289 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6290 else
6291 intel_crtc->config.dpll_hw_state.fp1 = fp;
6292
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006293 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006294 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006295 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6296 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006297 return -EINVAL;
6298 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006299 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006300 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006301
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006302 if (intel_crtc->config.has_dp_encoder)
6303 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006304
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006305 if (is_lvds && has_reduced_clock && i915_powersave)
6306 intel_crtc->lowfreq_avail = true;
6307 else
6308 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006309
Daniel Vetter8a654f32013-06-01 17:16:22 +02006310 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006311
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006312 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006313 intel_cpu_transcoder_set_m_n(intel_crtc,
6314 &intel_crtc->config.fdi_m_n);
6315 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006316
Daniel Vetter6ff93602013-04-19 11:24:36 +02006317 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006318
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006319 /* Set up the display plane register */
6320 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006321 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006322
Daniel Vetter94352cf2012-07-05 22:51:56 +02006323 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006324
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006325 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006326}
6327
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006328static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6329 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006330{
6331 struct drm_device *dev = crtc->base.dev;
6332 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006333 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006334
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006335 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6336 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6337 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6338 & ~TU_SIZE_MASK;
6339 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6340 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6341 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6342}
6343
6344static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6345 enum transcoder transcoder,
6346 struct intel_link_m_n *m_n)
6347{
6348 struct drm_device *dev = crtc->base.dev;
6349 struct drm_i915_private *dev_priv = dev->dev_private;
6350 enum pipe pipe = crtc->pipe;
6351
6352 if (INTEL_INFO(dev)->gen >= 5) {
6353 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6354 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6355 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6356 & ~TU_SIZE_MASK;
6357 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6358 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6359 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6360 } else {
6361 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6362 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6363 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6364 & ~TU_SIZE_MASK;
6365 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6366 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6367 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6368 }
6369}
6370
6371void intel_dp_get_m_n(struct intel_crtc *crtc,
6372 struct intel_crtc_config *pipe_config)
6373{
6374 if (crtc->config.has_pch_encoder)
6375 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6376 else
6377 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6378 &pipe_config->dp_m_n);
6379}
6380
Daniel Vetter72419202013-04-04 13:28:53 +02006381static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6382 struct intel_crtc_config *pipe_config)
6383{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006384 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6385 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006386}
6387
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006388static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6389 struct intel_crtc_config *pipe_config)
6390{
6391 struct drm_device *dev = crtc->base.dev;
6392 struct drm_i915_private *dev_priv = dev->dev_private;
6393 uint32_t tmp;
6394
6395 tmp = I915_READ(PF_CTL(crtc->pipe));
6396
6397 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006398 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006399 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6400 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006401
6402 /* We currently do not free assignements of panel fitters on
6403 * ivb/hsw (since we don't use the higher upscaling modes which
6404 * differentiates them) so just WARN about this case for now. */
6405 if (IS_GEN7(dev)) {
6406 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6407 PF_PIPE_SEL_IVB(crtc->pipe));
6408 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006409 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006410}
6411
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006412static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6413 struct intel_crtc_config *pipe_config)
6414{
6415 struct drm_device *dev = crtc->base.dev;
6416 struct drm_i915_private *dev_priv = dev->dev_private;
6417 uint32_t tmp;
6418
Daniel Vettere143a212013-07-04 12:01:15 +02006419 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006420 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006421
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006422 tmp = I915_READ(PIPECONF(crtc->pipe));
6423 if (!(tmp & PIPECONF_ENABLE))
6424 return false;
6425
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006426 switch (tmp & PIPECONF_BPC_MASK) {
6427 case PIPECONF_6BPC:
6428 pipe_config->pipe_bpp = 18;
6429 break;
6430 case PIPECONF_8BPC:
6431 pipe_config->pipe_bpp = 24;
6432 break;
6433 case PIPECONF_10BPC:
6434 pipe_config->pipe_bpp = 30;
6435 break;
6436 case PIPECONF_12BPC:
6437 pipe_config->pipe_bpp = 36;
6438 break;
6439 default:
6440 break;
6441 }
6442
Daniel Vetterab9412b2013-05-03 11:49:46 +02006443 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006444 struct intel_shared_dpll *pll;
6445
Daniel Vetter88adfff2013-03-28 10:42:01 +01006446 pipe_config->has_pch_encoder = true;
6447
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006448 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6449 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6450 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006451
6452 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006453
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006454 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006455 pipe_config->shared_dpll =
6456 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006457 } else {
6458 tmp = I915_READ(PCH_DPLL_SEL);
6459 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6460 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6461 else
6462 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6463 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006464
6465 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6466
6467 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6468 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006469
6470 tmp = pipe_config->dpll_hw_state.dpll;
6471 pipe_config->pixel_multiplier =
6472 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6473 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006474
6475 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006476 } else {
6477 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006478 }
6479
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006480 intel_get_pipe_timings(crtc, pipe_config);
6481
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006482 ironlake_get_pfit_config(crtc, pipe_config);
6483
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006484 return true;
6485}
6486
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006487static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6488{
6489 struct drm_device *dev = dev_priv->dev;
6490 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6491 struct intel_crtc *crtc;
6492 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006493 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006494
6495 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
Paulo Zanoni798183c2013-12-06 20:29:01 -02006496 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006497 pipe_name(crtc->pipe));
6498
6499 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6500 WARN(plls->spll_refcount, "SPLL enabled\n");
6501 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6502 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6503 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6504 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6505 "CPU PWM1 enabled\n");
6506 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6507 "CPU PWM2 enabled\n");
6508 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6509 "PCH PWM1 enabled\n");
6510 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6511 "Utility pin enabled\n");
6512 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6513
6514 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6515 val = I915_READ(DEIMR);
Paulo Zanoni6806e632013-11-21 13:47:24 -02006516 WARN((val | DE_PCH_EVENT_IVB) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006517 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6518 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006519 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006520 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6521 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6522}
6523
6524/*
6525 * This function implements pieces of two sequences from BSpec:
6526 * - Sequence for display software to disable LCPLL
6527 * - Sequence for display software to allow package C8+
6528 * The steps implemented here are just the steps that actually touch the LCPLL
6529 * register. Callers should take care of disabling all the display engine
6530 * functions, doing the mode unset, fixing interrupts, etc.
6531 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006532static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6533 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006534{
6535 uint32_t val;
6536
6537 assert_can_disable_lcpll(dev_priv);
6538
6539 val = I915_READ(LCPLL_CTL);
6540
6541 if (switch_to_fclk) {
6542 val |= LCPLL_CD_SOURCE_FCLK;
6543 I915_WRITE(LCPLL_CTL, val);
6544
6545 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6546 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6547 DRM_ERROR("Switching to FCLK failed\n");
6548
6549 val = I915_READ(LCPLL_CTL);
6550 }
6551
6552 val |= LCPLL_PLL_DISABLE;
6553 I915_WRITE(LCPLL_CTL, val);
6554 POSTING_READ(LCPLL_CTL);
6555
6556 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6557 DRM_ERROR("LCPLL still locked\n");
6558
6559 val = I915_READ(D_COMP);
6560 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006561 mutex_lock(&dev_priv->rps.hw_lock);
6562 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6563 DRM_ERROR("Failed to disable D_COMP\n");
6564 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006565 POSTING_READ(D_COMP);
6566 ndelay(100);
6567
6568 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6569 DRM_ERROR("D_COMP RCOMP still in progress\n");
6570
6571 if (allow_power_down) {
6572 val = I915_READ(LCPLL_CTL);
6573 val |= LCPLL_POWER_DOWN_ALLOW;
6574 I915_WRITE(LCPLL_CTL, val);
6575 POSTING_READ(LCPLL_CTL);
6576 }
6577}
6578
6579/*
6580 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6581 * source.
6582 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006583static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006584{
6585 uint32_t val;
6586
6587 val = I915_READ(LCPLL_CTL);
6588
6589 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6590 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6591 return;
6592
Paulo Zanoni215733f2013-08-19 13:18:07 -03006593 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6594 * we'll hang the machine! */
Deepak Sc8d9a592013-11-23 14:55:42 +05306595 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03006596
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006597 if (val & LCPLL_POWER_DOWN_ALLOW) {
6598 val &= ~LCPLL_POWER_DOWN_ALLOW;
6599 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006600 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006601 }
6602
6603 val = I915_READ(D_COMP);
6604 val |= D_COMP_COMP_FORCE;
6605 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006606 mutex_lock(&dev_priv->rps.hw_lock);
6607 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6608 DRM_ERROR("Failed to enable D_COMP\n");
6609 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006610 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006611
6612 val = I915_READ(LCPLL_CTL);
6613 val &= ~LCPLL_PLL_DISABLE;
6614 I915_WRITE(LCPLL_CTL, val);
6615
6616 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6617 DRM_ERROR("LCPLL not locked yet\n");
6618
6619 if (val & LCPLL_CD_SOURCE_FCLK) {
6620 val = I915_READ(LCPLL_CTL);
6621 val &= ~LCPLL_CD_SOURCE_FCLK;
6622 I915_WRITE(LCPLL_CTL, val);
6623
6624 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6625 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6626 DRM_ERROR("Switching back to LCPLL failed\n");
6627 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006628
Deepak Sc8d9a592013-11-23 14:55:42 +05306629 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006630}
6631
Paulo Zanonic67a4702013-08-19 13:18:09 -03006632void hsw_enable_pc8_work(struct work_struct *__work)
6633{
6634 struct drm_i915_private *dev_priv =
6635 container_of(to_delayed_work(__work), struct drm_i915_private,
6636 pc8.enable_work);
6637 struct drm_device *dev = dev_priv->dev;
6638 uint32_t val;
6639
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006640 WARN_ON(!HAS_PC8(dev));
6641
Paulo Zanonic67a4702013-08-19 13:18:09 -03006642 if (dev_priv->pc8.enabled)
6643 return;
6644
6645 DRM_DEBUG_KMS("Enabling package C8+\n");
6646
6647 dev_priv->pc8.enabled = true;
6648
6649 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6650 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6651 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6652 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6653 }
6654
6655 lpt_disable_clkout_dp(dev);
6656 hsw_pc8_disable_interrupts(dev);
6657 hsw_disable_lcpll(dev_priv, true, true);
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006658
6659 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006660}
6661
6662static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6663{
6664 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6665 WARN(dev_priv->pc8.disable_count < 1,
6666 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6667
6668 dev_priv->pc8.disable_count--;
6669 if (dev_priv->pc8.disable_count != 0)
6670 return;
6671
6672 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006673 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006674}
6675
6676static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6677{
6678 struct drm_device *dev = dev_priv->dev;
6679 uint32_t val;
6680
6681 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6682 WARN(dev_priv->pc8.disable_count < 0,
6683 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6684
6685 dev_priv->pc8.disable_count++;
6686 if (dev_priv->pc8.disable_count != 1)
6687 return;
6688
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006689 WARN_ON(!HAS_PC8(dev));
6690
Paulo Zanonic67a4702013-08-19 13:18:09 -03006691 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6692 if (!dev_priv->pc8.enabled)
6693 return;
6694
6695 DRM_DEBUG_KMS("Disabling package C8+\n");
6696
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006697 intel_runtime_pm_get(dev_priv);
6698
Paulo Zanonic67a4702013-08-19 13:18:09 -03006699 hsw_restore_lcpll(dev_priv);
6700 hsw_pc8_restore_interrupts(dev);
6701 lpt_init_pch_refclk(dev);
6702
6703 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6704 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6705 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6706 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6707 }
6708
6709 intel_prepare_ddi(dev);
6710 i915_gem_init_swizzling(dev);
6711 mutex_lock(&dev_priv->rps.hw_lock);
6712 gen6_update_ring_freq(dev);
6713 mutex_unlock(&dev_priv->rps.hw_lock);
6714 dev_priv->pc8.enabled = false;
6715}
6716
6717void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6718{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006719 if (!HAS_PC8(dev_priv->dev))
6720 return;
6721
Paulo Zanonic67a4702013-08-19 13:18:09 -03006722 mutex_lock(&dev_priv->pc8.lock);
6723 __hsw_enable_package_c8(dev_priv);
6724 mutex_unlock(&dev_priv->pc8.lock);
6725}
6726
6727void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6728{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006729 if (!HAS_PC8(dev_priv->dev))
6730 return;
6731
Paulo Zanonic67a4702013-08-19 13:18:09 -03006732 mutex_lock(&dev_priv->pc8.lock);
6733 __hsw_disable_package_c8(dev_priv);
6734 mutex_unlock(&dev_priv->pc8.lock);
6735}
6736
6737static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6738{
6739 struct drm_device *dev = dev_priv->dev;
6740 struct intel_crtc *crtc;
6741 uint32_t val;
6742
6743 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6744 if (crtc->base.enabled)
6745 return false;
6746
6747 /* This case is still possible since we have the i915.disable_power_well
6748 * parameter and also the KVMr or something else might be requesting the
6749 * power well. */
6750 val = I915_READ(HSW_PWR_WELL_DRIVER);
6751 if (val != 0) {
6752 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6753 return false;
6754 }
6755
6756 return true;
6757}
6758
6759/* Since we're called from modeset_global_resources there's no way to
6760 * symmetrically increase and decrease the refcount, so we use
6761 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6762 * or not.
6763 */
6764static void hsw_update_package_c8(struct drm_device *dev)
6765{
6766 struct drm_i915_private *dev_priv = dev->dev_private;
6767 bool allow;
6768
Chris Wilson7c6c2652013-11-18 18:32:37 -08006769 if (!HAS_PC8(dev_priv->dev))
6770 return;
6771
Paulo Zanonic67a4702013-08-19 13:18:09 -03006772 if (!i915_enable_pc8)
6773 return;
6774
6775 mutex_lock(&dev_priv->pc8.lock);
6776
6777 allow = hsw_can_enable_package_c8(dev_priv);
6778
6779 if (allow == dev_priv->pc8.requirements_met)
6780 goto done;
6781
6782 dev_priv->pc8.requirements_met = allow;
6783
6784 if (allow)
6785 __hsw_enable_package_c8(dev_priv);
6786 else
6787 __hsw_disable_package_c8(dev_priv);
6788
6789done:
6790 mutex_unlock(&dev_priv->pc8.lock);
6791}
6792
6793static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6794{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006795 if (!HAS_PC8(dev_priv->dev))
6796 return;
6797
Chris Wilson34581222013-11-18 18:32:36 -08006798 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006799 if (!dev_priv->pc8.gpu_idle) {
6800 dev_priv->pc8.gpu_idle = true;
Chris Wilson34581222013-11-18 18:32:36 -08006801 __hsw_enable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006802 }
Chris Wilson34581222013-11-18 18:32:36 -08006803 mutex_unlock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006804}
6805
6806static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6807{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006808 if (!HAS_PC8(dev_priv->dev))
6809 return;
6810
Chris Wilson34581222013-11-18 18:32:36 -08006811 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006812 if (dev_priv->pc8.gpu_idle) {
6813 dev_priv->pc8.gpu_idle = false;
Chris Wilson34581222013-11-18 18:32:36 -08006814 __hsw_disable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006815 }
Chris Wilson34581222013-11-18 18:32:36 -08006816 mutex_unlock(&dev_priv->pc8.lock);
Daniel Vetter94352cf2012-07-05 22:51:56 +02006817}
Eric Anholtf564048e2011-03-30 13:01:02 -07006818
Imre Deak6efdf352013-10-16 17:25:52 +03006819#define for_each_power_domain(domain, mask) \
6820 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6821 if ((1 << (domain)) & (mask))
6822
6823static unsigned long get_pipe_power_domains(struct drm_device *dev,
6824 enum pipe pipe, bool pfit_enabled)
6825{
6826 unsigned long mask;
6827 enum transcoder transcoder;
6828
6829 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6830
6831 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6832 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6833 if (pfit_enabled)
6834 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6835
6836 return mask;
6837}
6838
Imre Deakbaa70702013-10-25 17:36:48 +03006839void intel_display_set_init_power(struct drm_device *dev, bool enable)
6840{
6841 struct drm_i915_private *dev_priv = dev->dev_private;
6842
6843 if (dev_priv->power_domains.init_power_on == enable)
6844 return;
6845
6846 if (enable)
6847 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6848 else
6849 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6850
6851 dev_priv->power_domains.init_power_on = enable;
6852}
6853
Imre Deak4f074122013-10-16 17:25:51 +03006854static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006855{
Imre Deak6efdf352013-10-16 17:25:52 +03006856 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006857 struct intel_crtc *crtc;
6858
Imre Deak6efdf352013-10-16 17:25:52 +03006859 /*
6860 * First get all needed power domains, then put all unneeded, to avoid
6861 * any unnecessary toggling of the power wells.
6862 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006863 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006864 enum intel_display_power_domain domain;
6865
Jesse Barnes79e53942008-11-07 14:24:08 -08006866 if (!crtc->base.enabled)
6867 continue;
6868
Imre Deak6efdf352013-10-16 17:25:52 +03006869 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6870 crtc->pipe,
6871 crtc->config.pch_pfit.enabled);
6872
6873 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6874 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006875 }
6876
Imre Deak6efdf352013-10-16 17:25:52 +03006877 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6878 enum intel_display_power_domain domain;
6879
6880 for_each_power_domain(domain, crtc->enabled_power_domains)
6881 intel_display_power_put(dev, domain);
6882
6883 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6884 }
Imre Deakbaa70702013-10-25 17:36:48 +03006885
6886 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006887}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006888
Imre Deak4f074122013-10-16 17:25:51 +03006889static void haswell_modeset_global_resources(struct drm_device *dev)
6890{
6891 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006892 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006893}
6894
6895static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6896 int x, int y,
6897 struct drm_framebuffer *fb)
6898{
6899 struct drm_device *dev = crtc->dev;
6900 struct drm_i915_private *dev_priv = dev->dev_private;
6901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6902 int plane = intel_crtc->plane;
6903 int ret;
6904
Paulo Zanoni566b7342013-11-25 15:27:08 -02006905 if (!intel_ddi_pll_select(intel_crtc))
Chris Wilson560b85b2010-08-07 11:01:38 +01006906 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02006907 intel_ddi_pll_enable(intel_crtc);
Chris Wilson560b85b2010-08-07 11:01:38 +01006908
Chris Wilson560b85b2010-08-07 11:01:38 +01006909 if (intel_crtc->config.has_dp_encoder)
6910 intel_dp_set_m_n(intel_crtc);
6911
6912 intel_crtc->lowfreq_avail = false;
6913
6914 intel_set_pipe_timings(intel_crtc);
6915
6916 if (intel_crtc->config.has_pch_encoder) {
6917 intel_cpu_transcoder_set_m_n(intel_crtc,
6918 &intel_crtc->config.fdi_m_n);
6919 }
6920
6921 haswell_set_pipeconf(crtc);
6922
6923 intel_set_pipe_csc(crtc);
6924
6925 /* Set up the display plane register */
6926 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6927 POSTING_READ(DSPCNTR(plane));
6928
6929 ret = intel_pipe_set_base(crtc, x, y, fb);
6930
Chris Wilson560b85b2010-08-07 11:01:38 +01006931 return ret;
6932}
6933
6934static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6935 struct intel_crtc_config *pipe_config)
6936{
6937 struct drm_device *dev = crtc->base.dev;
6938 struct drm_i915_private *dev_priv = dev->dev_private;
6939 enum intel_display_power_domain pfit_domain;
6940 uint32_t tmp;
6941
6942 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6943 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6944
6945 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6946 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6947 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006948 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01006949 default:
6950 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006951 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6952 case TRANS_DDI_EDP_INPUT_A_ON:
6953 trans_edp_pipe = PIPE_A;
6954 break;
6955 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6956 trans_edp_pipe = PIPE_B;
6957 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01006958 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006959 trans_edp_pipe = PIPE_C;
6960 break;
6961 }
6962
Chris Wilson6b383a72010-09-13 13:54:26 +01006963 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006964 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6965 }
6966
6967 if (!intel_display_power_enabled(dev,
6968 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6969 return false;
6970
6971 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6972 if (!(tmp & PIPECONF_ENABLE))
6973 return false;
6974
6975 /*
6976 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6977 * DDI E. So just check whether this pipe is wired to DDI E and whether
6978 * the PCH transcoder is on.
6979 */
6980 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6981 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6982 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6983 pipe_config->has_pch_encoder = true;
6984
6985 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6986 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6987 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6988
6989 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6990 }
6991
Chris Wilson560b85b2010-08-07 11:01:38 +01006992 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006993
6994 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6995 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01006996 ironlake_get_pfit_config(crtc, pipe_config);
6997
6998 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6999 (I915_READ(IPS_CTL) & IPS_ENABLE);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007000
7001 pipe_config->pixel_multiplier = 1;
7002
7003 return true;
7004}
Jesse Barnes79e53942008-11-07 14:24:08 -08007005
Chris Wilson05394f32010-11-08 19:18:58 +00007006static int intel_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08007007 int x, int y,
7008 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07007009{
Daniel Vetter9256aa12012-10-31 19:26:13 +01007010 struct drm_device *dev = crtc->dev;
7011 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07007012 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07007013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007014 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07007015 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07007016 int ret;
7017
Eric Anholt0b701d22011-03-30 13:01:03 -07007018 drm_vblank_pre_modeset(dev, pipe);
7019
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007020 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7021
Jesse Barnes79e53942008-11-07 14:24:08 -08007022 drm_vblank_post_modeset(dev, pipe);
7023
Daniel Vetter9256aa12012-10-31 19:26:13 +01007024 if (ret != 0)
7025 return ret;
7026
7027 for_each_encoder_on_crtc(dev, crtc, encoder) {
7028 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7029 encoder->base.base.id,
7030 drm_get_encoder_name(&encoder->base),
7031 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02007032 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007033 }
7034
7035 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007036}
7037
Jani Nikula1a915102013-10-16 12:34:48 +03007038static struct {
7039 int clock;
7040 u32 config;
7041} hdmi_audio_clock[] = {
7042 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7043 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7044 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7045 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7046 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7047 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7048 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7049 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7050 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7051 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7052};
7053
7054/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7055static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7056{
7057 int i;
7058
7059 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7060 if (mode->clock == hdmi_audio_clock[i].clock)
7061 break;
7062 }
7063
7064 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7065 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7066 i = 1;
7067 }
7068
7069 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7070 hdmi_audio_clock[i].clock,
7071 hdmi_audio_clock[i].config);
7072
7073 return hdmi_audio_clock[i].config;
7074}
7075
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007076static bool intel_eld_uptodate(struct drm_connector *connector,
7077 int reg_eldv, uint32_t bits_eldv,
7078 int reg_elda, uint32_t bits_elda,
7079 int reg_edid)
7080{
7081 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7082 uint8_t *eld = connector->eld;
7083 uint32_t i;
7084
7085 i = I915_READ(reg_eldv);
7086 i &= bits_eldv;
7087
7088 if (!eld[0])
7089 return !i;
7090
7091 if (!i)
7092 return false;
7093
7094 i = I915_READ(reg_elda);
7095 i &= ~bits_elda;
7096 I915_WRITE(reg_elda, i);
7097
7098 for (i = 0; i < eld[2]; i++)
7099 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7100 return false;
7101
7102 return true;
7103}
7104
Wu Fengguange0dac652011-09-05 14:25:34 +08007105static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007106 struct drm_crtc *crtc,
7107 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007108{
7109 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7110 uint8_t *eld = connector->eld;
7111 uint32_t eldv;
7112 uint32_t len;
7113 uint32_t i;
7114
7115 i = I915_READ(G4X_AUD_VID_DID);
7116
7117 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7118 eldv = G4X_ELDV_DEVCL_DEVBLC;
7119 else
7120 eldv = G4X_ELDV_DEVCTG;
7121
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007122 if (intel_eld_uptodate(connector,
7123 G4X_AUD_CNTL_ST, eldv,
7124 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7125 G4X_HDMIW_HDMIEDID))
7126 return;
7127
Wu Fengguange0dac652011-09-05 14:25:34 +08007128 i = I915_READ(G4X_AUD_CNTL_ST);
7129 i &= ~(eldv | G4X_ELD_ADDR);
7130 len = (i >> 9) & 0x1f; /* ELD buffer size */
7131 I915_WRITE(G4X_AUD_CNTL_ST, i);
7132
7133 if (!eld[0])
7134 return;
7135
7136 len = min_t(uint8_t, eld[2], len);
7137 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7138 for (i = 0; i < len; i++)
7139 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7140
7141 i = I915_READ(G4X_AUD_CNTL_ST);
7142 i |= eldv;
7143 I915_WRITE(G4X_AUD_CNTL_ST, i);
7144}
7145
Wang Xingchao83358c852012-08-16 22:43:37 +08007146static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007147 struct drm_crtc *crtc,
7148 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007149{
7150 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7151 uint8_t *eld = connector->eld;
7152 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007154 uint32_t eldv;
7155 uint32_t i;
7156 int len;
7157 int pipe = to_intel_crtc(crtc)->pipe;
7158 int tmp;
7159
7160 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7161 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7162 int aud_config = HSW_AUD_CFG(pipe);
7163 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7164
7165
7166 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7167
7168 /* Audio output enable */
7169 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7170 tmp = I915_READ(aud_cntrl_st2);
7171 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7172 I915_WRITE(aud_cntrl_st2, tmp);
7173
7174 /* Wait for 1 vertical blank */
7175 intel_wait_for_vblank(dev, pipe);
7176
7177 /* Set ELD valid state */
7178 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007179 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007180 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7181 I915_WRITE(aud_cntrl_st2, tmp);
7182 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007183 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007184
7185 /* Enable HDMI mode */
7186 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007187 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007188 /* clear N_programing_enable and N_value_index */
7189 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7190 I915_WRITE(aud_config, tmp);
7191
7192 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7193
7194 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007195 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007196
7197 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7198 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7199 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7200 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007201 } else {
7202 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7203 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007204
7205 if (intel_eld_uptodate(connector,
7206 aud_cntrl_st2, eldv,
7207 aud_cntl_st, IBX_ELD_ADDRESS,
7208 hdmiw_hdmiedid))
7209 return;
7210
7211 i = I915_READ(aud_cntrl_st2);
7212 i &= ~eldv;
7213 I915_WRITE(aud_cntrl_st2, i);
7214
7215 if (!eld[0])
7216 return;
7217
7218 i = I915_READ(aud_cntl_st);
7219 i &= ~IBX_ELD_ADDRESS;
7220 I915_WRITE(aud_cntl_st, i);
7221 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7222 DRM_DEBUG_DRIVER("port num:%d\n", i);
7223
7224 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7225 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7226 for (i = 0; i < len; i++)
7227 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7228
7229 i = I915_READ(aud_cntrl_st2);
7230 i |= eldv;
7231 I915_WRITE(aud_cntrl_st2, i);
7232
7233}
7234
Wu Fengguange0dac652011-09-05 14:25:34 +08007235static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007236 struct drm_crtc *crtc,
7237 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007238{
7239 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7240 uint8_t *eld = connector->eld;
7241 uint32_t eldv;
7242 uint32_t i;
7243 int len;
7244 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007245 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007246 int aud_cntl_st;
7247 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007248 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007249
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007250 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007251 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7252 aud_config = IBX_AUD_CFG(pipe);
7253 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007254 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007255 } else if (IS_VALLEYVIEW(connector->dev)) {
7256 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7257 aud_config = VLV_AUD_CFG(pipe);
7258 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7259 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007260 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007261 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7262 aud_config = CPT_AUD_CFG(pipe);
7263 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007264 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007265 }
7266
Wang Xingchao9b138a82012-08-09 16:52:18 +08007267 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007268
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007269 if (IS_VALLEYVIEW(connector->dev)) {
7270 struct intel_encoder *intel_encoder;
7271 struct intel_digital_port *intel_dig_port;
7272
7273 intel_encoder = intel_attached_encoder(connector);
7274 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7275 i = intel_dig_port->port;
7276 } else {
7277 i = I915_READ(aud_cntl_st);
7278 i = (i >> 29) & DIP_PORT_SEL_MASK;
7279 /* DIP_Port_Select, 0x1 = PortB */
7280 }
7281
Wu Fengguange0dac652011-09-05 14:25:34 +08007282 if (!i) {
7283 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7284 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007285 eldv = IBX_ELD_VALIDB;
7286 eldv |= IBX_ELD_VALIDB << 4;
7287 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007288 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007289 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007290 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007291 }
7292
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007293 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7294 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7295 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007296 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007297 } else {
7298 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7299 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007300
7301 if (intel_eld_uptodate(connector,
7302 aud_cntrl_st2, eldv,
7303 aud_cntl_st, IBX_ELD_ADDRESS,
7304 hdmiw_hdmiedid))
7305 return;
7306
Wu Fengguange0dac652011-09-05 14:25:34 +08007307 i = I915_READ(aud_cntrl_st2);
7308 i &= ~eldv;
7309 I915_WRITE(aud_cntrl_st2, i);
7310
7311 if (!eld[0])
7312 return;
7313
Wu Fengguange0dac652011-09-05 14:25:34 +08007314 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007315 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007316 I915_WRITE(aud_cntl_st, i);
7317
7318 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7319 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7320 for (i = 0; i < len; i++)
7321 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7322
7323 i = I915_READ(aud_cntrl_st2);
7324 i |= eldv;
7325 I915_WRITE(aud_cntrl_st2, i);
7326}
7327
7328void intel_write_eld(struct drm_encoder *encoder,
7329 struct drm_display_mode *mode)
7330{
7331 struct drm_crtc *crtc = encoder->crtc;
7332 struct drm_connector *connector;
7333 struct drm_device *dev = encoder->dev;
7334 struct drm_i915_private *dev_priv = dev->dev_private;
7335
7336 connector = drm_select_eld(encoder, mode);
7337 if (!connector)
7338 return;
7339
7340 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7341 connector->base.id,
7342 drm_get_connector_name(connector),
7343 connector->encoder->base.id,
7344 drm_get_encoder_name(connector->encoder));
7345
7346 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7347
7348 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007349 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007350}
7351
Jesse Barnes79e53942008-11-07 14:24:08 -08007352static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7353{
7354 struct drm_device *dev = crtc->dev;
7355 struct drm_i915_private *dev_priv = dev->dev_private;
7356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7357 bool visible = base != 0;
7358 u32 cntl;
7359
7360 if (intel_crtc->cursor_visible == visible)
7361 return;
7362
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007363 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007364 if (visible) {
7365 /* On these chipsets we can only modify the base whilst
7366 * the cursor is disabled.
7367 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007368 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007369
7370 cntl &= ~(CURSOR_FORMAT_MASK);
7371 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7372 cntl |= CURSOR_ENABLE |
7373 CURSOR_GAMMA_ENABLE |
7374 CURSOR_FORMAT_ARGB;
7375 } else
7376 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007377 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007378
7379 intel_crtc->cursor_visible = visible;
7380}
7381
7382static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7383{
7384 struct drm_device *dev = crtc->dev;
7385 struct drm_i915_private *dev_priv = dev->dev_private;
7386 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7387 int pipe = intel_crtc->pipe;
7388 bool visible = base != 0;
7389
7390 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007391 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007392 if (base) {
7393 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7394 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7395 cntl |= pipe << 28; /* Connect to correct pipe */
7396 } else {
7397 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7398 cntl |= CURSOR_MODE_DISABLE;
7399 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007400 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007401
7402 intel_crtc->cursor_visible = visible;
7403 }
7404 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007405 POSTING_READ(CURCNTR(pipe));
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007406 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007407 POSTING_READ(CURBASE(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007408}
7409
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007410static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7411{
7412 struct drm_device *dev = crtc->dev;
7413 struct drm_i915_private *dev_priv = dev->dev_private;
7414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7415 int pipe = intel_crtc->pipe;
7416 bool visible = base != 0;
7417
7418 if (intel_crtc->cursor_visible != visible) {
7419 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7420 if (base) {
7421 cntl &= ~CURSOR_MODE;
7422 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7423 } else {
7424 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7425 cntl |= CURSOR_MODE_DISABLE;
7426 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007427 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007428 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007429 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7430 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007431 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7432
7433 intel_crtc->cursor_visible = visible;
7434 }
7435 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007436 POSTING_READ(CURCNTR_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007437 I915_WRITE(CURBASE_IVB(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007438 POSTING_READ(CURBASE_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007439}
7440
Jesse Barnes79e53942008-11-07 14:24:08 -08007441/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7442static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7443 bool on)
7444{
7445 struct drm_device *dev = crtc->dev;
7446 struct drm_i915_private *dev_priv = dev->dev_private;
7447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7448 int pipe = intel_crtc->pipe;
7449 int x = intel_crtc->cursor_x;
7450 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007451 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007452 bool visible;
7453
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007454 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007455 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007456
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007457 if (x >= intel_crtc->config.pipe_src_w)
7458 base = 0;
7459
7460 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007461 base = 0;
7462
7463 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007464 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007465 base = 0;
7466
7467 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7468 x = -x;
7469 }
7470 pos |= x << CURSOR_X_SHIFT;
7471
7472 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007473 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007474 base = 0;
7475
7476 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7477 y = -y;
7478 }
7479 pos |= y << CURSOR_Y_SHIFT;
7480
7481 visible = base != 0;
7482 if (!visible && !intel_crtc->cursor_visible)
7483 return;
7484
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007485 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007486 I915_WRITE(CURPOS_IVB(pipe), pos);
7487 ivb_update_cursor(crtc, base);
7488 } else {
7489 I915_WRITE(CURPOS(pipe), pos);
7490 if (IS_845G(dev) || IS_I865G(dev))
7491 i845_update_cursor(crtc, base);
7492 else
7493 i9xx_update_cursor(crtc, base);
7494 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007495}
7496
7497static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7498 struct drm_file *file,
7499 uint32_t handle,
7500 uint32_t width, uint32_t height)
7501{
7502 struct drm_device *dev = crtc->dev;
7503 struct drm_i915_private *dev_priv = dev->dev_private;
7504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007505 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007506 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007507 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007508
Jesse Barnes79e53942008-11-07 14:24:08 -08007509 /* if we want to turn off the cursor ignore width and height */
7510 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007511 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007512 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007513 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007514 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007515 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007516 }
7517
7518 /* Currently we only support 64x64 cursors */
7519 if (width != 64 || height != 64) {
7520 DRM_ERROR("we currently only support 64x64 cursors\n");
7521 return -EINVAL;
7522 }
7523
Chris Wilson05394f32010-11-08 19:18:58 +00007524 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007525 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007526 return -ENOENT;
7527
Chris Wilson05394f32010-11-08 19:18:58 +00007528 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007529 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007530 ret = -ENOMEM;
7531 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007532 }
7533
Dave Airlie71acb5e2008-12-30 20:31:46 +10007534 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007535 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007536 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007537 unsigned alignment;
7538
Chris Wilsond9e86c02010-11-10 16:40:20 +00007539 if (obj->tiling_mode) {
7540 DRM_ERROR("cursor cannot be tiled\n");
7541 ret = -EINVAL;
7542 goto fail_locked;
7543 }
7544
Chris Wilson693db182013-03-05 14:52:39 +00007545 /* Note that the w/a also requires 2 PTE of padding following
7546 * the bo. We currently fill all unused PTE with the shadow
7547 * page and so we should always have valid PTE following the
7548 * cursor preventing the VT-d warning.
7549 */
7550 alignment = 0;
7551 if (need_vtd_wa(dev))
7552 alignment = 64*1024;
7553
7554 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007555 if (ret) {
7556 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007557 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007558 }
7559
Chris Wilsond9e86c02010-11-10 16:40:20 +00007560 ret = i915_gem_object_put_fence(obj);
7561 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007562 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007563 goto fail_unpin;
7564 }
7565
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007566 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007567 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007568 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007569 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007570 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7571 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007572 if (ret) {
7573 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007574 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007575 }
Chris Wilson05394f32010-11-08 19:18:58 +00007576 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007577 }
7578
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007579 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007580 I915_WRITE(CURSIZE, (height << 12) | width);
7581
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007582 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007583 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007584 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007585 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007586 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7587 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007588 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007589 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007590 }
Jesse Barnes80824002009-09-10 15:28:06 -07007591
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007592 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007593
7594 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007595 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007596 intel_crtc->cursor_width = width;
7597 intel_crtc->cursor_height = height;
7598
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007599 if (intel_crtc->active)
7600 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007601
Jesse Barnes79e53942008-11-07 14:24:08 -08007602 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007603fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007604 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007605fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007606 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007607fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007608 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007609 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007610}
7611
7612static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7613{
Jesse Barnes79e53942008-11-07 14:24:08 -08007614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007615
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007616 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7617 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007618
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007619 if (intel_crtc->active)
7620 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007621
7622 return 0;
7623}
7624
Jesse Barnes79e53942008-11-07 14:24:08 -08007625static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007626 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007627{
James Simmons72034252010-08-03 01:33:19 +01007628 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007630
James Simmons72034252010-08-03 01:33:19 +01007631 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007632 intel_crtc->lut_r[i] = red[i] >> 8;
7633 intel_crtc->lut_g[i] = green[i] >> 8;
7634 intel_crtc->lut_b[i] = blue[i] >> 8;
7635 }
7636
7637 intel_crtc_load_lut(crtc);
7638}
7639
Jesse Barnes79e53942008-11-07 14:24:08 -08007640/* VESA 640x480x72Hz mode to set on the pipe */
7641static struct drm_display_mode load_detect_mode = {
7642 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7643 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7644};
7645
Chris Wilsond2dff872011-04-19 08:36:26 +01007646static struct drm_framebuffer *
7647intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007648 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007649 struct drm_i915_gem_object *obj)
7650{
7651 struct intel_framebuffer *intel_fb;
7652 int ret;
7653
7654 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7655 if (!intel_fb) {
7656 drm_gem_object_unreference_unlocked(&obj->base);
7657 return ERR_PTR(-ENOMEM);
7658 }
7659
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007660 ret = i915_mutex_lock_interruptible(dev);
7661 if (ret)
7662 goto err;
7663
Chris Wilsond2dff872011-04-19 08:36:26 +01007664 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007665 mutex_unlock(&dev->struct_mutex);
7666 if (ret)
7667 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007668
7669 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007670err:
7671 drm_gem_object_unreference_unlocked(&obj->base);
7672 kfree(intel_fb);
7673
7674 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007675}
7676
7677static u32
7678intel_framebuffer_pitch_for_width(int width, int bpp)
7679{
7680 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7681 return ALIGN(pitch, 64);
7682}
7683
7684static u32
7685intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7686{
7687 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7688 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7689}
7690
7691static struct drm_framebuffer *
7692intel_framebuffer_create_for_mode(struct drm_device *dev,
7693 struct drm_display_mode *mode,
7694 int depth, int bpp)
7695{
7696 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007697 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007698
7699 obj = i915_gem_alloc_object(dev,
7700 intel_framebuffer_size_for_mode(mode, bpp));
7701 if (obj == NULL)
7702 return ERR_PTR(-ENOMEM);
7703
7704 mode_cmd.width = mode->hdisplay;
7705 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007706 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7707 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007708 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007709
7710 return intel_framebuffer_create(dev, &mode_cmd, obj);
7711}
7712
7713static struct drm_framebuffer *
7714mode_fits_in_fbdev(struct drm_device *dev,
7715 struct drm_display_mode *mode)
7716{
Daniel Vetter4520f532013-10-09 09:18:51 +02007717#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007718 struct drm_i915_private *dev_priv = dev->dev_private;
7719 struct drm_i915_gem_object *obj;
7720 struct drm_framebuffer *fb;
7721
7722 if (dev_priv->fbdev == NULL)
7723 return NULL;
7724
7725 obj = dev_priv->fbdev->ifb.obj;
7726 if (obj == NULL)
7727 return NULL;
7728
7729 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007730 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7731 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007732 return NULL;
7733
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007734 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007735 return NULL;
7736
7737 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007738#else
7739 return NULL;
7740#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007741}
7742
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007743bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007744 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007745 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007746{
7747 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007748 struct intel_encoder *intel_encoder =
7749 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007750 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007751 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007752 struct drm_crtc *crtc = NULL;
7753 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007754 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007755 int i = -1;
7756
Chris Wilsond2dff872011-04-19 08:36:26 +01007757 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7758 connector->base.id, drm_get_connector_name(connector),
7759 encoder->base.id, drm_get_encoder_name(encoder));
7760
Jesse Barnes79e53942008-11-07 14:24:08 -08007761 /*
7762 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007763 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007764 * - if the connector already has an assigned crtc, use it (but make
7765 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007766 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007767 * - try to find the first unused crtc that can drive this connector,
7768 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007769 */
7770
7771 /* See if we already have a CRTC for this connector */
7772 if (encoder->crtc) {
7773 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007774
Daniel Vetter7b240562012-12-12 00:35:33 +01007775 mutex_lock(&crtc->mutex);
7776
Daniel Vetter24218aa2012-08-12 19:27:11 +02007777 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007778 old->load_detect_temp = false;
7779
7780 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007781 if (connector->dpms != DRM_MODE_DPMS_ON)
7782 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007783
Chris Wilson71731882011-04-19 23:10:58 +01007784 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007785 }
7786
7787 /* Find an unused one (if possible) */
7788 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7789 i++;
7790 if (!(encoder->possible_crtcs & (1 << i)))
7791 continue;
7792 if (!possible_crtc->enabled) {
7793 crtc = possible_crtc;
7794 break;
7795 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007796 }
7797
7798 /*
7799 * If we didn't find an unused CRTC, don't use any.
7800 */
7801 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007802 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7803 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007804 }
7805
Daniel Vetter7b240562012-12-12 00:35:33 +01007806 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007807 intel_encoder->new_crtc = to_intel_crtc(crtc);
7808 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007809
7810 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007811 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007812 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007813 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007814
Chris Wilson64927112011-04-20 07:25:26 +01007815 if (!mode)
7816 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007817
Chris Wilsond2dff872011-04-19 08:36:26 +01007818 /* We need a framebuffer large enough to accommodate all accesses
7819 * that the plane may generate whilst we perform load detection.
7820 * We can not rely on the fbcon either being present (we get called
7821 * during its initialisation to detect all boot displays, or it may
7822 * not even exist) or that it is large enough to satisfy the
7823 * requested mode.
7824 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007825 fb = mode_fits_in_fbdev(dev, mode);
7826 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007827 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007828 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7829 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007830 } else
7831 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007832 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007833 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007834 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007835 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007836 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007837
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007838 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007839 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007840 if (old->release_fb)
7841 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007842 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007843 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007844 }
Chris Wilson71731882011-04-19 23:10:58 +01007845
Jesse Barnes79e53942008-11-07 14:24:08 -08007846 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007847 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007848 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007849}
7850
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007851void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007852 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007853{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007854 struct intel_encoder *intel_encoder =
7855 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007856 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007857 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007858
Chris Wilsond2dff872011-04-19 08:36:26 +01007859 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7860 connector->base.id, drm_get_connector_name(connector),
7861 encoder->base.id, drm_get_encoder_name(encoder));
7862
Chris Wilson8261b192011-04-19 23:18:09 +01007863 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007864 to_intel_connector(connector)->new_encoder = NULL;
7865 intel_encoder->new_crtc = NULL;
7866 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007867
Daniel Vetter36206362012-12-10 20:42:17 +01007868 if (old->release_fb) {
7869 drm_framebuffer_unregister_private(old->release_fb);
7870 drm_framebuffer_unreference(old->release_fb);
7871 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007872
Daniel Vetter67c96402013-01-23 16:25:09 +00007873 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007874 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007875 }
7876
Eric Anholtc751ce42010-03-25 11:48:48 -07007877 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007878 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7879 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007880
7881 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007882}
7883
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007884static int i9xx_pll_refclk(struct drm_device *dev,
7885 const struct intel_crtc_config *pipe_config)
7886{
7887 struct drm_i915_private *dev_priv = dev->dev_private;
7888 u32 dpll = pipe_config->dpll_hw_state.dpll;
7889
7890 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007891 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007892 else if (HAS_PCH_SPLIT(dev))
7893 return 120000;
7894 else if (!IS_GEN2(dev))
7895 return 96000;
7896 else
7897 return 48000;
7898}
7899
Jesse Barnes79e53942008-11-07 14:24:08 -08007900/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007901static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7902 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007903{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007904 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007905 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007906 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007907 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007908 u32 fp;
7909 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007910 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007911
7912 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007913 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007914 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007915 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007916
7917 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007918 if (IS_PINEVIEW(dev)) {
7919 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7920 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007921 } else {
7922 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7923 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7924 }
7925
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007926 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007927 if (IS_PINEVIEW(dev))
7928 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7929 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007930 else
7931 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007932 DPLL_FPA01_P1_POST_DIV_SHIFT);
7933
7934 switch (dpll & DPLL_MODE_MASK) {
7935 case DPLLB_MODE_DAC_SERIAL:
7936 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7937 5 : 10;
7938 break;
7939 case DPLLB_MODE_LVDS:
7940 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7941 7 : 14;
7942 break;
7943 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007944 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007945 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007946 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007947 }
7948
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007949 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007950 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007951 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007952 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007953 } else {
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02007954 u32 lvds = I915_READ(LVDS);
7955 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08007956
7957 if (is_lvds) {
7958 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7959 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02007960
7961 if (lvds & LVDS_CLKB_POWER_UP)
7962 clock.p2 = 7;
7963 else
7964 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007965 } else {
7966 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7967 clock.p1 = 2;
7968 else {
7969 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7970 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7971 }
7972 if (dpll & PLL_P2_DIVIDE_BY_4)
7973 clock.p2 = 4;
7974 else
7975 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007976 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007977
7978 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007979 }
7980
Ville Syrjälä18442d02013-09-13 16:00:08 +03007981 /*
7982 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007983 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007984 * encoder's get_config() function.
7985 */
7986 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007987}
7988
Ville Syrjälä6878da02013-09-13 15:59:11 +03007989int intel_dotclock_calculate(int link_freq,
7990 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007991{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007992 /*
7993 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007994 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007995 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007996 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007997 *
7998 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007999 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008000 */
8001
Ville Syrjälä6878da02013-09-13 15:59:11 +03008002 if (!m_n->link_n)
8003 return 0;
8004
8005 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8006}
8007
Ville Syrjälä18442d02013-09-13 16:00:08 +03008008static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8009 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008010{
8011 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008012
8013 /* read out port_clock from the DPLL */
8014 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008015
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008016 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008017 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008018 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008019 * agree once we know their relationship in the encoder's
8020 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008021 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008022 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008023 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8024 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008025}
8026
8027/** Returns the currently programmed mode of the given pipe. */
8028struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8029 struct drm_crtc *crtc)
8030{
Jesse Barnes548f2452011-02-17 10:40:53 -08008031 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008033 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008034 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008035 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008036 int htot = I915_READ(HTOTAL(cpu_transcoder));
8037 int hsync = I915_READ(HSYNC(cpu_transcoder));
8038 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8039 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008040 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008041
8042 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8043 if (!mode)
8044 return NULL;
8045
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008046 /*
8047 * Construct a pipe_config sufficient for getting the clock info
8048 * back out of crtc_clock_get.
8049 *
8050 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8051 * to use a real value here instead.
8052 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008053 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008054 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008055 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8056 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8057 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008058 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8059
Ville Syrjälä773ae032013-09-23 17:48:20 +03008060 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008061 mode->hdisplay = (htot & 0xffff) + 1;
8062 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8063 mode->hsync_start = (hsync & 0xffff) + 1;
8064 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8065 mode->vdisplay = (vtot & 0xffff) + 1;
8066 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8067 mode->vsync_start = (vsync & 0xffff) + 1;
8068 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8069
8070 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008071
8072 return mode;
8073}
8074
Daniel Vetter3dec0092010-08-20 21:40:52 +02008075static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008076{
8077 struct drm_device *dev = crtc->dev;
8078 drm_i915_private_t *dev_priv = dev->dev_private;
8079 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8080 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008081 int dpll_reg = DPLL(pipe);
8082 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008083
Eric Anholtbad720f2009-10-22 16:11:14 -07008084 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008085 return;
8086
8087 if (!dev_priv->lvds_downclock_avail)
8088 return;
8089
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008090 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008091 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008092 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008093
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008094 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008095
8096 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 Barnesdbdc6472010-12-30 09:36:39 -08008099
Jesse Barnes652c3932009-08-17 13:31:43 -07008100 dpll = I915_READ(dpll_reg);
8101 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008102 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008103 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008104}
8105
8106static void intel_decrease_pllclock(struct drm_crtc *crtc)
8107{
8108 struct drm_device *dev = crtc->dev;
8109 drm_i915_private_t *dev_priv = dev->dev_private;
8110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008111
Eric Anholtbad720f2009-10-22 16:11:14 -07008112 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008113 return;
8114
8115 if (!dev_priv->lvds_downclock_avail)
8116 return;
8117
8118 /*
8119 * Since this is called by a timer, we should never get here in
8120 * the manual case.
8121 */
8122 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008123 int pipe = intel_crtc->pipe;
8124 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008125 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008126
Zhao Yakui44d98a62009-10-09 11:39:40 +08008127 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008128
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008129 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008130
Chris Wilson074b5e12012-05-02 12:07:06 +01008131 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008132 dpll |= DISPLAY_RATE_SELECT_FPA1;
8133 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008134 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008135 dpll = I915_READ(dpll_reg);
8136 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008137 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008138 }
8139
8140}
8141
Chris Wilsonf047e392012-07-21 12:31:41 +01008142void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008143{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008144 struct drm_i915_private *dev_priv = dev->dev_private;
8145
8146 hsw_package_c8_gpu_busy(dev_priv);
8147 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008148}
8149
8150void intel_mark_idle(struct drm_device *dev)
8151{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008152 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008153 struct drm_crtc *crtc;
8154
Paulo Zanonic67a4702013-08-19 13:18:09 -03008155 hsw_package_c8_gpu_idle(dev_priv);
8156
Chris Wilson725a5b52013-01-08 11:02:57 +00008157 if (!i915_powersave)
8158 return;
8159
8160 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8161 if (!crtc->fb)
8162 continue;
8163
8164 intel_decrease_pllclock(crtc);
8165 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008166
8167 if (dev_priv->info->gen >= 6)
8168 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008169}
8170
Chris Wilsonc65355b2013-06-06 16:53:41 -03008171void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8172 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008173{
8174 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008175 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008176
8177 if (!i915_powersave)
8178 return;
8179
Jesse Barnes652c3932009-08-17 13:31:43 -07008180 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008181 if (!crtc->fb)
8182 continue;
8183
Chris Wilsonc65355b2013-06-06 16:53:41 -03008184 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8185 continue;
8186
8187 intel_increase_pllclock(crtc);
8188 if (ring && intel_fbc_enabled(dev))
8189 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008190 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008191}
8192
Jesse Barnes79e53942008-11-07 14:24:08 -08008193static void intel_crtc_destroy(struct drm_crtc *crtc)
8194{
8195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008196 struct drm_device *dev = crtc->dev;
8197 struct intel_unpin_work *work;
8198 unsigned long flags;
8199
8200 spin_lock_irqsave(&dev->event_lock, flags);
8201 work = intel_crtc->unpin_work;
8202 intel_crtc->unpin_work = NULL;
8203 spin_unlock_irqrestore(&dev->event_lock, flags);
8204
8205 if (work) {
8206 cancel_work_sync(&work->work);
8207 kfree(work);
8208 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008209
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008210 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8211
Jesse Barnes79e53942008-11-07 14:24:08 -08008212 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008213
Jesse Barnes79e53942008-11-07 14:24:08 -08008214 kfree(intel_crtc);
8215}
8216
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008217static void intel_unpin_work_fn(struct work_struct *__work)
8218{
8219 struct intel_unpin_work *work =
8220 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008221 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008222
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008223 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008224 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008225 drm_gem_object_unreference(&work->pending_flip_obj->base);
8226 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008227
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008228 intel_update_fbc(dev);
8229 mutex_unlock(&dev->struct_mutex);
8230
8231 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8232 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8233
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008234 kfree(work);
8235}
8236
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008237static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008238 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008239{
8240 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8242 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008243 unsigned long flags;
8244
8245 /* Ignore early vblank irqs */
8246 if (intel_crtc == NULL)
8247 return;
8248
8249 spin_lock_irqsave(&dev->event_lock, flags);
8250 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008251
8252 /* Ensure we don't miss a work->pending update ... */
8253 smp_rmb();
8254
8255 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008256 spin_unlock_irqrestore(&dev->event_lock, flags);
8257 return;
8258 }
8259
Chris Wilsone7d841c2012-12-03 11:36:30 +00008260 /* and that the unpin work is consistent wrt ->pending. */
8261 smp_rmb();
8262
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008263 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008264
Rob Clark45a066e2012-10-08 14:50:40 -05008265 if (work->event)
8266 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008267
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008268 drm_vblank_put(dev, intel_crtc->pipe);
8269
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008270 spin_unlock_irqrestore(&dev->event_lock, flags);
8271
Daniel Vetter2c10d572012-12-20 21:24:07 +01008272 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008273
8274 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008275
8276 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008277}
8278
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008279void intel_finish_page_flip(struct drm_device *dev, int pipe)
8280{
8281 drm_i915_private_t *dev_priv = dev->dev_private;
8282 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8283
Mario Kleiner49b14a52010-12-09 07:00:07 +01008284 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008285}
8286
8287void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8288{
8289 drm_i915_private_t *dev_priv = dev->dev_private;
8290 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8291
Mario Kleiner49b14a52010-12-09 07:00:07 +01008292 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008293}
8294
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008295void intel_prepare_page_flip(struct drm_device *dev, int plane)
8296{
8297 drm_i915_private_t *dev_priv = dev->dev_private;
8298 struct intel_crtc *intel_crtc =
8299 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8300 unsigned long flags;
8301
Chris Wilsone7d841c2012-12-03 11:36:30 +00008302 /* NB: An MMIO update of the plane base pointer will also
8303 * generate a page-flip completion irq, i.e. every modeset
8304 * is also accompanied by a spurious intel_prepare_page_flip().
8305 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008306 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008307 if (intel_crtc->unpin_work)
8308 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008309 spin_unlock_irqrestore(&dev->event_lock, flags);
8310}
8311
Chris Wilsone7d841c2012-12-03 11:36:30 +00008312inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8313{
8314 /* Ensure that the work item is consistent when activating it ... */
8315 smp_wmb();
8316 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8317 /* and that it is marked active as soon as the irq could fire. */
8318 smp_wmb();
8319}
8320
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008321static int intel_gen2_queue_flip(struct drm_device *dev,
8322 struct drm_crtc *crtc,
8323 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008324 struct drm_i915_gem_object *obj,
8325 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008326{
8327 struct drm_i915_private *dev_priv = dev->dev_private;
8328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008329 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008330 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008331 int ret;
8332
Daniel Vetter6d90c952012-04-26 23:28:05 +02008333 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008334 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008335 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008336
Daniel Vetter6d90c952012-04-26 23:28:05 +02008337 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008338 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008339 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008340
8341 /* Can't queue multiple flips, so wait for the previous
8342 * one to finish before executing the next.
8343 */
8344 if (intel_crtc->plane)
8345 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8346 else
8347 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008348 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8349 intel_ring_emit(ring, MI_NOOP);
8350 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8351 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8352 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008353 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008354 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008355
8356 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008357 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008358 return 0;
8359
8360err_unpin:
8361 intel_unpin_fb_obj(obj);
8362err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008363 return ret;
8364}
8365
8366static int intel_gen3_queue_flip(struct drm_device *dev,
8367 struct drm_crtc *crtc,
8368 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008369 struct drm_i915_gem_object *obj,
8370 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008371{
8372 struct drm_i915_private *dev_priv = dev->dev_private;
8373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008374 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008375 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008376 int ret;
8377
Daniel Vetter6d90c952012-04-26 23:28:05 +02008378 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008379 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008380 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008381
Daniel Vetter6d90c952012-04-26 23:28:05 +02008382 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008383 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008384 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008385
8386 if (intel_crtc->plane)
8387 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8388 else
8389 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008390 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8391 intel_ring_emit(ring, MI_NOOP);
8392 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8393 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8394 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008395 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008396 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008397
Chris Wilsone7d841c2012-12-03 11:36:30 +00008398 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008399 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008400 return 0;
8401
8402err_unpin:
8403 intel_unpin_fb_obj(obj);
8404err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008405 return ret;
8406}
8407
8408static int intel_gen4_queue_flip(struct drm_device *dev,
8409 struct drm_crtc *crtc,
8410 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008411 struct drm_i915_gem_object *obj,
8412 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008413{
8414 struct drm_i915_private *dev_priv = dev->dev_private;
8415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8416 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008417 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008418 int ret;
8419
Daniel Vetter6d90c952012-04-26 23:28:05 +02008420 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008421 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008422 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008423
Daniel Vetter6d90c952012-04-26 23:28:05 +02008424 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008425 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008426 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008427
8428 /* i965+ uses the linear or tiled offsets from the
8429 * Display Registers (which do not change across a page-flip)
8430 * so we need only reprogram the base address.
8431 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008432 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8433 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8434 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008435 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008436 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008437 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008438
8439 /* XXX Enabling the panel-fitter across page-flip is so far
8440 * untested on non-native modes, so ignore it for now.
8441 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8442 */
8443 pf = 0;
8444 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008445 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008446
8447 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008448 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008449 return 0;
8450
8451err_unpin:
8452 intel_unpin_fb_obj(obj);
8453err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008454 return ret;
8455}
8456
8457static int intel_gen6_queue_flip(struct drm_device *dev,
8458 struct drm_crtc *crtc,
8459 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008460 struct drm_i915_gem_object *obj,
8461 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008462{
8463 struct drm_i915_private *dev_priv = dev->dev_private;
8464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008465 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008466 uint32_t pf, pipesrc;
8467 int ret;
8468
Daniel Vetter6d90c952012-04-26 23:28:05 +02008469 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008470 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008471 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008472
Daniel Vetter6d90c952012-04-26 23:28:05 +02008473 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008474 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008475 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008476
Daniel Vetter6d90c952012-04-26 23:28:05 +02008477 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8478 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8479 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008480 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008481
Chris Wilson99d9acd2012-04-17 20:37:00 +01008482 /* Contrary to the suggestions in the documentation,
8483 * "Enable Panel Fitter" does not seem to be required when page
8484 * flipping with a non-native mode, and worse causes a normal
8485 * modeset to fail.
8486 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8487 */
8488 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008489 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008490 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008491
8492 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008493 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008494 return 0;
8495
8496err_unpin:
8497 intel_unpin_fb_obj(obj);
8498err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008499 return ret;
8500}
8501
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008502static int intel_gen7_queue_flip(struct drm_device *dev,
8503 struct drm_crtc *crtc,
8504 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008505 struct drm_i915_gem_object *obj,
8506 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008507{
8508 struct drm_i915_private *dev_priv = dev->dev_private;
8509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008510 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008511 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008512 int len, ret;
8513
8514 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008515 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008516 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008517
8518 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8519 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008520 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008521
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008522 switch(intel_crtc->plane) {
8523 case PLANE_A:
8524 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8525 break;
8526 case PLANE_B:
8527 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8528 break;
8529 case PLANE_C:
8530 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8531 break;
8532 default:
8533 WARN_ONCE(1, "unknown plane in flip command\n");
8534 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008535 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008536 }
8537
Chris Wilsonffe74d72013-08-26 20:58:12 +01008538 len = 4;
8539 if (ring->id == RCS)
8540 len += 6;
8541
8542 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008543 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008544 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008545
Chris Wilsonffe74d72013-08-26 20:58:12 +01008546 /* Unmask the flip-done completion message. Note that the bspec says that
8547 * we should do this for both the BCS and RCS, and that we must not unmask
8548 * more than one flip event at any time (or ensure that one flip message
8549 * can be sent by waiting for flip-done prior to queueing new flips).
8550 * Experimentation says that BCS works despite DERRMR masking all
8551 * flip-done completion events and that unmasking all planes at once
8552 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8553 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8554 */
8555 if (ring->id == RCS) {
8556 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8557 intel_ring_emit(ring, DERRMR);
8558 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8559 DERRMR_PIPEB_PRI_FLIP_DONE |
8560 DERRMR_PIPEC_PRI_FLIP_DONE));
8561 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8562 intel_ring_emit(ring, DERRMR);
8563 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8564 }
8565
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008566 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008567 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008568 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008569 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008570
8571 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008572 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008573 return 0;
8574
8575err_unpin:
8576 intel_unpin_fb_obj(obj);
8577err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008578 return ret;
8579}
8580
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008581static int intel_default_queue_flip(struct drm_device *dev,
8582 struct drm_crtc *crtc,
8583 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008584 struct drm_i915_gem_object *obj,
8585 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008586{
8587 return -ENODEV;
8588}
8589
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008590static int intel_crtc_page_flip(struct drm_crtc *crtc,
8591 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008592 struct drm_pending_vblank_event *event,
8593 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008594{
8595 struct drm_device *dev = crtc->dev;
8596 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008597 struct drm_framebuffer *old_fb = crtc->fb;
8598 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008599 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8600 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008601 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008602 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008603
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008604 /* Can't change pixel format via MI display flips. */
8605 if (fb->pixel_format != crtc->fb->pixel_format)
8606 return -EINVAL;
8607
8608 /*
8609 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8610 * Note that pitch changes could also affect these register.
8611 */
8612 if (INTEL_INFO(dev)->gen > 3 &&
8613 (fb->offsets[0] != crtc->fb->offsets[0] ||
8614 fb->pitches[0] != crtc->fb->pitches[0]))
8615 return -EINVAL;
8616
Daniel Vetterb14c5672013-09-19 12:18:32 +02008617 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008618 if (work == NULL)
8619 return -ENOMEM;
8620
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008621 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008622 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008623 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008624 INIT_WORK(&work->work, intel_unpin_work_fn);
8625
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008626 ret = drm_vblank_get(dev, intel_crtc->pipe);
8627 if (ret)
8628 goto free_work;
8629
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008630 /* We borrow the event spin lock for protecting unpin_work */
8631 spin_lock_irqsave(&dev->event_lock, flags);
8632 if (intel_crtc->unpin_work) {
8633 spin_unlock_irqrestore(&dev->event_lock, flags);
8634 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008635 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008636
8637 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008638 return -EBUSY;
8639 }
8640 intel_crtc->unpin_work = work;
8641 spin_unlock_irqrestore(&dev->event_lock, flags);
8642
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008643 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8644 flush_workqueue(dev_priv->wq);
8645
Chris Wilson79158102012-05-23 11:13:58 +01008646 ret = i915_mutex_lock_interruptible(dev);
8647 if (ret)
8648 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008649
Jesse Barnes75dfca82010-02-10 15:09:44 -08008650 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008651 drm_gem_object_reference(&work->old_fb_obj->base);
8652 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008653
8654 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008655
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008656 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008657
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008658 work->enable_stall_check = true;
8659
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008660 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008661 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008662
Keith Packarded8d1972013-07-22 18:49:58 -07008663 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008664 if (ret)
8665 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008666
Chris Wilson7782de32011-07-08 12:22:41 +01008667 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008668 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008669 mutex_unlock(&dev->struct_mutex);
8670
Jesse Barnese5510fa2010-07-01 16:48:37 -07008671 trace_i915_flip_request(intel_crtc->plane, obj);
8672
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008673 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008674
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008675cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008676 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008677 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008678 drm_gem_object_unreference(&work->old_fb_obj->base);
8679 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008680 mutex_unlock(&dev->struct_mutex);
8681
Chris Wilson79158102012-05-23 11:13:58 +01008682cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008683 spin_lock_irqsave(&dev->event_lock, flags);
8684 intel_crtc->unpin_work = NULL;
8685 spin_unlock_irqrestore(&dev->event_lock, flags);
8686
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008687 drm_vblank_put(dev, intel_crtc->pipe);
8688free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008689 kfree(work);
8690
8691 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008692}
8693
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008694static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008695 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8696 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008697};
8698
Daniel Vetter50f56112012-07-02 09:35:43 +02008699static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8700 struct drm_crtc *crtc)
8701{
8702 struct drm_device *dev;
8703 struct drm_crtc *tmp;
8704 int crtc_mask = 1;
8705
8706 WARN(!crtc, "checking null crtc?\n");
8707
8708 dev = crtc->dev;
8709
8710 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8711 if (tmp == crtc)
8712 break;
8713 crtc_mask <<= 1;
8714 }
8715
8716 if (encoder->possible_crtcs & crtc_mask)
8717 return true;
8718 return false;
8719}
8720
Daniel Vetter9a935852012-07-05 22:34:27 +02008721/**
8722 * intel_modeset_update_staged_output_state
8723 *
8724 * Updates the staged output configuration state, e.g. after we've read out the
8725 * current hw state.
8726 */
8727static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8728{
8729 struct intel_encoder *encoder;
8730 struct intel_connector *connector;
8731
8732 list_for_each_entry(connector, &dev->mode_config.connector_list,
8733 base.head) {
8734 connector->new_encoder =
8735 to_intel_encoder(connector->base.encoder);
8736 }
8737
8738 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8739 base.head) {
8740 encoder->new_crtc =
8741 to_intel_crtc(encoder->base.crtc);
8742 }
8743}
8744
8745/**
8746 * intel_modeset_commit_output_state
8747 *
8748 * This function copies the stage display pipe configuration to the real one.
8749 */
8750static void intel_modeset_commit_output_state(struct drm_device *dev)
8751{
8752 struct intel_encoder *encoder;
8753 struct intel_connector *connector;
8754
8755 list_for_each_entry(connector, &dev->mode_config.connector_list,
8756 base.head) {
8757 connector->base.encoder = &connector->new_encoder->base;
8758 }
8759
8760 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8761 base.head) {
8762 encoder->base.crtc = &encoder->new_crtc->base;
8763 }
8764}
8765
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008766static void
8767connected_sink_compute_bpp(struct intel_connector * connector,
8768 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008769{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008770 int bpp = pipe_config->pipe_bpp;
8771
8772 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8773 connector->base.base.id,
8774 drm_get_connector_name(&connector->base));
8775
8776 /* Don't use an invalid EDID bpc value */
8777 if (connector->base.display_info.bpc &&
8778 connector->base.display_info.bpc * 3 < bpp) {
8779 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8780 bpp, connector->base.display_info.bpc*3);
8781 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8782 }
8783
8784 /* Clamp bpp to 8 on screens without EDID 1.4 */
8785 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8786 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8787 bpp);
8788 pipe_config->pipe_bpp = 24;
8789 }
8790}
8791
8792static int
8793compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8794 struct drm_framebuffer *fb,
8795 struct intel_crtc_config *pipe_config)
8796{
8797 struct drm_device *dev = crtc->base.dev;
8798 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008799 int bpp;
8800
Daniel Vetterd42264b2013-03-28 16:38:08 +01008801 switch (fb->pixel_format) {
8802 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008803 bpp = 8*3; /* since we go through a colormap */
8804 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008805 case DRM_FORMAT_XRGB1555:
8806 case DRM_FORMAT_ARGB1555:
8807 /* checked in intel_framebuffer_init already */
8808 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8809 return -EINVAL;
8810 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008811 bpp = 6*3; /* min is 18bpp */
8812 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008813 case DRM_FORMAT_XBGR8888:
8814 case DRM_FORMAT_ABGR8888:
8815 /* checked in intel_framebuffer_init already */
8816 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8817 return -EINVAL;
8818 case DRM_FORMAT_XRGB8888:
8819 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008820 bpp = 8*3;
8821 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008822 case DRM_FORMAT_XRGB2101010:
8823 case DRM_FORMAT_ARGB2101010:
8824 case DRM_FORMAT_XBGR2101010:
8825 case DRM_FORMAT_ABGR2101010:
8826 /* checked in intel_framebuffer_init already */
8827 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008828 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008829 bpp = 10*3;
8830 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008831 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008832 default:
8833 DRM_DEBUG_KMS("unsupported depth\n");
8834 return -EINVAL;
8835 }
8836
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008837 pipe_config->pipe_bpp = bpp;
8838
8839 /* Clamp display bpp to EDID value */
8840 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008841 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008842 if (!connector->new_encoder ||
8843 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008844 continue;
8845
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008846 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008847 }
8848
8849 return bpp;
8850}
8851
Daniel Vetter644db712013-09-19 14:53:58 +02008852static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8853{
8854 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8855 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008856 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008857 mode->crtc_hdisplay, mode->crtc_hsync_start,
8858 mode->crtc_hsync_end, mode->crtc_htotal,
8859 mode->crtc_vdisplay, mode->crtc_vsync_start,
8860 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8861}
8862
Daniel Vetterc0b03412013-05-28 12:05:54 +02008863static void intel_dump_pipe_config(struct intel_crtc *crtc,
8864 struct intel_crtc_config *pipe_config,
8865 const char *context)
8866{
8867 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8868 context, pipe_name(crtc->pipe));
8869
8870 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8871 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8872 pipe_config->pipe_bpp, pipe_config->dither);
8873 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8874 pipe_config->has_pch_encoder,
8875 pipe_config->fdi_lanes,
8876 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8877 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8878 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008879 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8880 pipe_config->has_dp_encoder,
8881 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8882 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8883 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008884 DRM_DEBUG_KMS("requested mode:\n");
8885 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8886 DRM_DEBUG_KMS("adjusted mode:\n");
8887 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008888 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008889 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008890 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8891 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008892 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8893 pipe_config->gmch_pfit.control,
8894 pipe_config->gmch_pfit.pgm_ratios,
8895 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008896 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008897 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008898 pipe_config->pch_pfit.size,
8899 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008900 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008901 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008902}
8903
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008904static bool check_encoder_cloning(struct drm_crtc *crtc)
8905{
8906 int num_encoders = 0;
8907 bool uncloneable_encoders = false;
8908 struct intel_encoder *encoder;
8909
8910 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8911 base.head) {
8912 if (&encoder->new_crtc->base != crtc)
8913 continue;
8914
8915 num_encoders++;
8916 if (!encoder->cloneable)
8917 uncloneable_encoders = true;
8918 }
8919
8920 return !(num_encoders > 1 && uncloneable_encoders);
8921}
8922
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008923static struct intel_crtc_config *
8924intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008925 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008926 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008927{
8928 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008929 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008930 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008931 int plane_bpp, ret = -EINVAL;
8932 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008933
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008934 if (!check_encoder_cloning(crtc)) {
8935 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8936 return ERR_PTR(-EINVAL);
8937 }
8938
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008939 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8940 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008941 return ERR_PTR(-ENOMEM);
8942
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008943 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8944 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008945
Daniel Vettere143a212013-07-04 12:01:15 +02008946 pipe_config->cpu_transcoder =
8947 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008948 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008949
Imre Deak2960bc92013-07-30 13:36:32 +03008950 /*
8951 * Sanitize sync polarity flags based on requested ones. If neither
8952 * positive or negative polarity is requested, treat this as meaning
8953 * negative polarity.
8954 */
8955 if (!(pipe_config->adjusted_mode.flags &
8956 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8957 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8958
8959 if (!(pipe_config->adjusted_mode.flags &
8960 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8961 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8962
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008963 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8964 * plane pixel format and any sink constraints into account. Returns the
8965 * source plane bpp so that dithering can be selected on mismatches
8966 * after encoders and crtc also have had their say. */
8967 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8968 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008969 if (plane_bpp < 0)
8970 goto fail;
8971
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008972 /*
8973 * Determine the real pipe dimensions. Note that stereo modes can
8974 * increase the actual pipe size due to the frame doubling and
8975 * insertion of additional space for blanks between the frame. This
8976 * is stored in the crtc timings. We use the requested mode to do this
8977 * computation to clearly distinguish it from the adjusted mode, which
8978 * can be changed by the connectors in the below retry loop.
8979 */
8980 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8981 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8982 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8983
Daniel Vettere29c22c2013-02-21 00:00:16 +01008984encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008985 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008986 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008987 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008988
Daniel Vetter135c81b2013-07-21 21:37:09 +02008989 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008990 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008991
Daniel Vetter7758a112012-07-08 19:40:39 +02008992 /* Pass our mode to the connectors and the CRTC to give them a chance to
8993 * adjust it according to limitations or connector properties, and also
8994 * a chance to reject the mode entirely.
8995 */
8996 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8997 base.head) {
8998
8999 if (&encoder->new_crtc->base != crtc)
9000 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009001
Daniel Vetterefea6e82013-07-21 21:36:59 +02009002 if (!(encoder->compute_config(encoder, pipe_config))) {
9003 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009004 goto fail;
9005 }
9006 }
9007
Daniel Vetterff9a6752013-06-01 17:16:21 +02009008 /* Set default port clock if not overwritten by the encoder. Needs to be
9009 * done afterwards in case the encoder adjusts the mode. */
9010 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009011 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9012 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009013
Daniel Vettera43f6e02013-06-07 23:10:32 +02009014 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009015 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009016 DRM_DEBUG_KMS("CRTC fixup failed\n");
9017 goto fail;
9018 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009019
9020 if (ret == RETRY) {
9021 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9022 ret = -EINVAL;
9023 goto fail;
9024 }
9025
9026 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9027 retry = false;
9028 goto encoder_retry;
9029 }
9030
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009031 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9032 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9033 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9034
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009035 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009036fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009037 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009038 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009039}
9040
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009041/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9042 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9043static void
9044intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9045 unsigned *prepare_pipes, unsigned *disable_pipes)
9046{
9047 struct intel_crtc *intel_crtc;
9048 struct drm_device *dev = crtc->dev;
9049 struct intel_encoder *encoder;
9050 struct intel_connector *connector;
9051 struct drm_crtc *tmp_crtc;
9052
9053 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9054
9055 /* Check which crtcs have changed outputs connected to them, these need
9056 * to be part of the prepare_pipes mask. We don't (yet) support global
9057 * modeset across multiple crtcs, so modeset_pipes will only have one
9058 * bit set at most. */
9059 list_for_each_entry(connector, &dev->mode_config.connector_list,
9060 base.head) {
9061 if (connector->base.encoder == &connector->new_encoder->base)
9062 continue;
9063
9064 if (connector->base.encoder) {
9065 tmp_crtc = connector->base.encoder->crtc;
9066
9067 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9068 }
9069
9070 if (connector->new_encoder)
9071 *prepare_pipes |=
9072 1 << connector->new_encoder->new_crtc->pipe;
9073 }
9074
9075 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9076 base.head) {
9077 if (encoder->base.crtc == &encoder->new_crtc->base)
9078 continue;
9079
9080 if (encoder->base.crtc) {
9081 tmp_crtc = encoder->base.crtc;
9082
9083 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9084 }
9085
9086 if (encoder->new_crtc)
9087 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9088 }
9089
9090 /* Check for any pipes that will be fully disabled ... */
9091 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9092 base.head) {
9093 bool used = false;
9094
9095 /* Don't try to disable disabled crtcs. */
9096 if (!intel_crtc->base.enabled)
9097 continue;
9098
9099 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9100 base.head) {
9101 if (encoder->new_crtc == intel_crtc)
9102 used = true;
9103 }
9104
9105 if (!used)
9106 *disable_pipes |= 1 << intel_crtc->pipe;
9107 }
9108
9109
9110 /* set_mode is also used to update properties on life display pipes. */
9111 intel_crtc = to_intel_crtc(crtc);
9112 if (crtc->enabled)
9113 *prepare_pipes |= 1 << intel_crtc->pipe;
9114
Daniel Vetterb6c51642013-04-12 18:48:43 +02009115 /*
9116 * For simplicity do a full modeset on any pipe where the output routing
9117 * changed. We could be more clever, but that would require us to be
9118 * more careful with calling the relevant encoder->mode_set functions.
9119 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009120 if (*prepare_pipes)
9121 *modeset_pipes = *prepare_pipes;
9122
9123 /* ... and mask these out. */
9124 *modeset_pipes &= ~(*disable_pipes);
9125 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009126
9127 /*
9128 * HACK: We don't (yet) fully support global modesets. intel_set_config
9129 * obies this rule, but the modeset restore mode of
9130 * intel_modeset_setup_hw_state does not.
9131 */
9132 *modeset_pipes &= 1 << intel_crtc->pipe;
9133 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009134
9135 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9136 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009137}
9138
Daniel Vetterea9d7582012-07-10 10:42:52 +02009139static bool intel_crtc_in_use(struct drm_crtc *crtc)
9140{
9141 struct drm_encoder *encoder;
9142 struct drm_device *dev = crtc->dev;
9143
9144 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9145 if (encoder->crtc == crtc)
9146 return true;
9147
9148 return false;
9149}
9150
9151static void
9152intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9153{
9154 struct intel_encoder *intel_encoder;
9155 struct intel_crtc *intel_crtc;
9156 struct drm_connector *connector;
9157
9158 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9159 base.head) {
9160 if (!intel_encoder->base.crtc)
9161 continue;
9162
9163 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9164
9165 if (prepare_pipes & (1 << intel_crtc->pipe))
9166 intel_encoder->connectors_active = false;
9167 }
9168
9169 intel_modeset_commit_output_state(dev);
9170
9171 /* Update computed state. */
9172 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9173 base.head) {
9174 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9175 }
9176
9177 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9178 if (!connector->encoder || !connector->encoder->crtc)
9179 continue;
9180
9181 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9182
9183 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009184 struct drm_property *dpms_property =
9185 dev->mode_config.dpms_property;
9186
Daniel Vetterea9d7582012-07-10 10:42:52 +02009187 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009188 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009189 dpms_property,
9190 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009191
9192 intel_encoder = to_intel_encoder(connector->encoder);
9193 intel_encoder->connectors_active = true;
9194 }
9195 }
9196
9197}
9198
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009199static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009200{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009201 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009202
9203 if (clock1 == clock2)
9204 return true;
9205
9206 if (!clock1 || !clock2)
9207 return false;
9208
9209 diff = abs(clock1 - clock2);
9210
9211 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9212 return true;
9213
9214 return false;
9215}
9216
Daniel Vetter25c5b262012-07-08 22:08:04 +02009217#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9218 list_for_each_entry((intel_crtc), \
9219 &(dev)->mode_config.crtc_list, \
9220 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009221 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009222
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009223static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009224intel_pipe_config_compare(struct drm_device *dev,
9225 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009226 struct intel_crtc_config *pipe_config)
9227{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009228#define PIPE_CONF_CHECK_X(name) \
9229 if (current_config->name != pipe_config->name) { \
9230 DRM_ERROR("mismatch in " #name " " \
9231 "(expected 0x%08x, found 0x%08x)\n", \
9232 current_config->name, \
9233 pipe_config->name); \
9234 return false; \
9235 }
9236
Daniel Vetter08a24032013-04-19 11:25:34 +02009237#define PIPE_CONF_CHECK_I(name) \
9238 if (current_config->name != pipe_config->name) { \
9239 DRM_ERROR("mismatch in " #name " " \
9240 "(expected %i, found %i)\n", \
9241 current_config->name, \
9242 pipe_config->name); \
9243 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009244 }
9245
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009246#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9247 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009248 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009249 "(expected %i, found %i)\n", \
9250 current_config->name & (mask), \
9251 pipe_config->name & (mask)); \
9252 return false; \
9253 }
9254
Ville Syrjälä5e550652013-09-06 23:29:07 +03009255#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9256 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9257 DRM_ERROR("mismatch in " #name " " \
9258 "(expected %i, found %i)\n", \
9259 current_config->name, \
9260 pipe_config->name); \
9261 return false; \
9262 }
9263
Daniel Vetterbb760062013-06-06 14:55:52 +02009264#define PIPE_CONF_QUIRK(quirk) \
9265 ((current_config->quirks | pipe_config->quirks) & (quirk))
9266
Daniel Vettereccb1402013-05-22 00:50:22 +02009267 PIPE_CONF_CHECK_I(cpu_transcoder);
9268
Daniel Vetter08a24032013-04-19 11:25:34 +02009269 PIPE_CONF_CHECK_I(has_pch_encoder);
9270 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009271 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9272 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9273 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9274 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9275 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009276
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009277 PIPE_CONF_CHECK_I(has_dp_encoder);
9278 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9279 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9280 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9281 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9282 PIPE_CONF_CHECK_I(dp_m_n.tu);
9283
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009284 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9285 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9286 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9287 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9288 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9289 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9290
9291 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9292 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9293 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9294 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9295 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9296 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9297
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009298 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009299
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009300 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9301 DRM_MODE_FLAG_INTERLACE);
9302
Daniel Vetterbb760062013-06-06 14:55:52 +02009303 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9304 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9305 DRM_MODE_FLAG_PHSYNC);
9306 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9307 DRM_MODE_FLAG_NHSYNC);
9308 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9309 DRM_MODE_FLAG_PVSYNC);
9310 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9311 DRM_MODE_FLAG_NVSYNC);
9312 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009313
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009314 PIPE_CONF_CHECK_I(pipe_src_w);
9315 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009316
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009317 PIPE_CONF_CHECK_I(gmch_pfit.control);
9318 /* pfit ratios are autocomputed by the hw on gen4+ */
9319 if (INTEL_INFO(dev)->gen < 4)
9320 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9321 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009322 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9323 if (current_config->pch_pfit.enabled) {
9324 PIPE_CONF_CHECK_I(pch_pfit.pos);
9325 PIPE_CONF_CHECK_I(pch_pfit.size);
9326 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009327
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009328 PIPE_CONF_CHECK_I(ips_enabled);
9329
Ville Syrjälä282740f2013-09-04 18:30:03 +03009330 PIPE_CONF_CHECK_I(double_wide);
9331
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009332 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009333 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009334 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009335 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9336 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009337
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009338 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9339 PIPE_CONF_CHECK_I(pipe_bpp);
9340
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009341 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01009342 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009343 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9344 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03009345
Daniel Vetter66e985c2013-06-05 13:34:20 +02009346#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009347#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009348#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009349#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009350#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009351
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009352 return true;
9353}
9354
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009355static void
9356check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009357{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009358 struct intel_connector *connector;
9359
9360 list_for_each_entry(connector, &dev->mode_config.connector_list,
9361 base.head) {
9362 /* This also checks the encoder/connector hw state with the
9363 * ->get_hw_state callbacks. */
9364 intel_connector_check_state(connector);
9365
9366 WARN(&connector->new_encoder->base != connector->base.encoder,
9367 "connector's staged encoder doesn't match current encoder\n");
9368 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009369}
9370
9371static void
9372check_encoder_state(struct drm_device *dev)
9373{
9374 struct intel_encoder *encoder;
9375 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009376
9377 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9378 base.head) {
9379 bool enabled = false;
9380 bool active = false;
9381 enum pipe pipe, tracked_pipe;
9382
9383 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9384 encoder->base.base.id,
9385 drm_get_encoder_name(&encoder->base));
9386
9387 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9388 "encoder's stage crtc doesn't match current crtc\n");
9389 WARN(encoder->connectors_active && !encoder->base.crtc,
9390 "encoder's active_connectors set, but no crtc\n");
9391
9392 list_for_each_entry(connector, &dev->mode_config.connector_list,
9393 base.head) {
9394 if (connector->base.encoder != &encoder->base)
9395 continue;
9396 enabled = true;
9397 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9398 active = true;
9399 }
9400 WARN(!!encoder->base.crtc != enabled,
9401 "encoder's enabled state mismatch "
9402 "(expected %i, found %i)\n",
9403 !!encoder->base.crtc, enabled);
9404 WARN(active && !encoder->base.crtc,
9405 "active encoder with no crtc\n");
9406
9407 WARN(encoder->connectors_active != active,
9408 "encoder's computed active state doesn't match tracked active state "
9409 "(expected %i, found %i)\n", active, encoder->connectors_active);
9410
9411 active = encoder->get_hw_state(encoder, &pipe);
9412 WARN(active != encoder->connectors_active,
9413 "encoder's hw state doesn't match sw tracking "
9414 "(expected %i, found %i)\n",
9415 encoder->connectors_active, active);
9416
9417 if (!encoder->base.crtc)
9418 continue;
9419
9420 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9421 WARN(active && pipe != tracked_pipe,
9422 "active encoder's pipe doesn't match"
9423 "(expected %i, found %i)\n",
9424 tracked_pipe, pipe);
9425
9426 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009427}
9428
9429static void
9430check_crtc_state(struct drm_device *dev)
9431{
9432 drm_i915_private_t *dev_priv = dev->dev_private;
9433 struct intel_crtc *crtc;
9434 struct intel_encoder *encoder;
9435 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009436
9437 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9438 base.head) {
9439 bool enabled = false;
9440 bool active = false;
9441
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009442 memset(&pipe_config, 0, sizeof(pipe_config));
9443
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009444 DRM_DEBUG_KMS("[CRTC:%d]\n",
9445 crtc->base.base.id);
9446
9447 WARN(crtc->active && !crtc->base.enabled,
9448 "active crtc, but not enabled in sw tracking\n");
9449
9450 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9451 base.head) {
9452 if (encoder->base.crtc != &crtc->base)
9453 continue;
9454 enabled = true;
9455 if (encoder->connectors_active)
9456 active = true;
9457 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009458
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009459 WARN(active != crtc->active,
9460 "crtc's computed active state doesn't match tracked active state "
9461 "(expected %i, found %i)\n", active, crtc->active);
9462 WARN(enabled != crtc->base.enabled,
9463 "crtc's computed enabled state doesn't match tracked enabled state "
9464 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9465
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009466 active = dev_priv->display.get_pipe_config(crtc,
9467 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009468
9469 /* hw state is inconsistent with the pipe A quirk */
9470 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9471 active = crtc->active;
9472
Daniel Vetter6c49f242013-06-06 12:45:25 +02009473 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9474 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009475 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009476 if (encoder->base.crtc != &crtc->base)
9477 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +01009478 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009479 encoder->get_config(encoder, &pipe_config);
9480 }
9481
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009482 WARN(crtc->active != active,
9483 "crtc active state doesn't match with hw state "
9484 "(expected %i, found %i)\n", crtc->active, active);
9485
Daniel Vetterc0b03412013-05-28 12:05:54 +02009486 if (active &&
9487 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9488 WARN(1, "pipe state doesn't match!\n");
9489 intel_dump_pipe_config(crtc, &pipe_config,
9490 "[hw state]");
9491 intel_dump_pipe_config(crtc, &crtc->config,
9492 "[sw state]");
9493 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009494 }
9495}
9496
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009497static void
9498check_shared_dpll_state(struct drm_device *dev)
9499{
9500 drm_i915_private_t *dev_priv = dev->dev_private;
9501 struct intel_crtc *crtc;
9502 struct intel_dpll_hw_state dpll_hw_state;
9503 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009504
9505 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9506 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9507 int enabled_crtcs = 0, active_crtcs = 0;
9508 bool active;
9509
9510 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9511
9512 DRM_DEBUG_KMS("%s\n", pll->name);
9513
9514 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9515
9516 WARN(pll->active > pll->refcount,
9517 "more active pll users than references: %i vs %i\n",
9518 pll->active, pll->refcount);
9519 WARN(pll->active && !pll->on,
9520 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009521 WARN(pll->on && !pll->active,
9522 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009523 WARN(pll->on != active,
9524 "pll on state mismatch (expected %i, found %i)\n",
9525 pll->on, active);
9526
9527 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9528 base.head) {
9529 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9530 enabled_crtcs++;
9531 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9532 active_crtcs++;
9533 }
9534 WARN(pll->active != active_crtcs,
9535 "pll active crtcs mismatch (expected %i, found %i)\n",
9536 pll->active, active_crtcs);
9537 WARN(pll->refcount != enabled_crtcs,
9538 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9539 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009540
9541 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9542 sizeof(dpll_hw_state)),
9543 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009544 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009545}
9546
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009547void
9548intel_modeset_check_state(struct drm_device *dev)
9549{
9550 check_connector_state(dev);
9551 check_encoder_state(dev);
9552 check_crtc_state(dev);
9553 check_shared_dpll_state(dev);
9554}
9555
Ville Syrjälä18442d02013-09-13 16:00:08 +03009556void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9557 int dotclock)
9558{
9559 /*
9560 * FDI already provided one idea for the dotclock.
9561 * Yell if the encoder disagrees.
9562 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009563 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009564 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009565 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009566}
9567
Daniel Vetterf30da182013-04-11 20:22:50 +02009568static int __intel_set_mode(struct drm_crtc *crtc,
9569 struct drm_display_mode *mode,
9570 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009571{
9572 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009573 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009574 struct drm_display_mode *saved_mode, *saved_hwmode;
9575 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009576 struct intel_crtc *intel_crtc;
9577 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009578 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009579
Daniel Vettera1e22652013-09-21 00:35:38 +02009580 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009581 if (!saved_mode)
9582 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009583 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009584
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009585 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009586 &prepare_pipes, &disable_pipes);
9587
Tim Gardner3ac18232012-12-07 07:54:26 -07009588 *saved_hwmode = crtc->hwmode;
9589 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009590
Daniel Vetter25c5b262012-07-08 22:08:04 +02009591 /* Hack: Because we don't (yet) support global modeset on multiple
9592 * crtcs, we don't keep track of the new mode for more than one crtc.
9593 * Hence simply check whether any bit is set in modeset_pipes in all the
9594 * pieces of code that are not yet converted to deal with mutliple crtcs
9595 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009596 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009597 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009598 if (IS_ERR(pipe_config)) {
9599 ret = PTR_ERR(pipe_config);
9600 pipe_config = NULL;
9601
Tim Gardner3ac18232012-12-07 07:54:26 -07009602 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009603 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009604 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9605 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009606 }
9607
Jesse Barnes30a970c2013-11-04 13:48:12 -08009608 /*
9609 * See if the config requires any additional preparation, e.g.
9610 * to adjust global state with pipes off. We need to do this
9611 * here so we can get the modeset_pipe updated config for the new
9612 * mode set on this crtc. For other crtcs we need to use the
9613 * adjusted_mode bits in the crtc directly.
9614 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009615 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08009616 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9617 modeset_pipes, pipe_config);
9618
Ville Syrjäläc164f832013-11-05 22:34:12 +02009619 /* may have added more to prepare_pipes than we should */
9620 prepare_pipes &= ~disable_pipes;
9621 }
9622
Daniel Vetter460da9162013-03-27 00:44:51 +01009623 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9624 intel_crtc_disable(&intel_crtc->base);
9625
Daniel Vetterea9d7582012-07-10 10:42:52 +02009626 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9627 if (intel_crtc->base.enabled)
9628 dev_priv->display.crtc_disable(&intel_crtc->base);
9629 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009630
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009631 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9632 * to set it here already despite that we pass it down the callchain.
9633 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009634 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009635 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009636 /* mode_set/enable/disable functions rely on a correct pipe
9637 * config. */
9638 to_intel_crtc(crtc)->config = *pipe_config;
9639 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009640
Daniel Vetterea9d7582012-07-10 10:42:52 +02009641 /* Only after disabling all output pipelines that will be changed can we
9642 * update the the output configuration. */
9643 intel_modeset_update_state(dev, prepare_pipes);
9644
Daniel Vetter47fab732012-10-26 10:58:18 +02009645 if (dev_priv->display.modeset_global_resources)
9646 dev_priv->display.modeset_global_resources(dev);
9647
Daniel Vettera6778b32012-07-02 09:56:42 +02009648 /* Set up the DPLL and any encoders state that needs to adjust or depend
9649 * on the DPLL.
9650 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009651 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009652 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009653 x, y, fb);
9654 if (ret)
9655 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009656 }
9657
9658 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009659 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9660 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009661
Daniel Vetter25c5b262012-07-08 22:08:04 +02009662 if (modeset_pipes) {
9663 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009664 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009665
Daniel Vetter25c5b262012-07-08 22:08:04 +02009666 /* Calculate and store various constants which
9667 * are later needed by vblank and swap-completion
9668 * timestamping. They are derived from true hwmode.
9669 */
9670 drm_calc_timestamping_constants(crtc);
9671 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009672
9673 /* FIXME: add subpixel order */
9674done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009675 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009676 crtc->hwmode = *saved_hwmode;
9677 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009678 }
9679
Tim Gardner3ac18232012-12-07 07:54:26 -07009680out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009681 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009682 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009683 return ret;
9684}
9685
Damien Lespiaue7457a92013-08-08 22:28:59 +01009686static int intel_set_mode(struct drm_crtc *crtc,
9687 struct drm_display_mode *mode,
9688 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009689{
9690 int ret;
9691
9692 ret = __intel_set_mode(crtc, mode, x, y, fb);
9693
9694 if (ret == 0)
9695 intel_modeset_check_state(crtc->dev);
9696
9697 return ret;
9698}
9699
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009700void intel_crtc_restore_mode(struct drm_crtc *crtc)
9701{
9702 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9703}
9704
Daniel Vetter25c5b262012-07-08 22:08:04 +02009705#undef for_each_intel_crtc_masked
9706
Daniel Vetterd9e55602012-07-04 22:16:09 +02009707static void intel_set_config_free(struct intel_set_config *config)
9708{
9709 if (!config)
9710 return;
9711
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009712 kfree(config->save_connector_encoders);
9713 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009714 kfree(config);
9715}
9716
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009717static int intel_set_config_save_state(struct drm_device *dev,
9718 struct intel_set_config *config)
9719{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009720 struct drm_encoder *encoder;
9721 struct drm_connector *connector;
9722 int count;
9723
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009724 config->save_encoder_crtcs =
9725 kcalloc(dev->mode_config.num_encoder,
9726 sizeof(struct drm_crtc *), GFP_KERNEL);
9727 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009728 return -ENOMEM;
9729
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009730 config->save_connector_encoders =
9731 kcalloc(dev->mode_config.num_connector,
9732 sizeof(struct drm_encoder *), GFP_KERNEL);
9733 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009734 return -ENOMEM;
9735
9736 /* Copy data. Note that driver private data is not affected.
9737 * Should anything bad happen only the expected state is
9738 * restored, not the drivers personal bookkeeping.
9739 */
9740 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009741 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009742 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009743 }
9744
9745 count = 0;
9746 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009747 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009748 }
9749
9750 return 0;
9751}
9752
9753static void intel_set_config_restore_state(struct drm_device *dev,
9754 struct intel_set_config *config)
9755{
Daniel Vetter9a935852012-07-05 22:34:27 +02009756 struct intel_encoder *encoder;
9757 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009758 int count;
9759
9760 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009761 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9762 encoder->new_crtc =
9763 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009764 }
9765
9766 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009767 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9768 connector->new_encoder =
9769 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009770 }
9771}
9772
Imre Deake3de42b2013-05-03 19:44:07 +02009773static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009774is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009775{
9776 int i;
9777
Chris Wilson2e57f472013-07-17 12:14:40 +01009778 if (set->num_connectors == 0)
9779 return false;
9780
9781 if (WARN_ON(set->connectors == NULL))
9782 return false;
9783
9784 for (i = 0; i < set->num_connectors; i++)
9785 if (set->connectors[i]->encoder &&
9786 set->connectors[i]->encoder->crtc == set->crtc &&
9787 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009788 return true;
9789
9790 return false;
9791}
9792
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009793static void
9794intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9795 struct intel_set_config *config)
9796{
9797
9798 /* We should be able to check here if the fb has the same properties
9799 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009800 if (is_crtc_connector_off(set)) {
9801 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009802 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009803 /* If we have no fb then treat it as a full mode set */
9804 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009805 struct intel_crtc *intel_crtc =
9806 to_intel_crtc(set->crtc);
9807
9808 if (intel_crtc->active && i915_fastboot) {
9809 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9810 config->fb_changed = true;
9811 } else {
9812 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9813 config->mode_changed = true;
9814 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009815 } else if (set->fb == NULL) {
9816 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009817 } else if (set->fb->pixel_format !=
9818 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009819 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009820 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009821 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009822 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009823 }
9824
Daniel Vetter835c5872012-07-10 18:11:08 +02009825 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009826 config->fb_changed = true;
9827
9828 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9829 DRM_DEBUG_KMS("modes are different, full mode set\n");
9830 drm_mode_debug_printmodeline(&set->crtc->mode);
9831 drm_mode_debug_printmodeline(set->mode);
9832 config->mode_changed = true;
9833 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009834
9835 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9836 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009837}
9838
Daniel Vetter2e431052012-07-04 22:42:15 +02009839static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009840intel_modeset_stage_output_state(struct drm_device *dev,
9841 struct drm_mode_set *set,
9842 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009843{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009844 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009845 struct intel_connector *connector;
9846 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009847 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009848
Damien Lespiau9abdda72013-02-13 13:29:23 +00009849 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009850 * of connectors. For paranoia, double-check this. */
9851 WARN_ON(!set->fb && (set->num_connectors != 0));
9852 WARN_ON(set->fb && (set->num_connectors == 0));
9853
Daniel Vetter9a935852012-07-05 22:34:27 +02009854 list_for_each_entry(connector, &dev->mode_config.connector_list,
9855 base.head) {
9856 /* Otherwise traverse passed in connector list and get encoders
9857 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009858 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009859 if (set->connectors[ro] == &connector->base) {
9860 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009861 break;
9862 }
9863 }
9864
Daniel Vetter9a935852012-07-05 22:34:27 +02009865 /* If we disable the crtc, disable all its connectors. Also, if
9866 * the connector is on the changing crtc but not on the new
9867 * connector list, disable it. */
9868 if ((!set->fb || ro == set->num_connectors) &&
9869 connector->base.encoder &&
9870 connector->base.encoder->crtc == set->crtc) {
9871 connector->new_encoder = NULL;
9872
9873 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9874 connector->base.base.id,
9875 drm_get_connector_name(&connector->base));
9876 }
9877
9878
9879 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009880 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009881 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009882 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009883 }
9884 /* connector->new_encoder is now updated for all connectors. */
9885
9886 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009887 list_for_each_entry(connector, &dev->mode_config.connector_list,
9888 base.head) {
9889 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009890 continue;
9891
Daniel Vetter9a935852012-07-05 22:34:27 +02009892 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009893
9894 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009895 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009896 new_crtc = set->crtc;
9897 }
9898
9899 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009900 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9901 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009902 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009903 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009904 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9905
9906 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9907 connector->base.base.id,
9908 drm_get_connector_name(&connector->base),
9909 new_crtc->base.id);
9910 }
9911
9912 /* Check for any encoders that needs to be disabled. */
9913 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9914 base.head) {
9915 list_for_each_entry(connector,
9916 &dev->mode_config.connector_list,
9917 base.head) {
9918 if (connector->new_encoder == encoder) {
9919 WARN_ON(!connector->new_encoder->new_crtc);
9920
9921 goto next_encoder;
9922 }
9923 }
9924 encoder->new_crtc = NULL;
9925next_encoder:
9926 /* Only now check for crtc changes so we don't miss encoders
9927 * that will be disabled. */
9928 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009929 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009930 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009931 }
9932 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009933 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009934
Daniel Vetter2e431052012-07-04 22:42:15 +02009935 return 0;
9936}
9937
9938static int intel_crtc_set_config(struct drm_mode_set *set)
9939{
9940 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009941 struct drm_mode_set save_set;
9942 struct intel_set_config *config;
9943 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009944
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009945 BUG_ON(!set);
9946 BUG_ON(!set->crtc);
9947 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009948
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009949 /* Enforce sane interface api - has been abused by the fb helper. */
9950 BUG_ON(!set->mode && set->fb);
9951 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009952
Daniel Vetter2e431052012-07-04 22:42:15 +02009953 if (set->fb) {
9954 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9955 set->crtc->base.id, set->fb->base.id,
9956 (int)set->num_connectors, set->x, set->y);
9957 } else {
9958 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009959 }
9960
9961 dev = set->crtc->dev;
9962
9963 ret = -ENOMEM;
9964 config = kzalloc(sizeof(*config), GFP_KERNEL);
9965 if (!config)
9966 goto out_config;
9967
9968 ret = intel_set_config_save_state(dev, config);
9969 if (ret)
9970 goto out_config;
9971
9972 save_set.crtc = set->crtc;
9973 save_set.mode = &set->crtc->mode;
9974 save_set.x = set->crtc->x;
9975 save_set.y = set->crtc->y;
9976 save_set.fb = set->crtc->fb;
9977
9978 /* Compute whether we need a full modeset, only an fb base update or no
9979 * change at all. In the future we might also check whether only the
9980 * mode changed, e.g. for LVDS where we only change the panel fitter in
9981 * such cases. */
9982 intel_set_config_compute_mode_changes(set, config);
9983
Daniel Vetter9a935852012-07-05 22:34:27 +02009984 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009985 if (ret)
9986 goto fail;
9987
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009988 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009989 ret = intel_set_mode(set->crtc, set->mode,
9990 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009991 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009992 intel_crtc_wait_for_pending_flips(set->crtc);
9993
Daniel Vetter4f660f42012-07-02 09:47:37 +02009994 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009995 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009996 }
9997
Chris Wilson2d05eae2013-05-03 17:36:25 +01009998 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009999 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10000 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010001fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010002 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010003
Chris Wilson2d05eae2013-05-03 17:36:25 +010010004 /* Try to restore the config */
10005 if (config->mode_changed &&
10006 intel_set_mode(save_set.crtc, save_set.mode,
10007 save_set.x, save_set.y, save_set.fb))
10008 DRM_ERROR("failed to restore config after modeset failure\n");
10009 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010010
Daniel Vetterd9e55602012-07-04 22:16:09 +020010011out_config:
10012 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010013 return ret;
10014}
10015
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010016static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010017 .cursor_set = intel_crtc_cursor_set,
10018 .cursor_move = intel_crtc_cursor_move,
10019 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010020 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010021 .destroy = intel_crtc_destroy,
10022 .page_flip = intel_crtc_page_flip,
10023};
10024
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010025static void intel_cpu_pll_init(struct drm_device *dev)
10026{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010027 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010028 intel_ddi_pll_init(dev);
10029}
10030
Daniel Vetter53589012013-06-05 13:34:16 +020010031static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10032 struct intel_shared_dpll *pll,
10033 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010034{
Daniel Vetter53589012013-06-05 13:34:16 +020010035 uint32_t val;
10036
10037 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010038 hw_state->dpll = val;
10039 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10040 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010041
10042 return val & DPLL_VCO_ENABLE;
10043}
10044
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010045static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10046 struct intel_shared_dpll *pll)
10047{
10048 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10049 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10050}
10051
Daniel Vettere7b903d2013-06-05 13:34:14 +020010052static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10053 struct intel_shared_dpll *pll)
10054{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010055 /* PCH refclock must be enabled first */
10056 assert_pch_refclk_enabled(dev_priv);
10057
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010058 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10059
10060 /* Wait for the clocks to stabilize. */
10061 POSTING_READ(PCH_DPLL(pll->id));
10062 udelay(150);
10063
10064 /* The pixel multiplier can only be updated once the
10065 * DPLL is enabled and the clocks are stable.
10066 *
10067 * So write it again.
10068 */
10069 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10070 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010071 udelay(200);
10072}
10073
10074static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10075 struct intel_shared_dpll *pll)
10076{
10077 struct drm_device *dev = dev_priv->dev;
10078 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010079
10080 /* Make sure no transcoder isn't still depending on us. */
10081 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10082 if (intel_crtc_to_shared_dpll(crtc) == pll)
10083 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10084 }
10085
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010086 I915_WRITE(PCH_DPLL(pll->id), 0);
10087 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010088 udelay(200);
10089}
10090
Daniel Vetter46edb022013-06-05 13:34:12 +020010091static char *ibx_pch_dpll_names[] = {
10092 "PCH DPLL A",
10093 "PCH DPLL B",
10094};
10095
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010096static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010097{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010098 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010099 int i;
10100
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010101 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010102
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010103 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010104 dev_priv->shared_dplls[i].id = i;
10105 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010106 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010107 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10108 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010109 dev_priv->shared_dplls[i].get_hw_state =
10110 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010111 }
10112}
10113
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010114static void intel_shared_dpll_init(struct drm_device *dev)
10115{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010116 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010117
10118 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10119 ibx_pch_dpll_init(dev);
10120 else
10121 dev_priv->num_shared_dpll = 0;
10122
10123 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10124 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
10125 dev_priv->num_shared_dpll);
10126}
10127
Hannes Ederb358d0a2008-12-18 21:18:47 +010010128static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010129{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010130 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010131 struct intel_crtc *intel_crtc;
10132 int i;
10133
Daniel Vetter955382f2013-09-19 14:05:45 +020010134 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010135 if (intel_crtc == NULL)
10136 return;
10137
10138 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10139
10140 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010141 for (i = 0; i < 256; i++) {
10142 intel_crtc->lut_r[i] = i;
10143 intel_crtc->lut_g[i] = i;
10144 intel_crtc->lut_b[i] = i;
10145 }
10146
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010147 /*
10148 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10149 * is hooked to plane B. Hence we want plane A feeding pipe B.
10150 */
Jesse Barnes80824002009-09-10 15:28:06 -070010151 intel_crtc->pipe = pipe;
10152 intel_crtc->plane = pipe;
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010153 if (IS_MOBILE(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010154 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010155 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010156 }
10157
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010158 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10159 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10160 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10161 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10162
Jesse Barnes79e53942008-11-07 14:24:08 -080010163 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010164}
10165
Jesse Barnes752aa882013-10-31 18:55:49 +020010166enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10167{
10168 struct drm_encoder *encoder = connector->base.encoder;
10169
10170 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10171
10172 if (!encoder)
10173 return INVALID_PIPE;
10174
10175 return to_intel_crtc(encoder->crtc)->pipe;
10176}
10177
Carl Worth08d7b3d2009-04-29 14:43:54 -070010178int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010179 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010180{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010181 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010182 struct drm_mode_object *drmmode_obj;
10183 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010184
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010185 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10186 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010187
Daniel Vetterc05422d2009-08-11 16:05:30 +020010188 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10189 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010190
Daniel Vetterc05422d2009-08-11 16:05:30 +020010191 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010192 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010193 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010194 }
10195
Daniel Vetterc05422d2009-08-11 16:05:30 +020010196 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10197 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010198
Daniel Vetterc05422d2009-08-11 16:05:30 +020010199 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010200}
10201
Daniel Vetter66a92782012-07-12 20:08:18 +020010202static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010203{
Daniel Vetter66a92782012-07-12 20:08:18 +020010204 struct drm_device *dev = encoder->base.dev;
10205 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010206 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010207 int entry = 0;
10208
Daniel Vetter66a92782012-07-12 20:08:18 +020010209 list_for_each_entry(source_encoder,
10210 &dev->mode_config.encoder_list, base.head) {
10211
10212 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010213 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010214
10215 /* Intel hw has only one MUX where enocoders could be cloned. */
10216 if (encoder->cloneable && source_encoder->cloneable)
10217 index_mask |= (1 << entry);
10218
Jesse Barnes79e53942008-11-07 14:24:08 -080010219 entry++;
10220 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010221
Jesse Barnes79e53942008-11-07 14:24:08 -080010222 return index_mask;
10223}
10224
Chris Wilson4d302442010-12-14 19:21:29 +000010225static bool has_edp_a(struct drm_device *dev)
10226{
10227 struct drm_i915_private *dev_priv = dev->dev_private;
10228
10229 if (!IS_MOBILE(dev))
10230 return false;
10231
10232 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10233 return false;
10234
10235 if (IS_GEN5(dev) &&
10236 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10237 return false;
10238
10239 return true;
10240}
10241
Jesse Barnes79e53942008-11-07 14:24:08 -080010242static void intel_setup_outputs(struct drm_device *dev)
10243{
Eric Anholt725e30a2009-01-22 13:01:02 -080010244 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010245 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010246 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010247
Daniel Vetterc9093352013-06-06 22:22:47 +020010248 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010249
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010250 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010251 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010252
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010253 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010254 int found;
10255
10256 /* Haswell uses DDI functions to detect digital outputs */
10257 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10258 /* DDI A only supports eDP */
10259 if (found)
10260 intel_ddi_init(dev, PORT_A);
10261
10262 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10263 * register */
10264 found = I915_READ(SFUSE_STRAP);
10265
10266 if (found & SFUSE_STRAP_DDIB_DETECTED)
10267 intel_ddi_init(dev, PORT_B);
10268 if (found & SFUSE_STRAP_DDIC_DETECTED)
10269 intel_ddi_init(dev, PORT_C);
10270 if (found & SFUSE_STRAP_DDID_DETECTED)
10271 intel_ddi_init(dev, PORT_D);
10272 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010273 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +020010274 dpd_is_edp = intel_dpd_is_edp(dev);
10275
10276 if (has_edp_a(dev))
10277 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010278
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010279 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010280 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010281 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010282 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010283 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010284 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010285 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010286 }
10287
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010288 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010289 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010290
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010291 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010292 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010293
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010294 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010295 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010296
Daniel Vetter270b3042012-10-27 15:52:05 +020010297 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010298 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010299 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010300 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10301 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10302 PORT_B);
10303 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10304 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10305 }
10306
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010307 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10308 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10309 PORT_C);
10310 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10311 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10312 PORT_C);
10313 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010314
Jani Nikula3cfca972013-08-27 15:12:26 +030010315 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010316 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010317 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010318
Paulo Zanonie2debe92013-02-18 19:00:27 -030010319 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010320 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010321 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010322 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10323 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010324 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010325 }
Ma Ling27185ae2009-08-24 13:50:23 +080010326
Imre Deake7281ea2013-05-08 13:14:08 +030010327 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010328 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010329 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010330
10331 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010332
Paulo Zanonie2debe92013-02-18 19:00:27 -030010333 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010334 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010335 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010336 }
Ma Ling27185ae2009-08-24 13:50:23 +080010337
Paulo Zanonie2debe92013-02-18 19:00:27 -030010338 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010339
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010340 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10341 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010342 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010343 }
Imre Deake7281ea2013-05-08 13:14:08 +030010344 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010345 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010346 }
Ma Ling27185ae2009-08-24 13:50:23 +080010347
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010348 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010349 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010350 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010351 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010352 intel_dvo_init(dev);
10353
Zhenyu Wang103a1962009-11-27 11:44:36 +080010354 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010355 intel_tv_init(dev);
10356
Chris Wilson4ef69c72010-09-09 15:14:28 +010010357 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10358 encoder->base.possible_crtcs = encoder->crtc_mask;
10359 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010360 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010361 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010362
Paulo Zanonidde86e22012-12-01 12:04:25 -020010363 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010364
10365 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010366}
10367
Chris Wilsonddfe1562013-08-06 17:43:07 +010010368void intel_framebuffer_fini(struct intel_framebuffer *fb)
10369{
10370 drm_framebuffer_cleanup(&fb->base);
Daniel Vetter80075d42013-10-09 21:23:52 +020010371 WARN_ON(!fb->obj->framebuffer_references--);
Chris Wilsonddfe1562013-08-06 17:43:07 +010010372 drm_gem_object_unreference_unlocked(&fb->obj->base);
10373}
10374
Jesse Barnes79e53942008-11-07 14:24:08 -080010375static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10376{
10377 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010378
Chris Wilsonddfe1562013-08-06 17:43:07 +010010379 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010380 kfree(intel_fb);
10381}
10382
10383static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010384 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010385 unsigned int *handle)
10386{
10387 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010388 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010389
Chris Wilson05394f32010-11-08 19:18:58 +000010390 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010391}
10392
10393static const struct drm_framebuffer_funcs intel_fb_funcs = {
10394 .destroy = intel_user_framebuffer_destroy,
10395 .create_handle = intel_user_framebuffer_create_handle,
10396};
10397
Dave Airlie38651672010-03-30 05:34:13 +000010398int intel_framebuffer_init(struct drm_device *dev,
10399 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010400 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010401 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010402{
Daniel Vetter53155c02013-10-09 21:55:33 +020010403 int aligned_height, tile_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010404 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010405 int ret;
10406
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010407 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10408
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010409 if (obj->tiling_mode == I915_TILING_Y) {
10410 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010411 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010412 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010413
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010414 if (mode_cmd->pitches[0] & 63) {
10415 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10416 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010417 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010418 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010419
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010420 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10421 pitch_limit = 32*1024;
10422 } else if (INTEL_INFO(dev)->gen >= 4) {
10423 if (obj->tiling_mode)
10424 pitch_limit = 16*1024;
10425 else
10426 pitch_limit = 32*1024;
10427 } else if (INTEL_INFO(dev)->gen >= 3) {
10428 if (obj->tiling_mode)
10429 pitch_limit = 8*1024;
10430 else
10431 pitch_limit = 16*1024;
10432 } else
10433 /* XXX DSPC is limited to 4k tiled */
10434 pitch_limit = 8*1024;
10435
10436 if (mode_cmd->pitches[0] > pitch_limit) {
10437 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10438 obj->tiling_mode ? "tiled" : "linear",
10439 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010440 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010441 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010442
10443 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010444 mode_cmd->pitches[0] != obj->stride) {
10445 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10446 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010447 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010448 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010449
Ville Syrjälä57779d02012-10-31 17:50:14 +020010450 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010451 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010452 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010453 case DRM_FORMAT_RGB565:
10454 case DRM_FORMAT_XRGB8888:
10455 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010456 break;
10457 case DRM_FORMAT_XRGB1555:
10458 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010459 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010460 DRM_DEBUG("unsupported pixel format: %s\n",
10461 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010462 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010463 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010464 break;
10465 case DRM_FORMAT_XBGR8888:
10466 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010467 case DRM_FORMAT_XRGB2101010:
10468 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010469 case DRM_FORMAT_XBGR2101010:
10470 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010471 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010472 DRM_DEBUG("unsupported pixel format: %s\n",
10473 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010474 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010475 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010476 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010477 case DRM_FORMAT_YUYV:
10478 case DRM_FORMAT_UYVY:
10479 case DRM_FORMAT_YVYU:
10480 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010481 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010482 DRM_DEBUG("unsupported pixel format: %s\n",
10483 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010484 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010485 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010486 break;
10487 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010488 DRM_DEBUG("unsupported pixel format: %s\n",
10489 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010490 return -EINVAL;
10491 }
10492
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010493 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10494 if (mode_cmd->offsets[0] != 0)
10495 return -EINVAL;
10496
Daniel Vetter53155c02013-10-09 21:55:33 +020010497 tile_height = IS_GEN2(dev) ? 16 : 8;
10498 aligned_height = ALIGN(mode_cmd->height,
10499 obj->tiling_mode ? tile_height : 1);
10500 /* FIXME drm helper for size checks (especially planar formats)? */
10501 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10502 return -EINVAL;
10503
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010504 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10505 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010506 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010507
Jesse Barnes79e53942008-11-07 14:24:08 -080010508 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10509 if (ret) {
10510 DRM_ERROR("framebuffer init failed %d\n", ret);
10511 return ret;
10512 }
10513
Jesse Barnes79e53942008-11-07 14:24:08 -080010514 return 0;
10515}
10516
Jesse Barnes79e53942008-11-07 14:24:08 -080010517static struct drm_framebuffer *
10518intel_user_framebuffer_create(struct drm_device *dev,
10519 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010520 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010521{
Chris Wilson05394f32010-11-08 19:18:58 +000010522 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010523
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010524 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10525 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010526 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010527 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010528
Chris Wilsond2dff872011-04-19 08:36:26 +010010529 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010530}
10531
Daniel Vetter4520f532013-10-09 09:18:51 +020010532#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010533static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010534{
10535}
10536#endif
10537
Jesse Barnes79e53942008-11-07 14:24:08 -080010538static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010539 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010540 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010541};
10542
Jesse Barnese70236a2009-09-21 10:42:27 -070010543/* Set up chip specific display functions */
10544static void intel_init_display(struct drm_device *dev)
10545{
10546 struct drm_i915_private *dev_priv = dev->dev_private;
10547
Daniel Vetteree9300b2013-06-03 22:40:22 +020010548 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10549 dev_priv->display.find_dpll = g4x_find_best_dpll;
10550 else if (IS_VALLEYVIEW(dev))
10551 dev_priv->display.find_dpll = vlv_find_best_dpll;
10552 else if (IS_PINEVIEW(dev))
10553 dev_priv->display.find_dpll = pnv_find_best_dpll;
10554 else
10555 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10556
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010557 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010558 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010559 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010560 dev_priv->display.crtc_enable = haswell_crtc_enable;
10561 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010562 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010563 dev_priv->display.update_plane = ironlake_update_plane;
10564 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010565 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010566 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010567 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10568 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010569 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010570 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010571 } else if (IS_VALLEYVIEW(dev)) {
10572 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10573 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10574 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10575 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10576 dev_priv->display.off = i9xx_crtc_off;
10577 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010578 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010579 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010580 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010581 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10582 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010583 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010584 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010585 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010586
Jesse Barnese70236a2009-09-21 10:42:27 -070010587 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010588 if (IS_VALLEYVIEW(dev))
10589 dev_priv->display.get_display_clock_speed =
10590 valleyview_get_display_clock_speed;
10591 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010592 dev_priv->display.get_display_clock_speed =
10593 i945_get_display_clock_speed;
10594 else if (IS_I915G(dev))
10595 dev_priv->display.get_display_clock_speed =
10596 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010597 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010598 dev_priv->display.get_display_clock_speed =
10599 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010600 else if (IS_PINEVIEW(dev))
10601 dev_priv->display.get_display_clock_speed =
10602 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010603 else if (IS_I915GM(dev))
10604 dev_priv->display.get_display_clock_speed =
10605 i915gm_get_display_clock_speed;
10606 else if (IS_I865G(dev))
10607 dev_priv->display.get_display_clock_speed =
10608 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010609 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010610 dev_priv->display.get_display_clock_speed =
10611 i855_get_display_clock_speed;
10612 else /* 852, 830 */
10613 dev_priv->display.get_display_clock_speed =
10614 i830_get_display_clock_speed;
10615
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010616 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010617 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010618 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010619 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010620 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010621 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010622 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010623 } else if (IS_IVYBRIDGE(dev)) {
10624 /* FIXME: detect B0+ stepping and use auto training */
10625 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010626 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010627 dev_priv->display.modeset_global_resources =
10628 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010629 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010630 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010631 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010632 dev_priv->display.modeset_global_resources =
10633 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010634 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010635 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010636 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010637 } else if (IS_VALLEYVIEW(dev)) {
10638 dev_priv->display.modeset_global_resources =
10639 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040010640 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010641 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010642
10643 /* Default just returns -ENODEV to indicate unsupported */
10644 dev_priv->display.queue_flip = intel_default_queue_flip;
10645
10646 switch (INTEL_INFO(dev)->gen) {
10647 case 2:
10648 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10649 break;
10650
10651 case 3:
10652 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10653 break;
10654
10655 case 4:
10656 case 5:
10657 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10658 break;
10659
10660 case 6:
10661 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10662 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010663 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010664 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010665 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10666 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010667 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020010668
10669 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010670}
10671
Jesse Barnesb690e962010-07-19 13:53:12 -070010672/*
10673 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10674 * resume, or other times. This quirk makes sure that's the case for
10675 * affected systems.
10676 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010677static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010678{
10679 struct drm_i915_private *dev_priv = dev->dev_private;
10680
10681 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010682 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010683}
10684
Keith Packard435793d2011-07-12 14:56:22 -070010685/*
10686 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10687 */
10688static void quirk_ssc_force_disable(struct drm_device *dev)
10689{
10690 struct drm_i915_private *dev_priv = dev->dev_private;
10691 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010692 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010693}
10694
Carsten Emde4dca20e2012-03-15 15:56:26 +010010695/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010696 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10697 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010698 */
10699static void quirk_invert_brightness(struct drm_device *dev)
10700{
10701 struct drm_i915_private *dev_priv = dev->dev_private;
10702 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010703 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010704}
10705
10706struct intel_quirk {
10707 int device;
10708 int subsystem_vendor;
10709 int subsystem_device;
10710 void (*hook)(struct drm_device *dev);
10711};
10712
Egbert Eich5f85f1762012-10-14 15:46:38 +020010713/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10714struct intel_dmi_quirk {
10715 void (*hook)(struct drm_device *dev);
10716 const struct dmi_system_id (*dmi_id_list)[];
10717};
10718
10719static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10720{
10721 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10722 return 1;
10723}
10724
10725static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10726 {
10727 .dmi_id_list = &(const struct dmi_system_id[]) {
10728 {
10729 .callback = intel_dmi_reverse_brightness,
10730 .ident = "NCR Corporation",
10731 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10732 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10733 },
10734 },
10735 { } /* terminating entry */
10736 },
10737 .hook = quirk_invert_brightness,
10738 },
10739};
10740
Ben Widawskyc43b5632012-04-16 14:07:40 -070010741static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010742 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010743 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010744
Jesse Barnesb690e962010-07-19 13:53:12 -070010745 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10746 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10747
Jesse Barnesb690e962010-07-19 13:53:12 -070010748 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10749 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10750
Chris Wilsona4945f92013-10-08 11:16:59 +010010751 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010752 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010753
10754 /* Lenovo U160 cannot use SSC on LVDS */
10755 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010756
10757 /* Sony Vaio Y cannot use SSC on LVDS */
10758 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010759
Jani Nikulaee1452d2013-09-20 15:05:30 +030010760 /*
10761 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10762 * seem to use inverted backlight PWM.
10763 */
10764 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070010765};
10766
10767static void intel_init_quirks(struct drm_device *dev)
10768{
10769 struct pci_dev *d = dev->pdev;
10770 int i;
10771
10772 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10773 struct intel_quirk *q = &intel_quirks[i];
10774
10775 if (d->device == q->device &&
10776 (d->subsystem_vendor == q->subsystem_vendor ||
10777 q->subsystem_vendor == PCI_ANY_ID) &&
10778 (d->subsystem_device == q->subsystem_device ||
10779 q->subsystem_device == PCI_ANY_ID))
10780 q->hook(dev);
10781 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010782 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10783 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10784 intel_dmi_quirks[i].hook(dev);
10785 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010786}
10787
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010788/* Disable the VGA plane that we never use */
10789static void i915_disable_vga(struct drm_device *dev)
10790{
10791 struct drm_i915_private *dev_priv = dev->dev_private;
10792 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010793 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010794
10795 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010796 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010797 sr1 = inb(VGA_SR_DATA);
10798 outb(sr1 | 1<<5, VGA_SR_DATA);
10799 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10800 udelay(300);
10801
10802 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10803 POSTING_READ(vga_reg);
10804}
10805
Daniel Vetterf8175862012-04-10 15:50:11 +020010806void intel_modeset_init_hw(struct drm_device *dev)
10807{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010808 intel_prepare_ddi(dev);
10809
Daniel Vetterf8175862012-04-10 15:50:11 +020010810 intel_init_clock_gating(dev);
10811
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010812 intel_init_dpio(dev);
10813
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010814 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010815 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010816 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010817}
10818
Imre Deak7d708ee2013-04-17 14:04:50 +030010819void intel_modeset_suspend_hw(struct drm_device *dev)
10820{
10821 intel_suspend_hw(dev);
10822}
10823
Jesse Barnes79e53942008-11-07 14:24:08 -080010824void intel_modeset_init(struct drm_device *dev)
10825{
Jesse Barnes652c3932009-08-17 13:31:43 -070010826 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010827 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010828
10829 drm_mode_config_init(dev);
10830
10831 dev->mode_config.min_width = 0;
10832 dev->mode_config.min_height = 0;
10833
Dave Airlie019d96c2011-09-29 16:20:42 +010010834 dev->mode_config.preferred_depth = 24;
10835 dev->mode_config.prefer_shadow = 1;
10836
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010837 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010838
Jesse Barnesb690e962010-07-19 13:53:12 -070010839 intel_init_quirks(dev);
10840
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010841 intel_init_pm(dev);
10842
Ben Widawskye3c74752013-04-05 13:12:39 -070010843 if (INTEL_INFO(dev)->num_pipes == 0)
10844 return;
10845
Jesse Barnese70236a2009-09-21 10:42:27 -070010846 intel_init_display(dev);
10847
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010848 if (IS_GEN2(dev)) {
10849 dev->mode_config.max_width = 2048;
10850 dev->mode_config.max_height = 2048;
10851 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010852 dev->mode_config.max_width = 4096;
10853 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010854 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010855 dev->mode_config.max_width = 8192;
10856 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010857 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010858 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010859
Zhao Yakui28c97732009-10-09 11:39:41 +080010860 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010861 INTEL_INFO(dev)->num_pipes,
10862 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010863
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010864 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010865 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010866 for (j = 0; j < dev_priv->num_plane; j++) {
10867 ret = intel_plane_init(dev, i, j);
10868 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010869 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10870 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010871 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010872 }
10873
Jesse Barnesf42bb702013-12-16 16:34:23 -080010874 intel_init_dpio(dev);
10875
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010876 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010877 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010878
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010879 /* Just disable it once at startup */
10880 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010881 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010882
10883 /* Just in case the BIOS is doing something questionable. */
10884 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010885}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010886
Daniel Vetter24929352012-07-02 20:28:59 +020010887static void
10888intel_connector_break_all_links(struct intel_connector *connector)
10889{
10890 connector->base.dpms = DRM_MODE_DPMS_OFF;
10891 connector->base.encoder = NULL;
10892 connector->encoder->connectors_active = false;
10893 connector->encoder->base.crtc = NULL;
10894}
10895
Daniel Vetter7fad7982012-07-04 17:51:47 +020010896static void intel_enable_pipe_a(struct drm_device *dev)
10897{
10898 struct intel_connector *connector;
10899 struct drm_connector *crt = NULL;
10900 struct intel_load_detect_pipe load_detect_temp;
10901
10902 /* We can't just switch on the pipe A, we need to set things up with a
10903 * proper mode and output configuration. As a gross hack, enable pipe A
10904 * by enabling the load detect pipe once. */
10905 list_for_each_entry(connector,
10906 &dev->mode_config.connector_list,
10907 base.head) {
10908 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10909 crt = &connector->base;
10910 break;
10911 }
10912 }
10913
10914 if (!crt)
10915 return;
10916
10917 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10918 intel_release_load_detect_pipe(crt, &load_detect_temp);
10919
10920
10921}
10922
Daniel Vetterfa555832012-10-10 23:14:00 +020010923static bool
10924intel_check_plane_mapping(struct intel_crtc *crtc)
10925{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010926 struct drm_device *dev = crtc->base.dev;
10927 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010928 u32 reg, val;
10929
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010930 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010931 return true;
10932
10933 reg = DSPCNTR(!crtc->plane);
10934 val = I915_READ(reg);
10935
10936 if ((val & DISPLAY_PLANE_ENABLE) &&
10937 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10938 return false;
10939
10940 return true;
10941}
10942
Daniel Vetter24929352012-07-02 20:28:59 +020010943static void intel_sanitize_crtc(struct intel_crtc *crtc)
10944{
10945 struct drm_device *dev = crtc->base.dev;
10946 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010947 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010948
Daniel Vetter24929352012-07-02 20:28:59 +020010949 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010950 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010951 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10952
10953 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010954 * disable the crtc (and hence change the state) if it is wrong. Note
10955 * that gen4+ has a fixed plane -> pipe mapping. */
10956 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010957 struct intel_connector *connector;
10958 bool plane;
10959
Daniel Vetter24929352012-07-02 20:28:59 +020010960 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10961 crtc->base.base.id);
10962
10963 /* Pipe has the wrong plane attached and the plane is active.
10964 * Temporarily change the plane mapping and disable everything
10965 * ... */
10966 plane = crtc->plane;
10967 crtc->plane = !plane;
10968 dev_priv->display.crtc_disable(&crtc->base);
10969 crtc->plane = plane;
10970
10971 /* ... and break all links. */
10972 list_for_each_entry(connector, &dev->mode_config.connector_list,
10973 base.head) {
10974 if (connector->encoder->base.crtc != &crtc->base)
10975 continue;
10976
10977 intel_connector_break_all_links(connector);
10978 }
10979
10980 WARN_ON(crtc->active);
10981 crtc->base.enabled = false;
10982 }
Daniel Vetter24929352012-07-02 20:28:59 +020010983
Daniel Vetter7fad7982012-07-04 17:51:47 +020010984 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10985 crtc->pipe == PIPE_A && !crtc->active) {
10986 /* BIOS forgot to enable pipe A, this mostly happens after
10987 * resume. Force-enable the pipe to fix this, the update_dpms
10988 * call below we restore the pipe to the right state, but leave
10989 * the required bits on. */
10990 intel_enable_pipe_a(dev);
10991 }
10992
Daniel Vetter24929352012-07-02 20:28:59 +020010993 /* Adjust the state of the output pipe according to whether we
10994 * have active connectors/encoders. */
10995 intel_crtc_update_dpms(&crtc->base);
10996
10997 if (crtc->active != crtc->base.enabled) {
10998 struct intel_encoder *encoder;
10999
11000 /* This can happen either due to bugs in the get_hw_state
11001 * functions or because the pipe is force-enabled due to the
11002 * pipe A quirk. */
11003 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11004 crtc->base.base.id,
11005 crtc->base.enabled ? "enabled" : "disabled",
11006 crtc->active ? "enabled" : "disabled");
11007
11008 crtc->base.enabled = crtc->active;
11009
11010 /* Because we only establish the connector -> encoder ->
11011 * crtc links if something is active, this means the
11012 * crtc is now deactivated. Break the links. connector
11013 * -> encoder links are only establish when things are
11014 * actually up, hence no need to break them. */
11015 WARN_ON(crtc->active);
11016
11017 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11018 WARN_ON(encoder->connectors_active);
11019 encoder->base.crtc = NULL;
11020 }
11021 }
11022}
11023
11024static void intel_sanitize_encoder(struct intel_encoder *encoder)
11025{
11026 struct intel_connector *connector;
11027 struct drm_device *dev = encoder->base.dev;
11028
11029 /* We need to check both for a crtc link (meaning that the
11030 * encoder is active and trying to read from a pipe) and the
11031 * pipe itself being active. */
11032 bool has_active_crtc = encoder->base.crtc &&
11033 to_intel_crtc(encoder->base.crtc)->active;
11034
11035 if (encoder->connectors_active && !has_active_crtc) {
11036 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11037 encoder->base.base.id,
11038 drm_get_encoder_name(&encoder->base));
11039
11040 /* Connector is active, but has no active pipe. This is
11041 * fallout from our resume register restoring. Disable
11042 * the encoder manually again. */
11043 if (encoder->base.crtc) {
11044 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11045 encoder->base.base.id,
11046 drm_get_encoder_name(&encoder->base));
11047 encoder->disable(encoder);
11048 }
11049
11050 /* Inconsistent output/port/pipe state happens presumably due to
11051 * a bug in one of the get_hw_state functions. Or someplace else
11052 * in our code, like the register restore mess on resume. Clamp
11053 * things to off as a safer default. */
11054 list_for_each_entry(connector,
11055 &dev->mode_config.connector_list,
11056 base.head) {
11057 if (connector->encoder != encoder)
11058 continue;
11059
11060 intel_connector_break_all_links(connector);
11061 }
11062 }
11063 /* Enabled encoders without active connectors will be fixed in
11064 * the crtc fixup. */
11065}
11066
Daniel Vetter44cec742013-01-25 17:53:21 +010011067void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011068{
11069 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011070 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011071
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011072 /* This function can be called both from intel_modeset_setup_hw_state or
11073 * at a very early point in our resume sequence, where the power well
11074 * structures are not yet restored. Since this function is at a very
11075 * paranoid "someone might have enabled VGA while we were not looking"
11076 * level, just check if the power well is enabled instead of trying to
11077 * follow the "don't touch the power well if we don't need it" policy
11078 * the rest of the driver uses. */
Jesse Barnesf9e711e2013-11-25 17:15:32 +020011079 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030011080 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011081 return;
11082
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030011083 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011084 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020011085 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011086 }
11087}
11088
Daniel Vetter30e984d2013-06-05 13:34:17 +020011089static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011090{
11091 struct drm_i915_private *dev_priv = dev->dev_private;
11092 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011093 struct intel_crtc *crtc;
11094 struct intel_encoder *encoder;
11095 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011096 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011097
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011098 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11099 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011100 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011101
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011102 crtc->active = dev_priv->display.get_pipe_config(crtc,
11103 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011104
11105 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030011106 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020011107
11108 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11109 crtc->base.base.id,
11110 crtc->active ? "enabled" : "disabled");
11111 }
11112
Daniel Vetter53589012013-06-05 13:34:16 +020011113 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011114 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011115 intel_ddi_setup_hw_pll_state(dev);
11116
Daniel Vetter53589012013-06-05 13:34:16 +020011117 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11118 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11119
11120 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11121 pll->active = 0;
11122 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11123 base.head) {
11124 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11125 pll->active++;
11126 }
11127 pll->refcount = pll->active;
11128
Daniel Vetter35c95372013-07-17 06:55:04 +020011129 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11130 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011131 }
11132
Daniel Vetter24929352012-07-02 20:28:59 +020011133 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11134 base.head) {
11135 pipe = 0;
11136
11137 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011138 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11139 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011140 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011141 } else {
11142 encoder->base.crtc = NULL;
11143 }
11144
11145 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011146 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011147 encoder->base.base.id,
11148 drm_get_encoder_name(&encoder->base),
11149 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011150 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011151 }
11152
11153 list_for_each_entry(connector, &dev->mode_config.connector_list,
11154 base.head) {
11155 if (connector->get_hw_state(connector)) {
11156 connector->base.dpms = DRM_MODE_DPMS_ON;
11157 connector->encoder->connectors_active = true;
11158 connector->base.encoder = &connector->encoder->base;
11159 } else {
11160 connector->base.dpms = DRM_MODE_DPMS_OFF;
11161 connector->base.encoder = NULL;
11162 }
11163 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11164 connector->base.base.id,
11165 drm_get_connector_name(&connector->base),
11166 connector->base.encoder ? "enabled" : "disabled");
11167 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011168}
11169
11170/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11171 * and i915 state tracking structures. */
11172void intel_modeset_setup_hw_state(struct drm_device *dev,
11173 bool force_restore)
11174{
11175 struct drm_i915_private *dev_priv = dev->dev_private;
11176 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011177 struct intel_crtc *crtc;
11178 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011179 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011180
11181 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011182
Jesse Barnesbabea612013-06-26 18:57:38 +030011183 /*
11184 * Now that we have the config, copy it to each CRTC struct
11185 * Note that this could go away if we move to using crtc_config
11186 * checking everywhere.
11187 */
11188 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11189 base.head) {
11190 if (crtc->active && i915_fastboot) {
11191 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11192
11193 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11194 crtc->base.base.id);
11195 drm_mode_debug_printmodeline(&crtc->base.mode);
11196 }
11197 }
11198
Daniel Vetter24929352012-07-02 20:28:59 +020011199 /* HW state is read out, now we need to sanitize this mess. */
11200 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11201 base.head) {
11202 intel_sanitize_encoder(encoder);
11203 }
11204
11205 for_each_pipe(pipe) {
11206 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11207 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011208 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011209 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011210
Daniel Vetter35c95372013-07-17 06:55:04 +020011211 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11212 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11213
11214 if (!pll->on || pll->active)
11215 continue;
11216
11217 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11218
11219 pll->disable(dev_priv, pll);
11220 pll->on = false;
11221 }
11222
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011223 if (IS_HASWELL(dev))
11224 ilk_wm_get_hw_state(dev);
11225
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011226 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011227 i915_redisable_vga(dev);
11228
Daniel Vetterf30da182013-04-11 20:22:50 +020011229 /*
11230 * We need to use raw interfaces for restoring state to avoid
11231 * checking (bogus) intermediate states.
11232 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011233 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011234 struct drm_crtc *crtc =
11235 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011236
11237 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11238 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011239 }
11240 } else {
11241 intel_modeset_update_staged_output_state(dev);
11242 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011243
11244 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020011245
11246 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011247}
11248
11249void intel_modeset_gem_init(struct drm_device *dev)
11250{
Chris Wilson1833b132012-05-09 11:56:28 +010011251 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011252
11253 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011254
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011255 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080011256}
11257
11258void intel_modeset_cleanup(struct drm_device *dev)
11259{
Jesse Barnes652c3932009-08-17 13:31:43 -070011260 struct drm_i915_private *dev_priv = dev->dev_private;
11261 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011262 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011263
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011264 /*
11265 * Interrupts and polling as the first thing to avoid creating havoc.
11266 * Too much stuff here (turning of rps, connectors, ...) would
11267 * experience fancy races otherwise.
11268 */
11269 drm_irq_uninstall(dev);
11270 cancel_work_sync(&dev_priv->hotplug_work);
11271 /*
11272 * Due to the hpd irq storm handling the hotplug work can re-arm the
11273 * poll handlers. Hence disable polling after hpd handling is shut down.
11274 */
Keith Packardf87ea762010-10-03 19:36:26 -070011275 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011276
Jesse Barnes652c3932009-08-17 13:31:43 -070011277 mutex_lock(&dev->struct_mutex);
11278
Jesse Barnes723bfd72010-10-07 16:01:13 -070011279 intel_unregister_dsm_handler();
11280
Jesse Barnes652c3932009-08-17 13:31:43 -070011281 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11282 /* Skip inactive CRTCs */
11283 if (!crtc->fb)
11284 continue;
11285
Daniel Vetter3dec0092010-08-20 21:40:52 +020011286 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011287 }
11288
Chris Wilson973d04f2011-07-08 12:22:37 +010011289 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011290
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011291 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011292
Daniel Vetter930ebb42012-06-29 23:32:16 +020011293 ironlake_teardown_rc6(dev);
11294
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011295 mutex_unlock(&dev->struct_mutex);
11296
Chris Wilson1630fe72011-07-08 12:22:42 +010011297 /* flush any delayed tasks or pending work */
11298 flush_scheduled_work();
11299
Jani Nikuladb31af12013-11-08 16:48:53 +020011300 /* destroy the backlight and sysfs files before encoders/connectors */
11301 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11302 intel_panel_destroy_backlight(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030011303 drm_sysfs_connector_remove(connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011304 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011305
Jesse Barnes79e53942008-11-07 14:24:08 -080011306 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011307
11308 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011309}
11310
Dave Airlie28d52042009-09-21 14:33:58 +100011311/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011312 * Return which encoder is currently attached for connector.
11313 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011314struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011315{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011316 return &intel_attached_encoder(connector)->base;
11317}
Jesse Barnes79e53942008-11-07 14:24:08 -080011318
Chris Wilsondf0e9242010-09-09 16:20:55 +010011319void intel_connector_attach_encoder(struct intel_connector *connector,
11320 struct intel_encoder *encoder)
11321{
11322 connector->encoder = encoder;
11323 drm_mode_connector_attach_encoder(&connector->base,
11324 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011325}
Dave Airlie28d52042009-09-21 14:33:58 +100011326
11327/*
11328 * set vga decode state - true == enable VGA decode
11329 */
11330int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11331{
11332 struct drm_i915_private *dev_priv = dev->dev_private;
11333 u16 gmch_ctrl;
11334
11335 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11336 if (state)
11337 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11338 else
11339 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11340 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11341 return 0;
11342}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011343
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011344struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011345
11346 u32 power_well_driver;
11347
Chris Wilson63b66e52013-08-08 15:12:06 +020011348 int num_transcoders;
11349
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011350 struct intel_cursor_error_state {
11351 u32 control;
11352 u32 position;
11353 u32 base;
11354 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011355 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011356
11357 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011358 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011359 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011360 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011361
11362 struct intel_plane_error_state {
11363 u32 control;
11364 u32 stride;
11365 u32 size;
11366 u32 pos;
11367 u32 addr;
11368 u32 surface;
11369 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011370 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011371
11372 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011373 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020011374 enum transcoder cpu_transcoder;
11375
11376 u32 conf;
11377
11378 u32 htotal;
11379 u32 hblank;
11380 u32 hsync;
11381 u32 vtotal;
11382 u32 vblank;
11383 u32 vsync;
11384 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011385};
11386
11387struct intel_display_error_state *
11388intel_display_capture_error_state(struct drm_device *dev)
11389{
Akshay Joshi0206e352011-08-16 15:34:10 -040011390 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011391 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011392 int transcoders[] = {
11393 TRANSCODER_A,
11394 TRANSCODER_B,
11395 TRANSCODER_C,
11396 TRANSCODER_EDP,
11397 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011398 int i;
11399
Chris Wilson63b66e52013-08-08 15:12:06 +020011400 if (INTEL_INFO(dev)->num_pipes == 0)
11401 return NULL;
11402
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011403 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011404 if (error == NULL)
11405 return NULL;
11406
Imre Deak190be112013-11-25 17:15:31 +020011407 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011408 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11409
Damien Lespiau52331302012-08-15 19:23:25 +010011410 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020011411 error->pipe[i].power_domain_on =
11412 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11413 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011414 continue;
11415
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011416 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11417 error->cursor[i].control = I915_READ(CURCNTR(i));
11418 error->cursor[i].position = I915_READ(CURPOS(i));
11419 error->cursor[i].base = I915_READ(CURBASE(i));
11420 } else {
11421 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11422 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11423 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11424 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011425
11426 error->plane[i].control = I915_READ(DSPCNTR(i));
11427 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011428 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011429 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011430 error->plane[i].pos = I915_READ(DSPPOS(i));
11431 }
Paulo Zanonica291362013-03-06 20:03:14 -030011432 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11433 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011434 if (INTEL_INFO(dev)->gen >= 4) {
11435 error->plane[i].surface = I915_READ(DSPSURF(i));
11436 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11437 }
11438
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011439 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011440 }
11441
11442 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11443 if (HAS_DDI(dev_priv->dev))
11444 error->num_transcoders++; /* Account for eDP. */
11445
11446 for (i = 0; i < error->num_transcoders; i++) {
11447 enum transcoder cpu_transcoder = transcoders[i];
11448
Imre Deakddf9c532013-11-27 22:02:02 +020011449 error->transcoder[i].power_domain_on =
11450 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11451 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011452 continue;
11453
Chris Wilson63b66e52013-08-08 15:12:06 +020011454 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11455
11456 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11457 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11458 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11459 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11460 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11461 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11462 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011463 }
11464
11465 return error;
11466}
11467
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011468#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11469
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011470void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011471intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011472 struct drm_device *dev,
11473 struct intel_display_error_state *error)
11474{
11475 int i;
11476
Chris Wilson63b66e52013-08-08 15:12:06 +020011477 if (!error)
11478 return;
11479
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011480 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020011481 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011482 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011483 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011484 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011485 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020011486 err_printf(m, " Power: %s\n",
11487 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011488 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011489
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011490 err_printf(m, "Plane [%d]:\n", i);
11491 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11492 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011493 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011494 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11495 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011496 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011497 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011498 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011499 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011500 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11501 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011502 }
11503
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011504 err_printf(m, "Cursor [%d]:\n", i);
11505 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11506 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11507 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011508 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011509
11510 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011511 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011512 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011513 err_printf(m, " Power: %s\n",
11514 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020011515 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11516 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11517 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11518 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11519 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11520 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11521 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11522 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011523}