blob: 767e05c2cb768b5ed5b1c877c34b1ee5e223a750 [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 Barnes5382f5f352013-12-16 16:34:24 -08001375}
1376
1377static void intel_reset_dpio(struct drm_device *dev)
1378{
1379 struct drm_i915_private *dev_priv = dev->dev_private;
1380
1381 if (!IS_VALLEYVIEW(dev))
1382 return;
1383
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001384 /*
1385 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1386 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1387 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1388 * b. The other bits such as sfr settings / modesel may all be set
1389 * to 0.
1390 *
1391 * This should only be done on init and resume from S3 with both
1392 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1393 */
1394 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1395}
1396
Daniel Vetter426115c2013-07-11 22:13:42 +02001397static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001398{
Daniel Vetter426115c2013-07-11 22:13:42 +02001399 struct drm_device *dev = crtc->base.dev;
1400 struct drm_i915_private *dev_priv = dev->dev_private;
1401 int reg = DPLL(crtc->pipe);
1402 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001403
Daniel Vetter426115c2013-07-11 22:13:42 +02001404 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001405
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001406 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001407 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1408
1409 /* PLL is protected by panel, make sure we can write it */
1410 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001411 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001412
Daniel Vetter426115c2013-07-11 22:13:42 +02001413 I915_WRITE(reg, dpll);
1414 POSTING_READ(reg);
1415 udelay(150);
1416
1417 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1418 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1419
1420 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1421 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001422
1423 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001424 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001425 POSTING_READ(reg);
1426 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001427 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001428 POSTING_READ(reg);
1429 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001430 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001431 POSTING_READ(reg);
1432 udelay(150); /* wait for warmup */
1433}
1434
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001435static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001436{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001437 struct drm_device *dev = crtc->base.dev;
1438 struct drm_i915_private *dev_priv = dev->dev_private;
1439 int reg = DPLL(crtc->pipe);
1440 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001441
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001442 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001443
1444 /* No really, not for ILK+ */
1445 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001446
1447 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001448 if (IS_MOBILE(dev) && !IS_I830(dev))
1449 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001450
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001451 I915_WRITE(reg, dpll);
1452
1453 /* Wait for the clocks to stabilize. */
1454 POSTING_READ(reg);
1455 udelay(150);
1456
1457 if (INTEL_INFO(dev)->gen >= 4) {
1458 I915_WRITE(DPLL_MD(crtc->pipe),
1459 crtc->config.dpll_hw_state.dpll_md);
1460 } else {
1461 /* The pixel multiplier can only be updated once the
1462 * DPLL is enabled and the clocks are stable.
1463 *
1464 * So write it again.
1465 */
1466 I915_WRITE(reg, dpll);
1467 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001468
1469 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001470 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001471 POSTING_READ(reg);
1472 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001473 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001474 POSTING_READ(reg);
1475 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001476 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001477 POSTING_READ(reg);
1478 udelay(150); /* wait for warmup */
1479}
1480
1481/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001482 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001483 * @dev_priv: i915 private structure
1484 * @pipe: pipe PLL to disable
1485 *
1486 * Disable the PLL for @pipe, making sure the pipe is off first.
1487 *
1488 * Note! This is for pre-ILK only.
1489 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001490static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001491{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001492 /* Don't disable pipe A or pipe A PLLs if needed */
1493 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1494 return;
1495
1496 /* Make sure the pipe isn't still relying on us */
1497 assert_pipe_disabled(dev_priv, pipe);
1498
Daniel Vetter50b44a42013-06-05 13:34:33 +02001499 I915_WRITE(DPLL(pipe), 0);
1500 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001501}
1502
Jesse Barnesf6071162013-10-01 10:41:38 -07001503static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1504{
1505 u32 val = 0;
1506
1507 /* Make sure the pipe isn't still relying on us */
1508 assert_pipe_disabled(dev_priv, pipe);
1509
1510 /* Leave integrated clock source enabled */
1511 if (pipe == PIPE_B)
1512 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1513 I915_WRITE(DPLL(pipe), val);
1514 POSTING_READ(DPLL(pipe));
1515}
1516
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001517void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1518 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001519{
1520 u32 port_mask;
1521
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001522 switch (dport->port) {
1523 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001524 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001525 break;
1526 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001527 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001528 break;
1529 default:
1530 BUG();
1531 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001532
1533 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1534 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Ville Syrjäläbe46ffd2013-11-29 13:21:49 +02001535 port_name(dport->port), I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001536}
1537
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001538/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001539 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001540 * @dev_priv: i915 private structure
1541 * @pipe: pipe PLL to enable
1542 *
1543 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1544 * drives the transcoder clock.
1545 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001546static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001547{
Daniel Vettere2b78262013-06-07 23:10:03 +02001548 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1549 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001550
Chris Wilson48da64a2012-05-13 20:16:12 +01001551 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001552 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001553 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001554 return;
1555
1556 if (WARN_ON(pll->refcount == 0))
1557 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001558
Daniel Vetter46edb022013-06-05 13:34:12 +02001559 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1560 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001561 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001562
Daniel Vettercdbd2312013-06-05 13:34:03 +02001563 if (pll->active++) {
1564 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001565 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001566 return;
1567 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001568 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001569
Daniel Vetter46edb022013-06-05 13:34:12 +02001570 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001571 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001572 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001573}
1574
Daniel Vettere2b78262013-06-07 23:10:03 +02001575static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001576{
Daniel Vettere2b78262013-06-07 23:10:03 +02001577 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1578 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001579
Jesse Barnes92f25842011-01-04 15:09:34 -08001580 /* PCH only available on ILK+ */
1581 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001582 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001583 return;
1584
Chris Wilson48da64a2012-05-13 20:16:12 +01001585 if (WARN_ON(pll->refcount == 0))
1586 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001587
Daniel Vetter46edb022013-06-05 13:34:12 +02001588 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1589 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001590 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001591
Chris Wilson48da64a2012-05-13 20:16:12 +01001592 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001593 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001594 return;
1595 }
1596
Daniel Vettere9d69442013-06-05 13:34:15 +02001597 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001598 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001599 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001600 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001601
Daniel Vetter46edb022013-06-05 13:34:12 +02001602 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001603 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001604 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001605}
1606
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001607static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1608 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001609{
Daniel Vetter23670b322012-11-01 09:15:30 +01001610 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001611 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001613 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001614
1615 /* PCH only available on ILK+ */
1616 BUG_ON(dev_priv->info->gen < 5);
1617
1618 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001619 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001620 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001621
1622 /* FDI must be feeding us bits for PCH ports */
1623 assert_fdi_tx_enabled(dev_priv, pipe);
1624 assert_fdi_rx_enabled(dev_priv, pipe);
1625
Daniel Vetter23670b322012-11-01 09:15:30 +01001626 if (HAS_PCH_CPT(dev)) {
1627 /* Workaround: Set the timing override bit before enabling the
1628 * pch transcoder. */
1629 reg = TRANS_CHICKEN2(pipe);
1630 val = I915_READ(reg);
1631 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1632 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001633 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001634
Daniel Vetterab9412b2013-05-03 11:49:46 +02001635 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001636 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001637 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001638
1639 if (HAS_PCH_IBX(dev_priv->dev)) {
1640 /*
1641 * make the BPC in transcoder be consistent with
1642 * that in pipeconf reg.
1643 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001644 val &= ~PIPECONF_BPC_MASK;
1645 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001646 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001647
1648 val &= ~TRANS_INTERLACE_MASK;
1649 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001650 if (HAS_PCH_IBX(dev_priv->dev) &&
1651 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1652 val |= TRANS_LEGACY_INTERLACED_ILK;
1653 else
1654 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001655 else
1656 val |= TRANS_PROGRESSIVE;
1657
Jesse Barnes040484a2011-01-03 12:14:26 -08001658 I915_WRITE(reg, val | TRANS_ENABLE);
1659 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001660 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001661}
1662
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001663static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001664 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001665{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001666 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001667
1668 /* PCH only available on ILK+ */
1669 BUG_ON(dev_priv->info->gen < 5);
1670
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001671 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001672 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001673 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001674
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001675 /* Workaround: set timing override bit. */
1676 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001677 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001678 I915_WRITE(_TRANSA_CHICKEN2, val);
1679
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001680 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001681 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001682
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001683 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1684 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001685 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001686 else
1687 val |= TRANS_PROGRESSIVE;
1688
Daniel Vetterab9412b2013-05-03 11:49:46 +02001689 I915_WRITE(LPT_TRANSCONF, val);
1690 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001691 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001692}
1693
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001694static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1695 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001696{
Daniel Vetter23670b322012-11-01 09:15:30 +01001697 struct drm_device *dev = dev_priv->dev;
1698 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001699
1700 /* FDI relies on the transcoder */
1701 assert_fdi_tx_disabled(dev_priv, pipe);
1702 assert_fdi_rx_disabled(dev_priv, pipe);
1703
Jesse Barnes291906f2011-02-02 12:28:03 -08001704 /* Ports must be off as well */
1705 assert_pch_ports_disabled(dev_priv, pipe);
1706
Daniel Vetterab9412b2013-05-03 11:49:46 +02001707 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001708 val = I915_READ(reg);
1709 val &= ~TRANS_ENABLE;
1710 I915_WRITE(reg, val);
1711 /* wait for PCH transcoder off, transcoder state */
1712 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001713 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001714
1715 if (!HAS_PCH_IBX(dev)) {
1716 /* Workaround: Clear the timing override chicken bit again. */
1717 reg = TRANS_CHICKEN2(pipe);
1718 val = I915_READ(reg);
1719 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1720 I915_WRITE(reg, val);
1721 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001722}
1723
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001724static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001725{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001726 u32 val;
1727
Daniel Vetterab9412b2013-05-03 11:49:46 +02001728 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001729 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001730 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001731 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001732 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001733 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001734
1735 /* Workaround: clear timing override bit. */
1736 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001737 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001738 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001739}
1740
1741/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001742 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001743 * @dev_priv: i915 private structure
1744 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001745 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001746 *
1747 * Enable @pipe, making sure that various hardware specific requirements
1748 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1749 *
1750 * @pipe should be %PIPE_A or %PIPE_B.
1751 *
1752 * Will wait until the pipe is actually running (i.e. first vblank) before
1753 * returning.
1754 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001755static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001756 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001757{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001758 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1759 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001760 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001761 int reg;
1762 u32 val;
1763
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001764 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001765 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001766 assert_sprites_disabled(dev_priv, pipe);
1767
Paulo Zanoni681e5812012-12-06 11:12:38 -02001768 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001769 pch_transcoder = TRANSCODER_A;
1770 else
1771 pch_transcoder = pipe;
1772
Jesse Barnesb24e7172011-01-04 15:09:30 -08001773 /*
1774 * A pipe without a PLL won't actually be able to drive bits from
1775 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1776 * need the check.
1777 */
1778 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001779 if (dsi)
1780 assert_dsi_pll_enabled(dev_priv);
1781 else
1782 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001783 else {
1784 if (pch_port) {
1785 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001786 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001787 assert_fdi_tx_pll_enabled(dev_priv,
1788 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001789 }
1790 /* FIXME: assert CPU port conditions for SNB+ */
1791 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001792
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001793 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001794 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001795 if (val & PIPECONF_ENABLE)
1796 return;
1797
1798 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001799 intel_wait_for_vblank(dev_priv->dev, pipe);
1800}
1801
1802/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001803 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001804 * @dev_priv: i915 private structure
1805 * @pipe: pipe to disable
1806 *
1807 * Disable @pipe, making sure that various hardware specific requirements
1808 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1809 *
1810 * @pipe should be %PIPE_A or %PIPE_B.
1811 *
1812 * Will wait until the pipe has shut down before returning.
1813 */
1814static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1815 enum pipe pipe)
1816{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001817 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1818 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001819 int reg;
1820 u32 val;
1821
1822 /*
1823 * Make sure planes won't keep trying to pump pixels to us,
1824 * or we might hang the display.
1825 */
1826 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001827 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001828 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001829
1830 /* Don't disable pipe A or pipe A PLLs if needed */
1831 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1832 return;
1833
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001834 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001835 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001836 if ((val & PIPECONF_ENABLE) == 0)
1837 return;
1838
1839 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001840 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1841}
1842
Keith Packardd74362c2011-07-28 14:47:14 -07001843/*
1844 * Plane regs are double buffered, going from enabled->disabled needs a
1845 * trigger in order to latch. The display address reg provides this.
1846 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001847void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1848 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001849{
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001850 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1851
1852 I915_WRITE(reg, I915_READ(reg));
1853 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001854}
1855
Jesse Barnesb24e7172011-01-04 15:09:30 -08001856/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001857 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001858 * @dev_priv: i915 private structure
1859 * @plane: plane to enable
1860 * @pipe: pipe being fed
1861 *
1862 * Enable @plane on @pipe, making sure that @pipe is running first.
1863 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001864static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1865 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001866{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001867 struct intel_crtc *intel_crtc =
1868 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001869 int reg;
1870 u32 val;
1871
1872 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1873 assert_pipe_enabled(dev_priv, pipe);
1874
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001875 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001876
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001877 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001878
Jesse Barnesb24e7172011-01-04 15:09:30 -08001879 reg = DSPCNTR(plane);
1880 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001881 if (val & DISPLAY_PLANE_ENABLE)
1882 return;
1883
1884 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001885 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001886 intel_wait_for_vblank(dev_priv->dev, pipe);
1887}
1888
Jesse Barnesb24e7172011-01-04 15:09:30 -08001889/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001890 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001891 * @dev_priv: i915 private structure
1892 * @plane: plane to disable
1893 * @pipe: pipe consuming the data
1894 *
1895 * Disable @plane; should be an independent operation.
1896 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001897static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1898 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001899{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001900 struct intel_crtc *intel_crtc =
1901 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001902 int reg;
1903 u32 val;
1904
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001905 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001906
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001907 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001908
Jesse Barnesb24e7172011-01-04 15:09:30 -08001909 reg = DSPCNTR(plane);
1910 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001911 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1912 return;
1913
1914 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001915 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001916 intel_wait_for_vblank(dev_priv->dev, pipe);
1917}
1918
Chris Wilson693db182013-03-05 14:52:39 +00001919static bool need_vtd_wa(struct drm_device *dev)
1920{
1921#ifdef CONFIG_INTEL_IOMMU
1922 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1923 return true;
1924#endif
1925 return false;
1926}
1927
Chris Wilson127bd2a2010-07-23 23:32:05 +01001928int
Chris Wilson48b956c2010-09-14 12:50:34 +01001929intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001930 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001931 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001932{
Chris Wilsonce453d82011-02-21 14:43:56 +00001933 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001934 u32 alignment;
1935 int ret;
1936
Chris Wilson05394f32010-11-08 19:18:58 +00001937 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001938 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001939 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1940 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001941 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001942 alignment = 4 * 1024;
1943 else
1944 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001945 break;
1946 case I915_TILING_X:
1947 /* pin() will align the object as required by fence */
1948 alignment = 0;
1949 break;
1950 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001951 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001952 return -EINVAL;
1953 default:
1954 BUG();
1955 }
1956
Chris Wilson693db182013-03-05 14:52:39 +00001957 /* Note that the w/a also requires 64 PTE of padding following the
1958 * bo. We currently fill all unused PTE with the shadow page and so
1959 * we should always have valid PTE following the scanout preventing
1960 * the VT-d warning.
1961 */
1962 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1963 alignment = 256 * 1024;
1964
Chris Wilsonce453d82011-02-21 14:43:56 +00001965 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001966 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001967 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001968 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001969
1970 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1971 * fence, whereas 965+ only requires a fence if using
1972 * framebuffer compression. For simplicity, we always install
1973 * a fence as the cost is not that onerous.
1974 */
Chris Wilson06d98132012-04-17 15:31:24 +01001975 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001976 if (ret)
1977 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001978
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001979 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001980
Chris Wilsonce453d82011-02-21 14:43:56 +00001981 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001982 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001983
1984err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001985 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001986err_interruptible:
1987 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001988 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001989}
1990
Chris Wilson1690e1e2011-12-14 13:57:08 +01001991void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1992{
1993 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001994 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001995}
1996
Daniel Vetterc2c75132012-07-05 12:17:30 +02001997/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1998 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001999unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2000 unsigned int tiling_mode,
2001 unsigned int cpp,
2002 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002003{
Chris Wilsonbc752862013-02-21 20:04:31 +00002004 if (tiling_mode != I915_TILING_NONE) {
2005 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002006
Chris Wilsonbc752862013-02-21 20:04:31 +00002007 tile_rows = *y / 8;
2008 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002009
Chris Wilsonbc752862013-02-21 20:04:31 +00002010 tiles = *x / (512/cpp);
2011 *x %= 512/cpp;
2012
2013 return tile_rows * pitch * 8 + tiles * 4096;
2014 } else {
2015 unsigned int offset;
2016
2017 offset = *y * pitch + *x * cpp;
2018 *y = 0;
2019 *x = (offset & 4095) / cpp;
2020 return offset & -4096;
2021 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002022}
2023
Jesse Barnes17638cd2011-06-24 12:19:23 -07002024static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2025 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002026{
2027 struct drm_device *dev = crtc->dev;
2028 struct drm_i915_private *dev_priv = dev->dev_private;
2029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2030 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002031 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002032 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002033 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002034 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002035 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002036
2037 switch (plane) {
2038 case 0:
2039 case 1:
2040 break;
2041 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002042 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002043 return -EINVAL;
2044 }
2045
2046 intel_fb = to_intel_framebuffer(fb);
2047 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002048
Chris Wilson5eddb702010-09-11 13:48:45 +01002049 reg = DSPCNTR(plane);
2050 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002051 /* Mask out pixel format bits in case we change it */
2052 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002053 switch (fb->pixel_format) {
2054 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002055 dspcntr |= DISPPLANE_8BPP;
2056 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002057 case DRM_FORMAT_XRGB1555:
2058 case DRM_FORMAT_ARGB1555:
2059 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002060 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002061 case DRM_FORMAT_RGB565:
2062 dspcntr |= DISPPLANE_BGRX565;
2063 break;
2064 case DRM_FORMAT_XRGB8888:
2065 case DRM_FORMAT_ARGB8888:
2066 dspcntr |= DISPPLANE_BGRX888;
2067 break;
2068 case DRM_FORMAT_XBGR8888:
2069 case DRM_FORMAT_ABGR8888:
2070 dspcntr |= DISPPLANE_RGBX888;
2071 break;
2072 case DRM_FORMAT_XRGB2101010:
2073 case DRM_FORMAT_ARGB2101010:
2074 dspcntr |= DISPPLANE_BGRX101010;
2075 break;
2076 case DRM_FORMAT_XBGR2101010:
2077 case DRM_FORMAT_ABGR2101010:
2078 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002079 break;
2080 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002081 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002082 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002083
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002084 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002085 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002086 dspcntr |= DISPPLANE_TILED;
2087 else
2088 dspcntr &= ~DISPPLANE_TILED;
2089 }
2090
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002091 if (IS_G4X(dev))
2092 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2093
Chris Wilson5eddb702010-09-11 13:48:45 +01002094 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002095
Daniel Vettere506a0c2012-07-05 12:17:29 +02002096 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002097
Daniel Vetterc2c75132012-07-05 12:17:30 +02002098 if (INTEL_INFO(dev)->gen >= 4) {
2099 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002100 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2101 fb->bits_per_pixel / 8,
2102 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002103 linear_offset -= intel_crtc->dspaddr_offset;
2104 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002105 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002106 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002107
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002108 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2109 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2110 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002111 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002112 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002113 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002114 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002115 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002116 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002117 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002118 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002119 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002120
Jesse Barnes17638cd2011-06-24 12:19:23 -07002121 return 0;
2122}
2123
2124static int ironlake_update_plane(struct drm_crtc *crtc,
2125 struct drm_framebuffer *fb, int x, int y)
2126{
2127 struct drm_device *dev = crtc->dev;
2128 struct drm_i915_private *dev_priv = dev->dev_private;
2129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2130 struct intel_framebuffer *intel_fb;
2131 struct drm_i915_gem_object *obj;
2132 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002133 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002134 u32 dspcntr;
2135 u32 reg;
2136
2137 switch (plane) {
2138 case 0:
2139 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002140 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 break;
2142 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002143 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002144 return -EINVAL;
2145 }
2146
2147 intel_fb = to_intel_framebuffer(fb);
2148 obj = intel_fb->obj;
2149
2150 reg = DSPCNTR(plane);
2151 dspcntr = I915_READ(reg);
2152 /* Mask out pixel format bits in case we change it */
2153 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002154 switch (fb->pixel_format) {
2155 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002156 dspcntr |= DISPPLANE_8BPP;
2157 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002158 case DRM_FORMAT_RGB565:
2159 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002160 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002161 case DRM_FORMAT_XRGB8888:
2162 case DRM_FORMAT_ARGB8888:
2163 dspcntr |= DISPPLANE_BGRX888;
2164 break;
2165 case DRM_FORMAT_XBGR8888:
2166 case DRM_FORMAT_ABGR8888:
2167 dspcntr |= DISPPLANE_RGBX888;
2168 break;
2169 case DRM_FORMAT_XRGB2101010:
2170 case DRM_FORMAT_ARGB2101010:
2171 dspcntr |= DISPPLANE_BGRX101010;
2172 break;
2173 case DRM_FORMAT_XBGR2101010:
2174 case DRM_FORMAT_ABGR2101010:
2175 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002176 break;
2177 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002178 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002179 }
2180
2181 if (obj->tiling_mode != I915_TILING_NONE)
2182 dspcntr |= DISPPLANE_TILED;
2183 else
2184 dspcntr &= ~DISPPLANE_TILED;
2185
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002186 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002187 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2188 else
2189 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002190
2191 I915_WRITE(reg, dspcntr);
2192
Daniel Vettere506a0c2012-07-05 12:17:29 +02002193 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002194 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002195 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2196 fb->bits_per_pixel / 8,
2197 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002198 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002199
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002200 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2201 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2202 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002203 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002204 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002205 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002206 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002207 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2208 } else {
2209 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2210 I915_WRITE(DSPLINOFF(plane), linear_offset);
2211 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002212 POSTING_READ(reg);
2213
2214 return 0;
2215}
2216
2217/* Assume fb object is pinned & idle & fenced and just update base pointers */
2218static int
2219intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2220 int x, int y, enum mode_set_atomic state)
2221{
2222 struct drm_device *dev = crtc->dev;
2223 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002224
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002225 if (dev_priv->display.disable_fbc)
2226 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002227 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002228
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002229 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002230}
2231
Ville Syrjälä96a02912013-02-18 19:08:49 +02002232void intel_display_handle_reset(struct drm_device *dev)
2233{
2234 struct drm_i915_private *dev_priv = dev->dev_private;
2235 struct drm_crtc *crtc;
2236
2237 /*
2238 * Flips in the rings have been nuked by the reset,
2239 * so complete all pending flips so that user space
2240 * will get its events and not get stuck.
2241 *
2242 * Also update the base address of all primary
2243 * planes to the the last fb to make sure we're
2244 * showing the correct fb after a reset.
2245 *
2246 * Need to make two loops over the crtcs so that we
2247 * don't try to grab a crtc mutex before the
2248 * pending_flip_queue really got woken up.
2249 */
2250
2251 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2253 enum plane plane = intel_crtc->plane;
2254
2255 intel_prepare_page_flip(dev, plane);
2256 intel_finish_page_flip_plane(dev, plane);
2257 }
2258
2259 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2261
2262 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002263 /*
2264 * FIXME: Once we have proper support for primary planes (and
2265 * disabling them without disabling the entire crtc) allow again
2266 * a NULL crtc->fb.
2267 */
2268 if (intel_crtc->active && crtc->fb)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002269 dev_priv->display.update_plane(crtc, crtc->fb,
2270 crtc->x, crtc->y);
2271 mutex_unlock(&crtc->mutex);
2272 }
2273}
2274
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002275static int
Chris Wilson14667a42012-04-03 17:58:35 +01002276intel_finish_fb(struct drm_framebuffer *old_fb)
2277{
2278 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2279 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2280 bool was_interruptible = dev_priv->mm.interruptible;
2281 int ret;
2282
Chris Wilson14667a42012-04-03 17:58:35 +01002283 /* Big Hammer, we also need to ensure that any pending
2284 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2285 * current scanout is retired before unpinning the old
2286 * framebuffer.
2287 *
2288 * This should only fail upon a hung GPU, in which case we
2289 * can safely continue.
2290 */
2291 dev_priv->mm.interruptible = false;
2292 ret = i915_gem_object_finish_gpu(obj);
2293 dev_priv->mm.interruptible = was_interruptible;
2294
2295 return ret;
2296}
2297
Ville Syrjälä198598d2012-10-31 17:50:24 +02002298static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2299{
2300 struct drm_device *dev = crtc->dev;
2301 struct drm_i915_master_private *master_priv;
2302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2303
2304 if (!dev->primary->master)
2305 return;
2306
2307 master_priv = dev->primary->master->driver_priv;
2308 if (!master_priv->sarea_priv)
2309 return;
2310
2311 switch (intel_crtc->pipe) {
2312 case 0:
2313 master_priv->sarea_priv->pipeA_x = x;
2314 master_priv->sarea_priv->pipeA_y = y;
2315 break;
2316 case 1:
2317 master_priv->sarea_priv->pipeB_x = x;
2318 master_priv->sarea_priv->pipeB_y = y;
2319 break;
2320 default:
2321 break;
2322 }
2323}
2324
Chris Wilson14667a42012-04-03 17:58:35 +01002325static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002326intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002327 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002328{
2329 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002330 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002332 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002333 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002334
2335 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002336 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002337 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002338 return 0;
2339 }
2340
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002341 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002342 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2343 plane_name(intel_crtc->plane),
2344 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002345 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002346 }
2347
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002348 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002349 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002350 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002351 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002352 if (ret != 0) {
2353 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002354 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002355 return ret;
2356 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002357
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002358 /*
2359 * Update pipe size and adjust fitter if needed: the reason for this is
2360 * that in compute_mode_changes we check the native mode (not the pfit
2361 * mode) to see if we can flip rather than do a full mode set. In the
2362 * fastboot case, we'll flip, but if we don't update the pipesrc and
2363 * pfit state, we'll end up with a big fb scanned out into the wrong
2364 * sized surface.
2365 *
2366 * To fix this properly, we need to hoist the checks up into
2367 * compute_mode_changes (or above), check the actual pfit state and
2368 * whether the platform allows pfit disable with pipe active, and only
2369 * then update the pipesrc and pfit state, even on the flip path.
2370 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002371 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002372 const struct drm_display_mode *adjusted_mode =
2373 &intel_crtc->config.adjusted_mode;
2374
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002375 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002376 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2377 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002378 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002379 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2380 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2381 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2382 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2383 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2384 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002385 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2386 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002387 }
2388
Daniel Vetter94352cf2012-07-05 22:51:56 +02002389 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002390 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002391 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002392 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002393 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002394 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002395 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002396
Daniel Vetter94352cf2012-07-05 22:51:56 +02002397 old_fb = crtc->fb;
2398 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002399 crtc->x = x;
2400 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002401
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002402 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002403 if (intel_crtc->active && old_fb != fb)
2404 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002405 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002406 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002407
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002408 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002409 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002410 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002411
Ville Syrjälä198598d2012-10-31 17:50:24 +02002412 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002413
2414 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002415}
2416
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002417static void intel_fdi_normal_train(struct drm_crtc *crtc)
2418{
2419 struct drm_device *dev = crtc->dev;
2420 struct drm_i915_private *dev_priv = dev->dev_private;
2421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2422 int pipe = intel_crtc->pipe;
2423 u32 reg, temp;
2424
2425 /* enable normal train */
2426 reg = FDI_TX_CTL(pipe);
2427 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002428 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002429 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2430 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002431 } else {
2432 temp &= ~FDI_LINK_TRAIN_NONE;
2433 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002434 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002435 I915_WRITE(reg, temp);
2436
2437 reg = FDI_RX_CTL(pipe);
2438 temp = I915_READ(reg);
2439 if (HAS_PCH_CPT(dev)) {
2440 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2441 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2442 } else {
2443 temp &= ~FDI_LINK_TRAIN_NONE;
2444 temp |= FDI_LINK_TRAIN_NONE;
2445 }
2446 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2447
2448 /* wait one idle pattern time */
2449 POSTING_READ(reg);
2450 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002451
2452 /* IVB wants error correction enabled */
2453 if (IS_IVYBRIDGE(dev))
2454 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2455 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002456}
2457
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002458static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002459{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002460 return crtc->base.enabled && crtc->active &&
2461 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002462}
2463
Daniel Vetter01a415f2012-10-27 15:58:40 +02002464static void ivb_modeset_global_resources(struct drm_device *dev)
2465{
2466 struct drm_i915_private *dev_priv = dev->dev_private;
2467 struct intel_crtc *pipe_B_crtc =
2468 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2469 struct intel_crtc *pipe_C_crtc =
2470 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2471 uint32_t temp;
2472
Daniel Vetter1e833f42013-02-19 22:31:57 +01002473 /*
2474 * When everything is off disable fdi C so that we could enable fdi B
2475 * with all lanes. Note that we don't care about enabled pipes without
2476 * an enabled pch encoder.
2477 */
2478 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2479 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002480 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2481 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2482
2483 temp = I915_READ(SOUTH_CHICKEN1);
2484 temp &= ~FDI_BC_BIFURCATION_SELECT;
2485 DRM_DEBUG_KMS("disabling fdi C rx\n");
2486 I915_WRITE(SOUTH_CHICKEN1, temp);
2487 }
2488}
2489
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002490/* The FDI link training functions for ILK/Ibexpeak. */
2491static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2492{
2493 struct drm_device *dev = crtc->dev;
2494 struct drm_i915_private *dev_priv = dev->dev_private;
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2496 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002497 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002498 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002499
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002500 /* FDI needs bits from pipe & plane first */
2501 assert_pipe_enabled(dev_priv, pipe);
2502 assert_plane_enabled(dev_priv, plane);
2503
Adam Jacksone1a44742010-06-25 15:32:14 -04002504 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2505 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002506 reg = FDI_RX_IMR(pipe);
2507 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002508 temp &= ~FDI_RX_SYMBOL_LOCK;
2509 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 I915_WRITE(reg, temp);
2511 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002512 udelay(150);
2513
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002514 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 reg = FDI_TX_CTL(pipe);
2516 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002517 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2518 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519 temp &= ~FDI_LINK_TRAIN_NONE;
2520 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002521 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002522
Chris Wilson5eddb702010-09-11 13:48:45 +01002523 reg = FDI_RX_CTL(pipe);
2524 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 temp &= ~FDI_LINK_TRAIN_NONE;
2526 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002527 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2528
2529 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 udelay(150);
2531
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002532 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002533 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2534 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2535 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002536
Chris Wilson5eddb702010-09-11 13:48:45 +01002537 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002538 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002539 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002540 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2541
2542 if ((temp & FDI_RX_BIT_LOCK)) {
2543 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002544 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002545 break;
2546 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002548 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002549 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002550
2551 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002552 reg = FDI_TX_CTL(pipe);
2553 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002554 temp &= ~FDI_LINK_TRAIN_NONE;
2555 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557
Chris Wilson5eddb702010-09-11 13:48:45 +01002558 reg = FDI_RX_CTL(pipe);
2559 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002560 temp &= ~FDI_LINK_TRAIN_NONE;
2561 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002562 I915_WRITE(reg, temp);
2563
2564 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002565 udelay(150);
2566
Chris Wilson5eddb702010-09-11 13:48:45 +01002567 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002568 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002569 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002570 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2571
2572 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002573 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574 DRM_DEBUG_KMS("FDI train 2 done.\n");
2575 break;
2576 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002577 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002578 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002579 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002580
2581 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002582
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002583}
2584
Akshay Joshi0206e352011-08-16 15:34:10 -04002585static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002586 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2587 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2588 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2589 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2590};
2591
2592/* The FDI link training functions for SNB/Cougarpoint. */
2593static void gen6_fdi_link_train(struct drm_crtc *crtc)
2594{
2595 struct drm_device *dev = crtc->dev;
2596 struct drm_i915_private *dev_priv = dev->dev_private;
2597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2598 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002599 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002600
Adam Jacksone1a44742010-06-25 15:32:14 -04002601 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2602 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002603 reg = FDI_RX_IMR(pipe);
2604 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002605 temp &= ~FDI_RX_SYMBOL_LOCK;
2606 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002607 I915_WRITE(reg, temp);
2608
2609 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002610 udelay(150);
2611
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002612 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002613 reg = FDI_TX_CTL(pipe);
2614 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002615 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2616 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617 temp &= ~FDI_LINK_TRAIN_NONE;
2618 temp |= FDI_LINK_TRAIN_PATTERN_1;
2619 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2620 /* SNB-B */
2621 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002622 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002623
Daniel Vetterd74cf322012-10-26 10:58:13 +02002624 I915_WRITE(FDI_RX_MISC(pipe),
2625 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2626
Chris Wilson5eddb702010-09-11 13:48:45 +01002627 reg = FDI_RX_CTL(pipe);
2628 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002629 if (HAS_PCH_CPT(dev)) {
2630 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2631 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2632 } else {
2633 temp &= ~FDI_LINK_TRAIN_NONE;
2634 temp |= FDI_LINK_TRAIN_PATTERN_1;
2635 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002636 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2637
2638 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002639 udelay(150);
2640
Akshay Joshi0206e352011-08-16 15:34:10 -04002641 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002642 reg = FDI_TX_CTL(pipe);
2643 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002644 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2645 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002646 I915_WRITE(reg, temp);
2647
2648 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002649 udelay(500);
2650
Sean Paulfa37d392012-03-02 12:53:39 -05002651 for (retry = 0; retry < 5; retry++) {
2652 reg = FDI_RX_IIR(pipe);
2653 temp = I915_READ(reg);
2654 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2655 if (temp & FDI_RX_BIT_LOCK) {
2656 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2657 DRM_DEBUG_KMS("FDI train 1 done.\n");
2658 break;
2659 }
2660 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002661 }
Sean Paulfa37d392012-03-02 12:53:39 -05002662 if (retry < 5)
2663 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002664 }
2665 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002666 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002667
2668 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002669 reg = FDI_TX_CTL(pipe);
2670 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002671 temp &= ~FDI_LINK_TRAIN_NONE;
2672 temp |= FDI_LINK_TRAIN_PATTERN_2;
2673 if (IS_GEN6(dev)) {
2674 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2675 /* SNB-B */
2676 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2677 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002678 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002679
Chris Wilson5eddb702010-09-11 13:48:45 +01002680 reg = FDI_RX_CTL(pipe);
2681 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002682 if (HAS_PCH_CPT(dev)) {
2683 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2684 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2685 } else {
2686 temp &= ~FDI_LINK_TRAIN_NONE;
2687 temp |= FDI_LINK_TRAIN_PATTERN_2;
2688 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002689 I915_WRITE(reg, temp);
2690
2691 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002692 udelay(150);
2693
Akshay Joshi0206e352011-08-16 15:34:10 -04002694 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002695 reg = FDI_TX_CTL(pipe);
2696 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002697 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2698 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002699 I915_WRITE(reg, temp);
2700
2701 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002702 udelay(500);
2703
Sean Paulfa37d392012-03-02 12:53:39 -05002704 for (retry = 0; retry < 5; retry++) {
2705 reg = FDI_RX_IIR(pipe);
2706 temp = I915_READ(reg);
2707 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2708 if (temp & FDI_RX_SYMBOL_LOCK) {
2709 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2710 DRM_DEBUG_KMS("FDI train 2 done.\n");
2711 break;
2712 }
2713 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002714 }
Sean Paulfa37d392012-03-02 12:53:39 -05002715 if (retry < 5)
2716 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002717 }
2718 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002719 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002720
2721 DRM_DEBUG_KMS("FDI train done.\n");
2722}
2723
Jesse Barnes357555c2011-04-28 15:09:55 -07002724/* Manual link training for Ivy Bridge A0 parts */
2725static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2726{
2727 struct drm_device *dev = crtc->dev;
2728 struct drm_i915_private *dev_priv = dev->dev_private;
2729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2730 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002731 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002732
2733 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2734 for train result */
2735 reg = FDI_RX_IMR(pipe);
2736 temp = I915_READ(reg);
2737 temp &= ~FDI_RX_SYMBOL_LOCK;
2738 temp &= ~FDI_RX_BIT_LOCK;
2739 I915_WRITE(reg, temp);
2740
2741 POSTING_READ(reg);
2742 udelay(150);
2743
Daniel Vetter01a415f2012-10-27 15:58:40 +02002744 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2745 I915_READ(FDI_RX_IIR(pipe)));
2746
Jesse Barnes139ccd32013-08-19 11:04:55 -07002747 /* Try each vswing and preemphasis setting twice before moving on */
2748 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2749 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002750 reg = FDI_TX_CTL(pipe);
2751 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002752 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2753 temp &= ~FDI_TX_ENABLE;
2754 I915_WRITE(reg, temp);
2755
2756 reg = FDI_RX_CTL(pipe);
2757 temp = I915_READ(reg);
2758 temp &= ~FDI_LINK_TRAIN_AUTO;
2759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2760 temp &= ~FDI_RX_ENABLE;
2761 I915_WRITE(reg, temp);
2762
2763 /* enable CPU FDI TX and PCH FDI RX */
2764 reg = FDI_TX_CTL(pipe);
2765 temp = I915_READ(reg);
2766 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2767 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2768 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002769 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002770 temp |= snb_b_fdi_train_param[j/2];
2771 temp |= FDI_COMPOSITE_SYNC;
2772 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2773
2774 I915_WRITE(FDI_RX_MISC(pipe),
2775 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2776
2777 reg = FDI_RX_CTL(pipe);
2778 temp = I915_READ(reg);
2779 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2780 temp |= FDI_COMPOSITE_SYNC;
2781 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2782
2783 POSTING_READ(reg);
2784 udelay(1); /* should be 0.5us */
2785
2786 for (i = 0; i < 4; i++) {
2787 reg = FDI_RX_IIR(pipe);
2788 temp = I915_READ(reg);
2789 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2790
2791 if (temp & FDI_RX_BIT_LOCK ||
2792 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2793 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2794 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2795 i);
2796 break;
2797 }
2798 udelay(1); /* should be 0.5us */
2799 }
2800 if (i == 4) {
2801 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2802 continue;
2803 }
2804
2805 /* Train 2 */
2806 reg = FDI_TX_CTL(pipe);
2807 temp = I915_READ(reg);
2808 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2809 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2810 I915_WRITE(reg, temp);
2811
2812 reg = FDI_RX_CTL(pipe);
2813 temp = I915_READ(reg);
2814 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2815 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002816 I915_WRITE(reg, temp);
2817
2818 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002819 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002820
Jesse Barnes139ccd32013-08-19 11:04:55 -07002821 for (i = 0; i < 4; i++) {
2822 reg = FDI_RX_IIR(pipe);
2823 temp = I915_READ(reg);
2824 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002825
Jesse Barnes139ccd32013-08-19 11:04:55 -07002826 if (temp & FDI_RX_SYMBOL_LOCK ||
2827 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2828 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2829 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2830 i);
2831 goto train_done;
2832 }
2833 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002834 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002835 if (i == 4)
2836 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002837 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002838
Jesse Barnes139ccd32013-08-19 11:04:55 -07002839train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002840 DRM_DEBUG_KMS("FDI train done.\n");
2841}
2842
Daniel Vetter88cefb62012-08-12 19:27:14 +02002843static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002844{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002845 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002846 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002847 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002848 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002849
Jesse Barnesc64e3112010-09-10 11:27:03 -07002850
Jesse Barnes0e23b992010-09-10 11:10:00 -07002851 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002852 reg = FDI_RX_CTL(pipe);
2853 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002854 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2855 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002856 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002857 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2858
2859 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002860 udelay(200);
2861
2862 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002863 temp = I915_READ(reg);
2864 I915_WRITE(reg, temp | FDI_PCDCLK);
2865
2866 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002867 udelay(200);
2868
Paulo Zanoni20749732012-11-23 15:30:38 -02002869 /* Enable CPU FDI TX PLL, always on for Ironlake */
2870 reg = FDI_TX_CTL(pipe);
2871 temp = I915_READ(reg);
2872 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2873 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002874
Paulo Zanoni20749732012-11-23 15:30:38 -02002875 POSTING_READ(reg);
2876 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002877 }
2878}
2879
Daniel Vetter88cefb62012-08-12 19:27:14 +02002880static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2881{
2882 struct drm_device *dev = intel_crtc->base.dev;
2883 struct drm_i915_private *dev_priv = dev->dev_private;
2884 int pipe = intel_crtc->pipe;
2885 u32 reg, temp;
2886
2887 /* Switch from PCDclk to Rawclk */
2888 reg = FDI_RX_CTL(pipe);
2889 temp = I915_READ(reg);
2890 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2891
2892 /* Disable CPU FDI TX PLL */
2893 reg = FDI_TX_CTL(pipe);
2894 temp = I915_READ(reg);
2895 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2896
2897 POSTING_READ(reg);
2898 udelay(100);
2899
2900 reg = FDI_RX_CTL(pipe);
2901 temp = I915_READ(reg);
2902 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2903
2904 /* Wait for the clocks to turn off. */
2905 POSTING_READ(reg);
2906 udelay(100);
2907}
2908
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002909static void ironlake_fdi_disable(struct drm_crtc *crtc)
2910{
2911 struct drm_device *dev = crtc->dev;
2912 struct drm_i915_private *dev_priv = dev->dev_private;
2913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2914 int pipe = intel_crtc->pipe;
2915 u32 reg, temp;
2916
2917 /* disable CPU FDI tx and PCH FDI rx */
2918 reg = FDI_TX_CTL(pipe);
2919 temp = I915_READ(reg);
2920 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2921 POSTING_READ(reg);
2922
2923 reg = FDI_RX_CTL(pipe);
2924 temp = I915_READ(reg);
2925 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002926 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002927 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2928
2929 POSTING_READ(reg);
2930 udelay(100);
2931
2932 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002933 if (HAS_PCH_IBX(dev)) {
2934 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002935 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002936
2937 /* still set train pattern 1 */
2938 reg = FDI_TX_CTL(pipe);
2939 temp = I915_READ(reg);
2940 temp &= ~FDI_LINK_TRAIN_NONE;
2941 temp |= FDI_LINK_TRAIN_PATTERN_1;
2942 I915_WRITE(reg, temp);
2943
2944 reg = FDI_RX_CTL(pipe);
2945 temp = I915_READ(reg);
2946 if (HAS_PCH_CPT(dev)) {
2947 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2948 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2949 } else {
2950 temp &= ~FDI_LINK_TRAIN_NONE;
2951 temp |= FDI_LINK_TRAIN_PATTERN_1;
2952 }
2953 /* BPC in FDI rx is consistent with that in PIPECONF */
2954 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002955 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002956 I915_WRITE(reg, temp);
2957
2958 POSTING_READ(reg);
2959 udelay(100);
2960}
2961
Chris Wilson5bb61642012-09-27 21:25:58 +01002962static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2963{
2964 struct drm_device *dev = crtc->dev;
2965 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002967 unsigned long flags;
2968 bool pending;
2969
Ville Syrjälä10d83732013-01-29 18:13:34 +02002970 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2971 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002972 return false;
2973
2974 spin_lock_irqsave(&dev->event_lock, flags);
2975 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2976 spin_unlock_irqrestore(&dev->event_lock, flags);
2977
2978 return pending;
2979}
2980
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002981static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2982{
Chris Wilson0f911282012-04-17 10:05:38 +01002983 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002984 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002985
2986 if (crtc->fb == NULL)
2987 return;
2988
Daniel Vetter2c10d572012-12-20 21:24:07 +01002989 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2990
Chris Wilson5bb61642012-09-27 21:25:58 +01002991 wait_event(dev_priv->pending_flip_queue,
2992 !intel_crtc_has_pending_flip(crtc));
2993
Chris Wilson0f911282012-04-17 10:05:38 +01002994 mutex_lock(&dev->struct_mutex);
2995 intel_finish_fb(crtc->fb);
2996 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002997}
2998
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002999/* Program iCLKIP clock to the desired frequency */
3000static void lpt_program_iclkip(struct drm_crtc *crtc)
3001{
3002 struct drm_device *dev = crtc->dev;
3003 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003004 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003005 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3006 u32 temp;
3007
Daniel Vetter09153002012-12-12 14:06:44 +01003008 mutex_lock(&dev_priv->dpio_lock);
3009
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003010 /* It is necessary to ungate the pixclk gate prior to programming
3011 * the divisors, and gate it back when it is done.
3012 */
3013 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3014
3015 /* Disable SSCCTL */
3016 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003017 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3018 SBI_SSCCTL_DISABLE,
3019 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003020
3021 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003022 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003023 auxdiv = 1;
3024 divsel = 0x41;
3025 phaseinc = 0x20;
3026 } else {
3027 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003028 * but the adjusted_mode->crtc_clock in in KHz. To get the
3029 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003030 * convert the virtual clock precision to KHz here for higher
3031 * precision.
3032 */
3033 u32 iclk_virtual_root_freq = 172800 * 1000;
3034 u32 iclk_pi_range = 64;
3035 u32 desired_divisor, msb_divisor_value, pi_value;
3036
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003037 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003038 msb_divisor_value = desired_divisor / iclk_pi_range;
3039 pi_value = desired_divisor % iclk_pi_range;
3040
3041 auxdiv = 0;
3042 divsel = msb_divisor_value - 2;
3043 phaseinc = pi_value;
3044 }
3045
3046 /* This should not happen with any sane values */
3047 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3048 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3049 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3050 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3051
3052 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 +03003053 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003054 auxdiv,
3055 divsel,
3056 phasedir,
3057 phaseinc);
3058
3059 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003060 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003061 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3062 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3063 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3064 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3065 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3066 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003067 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003068
3069 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003070 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003071 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3072 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003073 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003074
3075 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003076 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003077 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003078 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003079
3080 /* Wait for initialization time */
3081 udelay(24);
3082
3083 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003084
3085 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003086}
3087
Daniel Vetter275f01b22013-05-03 11:49:47 +02003088static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3089 enum pipe pch_transcoder)
3090{
3091 struct drm_device *dev = crtc->base.dev;
3092 struct drm_i915_private *dev_priv = dev->dev_private;
3093 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3094
3095 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3096 I915_READ(HTOTAL(cpu_transcoder)));
3097 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3098 I915_READ(HBLANK(cpu_transcoder)));
3099 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3100 I915_READ(HSYNC(cpu_transcoder)));
3101
3102 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3103 I915_READ(VTOTAL(cpu_transcoder)));
3104 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3105 I915_READ(VBLANK(cpu_transcoder)));
3106 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3107 I915_READ(VSYNC(cpu_transcoder)));
3108 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3109 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3110}
3111
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003112static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3113{
3114 struct drm_i915_private *dev_priv = dev->dev_private;
3115 uint32_t temp;
3116
3117 temp = I915_READ(SOUTH_CHICKEN1);
3118 if (temp & FDI_BC_BIFURCATION_SELECT)
3119 return;
3120
3121 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3122 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3123
3124 temp |= FDI_BC_BIFURCATION_SELECT;
3125 DRM_DEBUG_KMS("enabling fdi C rx\n");
3126 I915_WRITE(SOUTH_CHICKEN1, temp);
3127 POSTING_READ(SOUTH_CHICKEN1);
3128}
3129
3130static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3131{
3132 struct drm_device *dev = intel_crtc->base.dev;
3133 struct drm_i915_private *dev_priv = dev->dev_private;
3134
3135 switch (intel_crtc->pipe) {
3136 case PIPE_A:
3137 break;
3138 case PIPE_B:
3139 if (intel_crtc->config.fdi_lanes > 2)
3140 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3141 else
3142 cpt_enable_fdi_bc_bifurcation(dev);
3143
3144 break;
3145 case PIPE_C:
3146 cpt_enable_fdi_bc_bifurcation(dev);
3147
3148 break;
3149 default:
3150 BUG();
3151 }
3152}
3153
Jesse Barnesf67a5592011-01-05 10:31:48 -08003154/*
3155 * Enable PCH resources required for PCH ports:
3156 * - PCH PLLs
3157 * - FDI training & RX/TX
3158 * - update transcoder timings
3159 * - DP transcoding bits
3160 * - transcoder
3161 */
3162static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003163{
3164 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003165 struct drm_i915_private *dev_priv = dev->dev_private;
3166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3167 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003168 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003169
Daniel Vetterab9412b2013-05-03 11:49:46 +02003170 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003171
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003172 if (IS_IVYBRIDGE(dev))
3173 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3174
Daniel Vettercd986ab2012-10-26 10:58:12 +02003175 /* Write the TU size bits before fdi link training, so that error
3176 * detection works. */
3177 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3178 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3179
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003180 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003181 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003182
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003183 /* We need to program the right clock selection before writing the pixel
3184 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003185 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003186 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003187
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003188 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003189 temp |= TRANS_DPLL_ENABLE(pipe);
3190 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003191 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003192 temp |= sel;
3193 else
3194 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003195 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003196 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003197
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003198 /* XXX: pch pll's can be enabled any time before we enable the PCH
3199 * transcoder, and we actually should do this to not upset any PCH
3200 * transcoder that already use the clock when we share it.
3201 *
3202 * Note that enable_shared_dpll tries to do the right thing, but
3203 * get_shared_dpll unconditionally resets the pll - we need that to have
3204 * the right LVDS enable sequence. */
3205 ironlake_enable_shared_dpll(intel_crtc);
3206
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003207 /* set transcoder timing, panel must allow it */
3208 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003209 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003210
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003211 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003212
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003213 /* For PCH DP, enable TRANS_DP_CTL */
3214 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003215 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3216 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003217 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003218 reg = TRANS_DP_CTL(pipe);
3219 temp = I915_READ(reg);
3220 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003221 TRANS_DP_SYNC_MASK |
3222 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003223 temp |= (TRANS_DP_OUTPUT_ENABLE |
3224 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003225 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003226
3227 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003228 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003229 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003230 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003231
3232 switch (intel_trans_dp_port_sel(crtc)) {
3233 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003234 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003235 break;
3236 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003237 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003238 break;
3239 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003240 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003241 break;
3242 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003243 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003244 }
3245
Chris Wilson5eddb702010-09-11 13:48:45 +01003246 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003247 }
3248
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003249 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003250}
3251
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003252static void lpt_pch_enable(struct drm_crtc *crtc)
3253{
3254 struct drm_device *dev = crtc->dev;
3255 struct drm_i915_private *dev_priv = dev->dev_private;
3256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003257 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003258
Daniel Vetterab9412b2013-05-03 11:49:46 +02003259 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003260
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003261 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003262
Paulo Zanoni0540e482012-10-31 18:12:40 -02003263 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003264 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003265
Paulo Zanoni937bb612012-10-31 18:12:47 -02003266 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003267}
3268
Daniel Vettere2b78262013-06-07 23:10:03 +02003269static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003270{
Daniel Vettere2b78262013-06-07 23:10:03 +02003271 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003272
3273 if (pll == NULL)
3274 return;
3275
3276 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003277 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003278 return;
3279 }
3280
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003281 if (--pll->refcount == 0) {
3282 WARN_ON(pll->on);
3283 WARN_ON(pll->active);
3284 }
3285
Daniel Vettera43f6e02013-06-07 23:10:32 +02003286 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003287}
3288
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003289static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003290{
Daniel Vettere2b78262013-06-07 23:10:03 +02003291 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3292 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3293 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003294
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003295 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003296 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3297 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003298 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003299 }
3300
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003301 if (HAS_PCH_IBX(dev_priv->dev)) {
3302 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003303 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003304 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003305
Daniel Vetter46edb022013-06-05 13:34:12 +02003306 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3307 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003308
3309 goto found;
3310 }
3311
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003312 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3313 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003314
3315 /* Only want to check enabled timings first */
3316 if (pll->refcount == 0)
3317 continue;
3318
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003319 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3320 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003321 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003322 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003323 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003324
3325 goto found;
3326 }
3327 }
3328
3329 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003330 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3331 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003332 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003333 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3334 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003335 goto found;
3336 }
3337 }
3338
3339 return NULL;
3340
3341found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003342 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003343 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3344 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003345
Daniel Vettercdbd2312013-06-05 13:34:03 +02003346 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003347 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3348 sizeof(pll->hw_state));
3349
Daniel Vetter46edb022013-06-05 13:34:12 +02003350 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003351 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003352 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003353
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003354 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003355 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003356 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003357
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003358 return pll;
3359}
3360
Daniel Vettera1520312013-05-03 11:49:50 +02003361static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003362{
3363 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003364 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003365 u32 temp;
3366
3367 temp = I915_READ(dslreg);
3368 udelay(500);
3369 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003370 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003371 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003372 }
3373}
3374
Jesse Barnesb074cec2013-04-25 12:55:02 -07003375static void ironlake_pfit_enable(struct intel_crtc *crtc)
3376{
3377 struct drm_device *dev = crtc->base.dev;
3378 struct drm_i915_private *dev_priv = dev->dev_private;
3379 int pipe = crtc->pipe;
3380
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003381 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003382 /* Force use of hard-coded filter coefficients
3383 * as some pre-programmed values are broken,
3384 * e.g. x201.
3385 */
3386 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3387 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3388 PF_PIPE_SEL_IVB(pipe));
3389 else
3390 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3391 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3392 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003393 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003394}
3395
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003396static void intel_enable_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_restore(&intel_plane->base);
3405}
3406
3407static void intel_disable_planes(struct drm_crtc *crtc)
3408{
3409 struct drm_device *dev = crtc->dev;
3410 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3411 struct intel_plane *intel_plane;
3412
3413 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3414 if (intel_plane->pipe == pipe)
3415 intel_plane_disable(&intel_plane->base);
3416}
3417
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003418void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003419{
3420 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3421
3422 if (!crtc->config.ips_enabled)
3423 return;
3424
3425 /* We can only enable IPS after we enable a plane and wait for a vblank.
3426 * We guarantee that the plane is enabled by calling intel_enable_ips
3427 * only after intel_enable_plane. And intel_enable_plane already waits
3428 * for a vblank, so all we need to do here is to enable the IPS bit. */
3429 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003430 if (IS_BROADWELL(crtc->base.dev)) {
3431 mutex_lock(&dev_priv->rps.hw_lock);
3432 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3433 mutex_unlock(&dev_priv->rps.hw_lock);
3434 /* Quoting Art Runyan: "its not safe to expect any particular
3435 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003436 * mailbox." Moreover, the mailbox may return a bogus state,
3437 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003438 */
3439 } else {
3440 I915_WRITE(IPS_CTL, IPS_ENABLE);
3441 /* The bit only becomes 1 in the next vblank, so this wait here
3442 * is essentially intel_wait_for_vblank. If we don't have this
3443 * and don't wait for vblanks until the end of crtc_enable, then
3444 * the HW state readout code will complain that the expected
3445 * IPS_CTL value is not the one we read. */
3446 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3447 DRM_ERROR("Timed out waiting for IPS enable\n");
3448 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003449}
3450
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003451void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003452{
3453 struct drm_device *dev = crtc->base.dev;
3454 struct drm_i915_private *dev_priv = dev->dev_private;
3455
3456 if (!crtc->config.ips_enabled)
3457 return;
3458
3459 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003460 if (IS_BROADWELL(crtc->base.dev)) {
3461 mutex_lock(&dev_priv->rps.hw_lock);
3462 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3463 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnese59150d2014-01-07 13:30:45 -08003464 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003465 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003466 POSTING_READ(IPS_CTL);
3467 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003468
3469 /* We need to wait for a vblank before we can disable the plane. */
3470 intel_wait_for_vblank(dev, crtc->pipe);
3471}
3472
3473/** Loads the palette/gamma unit for the CRTC with the prepared values */
3474static void intel_crtc_load_lut(struct drm_crtc *crtc)
3475{
3476 struct drm_device *dev = crtc->dev;
3477 struct drm_i915_private *dev_priv = dev->dev_private;
3478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3479 enum pipe pipe = intel_crtc->pipe;
3480 int palreg = PALETTE(pipe);
3481 int i;
3482 bool reenable_ips = false;
3483
3484 /* The clocks have to be on to load the palette. */
3485 if (!crtc->enabled || !intel_crtc->active)
3486 return;
3487
3488 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3489 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3490 assert_dsi_pll_enabled(dev_priv);
3491 else
3492 assert_pll_enabled(dev_priv, pipe);
3493 }
3494
3495 /* use legacy palette for Ironlake */
3496 if (HAS_PCH_SPLIT(dev))
3497 palreg = LGC_PALETTE(pipe);
3498
3499 /* Workaround : Do not read or write the pipe palette/gamma data while
3500 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3501 */
3502 if (intel_crtc->config.ips_enabled &&
3503 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3504 GAMMA_MODE_MODE_SPLIT)) {
3505 hsw_disable_ips(intel_crtc);
3506 reenable_ips = true;
3507 }
3508
3509 for (i = 0; i < 256; i++) {
3510 I915_WRITE(palreg + 4 * i,
3511 (intel_crtc->lut_r[i] << 16) |
3512 (intel_crtc->lut_g[i] << 8) |
3513 intel_crtc->lut_b[i]);
3514 }
3515
3516 if (reenable_ips)
3517 hsw_enable_ips(intel_crtc);
3518}
3519
Jesse Barnesf67a5592011-01-05 10:31:48 -08003520static void ironlake_crtc_enable(struct drm_crtc *crtc)
3521{
3522 struct drm_device *dev = crtc->dev;
3523 struct drm_i915_private *dev_priv = dev->dev_private;
3524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003525 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003526 int pipe = intel_crtc->pipe;
3527 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003528
Daniel Vetter08a48462012-07-02 11:43:47 +02003529 WARN_ON(!crtc->enabled);
3530
Jesse Barnesf67a5592011-01-05 10:31:48 -08003531 if (intel_crtc->active)
3532 return;
3533
3534 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003535
3536 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3537 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3538
Daniel Vetterf6736a12013-06-05 13:34:30 +02003539 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003540 if (encoder->pre_enable)
3541 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003542
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003543 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003544 /* Note: FDI PLL enabling _must_ be done before we enable the
3545 * cpu pipes, hence this is separate from all the other fdi/pch
3546 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003547 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003548 } else {
3549 assert_fdi_tx_disabled(dev_priv, pipe);
3550 assert_fdi_rx_disabled(dev_priv, pipe);
3551 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003552
Jesse Barnesb074cec2013-04-25 12:55:02 -07003553 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003554
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003555 /*
3556 * On ILK+ LUT must be loaded before the pipe is running but with
3557 * clocks enabled
3558 */
3559 intel_crtc_load_lut(crtc);
3560
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003561 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003562 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003563 intel_crtc->config.has_pch_encoder, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003564 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003565 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003566 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003567
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003568 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003569 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003570
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003571 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003572 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003573 mutex_unlock(&dev->struct_mutex);
3574
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003575 for_each_encoder_on_crtc(dev, crtc, encoder)
3576 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003577
3578 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003579 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003580
3581 /*
3582 * There seems to be a race in PCH platform hw (at least on some
3583 * outputs) where an enabled pipe still completes any pageflip right
3584 * away (as if the pipe is off) instead of waiting for vblank. As soon
3585 * as the first vblank happend, everything works as expected. Hence just
3586 * wait for one vblank before returning to avoid strange things
3587 * happening.
3588 */
3589 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003590}
3591
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003592/* IPS only exists on ULT machines and is tied to pipe A. */
3593static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3594{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003595 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003596}
3597
Ville Syrjälädda9a662013-09-19 17:00:37 -03003598static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3599{
3600 struct drm_device *dev = crtc->dev;
3601 struct drm_i915_private *dev_priv = dev->dev_private;
3602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3603 int pipe = intel_crtc->pipe;
3604 int plane = intel_crtc->plane;
3605
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003606 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003607 intel_enable_planes(crtc);
3608 intel_crtc_update_cursor(crtc, true);
3609
3610 hsw_enable_ips(intel_crtc);
3611
3612 mutex_lock(&dev->struct_mutex);
3613 intel_update_fbc(dev);
3614 mutex_unlock(&dev->struct_mutex);
3615}
3616
3617static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3618{
3619 struct drm_device *dev = crtc->dev;
3620 struct drm_i915_private *dev_priv = dev->dev_private;
3621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3622 int pipe = intel_crtc->pipe;
3623 int plane = intel_crtc->plane;
3624
3625 intel_crtc_wait_for_pending_flips(crtc);
3626 drm_vblank_off(dev, pipe);
3627
3628 /* FBC must be disabled before disabling the plane on HSW. */
3629 if (dev_priv->fbc.plane == plane)
3630 intel_disable_fbc(dev);
3631
3632 hsw_disable_ips(intel_crtc);
3633
3634 intel_crtc_update_cursor(crtc, false);
3635 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003636 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003637}
3638
Paulo Zanonie4916942013-09-20 16:21:19 -03003639/*
3640 * This implements the workaround described in the "notes" section of the mode
3641 * set sequence documentation. When going from no pipes or single pipe to
3642 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3643 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3644 */
3645static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3646{
3647 struct drm_device *dev = crtc->base.dev;
3648 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3649
3650 /* We want to get the other_active_crtc only if there's only 1 other
3651 * active crtc. */
3652 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3653 if (!crtc_it->active || crtc_it == crtc)
3654 continue;
3655
3656 if (other_active_crtc)
3657 return;
3658
3659 other_active_crtc = crtc_it;
3660 }
3661 if (!other_active_crtc)
3662 return;
3663
3664 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3665 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3666}
3667
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003668static void haswell_crtc_enable(struct drm_crtc *crtc)
3669{
3670 struct drm_device *dev = crtc->dev;
3671 struct drm_i915_private *dev_priv = dev->dev_private;
3672 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3673 struct intel_encoder *encoder;
3674 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003675
3676 WARN_ON(!crtc->enabled);
3677
3678 if (intel_crtc->active)
3679 return;
3680
3681 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003682
3683 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3684 if (intel_crtc->config.has_pch_encoder)
3685 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3686
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003687 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003688 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003689
3690 for_each_encoder_on_crtc(dev, crtc, encoder)
3691 if (encoder->pre_enable)
3692 encoder->pre_enable(encoder);
3693
Paulo Zanoni1f544382012-10-24 11:32:00 -02003694 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003695
Jesse Barnesb074cec2013-04-25 12:55:02 -07003696 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003697
3698 /*
3699 * On ILK+ LUT must be loaded before the pipe is running but with
3700 * clocks enabled
3701 */
3702 intel_crtc_load_lut(crtc);
3703
Paulo Zanoni1f544382012-10-24 11:32:00 -02003704 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003705 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003706
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003707 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003708 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003709 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003710
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003711 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003712 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003713
Jani Nikula8807e552013-08-30 19:40:32 +03003714 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003715 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003716 intel_opregion_notify_encoder(encoder, true);
3717 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003718
Paulo Zanonie4916942013-09-20 16:21:19 -03003719 /* If we change the relative order between pipe/planes enabling, we need
3720 * to change the workaround. */
3721 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003722 haswell_crtc_enable_planes(crtc);
3723
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003724 /*
3725 * There seems to be a race in PCH platform hw (at least on some
3726 * outputs) where an enabled pipe still completes any pageflip right
3727 * away (as if the pipe is off) instead of waiting for vblank. As soon
3728 * as the first vblank happend, everything works as expected. Hence just
3729 * wait for one vblank before returning to avoid strange things
3730 * happening.
3731 */
3732 intel_wait_for_vblank(dev, intel_crtc->pipe);
3733}
3734
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003735static void ironlake_pfit_disable(struct intel_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->base.dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 int pipe = crtc->pipe;
3740
3741 /* To avoid upsetting the power well on haswell only disable the pfit if
3742 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003743 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003744 I915_WRITE(PF_CTL(pipe), 0);
3745 I915_WRITE(PF_WIN_POS(pipe), 0);
3746 I915_WRITE(PF_WIN_SZ(pipe), 0);
3747 }
3748}
3749
Jesse Barnes6be4a602010-09-10 10:26:01 -07003750static void ironlake_crtc_disable(struct drm_crtc *crtc)
3751{
3752 struct drm_device *dev = crtc->dev;
3753 struct drm_i915_private *dev_priv = dev->dev_private;
3754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003755 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003756 int pipe = intel_crtc->pipe;
3757 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003758 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003759
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003760
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003761 if (!intel_crtc->active)
3762 return;
3763
Daniel Vetterea9d7582012-07-10 10:42:52 +02003764 for_each_encoder_on_crtc(dev, crtc, encoder)
3765 encoder->disable(encoder);
3766
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003767 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003768 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003769
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003770 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003771 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003772
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003773 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003774 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003775 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003776
Daniel Vetterd925c592013-06-05 13:34:04 +02003777 if (intel_crtc->config.has_pch_encoder)
3778 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3779
Jesse Barnesb24e7172011-01-04 15:09:30 -08003780 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003781
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003782 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003783
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003784 for_each_encoder_on_crtc(dev, crtc, encoder)
3785 if (encoder->post_disable)
3786 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003787
Daniel Vetterd925c592013-06-05 13:34:04 +02003788 if (intel_crtc->config.has_pch_encoder) {
3789 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003790
Daniel Vetterd925c592013-06-05 13:34:04 +02003791 ironlake_disable_pch_transcoder(dev_priv, pipe);
3792 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003793
Daniel Vetterd925c592013-06-05 13:34:04 +02003794 if (HAS_PCH_CPT(dev)) {
3795 /* disable TRANS_DP_CTL */
3796 reg = TRANS_DP_CTL(pipe);
3797 temp = I915_READ(reg);
3798 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3799 TRANS_DP_PORT_SEL_MASK);
3800 temp |= TRANS_DP_PORT_SEL_NONE;
3801 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003802
Daniel Vetterd925c592013-06-05 13:34:04 +02003803 /* disable DPLL_SEL */
3804 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003805 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003806 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003807 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003808
3809 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003810 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003811
3812 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003813 }
3814
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003815 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003816 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003817
3818 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003819 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003820 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003821}
3822
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003823static void haswell_crtc_disable(struct drm_crtc *crtc)
3824{
3825 struct drm_device *dev = crtc->dev;
3826 struct drm_i915_private *dev_priv = dev->dev_private;
3827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3828 struct intel_encoder *encoder;
3829 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003830 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003831
3832 if (!intel_crtc->active)
3833 return;
3834
Ville Syrjälädda9a662013-09-19 17:00:37 -03003835 haswell_crtc_disable_planes(crtc);
3836
Jani Nikula8807e552013-08-30 19:40:32 +03003837 for_each_encoder_on_crtc(dev, crtc, encoder) {
3838 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003839 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003840 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003841
Paulo Zanoni86642812013-04-12 17:57:57 -03003842 if (intel_crtc->config.has_pch_encoder)
3843 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003844 intel_disable_pipe(dev_priv, pipe);
3845
Paulo Zanoniad80a812012-10-24 16:06:19 -02003846 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003847
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003848 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003849
Paulo Zanoni1f544382012-10-24 11:32:00 -02003850 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003851
3852 for_each_encoder_on_crtc(dev, crtc, encoder)
3853 if (encoder->post_disable)
3854 encoder->post_disable(encoder);
3855
Daniel Vetter88adfff2013-03-28 10:42:01 +01003856 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003857 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003858 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003859 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003860 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003861
3862 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003863 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003864
3865 mutex_lock(&dev->struct_mutex);
3866 intel_update_fbc(dev);
3867 mutex_unlock(&dev->struct_mutex);
3868}
3869
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003870static void ironlake_crtc_off(struct drm_crtc *crtc)
3871{
3872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003873 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003874}
3875
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003876static void haswell_crtc_off(struct drm_crtc *crtc)
3877{
3878 intel_ddi_put_crtc_pll(crtc);
3879}
3880
Daniel Vetter02e792f2009-09-15 22:57:34 +02003881static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3882{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003883 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003884 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003885 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003886
Chris Wilson23f09ce2010-08-12 13:53:37 +01003887 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003888 dev_priv->mm.interruptible = false;
3889 (void) intel_overlay_switch_off(intel_crtc->overlay);
3890 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003891 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003892 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003893
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003894 /* Let userspace switch the overlay on again. In most cases userspace
3895 * has to recompute where to put it anyway.
3896 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003897}
3898
Egbert Eich61bc95c2013-03-04 09:24:38 -05003899/**
3900 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3901 * cursor plane briefly if not already running after enabling the display
3902 * plane.
3903 * This workaround avoids occasional blank screens when self refresh is
3904 * enabled.
3905 */
3906static void
3907g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3908{
3909 u32 cntl = I915_READ(CURCNTR(pipe));
3910
3911 if ((cntl & CURSOR_MODE) == 0) {
3912 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3913
3914 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3915 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3916 intel_wait_for_vblank(dev_priv->dev, pipe);
3917 I915_WRITE(CURCNTR(pipe), cntl);
3918 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3919 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3920 }
3921}
3922
Jesse Barnes2dd24552013-04-25 12:55:01 -07003923static void i9xx_pfit_enable(struct intel_crtc *crtc)
3924{
3925 struct drm_device *dev = crtc->base.dev;
3926 struct drm_i915_private *dev_priv = dev->dev_private;
3927 struct intel_crtc_config *pipe_config = &crtc->config;
3928
Daniel Vetter328d8e82013-05-08 10:36:31 +02003929 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003930 return;
3931
Daniel Vetterc0b03412013-05-28 12:05:54 +02003932 /*
3933 * The panel fitter should only be adjusted whilst the pipe is disabled,
3934 * according to register description and PRM.
3935 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003936 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3937 assert_pipe_disabled(dev_priv, crtc->pipe);
3938
Jesse Barnesb074cec2013-04-25 12:55:02 -07003939 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3940 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003941
3942 /* Border color in case we don't scale up to the full screen. Black by
3943 * default, change to something else for debugging. */
3944 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003945}
3946
Jesse Barnes586f49d2013-11-04 16:06:59 -08003947int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003948{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003949 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003950
Jesse Barnes586f49d2013-11-04 16:06:59 -08003951 /* Obtain SKU information */
3952 mutex_lock(&dev_priv->dpio_lock);
3953 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3954 CCK_FUSE_HPLL_FREQ_MASK;
3955 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003956
Jesse Barnes586f49d2013-11-04 16:06:59 -08003957 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003958}
3959
3960/* Adjust CDclk dividers to allow high res or save power if possible */
3961static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3962{
3963 struct drm_i915_private *dev_priv = dev->dev_private;
3964 u32 val, cmd;
3965
3966 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3967 cmd = 2;
3968 else if (cdclk == 266)
3969 cmd = 1;
3970 else
3971 cmd = 0;
3972
3973 mutex_lock(&dev_priv->rps.hw_lock);
3974 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3975 val &= ~DSPFREQGUAR_MASK;
3976 val |= (cmd << DSPFREQGUAR_SHIFT);
3977 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3978 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3979 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3980 50)) {
3981 DRM_ERROR("timed out waiting for CDclk change\n");
3982 }
3983 mutex_unlock(&dev_priv->rps.hw_lock);
3984
3985 if (cdclk == 400) {
3986 u32 divider, vco;
3987
3988 vco = valleyview_get_vco(dev_priv);
3989 divider = ((vco << 1) / cdclk) - 1;
3990
3991 mutex_lock(&dev_priv->dpio_lock);
3992 /* adjust cdclk divider */
3993 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3994 val &= ~0xf;
3995 val |= divider;
3996 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3997 mutex_unlock(&dev_priv->dpio_lock);
3998 }
3999
4000 mutex_lock(&dev_priv->dpio_lock);
4001 /* adjust self-refresh exit latency value */
4002 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4003 val &= ~0x7f;
4004
4005 /*
4006 * For high bandwidth configs, we set a higher latency in the bunit
4007 * so that the core display fetch happens in time to avoid underruns.
4008 */
4009 if (cdclk == 400)
4010 val |= 4500 / 250; /* 4.5 usec */
4011 else
4012 val |= 3000 / 250; /* 3.0 usec */
4013 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4014 mutex_unlock(&dev_priv->dpio_lock);
4015
4016 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4017 intel_i2c_reset(dev);
4018}
4019
4020static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4021{
4022 int cur_cdclk, vco;
4023 int divider;
4024
4025 vco = valleyview_get_vco(dev_priv);
4026
4027 mutex_lock(&dev_priv->dpio_lock);
4028 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4029 mutex_unlock(&dev_priv->dpio_lock);
4030
4031 divider &= 0xf;
4032
4033 cur_cdclk = (vco << 1) / (divider + 1);
4034
4035 return cur_cdclk;
4036}
4037
4038static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4039 int max_pixclk)
4040{
4041 int cur_cdclk;
4042
4043 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4044
4045 /*
4046 * Really only a few cases to deal with, as only 4 CDclks are supported:
4047 * 200MHz
4048 * 267MHz
4049 * 320MHz
4050 * 400MHz
4051 * So we check to see whether we're above 90% of the lower bin and
4052 * adjust if needed.
4053 */
4054 if (max_pixclk > 288000) {
4055 return 400;
4056 } else if (max_pixclk > 240000) {
4057 return 320;
4058 } else
4059 return 266;
4060 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4061}
4062
4063static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4064 unsigned modeset_pipes,
4065 struct intel_crtc_config *pipe_config)
4066{
4067 struct drm_device *dev = dev_priv->dev;
4068 struct intel_crtc *intel_crtc;
4069 int max_pixclk = 0;
4070
4071 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4072 base.head) {
4073 if (modeset_pipes & (1 << intel_crtc->pipe))
4074 max_pixclk = max(max_pixclk,
4075 pipe_config->adjusted_mode.crtc_clock);
4076 else if (intel_crtc->base.enabled)
4077 max_pixclk = max(max_pixclk,
4078 intel_crtc->config.adjusted_mode.crtc_clock);
4079 }
4080
4081 return max_pixclk;
4082}
4083
4084static void valleyview_modeset_global_pipes(struct drm_device *dev,
4085 unsigned *prepare_pipes,
4086 unsigned modeset_pipes,
4087 struct intel_crtc_config *pipe_config)
4088{
4089 struct drm_i915_private *dev_priv = dev->dev_private;
4090 struct intel_crtc *intel_crtc;
4091 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4092 pipe_config);
4093 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4094
4095 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4096 return;
4097
4098 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4099 base.head)
4100 if (intel_crtc->base.enabled)
4101 *prepare_pipes |= (1 << intel_crtc->pipe);
4102}
4103
4104static void valleyview_modeset_global_resources(struct drm_device *dev)
4105{
4106 struct drm_i915_private *dev_priv = dev->dev_private;
4107 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4108 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4109 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4110
4111 if (req_cdclk != cur_cdclk)
4112 valleyview_set_cdclk(dev, req_cdclk);
4113}
4114
Jesse Barnes89b667f2013-04-18 14:51:36 -07004115static void valleyview_crtc_enable(struct drm_crtc *crtc)
4116{
4117 struct drm_device *dev = crtc->dev;
4118 struct drm_i915_private *dev_priv = dev->dev_private;
4119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4120 struct intel_encoder *encoder;
4121 int pipe = intel_crtc->pipe;
4122 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004123 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004124
4125 WARN_ON(!crtc->enabled);
4126
4127 if (intel_crtc->active)
4128 return;
4129
4130 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004131
Jesse Barnes89b667f2013-04-18 14:51:36 -07004132 for_each_encoder_on_crtc(dev, crtc, encoder)
4133 if (encoder->pre_pll_enable)
4134 encoder->pre_pll_enable(encoder);
4135
Jani Nikula23538ef2013-08-27 15:12:22 +03004136 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4137
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004138 if (!is_dsi)
4139 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004140
4141 for_each_encoder_on_crtc(dev, crtc, encoder)
4142 if (encoder->pre_enable)
4143 encoder->pre_enable(encoder);
4144
Jesse Barnes2dd24552013-04-25 12:55:01 -07004145 i9xx_pfit_enable(intel_crtc);
4146
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004147 intel_crtc_load_lut(crtc);
4148
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004149 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004150 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004151 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004152 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004153 intel_crtc_update_cursor(crtc, true);
4154
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004155 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004156
4157 for_each_encoder_on_crtc(dev, crtc, encoder)
4158 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004159}
4160
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004161static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004162{
4163 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004164 struct drm_i915_private *dev_priv = dev->dev_private;
4165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004166 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004167 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004168 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004169
Daniel Vetter08a48462012-07-02 11:43:47 +02004170 WARN_ON(!crtc->enabled);
4171
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004172 if (intel_crtc->active)
4173 return;
4174
4175 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004176
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004177 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004178 if (encoder->pre_enable)
4179 encoder->pre_enable(encoder);
4180
Daniel Vetterf6736a12013-06-05 13:34:30 +02004181 i9xx_enable_pll(intel_crtc);
4182
Jesse Barnes2dd24552013-04-25 12:55:01 -07004183 i9xx_pfit_enable(intel_crtc);
4184
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004185 intel_crtc_load_lut(crtc);
4186
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004187 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004188 intel_enable_pipe(dev_priv, pipe, false, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004189 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004190 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004191 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004192 if (IS_G4X(dev))
4193 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004194 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004195
4196 /* Give the overlay scaler a chance to enable if it's on this pipe */
4197 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004198
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004199 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004200
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004201 for_each_encoder_on_crtc(dev, crtc, encoder)
4202 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004203}
4204
Daniel Vetter87476d62013-04-11 16:29:06 +02004205static void i9xx_pfit_disable(struct intel_crtc *crtc)
4206{
4207 struct drm_device *dev = crtc->base.dev;
4208 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004209
4210 if (!crtc->config.gmch_pfit.control)
4211 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004212
4213 assert_pipe_disabled(dev_priv, crtc->pipe);
4214
Daniel Vetter328d8e82013-05-08 10:36:31 +02004215 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4216 I915_READ(PFIT_CONTROL));
4217 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004218}
4219
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004220static void i9xx_crtc_disable(struct drm_crtc *crtc)
4221{
4222 struct drm_device *dev = crtc->dev;
4223 struct drm_i915_private *dev_priv = dev->dev_private;
4224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004225 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004226 int pipe = intel_crtc->pipe;
4227 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004228
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004229 if (!intel_crtc->active)
4230 return;
4231
Daniel Vetterea9d7582012-07-10 10:42:52 +02004232 for_each_encoder_on_crtc(dev, crtc, encoder)
4233 encoder->disable(encoder);
4234
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004235 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004236 intel_crtc_wait_for_pending_flips(crtc);
4237 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004238
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004239 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004240 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004241
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004242 intel_crtc_dpms_overlay(intel_crtc, false);
4243 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004244 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004245 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004246
Jesse Barnesb24e7172011-01-04 15:09:30 -08004247 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004248
Daniel Vetter87476d62013-04-11 16:29:06 +02004249 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004250
Jesse Barnes89b667f2013-04-18 14:51:36 -07004251 for_each_encoder_on_crtc(dev, crtc, encoder)
4252 if (encoder->post_disable)
4253 encoder->post_disable(encoder);
4254
Jesse Barnesf6071162013-10-01 10:41:38 -07004255 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4256 vlv_disable_pll(dev_priv, pipe);
4257 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004258 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004259
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004260 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004261 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004262
Chris Wilson6b383a72010-09-13 13:54:26 +01004263 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004264}
4265
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004266static void i9xx_crtc_off(struct drm_crtc *crtc)
4267{
4268}
4269
Daniel Vetter976f8a22012-07-08 22:34:21 +02004270static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4271 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004272{
4273 struct drm_device *dev = crtc->dev;
4274 struct drm_i915_master_private *master_priv;
4275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4276 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004277
4278 if (!dev->primary->master)
4279 return;
4280
4281 master_priv = dev->primary->master->driver_priv;
4282 if (!master_priv->sarea_priv)
4283 return;
4284
Jesse Barnes79e53942008-11-07 14:24:08 -08004285 switch (pipe) {
4286 case 0:
4287 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4288 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4289 break;
4290 case 1:
4291 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4292 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4293 break;
4294 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004295 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004296 break;
4297 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004298}
4299
Daniel Vetter976f8a22012-07-08 22:34:21 +02004300/**
4301 * Sets the power management mode of the pipe and plane.
4302 */
4303void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004304{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004305 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004306 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004307 struct intel_encoder *intel_encoder;
4308 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004309
Daniel Vetter976f8a22012-07-08 22:34:21 +02004310 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4311 enable |= intel_encoder->connectors_active;
4312
4313 if (enable)
4314 dev_priv->display.crtc_enable(crtc);
4315 else
4316 dev_priv->display.crtc_disable(crtc);
4317
4318 intel_crtc_update_sarea(crtc, enable);
4319}
4320
Daniel Vetter976f8a22012-07-08 22:34:21 +02004321static void intel_crtc_disable(struct drm_crtc *crtc)
4322{
4323 struct drm_device *dev = crtc->dev;
4324 struct drm_connector *connector;
4325 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004327
4328 /* crtc should still be enabled when we disable it. */
4329 WARN_ON(!crtc->enabled);
4330
4331 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004332 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004333 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004334 dev_priv->display.off(crtc);
4335
Chris Wilson931872f2012-01-16 23:01:13 +00004336 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004337 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004338 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004339
4340 if (crtc->fb) {
4341 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004342 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004343 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004344 crtc->fb = NULL;
4345 }
4346
4347 /* Update computed state. */
4348 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4349 if (!connector->encoder || !connector->encoder->crtc)
4350 continue;
4351
4352 if (connector->encoder->crtc != crtc)
4353 continue;
4354
4355 connector->dpms = DRM_MODE_DPMS_OFF;
4356 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004357 }
4358}
4359
Chris Wilsonea5b2132010-08-04 13:50:23 +01004360void intel_encoder_destroy(struct drm_encoder *encoder)
4361{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004362 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004363
Chris Wilsonea5b2132010-08-04 13:50:23 +01004364 drm_encoder_cleanup(encoder);
4365 kfree(intel_encoder);
4366}
4367
Damien Lespiau92373292013-08-08 22:28:57 +01004368/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004369 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4370 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004371static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004372{
4373 if (mode == DRM_MODE_DPMS_ON) {
4374 encoder->connectors_active = true;
4375
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004376 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004377 } else {
4378 encoder->connectors_active = false;
4379
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004380 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004381 }
4382}
4383
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004384/* Cross check the actual hw state with our own modeset state tracking (and it's
4385 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004386static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004387{
4388 if (connector->get_hw_state(connector)) {
4389 struct intel_encoder *encoder = connector->encoder;
4390 struct drm_crtc *crtc;
4391 bool encoder_enabled;
4392 enum pipe pipe;
4393
4394 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4395 connector->base.base.id,
4396 drm_get_connector_name(&connector->base));
4397
4398 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4399 "wrong connector dpms state\n");
4400 WARN(connector->base.encoder != &encoder->base,
4401 "active connector not linked to encoder\n");
4402 WARN(!encoder->connectors_active,
4403 "encoder->connectors_active not set\n");
4404
4405 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4406 WARN(!encoder_enabled, "encoder not enabled\n");
4407 if (WARN_ON(!encoder->base.crtc))
4408 return;
4409
4410 crtc = encoder->base.crtc;
4411
4412 WARN(!crtc->enabled, "crtc not enabled\n");
4413 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4414 WARN(pipe != to_intel_crtc(crtc)->pipe,
4415 "encoder active on the wrong pipe\n");
4416 }
4417}
4418
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004419/* Even simpler default implementation, if there's really no special case to
4420 * consider. */
4421void intel_connector_dpms(struct drm_connector *connector, int mode)
4422{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004423 /* All the simple cases only support two dpms states. */
4424 if (mode != DRM_MODE_DPMS_ON)
4425 mode = DRM_MODE_DPMS_OFF;
4426
4427 if (mode == connector->dpms)
4428 return;
4429
4430 connector->dpms = mode;
4431
4432 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004433 if (connector->encoder)
4434 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004435
Daniel Vetterb9805142012-08-31 17:37:33 +02004436 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004437}
4438
Daniel Vetterf0947c32012-07-02 13:10:34 +02004439/* Simple connector->get_hw_state implementation for encoders that support only
4440 * one connector and no cloning and hence the encoder state determines the state
4441 * of the connector. */
4442bool intel_connector_get_hw_state(struct intel_connector *connector)
4443{
Daniel Vetter24929352012-07-02 20:28:59 +02004444 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004445 struct intel_encoder *encoder = connector->encoder;
4446
4447 return encoder->get_hw_state(encoder, &pipe);
4448}
4449
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004450static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4451 struct intel_crtc_config *pipe_config)
4452{
4453 struct drm_i915_private *dev_priv = dev->dev_private;
4454 struct intel_crtc *pipe_B_crtc =
4455 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4456
4457 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4458 pipe_name(pipe), pipe_config->fdi_lanes);
4459 if (pipe_config->fdi_lanes > 4) {
4460 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4461 pipe_name(pipe), pipe_config->fdi_lanes);
4462 return false;
4463 }
4464
Paulo Zanonibafb6552013-11-02 21:07:44 -07004465 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004466 if (pipe_config->fdi_lanes > 2) {
4467 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4468 pipe_config->fdi_lanes);
4469 return false;
4470 } else {
4471 return true;
4472 }
4473 }
4474
4475 if (INTEL_INFO(dev)->num_pipes == 2)
4476 return true;
4477
4478 /* Ivybridge 3 pipe is really complicated */
4479 switch (pipe) {
4480 case PIPE_A:
4481 return true;
4482 case PIPE_B:
4483 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4484 pipe_config->fdi_lanes > 2) {
4485 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4486 pipe_name(pipe), pipe_config->fdi_lanes);
4487 return false;
4488 }
4489 return true;
4490 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004491 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004492 pipe_B_crtc->config.fdi_lanes <= 2) {
4493 if (pipe_config->fdi_lanes > 2) {
4494 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4495 pipe_name(pipe), pipe_config->fdi_lanes);
4496 return false;
4497 }
4498 } else {
4499 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4500 return false;
4501 }
4502 return true;
4503 default:
4504 BUG();
4505 }
4506}
4507
Daniel Vettere29c22c2013-02-21 00:00:16 +01004508#define RETRY 1
4509static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4510 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004511{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004512 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004513 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004514 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004515 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004516
Daniel Vettere29c22c2013-02-21 00:00:16 +01004517retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004518 /* FDI is a binary signal running at ~2.7GHz, encoding
4519 * each output octet as 10 bits. The actual frequency
4520 * is stored as a divider into a 100MHz clock, and the
4521 * mode pixel clock is stored in units of 1KHz.
4522 * Hence the bw of each lane in terms of the mode signal
4523 * is:
4524 */
4525 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4526
Damien Lespiau241bfc32013-09-25 16:45:37 +01004527 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004528
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004529 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004530 pipe_config->pipe_bpp);
4531
4532 pipe_config->fdi_lanes = lane;
4533
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004534 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004535 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004536
Daniel Vettere29c22c2013-02-21 00:00:16 +01004537 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4538 intel_crtc->pipe, pipe_config);
4539 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4540 pipe_config->pipe_bpp -= 2*3;
4541 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4542 pipe_config->pipe_bpp);
4543 needs_recompute = true;
4544 pipe_config->bw_constrained = true;
4545
4546 goto retry;
4547 }
4548
4549 if (needs_recompute)
4550 return RETRY;
4551
4552 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004553}
4554
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004555static void hsw_compute_ips_config(struct intel_crtc *crtc,
4556 struct intel_crtc_config *pipe_config)
4557{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004558 pipe_config->ips_enabled = i915_enable_ips &&
4559 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004560 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004561}
4562
Daniel Vettera43f6e02013-06-07 23:10:32 +02004563static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004564 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004565{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004566 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004567 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004568
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004569 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004570 if (INTEL_INFO(dev)->gen < 4) {
4571 struct drm_i915_private *dev_priv = dev->dev_private;
4572 int clock_limit =
4573 dev_priv->display.get_display_clock_speed(dev);
4574
4575 /*
4576 * Enable pixel doubling when the dot clock
4577 * is > 90% of the (display) core speed.
4578 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004579 * GDG double wide on either pipe,
4580 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004581 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004582 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004583 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004584 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004585 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004586 }
4587
Damien Lespiau241bfc32013-09-25 16:45:37 +01004588 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004589 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004590 }
Chris Wilson89749352010-09-12 18:25:19 +01004591
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004592 /*
4593 * Pipe horizontal size must be even in:
4594 * - DVO ganged mode
4595 * - LVDS dual channel mode
4596 * - Double wide pipe
4597 */
4598 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4599 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4600 pipe_config->pipe_src_w &= ~1;
4601
Damien Lespiau8693a822013-05-03 18:48:11 +01004602 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4603 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004604 */
4605 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4606 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004607 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004608
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004609 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004610 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004611 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004612 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4613 * for lvds. */
4614 pipe_config->pipe_bpp = 8*3;
4615 }
4616
Damien Lespiauf5adf942013-06-24 18:29:34 +01004617 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004618 hsw_compute_ips_config(crtc, pipe_config);
4619
4620 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4621 * clock survives for now. */
4622 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4623 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004624
Daniel Vetter877d48d2013-04-19 11:24:43 +02004625 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004626 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004627
Daniel Vettere29c22c2013-02-21 00:00:16 +01004628 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004629}
4630
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004631static int valleyview_get_display_clock_speed(struct drm_device *dev)
4632{
4633 return 400000; /* FIXME */
4634}
4635
Jesse Barnese70236a2009-09-21 10:42:27 -07004636static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004637{
Jesse Barnese70236a2009-09-21 10:42:27 -07004638 return 400000;
4639}
Jesse Barnes79e53942008-11-07 14:24:08 -08004640
Jesse Barnese70236a2009-09-21 10:42:27 -07004641static int i915_get_display_clock_speed(struct drm_device *dev)
4642{
4643 return 333000;
4644}
Jesse Barnes79e53942008-11-07 14:24:08 -08004645
Jesse Barnese70236a2009-09-21 10:42:27 -07004646static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4647{
4648 return 200000;
4649}
Jesse Barnes79e53942008-11-07 14:24:08 -08004650
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004651static int pnv_get_display_clock_speed(struct drm_device *dev)
4652{
4653 u16 gcfgc = 0;
4654
4655 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4656
4657 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4658 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4659 return 267000;
4660 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4661 return 333000;
4662 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4663 return 444000;
4664 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4665 return 200000;
4666 default:
4667 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4668 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4669 return 133000;
4670 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4671 return 167000;
4672 }
4673}
4674
Jesse Barnese70236a2009-09-21 10:42:27 -07004675static int i915gm_get_display_clock_speed(struct drm_device *dev)
4676{
4677 u16 gcfgc = 0;
4678
4679 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4680
4681 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004682 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004683 else {
4684 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4685 case GC_DISPLAY_CLOCK_333_MHZ:
4686 return 333000;
4687 default:
4688 case GC_DISPLAY_CLOCK_190_200_MHZ:
4689 return 190000;
4690 }
4691 }
4692}
Jesse Barnes79e53942008-11-07 14:24:08 -08004693
Jesse Barnese70236a2009-09-21 10:42:27 -07004694static int i865_get_display_clock_speed(struct drm_device *dev)
4695{
4696 return 266000;
4697}
4698
4699static int i855_get_display_clock_speed(struct drm_device *dev)
4700{
4701 u16 hpllcc = 0;
4702 /* Assume that the hardware is in the high speed state. This
4703 * should be the default.
4704 */
4705 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4706 case GC_CLOCK_133_200:
4707 case GC_CLOCK_100_200:
4708 return 200000;
4709 case GC_CLOCK_166_250:
4710 return 250000;
4711 case GC_CLOCK_100_133:
4712 return 133000;
4713 }
4714
4715 /* Shouldn't happen */
4716 return 0;
4717}
4718
4719static int i830_get_display_clock_speed(struct drm_device *dev)
4720{
4721 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004722}
4723
Zhenyu Wang2c072452009-06-05 15:38:42 +08004724static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004725intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004726{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004727 while (*num > DATA_LINK_M_N_MASK ||
4728 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004729 *num >>= 1;
4730 *den >>= 1;
4731 }
4732}
4733
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004734static void compute_m_n(unsigned int m, unsigned int n,
4735 uint32_t *ret_m, uint32_t *ret_n)
4736{
4737 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4738 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4739 intel_reduce_m_n_ratio(ret_m, ret_n);
4740}
4741
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004742void
4743intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4744 int pixel_clock, int link_clock,
4745 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004746{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004747 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004748
4749 compute_m_n(bits_per_pixel * pixel_clock,
4750 link_clock * nlanes * 8,
4751 &m_n->gmch_m, &m_n->gmch_n);
4752
4753 compute_m_n(pixel_clock, link_clock,
4754 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004755}
4756
Chris Wilsona7615032011-01-12 17:04:08 +00004757static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4758{
Keith Packard72bbe582011-09-26 16:09:45 -07004759 if (i915_panel_use_ssc >= 0)
4760 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004761 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004762 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004763}
4764
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004765static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4766{
4767 struct drm_device *dev = crtc->dev;
4768 struct drm_i915_private *dev_priv = dev->dev_private;
4769 int refclk;
4770
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004771 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004772 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004773 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004774 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02004775 refclk = dev_priv->vbt.lvds_ssc_freq;
4776 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004777 } else if (!IS_GEN2(dev)) {
4778 refclk = 96000;
4779 } else {
4780 refclk = 48000;
4781 }
4782
4783 return refclk;
4784}
4785
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004786static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004787{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004788 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004789}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004790
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004791static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4792{
4793 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004794}
4795
Daniel Vetterf47709a2013-03-28 10:42:02 +01004796static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004797 intel_clock_t *reduced_clock)
4798{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004799 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004800 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004801 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004802 u32 fp, fp2 = 0;
4803
4804 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004805 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004806 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004807 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004808 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004809 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004810 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004811 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004812 }
4813
4814 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004815 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004816
Daniel Vetterf47709a2013-03-28 10:42:02 +01004817 crtc->lowfreq_avail = false;
4818 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004819 reduced_clock && i915_powersave) {
4820 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004821 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004822 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004823 } else {
4824 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004825 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004826 }
4827}
4828
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004829static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4830 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004831{
4832 u32 reg_val;
4833
4834 /*
4835 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4836 * and set it to a reasonable value instead.
4837 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004838 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004839 reg_val &= 0xffffff00;
4840 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004841 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004842
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004843 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004844 reg_val &= 0x8cffffff;
4845 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004846 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004847
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004848 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004849 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004850 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004851
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004852 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004853 reg_val &= 0x00ffffff;
4854 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004855 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004856}
4857
Daniel Vetterb5518422013-05-03 11:49:48 +02004858static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4859 struct intel_link_m_n *m_n)
4860{
4861 struct drm_device *dev = crtc->base.dev;
4862 struct drm_i915_private *dev_priv = dev->dev_private;
4863 int pipe = crtc->pipe;
4864
Daniel Vettere3b95f12013-05-03 11:49:49 +02004865 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4866 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4867 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4868 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004869}
4870
4871static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4872 struct intel_link_m_n *m_n)
4873{
4874 struct drm_device *dev = crtc->base.dev;
4875 struct drm_i915_private *dev_priv = dev->dev_private;
4876 int pipe = crtc->pipe;
4877 enum transcoder transcoder = crtc->config.cpu_transcoder;
4878
4879 if (INTEL_INFO(dev)->gen >= 5) {
4880 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4881 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4882 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4883 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4884 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004885 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4886 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4887 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4888 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004889 }
4890}
4891
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004892static void intel_dp_set_m_n(struct intel_crtc *crtc)
4893{
4894 if (crtc->config.has_pch_encoder)
4895 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4896 else
4897 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4898}
4899
Daniel Vetterf47709a2013-03-28 10:42:02 +01004900static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004901{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004902 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004903 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004904 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004905 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004906 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004907 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004908
Daniel Vetter09153002012-12-12 14:06:44 +01004909 mutex_lock(&dev_priv->dpio_lock);
4910
Daniel Vetterf47709a2013-03-28 10:42:02 +01004911 bestn = crtc->config.dpll.n;
4912 bestm1 = crtc->config.dpll.m1;
4913 bestm2 = crtc->config.dpll.m2;
4914 bestp1 = crtc->config.dpll.p1;
4915 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004916
Jesse Barnes89b667f2013-04-18 14:51:36 -07004917 /* See eDP HDMI DPIO driver vbios notes doc */
4918
4919 /* PLL B needs special handling */
4920 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004921 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004922
4923 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004924 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004925
4926 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004927 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004928 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004929 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004930
4931 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004932 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004933
4934 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004935 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4936 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4937 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004938 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004939
4940 /*
4941 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4942 * but we don't support that).
4943 * Note: don't use the DAC post divider as it seems unstable.
4944 */
4945 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004946 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004947
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004948 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004950
Jesse Barnes89b667f2013-04-18 14:51:36 -07004951 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004952 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004953 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004954 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004956 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004957 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004958 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004959 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004960
Jesse Barnes89b667f2013-04-18 14:51:36 -07004961 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4962 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4963 /* Use SSC source */
4964 if (!pipe)
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 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004968 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004969 0x0df70000);
4970 } else { /* HDMI or VGA */
4971 /* Use bend source */
4972 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004974 0x0df70000);
4975 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004976 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004977 0x0df40000);
4978 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004979
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004980 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004981 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4982 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4983 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4984 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004985 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004986
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004988
Jesse Barnes89b667f2013-04-18 14:51:36 -07004989 /* Enable DPIO clock input */
4990 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4991 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004992 /* We should never disable this, set it here for state tracking */
4993 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004994 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004995 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004996 crtc->config.dpll_hw_state.dpll = dpll;
4997
Daniel Vetteref1b4602013-06-01 17:17:04 +02004998 dpll_md = (crtc->config.pixel_multiplier - 1)
4999 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005000 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5001
Daniel Vetterf47709a2013-03-28 10:42:02 +01005002 if (crtc->config.has_dp_encoder)
5003 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305004
Daniel Vetter09153002012-12-12 14:06:44 +01005005 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005006}
5007
Daniel Vetterf47709a2013-03-28 10:42:02 +01005008static void i9xx_update_pll(struct intel_crtc *crtc,
5009 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005010 int num_connectors)
5011{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005012 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005013 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005014 u32 dpll;
5015 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005016 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005017
Daniel Vetterf47709a2013-03-28 10:42:02 +01005018 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305019
Daniel Vetterf47709a2013-03-28 10:42:02 +01005020 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5021 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005022
5023 dpll = DPLL_VGA_MODE_DIS;
5024
Daniel Vetterf47709a2013-03-28 10:42:02 +01005025 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005026 dpll |= DPLLB_MODE_LVDS;
5027 else
5028 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005029
Daniel Vetteref1b4602013-06-01 17:17:04 +02005030 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005031 dpll |= (crtc->config.pixel_multiplier - 1)
5032 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005033 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005034
5035 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005036 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005037
Daniel Vetterf47709a2013-03-28 10:42:02 +01005038 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005039 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005040
5041 /* compute bitmask from p1 value */
5042 if (IS_PINEVIEW(dev))
5043 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5044 else {
5045 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5046 if (IS_G4X(dev) && reduced_clock)
5047 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5048 }
5049 switch (clock->p2) {
5050 case 5:
5051 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5052 break;
5053 case 7:
5054 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5055 break;
5056 case 10:
5057 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5058 break;
5059 case 14:
5060 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5061 break;
5062 }
5063 if (INTEL_INFO(dev)->gen >= 4)
5064 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5065
Daniel Vetter09ede542013-04-30 14:01:45 +02005066 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005067 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005068 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005069 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5070 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5071 else
5072 dpll |= PLL_REF_INPUT_DREFCLK;
5073
5074 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005075 crtc->config.dpll_hw_state.dpll = dpll;
5076
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005077 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005078 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5079 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005080 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005081 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005082
5083 if (crtc->config.has_dp_encoder)
5084 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005085}
5086
Daniel Vetterf47709a2013-03-28 10:42:02 +01005087static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005088 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005089 int num_connectors)
5090{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005091 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005092 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005093 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005094 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005095
Daniel Vetterf47709a2013-03-28 10:42:02 +01005096 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305097
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005098 dpll = DPLL_VGA_MODE_DIS;
5099
Daniel Vetterf47709a2013-03-28 10:42:02 +01005100 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005101 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5102 } else {
5103 if (clock->p1 == 2)
5104 dpll |= PLL_P1_DIVIDE_BY_TWO;
5105 else
5106 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5107 if (clock->p2 == 4)
5108 dpll |= PLL_P2_DIVIDE_BY_4;
5109 }
5110
Daniel Vetter4a33e482013-07-06 12:52:05 +02005111 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5112 dpll |= DPLL_DVO_2X_MODE;
5113
Daniel Vetterf47709a2013-03-28 10:42:02 +01005114 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005115 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5116 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5117 else
5118 dpll |= PLL_REF_INPUT_DREFCLK;
5119
5120 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005121 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005122}
5123
Daniel Vetter8a654f32013-06-01 17:16:22 +02005124static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005125{
5126 struct drm_device *dev = intel_crtc->base.dev;
5127 struct drm_i915_private *dev_priv = dev->dev_private;
5128 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005129 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005130 struct drm_display_mode *adjusted_mode =
5131 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005132 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5133
5134 /* We need to be careful not to changed the adjusted mode, for otherwise
5135 * the hw state checker will get angry at the mismatch. */
5136 crtc_vtotal = adjusted_mode->crtc_vtotal;
5137 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005138
5139 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5140 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005141 crtc_vtotal -= 1;
5142 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005143 vsyncshift = adjusted_mode->crtc_hsync_start
5144 - adjusted_mode->crtc_htotal / 2;
5145 } else {
5146 vsyncshift = 0;
5147 }
5148
5149 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005150 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005151
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005152 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005153 (adjusted_mode->crtc_hdisplay - 1) |
5154 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005155 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005156 (adjusted_mode->crtc_hblank_start - 1) |
5157 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005158 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005159 (adjusted_mode->crtc_hsync_start - 1) |
5160 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5161
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005162 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005163 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005164 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005165 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005166 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005167 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005168 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005169 (adjusted_mode->crtc_vsync_start - 1) |
5170 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5171
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005172 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5173 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5174 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5175 * bits. */
5176 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5177 (pipe == PIPE_B || pipe == PIPE_C))
5178 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5179
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005180 /* pipesrc controls the size that is scaled from, which should
5181 * always be the user's requested size.
5182 */
5183 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005184 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5185 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005186}
5187
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005188static void intel_get_pipe_timings(struct intel_crtc *crtc,
5189 struct intel_crtc_config *pipe_config)
5190{
5191 struct drm_device *dev = crtc->base.dev;
5192 struct drm_i915_private *dev_priv = dev->dev_private;
5193 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5194 uint32_t tmp;
5195
5196 tmp = I915_READ(HTOTAL(cpu_transcoder));
5197 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5198 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5199 tmp = I915_READ(HBLANK(cpu_transcoder));
5200 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5201 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5202 tmp = I915_READ(HSYNC(cpu_transcoder));
5203 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5204 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5205
5206 tmp = I915_READ(VTOTAL(cpu_transcoder));
5207 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5208 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5209 tmp = I915_READ(VBLANK(cpu_transcoder));
5210 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5211 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5212 tmp = I915_READ(VSYNC(cpu_transcoder));
5213 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5214 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5215
5216 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5217 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5218 pipe_config->adjusted_mode.crtc_vtotal += 1;
5219 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5220 }
5221
5222 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005223 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5224 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5225
5226 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5227 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005228}
5229
Jesse Barnesbabea612013-06-26 18:57:38 +03005230static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5231 struct intel_crtc_config *pipe_config)
5232{
5233 struct drm_crtc *crtc = &intel_crtc->base;
5234
5235 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5236 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5237 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5238 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5239
5240 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5241 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5242 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5243 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5244
5245 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5246
Damien Lespiau241bfc32013-09-25 16:45:37 +01005247 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005248 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5249}
5250
Daniel Vetter84b046f2013-02-19 18:48:54 +01005251static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5252{
5253 struct drm_device *dev = intel_crtc->base.dev;
5254 struct drm_i915_private *dev_priv = dev->dev_private;
5255 uint32_t pipeconf;
5256
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005257 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005258
Daniel Vetter67c72a12013-09-24 11:46:14 +02005259 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5260 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5261 pipeconf |= PIPECONF_ENABLE;
5262
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005263 if (intel_crtc->config.double_wide)
5264 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005265
Daniel Vetterff9ce462013-04-24 14:57:17 +02005266 /* only g4x and later have fancy bpc/dither controls */
5267 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005268 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5269 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5270 pipeconf |= PIPECONF_DITHER_EN |
5271 PIPECONF_DITHER_TYPE_SP;
5272
5273 switch (intel_crtc->config.pipe_bpp) {
5274 case 18:
5275 pipeconf |= PIPECONF_6BPC;
5276 break;
5277 case 24:
5278 pipeconf |= PIPECONF_8BPC;
5279 break;
5280 case 30:
5281 pipeconf |= PIPECONF_10BPC;
5282 break;
5283 default:
5284 /* Case prevented by intel_choose_pipe_bpp_dither. */
5285 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005286 }
5287 }
5288
5289 if (HAS_PIPE_CXSR(dev)) {
5290 if (intel_crtc->lowfreq_avail) {
5291 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5292 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5293 } else {
5294 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005295 }
5296 }
5297
Daniel Vetter84b046f2013-02-19 18:48:54 +01005298 if (!IS_GEN2(dev) &&
5299 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5300 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5301 else
5302 pipeconf |= PIPECONF_PROGRESSIVE;
5303
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005304 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5305 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005306
Daniel Vetter84b046f2013-02-19 18:48:54 +01005307 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5308 POSTING_READ(PIPECONF(intel_crtc->pipe));
5309}
5310
Eric Anholtf564048e2011-03-30 13:01:02 -07005311static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005312 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005313 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005314{
5315 struct drm_device *dev = crtc->dev;
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5318 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005319 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005320 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005321 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005322 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005323 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005324 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005325 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005326 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005327 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005328
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005329 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005330 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005331 case INTEL_OUTPUT_LVDS:
5332 is_lvds = true;
5333 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005334 case INTEL_OUTPUT_DSI:
5335 is_dsi = true;
5336 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005337 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005338
Eric Anholtc751ce42010-03-25 11:48:48 -07005339 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005340 }
5341
Jani Nikulaf2335332013-09-13 11:03:09 +03005342 if (is_dsi)
5343 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005344
Jani Nikulaf2335332013-09-13 11:03:09 +03005345 if (!intel_crtc->config.clock_set) {
5346 refclk = i9xx_get_refclk(crtc, num_connectors);
5347
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005348 /*
5349 * Returns a set of divisors for the desired target clock with
5350 * the given refclk, or FALSE. The returned values represent
5351 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5352 * 2) / p1 / p2.
5353 */
5354 limit = intel_limit(crtc, refclk);
5355 ok = dev_priv->display.find_dpll(limit, crtc,
5356 intel_crtc->config.port_clock,
5357 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005358 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005359 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5360 return -EINVAL;
5361 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005362
Jani Nikulaf2335332013-09-13 11:03:09 +03005363 if (is_lvds && dev_priv->lvds_downclock_avail) {
5364 /*
5365 * Ensure we match the reduced clock's P to the target
5366 * clock. If the clocks don't match, we can't switch
5367 * the display clock by using the FP0/FP1. In such case
5368 * we will disable the LVDS downclock feature.
5369 */
5370 has_reduced_clock =
5371 dev_priv->display.find_dpll(limit, crtc,
5372 dev_priv->lvds_downclock,
5373 refclk, &clock,
5374 &reduced_clock);
5375 }
5376 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005377 intel_crtc->config.dpll.n = clock.n;
5378 intel_crtc->config.dpll.m1 = clock.m1;
5379 intel_crtc->config.dpll.m2 = clock.m2;
5380 intel_crtc->config.dpll.p1 = clock.p1;
5381 intel_crtc->config.dpll.p2 = clock.p2;
5382 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005383
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005384 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005385 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305386 has_reduced_clock ? &reduced_clock : NULL,
5387 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005388 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005389 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005390 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005391 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005392 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005393 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005394 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005395
Jani Nikulaf2335332013-09-13 11:03:09 +03005396skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005397 /* Set up the display plane register */
5398 dspcntr = DISPPLANE_GAMMA_ENABLE;
5399
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005400 if (!IS_VALLEYVIEW(dev)) {
5401 if (pipe == 0)
5402 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5403 else
5404 dspcntr |= DISPPLANE_SEL_PIPE_B;
5405 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005406
Daniel Vetter8a654f32013-06-01 17:16:22 +02005407 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005408
5409 /* pipesrc and dspsize control the size that is scaled from,
5410 * which should always be the user's requested size.
5411 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005412 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005413 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5414 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005415 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005416
Daniel Vetter84b046f2013-02-19 18:48:54 +01005417 i9xx_set_pipeconf(intel_crtc);
5418
Eric Anholtf564048e2011-03-30 13:01:02 -07005419 I915_WRITE(DSPCNTR(plane), dspcntr);
5420 POSTING_READ(DSPCNTR(plane));
5421
Daniel Vetter94352cf2012-07-05 22:51:56 +02005422 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005423
Eric Anholtf564048e2011-03-30 13:01:02 -07005424 return ret;
5425}
5426
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005427static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5428 struct intel_crtc_config *pipe_config)
5429{
5430 struct drm_device *dev = crtc->base.dev;
5431 struct drm_i915_private *dev_priv = dev->dev_private;
5432 uint32_t tmp;
5433
5434 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005435 if (!(tmp & PFIT_ENABLE))
5436 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005437
Daniel Vetter06922822013-07-11 13:35:40 +02005438 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005439 if (INTEL_INFO(dev)->gen < 4) {
5440 if (crtc->pipe != PIPE_B)
5441 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005442 } else {
5443 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5444 return;
5445 }
5446
Daniel Vetter06922822013-07-11 13:35:40 +02005447 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005448 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5449 if (INTEL_INFO(dev)->gen < 5)
5450 pipe_config->gmch_pfit.lvds_border_bits =
5451 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5452}
5453
Jesse Barnesacbec812013-09-20 11:29:32 -07005454static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5455 struct intel_crtc_config *pipe_config)
5456{
5457 struct drm_device *dev = crtc->base.dev;
5458 struct drm_i915_private *dev_priv = dev->dev_private;
5459 int pipe = pipe_config->cpu_transcoder;
5460 intel_clock_t clock;
5461 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005462 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005463
5464 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005465 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005466 mutex_unlock(&dev_priv->dpio_lock);
5467
5468 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5469 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5470 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5471 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5472 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5473
Ville Syrjäläf6466282013-10-14 14:50:31 +03005474 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005475
Ville Syrjäläf6466282013-10-14 14:50:31 +03005476 /* clock.dot is the fast clock */
5477 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005478}
5479
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005480static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5481 struct intel_crtc_config *pipe_config)
5482{
5483 struct drm_device *dev = crtc->base.dev;
5484 struct drm_i915_private *dev_priv = dev->dev_private;
5485 uint32_t tmp;
5486
Daniel Vettere143a212013-07-04 12:01:15 +02005487 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005488 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005489
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005490 tmp = I915_READ(PIPECONF(crtc->pipe));
5491 if (!(tmp & PIPECONF_ENABLE))
5492 return false;
5493
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005494 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5495 switch (tmp & PIPECONF_BPC_MASK) {
5496 case PIPECONF_6BPC:
5497 pipe_config->pipe_bpp = 18;
5498 break;
5499 case PIPECONF_8BPC:
5500 pipe_config->pipe_bpp = 24;
5501 break;
5502 case PIPECONF_10BPC:
5503 pipe_config->pipe_bpp = 30;
5504 break;
5505 default:
5506 break;
5507 }
5508 }
5509
Ville Syrjälä282740f2013-09-04 18:30:03 +03005510 if (INTEL_INFO(dev)->gen < 4)
5511 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5512
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005513 intel_get_pipe_timings(crtc, pipe_config);
5514
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005515 i9xx_get_pfit_config(crtc, pipe_config);
5516
Daniel Vetter6c49f242013-06-06 12:45:25 +02005517 if (INTEL_INFO(dev)->gen >= 4) {
5518 tmp = I915_READ(DPLL_MD(crtc->pipe));
5519 pipe_config->pixel_multiplier =
5520 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5521 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005522 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005523 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5524 tmp = I915_READ(DPLL(crtc->pipe));
5525 pipe_config->pixel_multiplier =
5526 ((tmp & SDVO_MULTIPLIER_MASK)
5527 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5528 } else {
5529 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5530 * port and will be fixed up in the encoder->get_config
5531 * function. */
5532 pipe_config->pixel_multiplier = 1;
5533 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005534 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5535 if (!IS_VALLEYVIEW(dev)) {
5536 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5537 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005538 } else {
5539 /* Mask out read-only status bits. */
5540 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5541 DPLL_PORTC_READY_MASK |
5542 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005543 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005544
Jesse Barnesacbec812013-09-20 11:29:32 -07005545 if (IS_VALLEYVIEW(dev))
5546 vlv_crtc_clock_get(crtc, pipe_config);
5547 else
5548 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005549
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005550 return true;
5551}
5552
Paulo Zanonidde86e22012-12-01 12:04:25 -02005553static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005554{
5555 struct drm_i915_private *dev_priv = dev->dev_private;
5556 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005557 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005558 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005559 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005560 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005561 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005562 bool has_ck505 = false;
5563 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005564
5565 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005566 list_for_each_entry(encoder, &mode_config->encoder_list,
5567 base.head) {
5568 switch (encoder->type) {
5569 case INTEL_OUTPUT_LVDS:
5570 has_panel = true;
5571 has_lvds = true;
5572 break;
5573 case INTEL_OUTPUT_EDP:
5574 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005575 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005576 has_cpu_edp = true;
5577 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005578 }
5579 }
5580
Keith Packard99eb6a02011-09-26 14:29:12 -07005581 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005582 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005583 can_ssc = has_ck505;
5584 } else {
5585 has_ck505 = false;
5586 can_ssc = true;
5587 }
5588
Imre Deak2de69052013-05-08 13:14:04 +03005589 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5590 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005591
5592 /* Ironlake: try to setup display ref clock before DPLL
5593 * enabling. This is only under driver's control after
5594 * PCH B stepping, previous chipset stepping should be
5595 * ignoring this setting.
5596 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005597 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005598
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005599 /* As we must carefully and slowly disable/enable each source in turn,
5600 * compute the final state we want first and check if we need to
5601 * make any changes at all.
5602 */
5603 final = val;
5604 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005605 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005606 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005607 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005608 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5609
5610 final &= ~DREF_SSC_SOURCE_MASK;
5611 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5612 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005613
Keith Packard199e5d72011-09-22 12:01:57 -07005614 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005615 final |= DREF_SSC_SOURCE_ENABLE;
5616
5617 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5618 final |= DREF_SSC1_ENABLE;
5619
5620 if (has_cpu_edp) {
5621 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5622 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5623 else
5624 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5625 } else
5626 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5627 } else {
5628 final |= DREF_SSC_SOURCE_DISABLE;
5629 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5630 }
5631
5632 if (final == val)
5633 return;
5634
5635 /* Always enable nonspread source */
5636 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5637
5638 if (has_ck505)
5639 val |= DREF_NONSPREAD_CK505_ENABLE;
5640 else
5641 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5642
5643 if (has_panel) {
5644 val &= ~DREF_SSC_SOURCE_MASK;
5645 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005646
Keith Packard199e5d72011-09-22 12:01:57 -07005647 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005648 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005649 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005650 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005651 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005652 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005653
5654 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005655 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005656 POSTING_READ(PCH_DREF_CONTROL);
5657 udelay(200);
5658
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005659 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005660
5661 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005662 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005663 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005664 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005665 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005666 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005667 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005668 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005669 } else
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 } else {
5676 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5677
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005678 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005679
5680 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005681 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005682
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005683 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005684 POSTING_READ(PCH_DREF_CONTROL);
5685 udelay(200);
5686
5687 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005688 val &= ~DREF_SSC_SOURCE_MASK;
5689 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005690
5691 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005692 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005693
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005694 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005695 POSTING_READ(PCH_DREF_CONTROL);
5696 udelay(200);
5697 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005698
5699 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005700}
5701
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005702static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005703{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005704 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005705
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005706 tmp = I915_READ(SOUTH_CHICKEN2);
5707 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5708 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005709
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005710 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5711 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5712 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005713
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005714 tmp = I915_READ(SOUTH_CHICKEN2);
5715 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5716 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005717
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005718 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5719 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5720 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005721}
5722
5723/* WaMPhyProgramming:hsw */
5724static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5725{
5726 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005727
5728 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5729 tmp &= ~(0xFF << 24);
5730 tmp |= (0x12 << 24);
5731 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5732
Paulo Zanonidde86e22012-12-01 12:04:25 -02005733 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5734 tmp |= (1 << 11);
5735 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5736
5737 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5738 tmp |= (1 << 11);
5739 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5740
Paulo Zanonidde86e22012-12-01 12:04:25 -02005741 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5742 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5743 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5744
5745 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5746 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5747 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5748
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005749 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5750 tmp &= ~(7 << 13);
5751 tmp |= (5 << 13);
5752 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005753
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005754 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5755 tmp &= ~(7 << 13);
5756 tmp |= (5 << 13);
5757 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005758
5759 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5760 tmp &= ~0xFF;
5761 tmp |= 0x1C;
5762 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5763
5764 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5765 tmp &= ~0xFF;
5766 tmp |= 0x1C;
5767 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5768
5769 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5770 tmp &= ~(0xFF << 16);
5771 tmp |= (0x1C << 16);
5772 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5773
5774 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5775 tmp &= ~(0xFF << 16);
5776 tmp |= (0x1C << 16);
5777 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5778
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005779 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5780 tmp |= (1 << 27);
5781 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005782
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005783 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5784 tmp |= (1 << 27);
5785 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005786
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005787 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5788 tmp &= ~(0xF << 28);
5789 tmp |= (4 << 28);
5790 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005791
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005792 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5793 tmp &= ~(0xF << 28);
5794 tmp |= (4 << 28);
5795 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005796}
5797
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005798/* Implements 3 different sequences from BSpec chapter "Display iCLK
5799 * Programming" based on the parameters passed:
5800 * - Sequence to enable CLKOUT_DP
5801 * - Sequence to enable CLKOUT_DP without spread
5802 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5803 */
5804static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5805 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005806{
5807 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005808 uint32_t reg, tmp;
5809
5810 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5811 with_spread = true;
5812 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5813 with_fdi, "LP PCH doesn't have FDI\n"))
5814 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005815
5816 mutex_lock(&dev_priv->dpio_lock);
5817
5818 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5819 tmp &= ~SBI_SSCCTL_DISABLE;
5820 tmp |= SBI_SSCCTL_PATHALT;
5821 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5822
5823 udelay(24);
5824
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005825 if (with_spread) {
5826 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5827 tmp &= ~SBI_SSCCTL_PATHALT;
5828 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005829
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005830 if (with_fdi) {
5831 lpt_reset_fdi_mphy(dev_priv);
5832 lpt_program_fdi_mphy(dev_priv);
5833 }
5834 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005835
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005836 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5837 SBI_GEN0 : SBI_DBUFF0;
5838 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5839 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5840 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005841
5842 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005843}
5844
Paulo Zanoni47701c32013-07-23 11:19:25 -03005845/* Sequence to disable CLKOUT_DP */
5846static void lpt_disable_clkout_dp(struct drm_device *dev)
5847{
5848 struct drm_i915_private *dev_priv = dev->dev_private;
5849 uint32_t reg, tmp;
5850
5851 mutex_lock(&dev_priv->dpio_lock);
5852
5853 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5854 SBI_GEN0 : SBI_DBUFF0;
5855 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5856 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5857 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5858
5859 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5860 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5861 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5862 tmp |= SBI_SSCCTL_PATHALT;
5863 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5864 udelay(32);
5865 }
5866 tmp |= SBI_SSCCTL_DISABLE;
5867 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5868 }
5869
5870 mutex_unlock(&dev_priv->dpio_lock);
5871}
5872
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005873static void lpt_init_pch_refclk(struct drm_device *dev)
5874{
5875 struct drm_mode_config *mode_config = &dev->mode_config;
5876 struct intel_encoder *encoder;
5877 bool has_vga = false;
5878
5879 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5880 switch (encoder->type) {
5881 case INTEL_OUTPUT_ANALOG:
5882 has_vga = true;
5883 break;
5884 }
5885 }
5886
Paulo Zanoni47701c32013-07-23 11:19:25 -03005887 if (has_vga)
5888 lpt_enable_clkout_dp(dev, true, true);
5889 else
5890 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005891}
5892
Paulo Zanonidde86e22012-12-01 12:04:25 -02005893/*
5894 * Initialize reference clocks when the driver loads
5895 */
5896void intel_init_pch_refclk(struct drm_device *dev)
5897{
5898 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5899 ironlake_init_pch_refclk(dev);
5900 else if (HAS_PCH_LPT(dev))
5901 lpt_init_pch_refclk(dev);
5902}
5903
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005904static int ironlake_get_refclk(struct drm_crtc *crtc)
5905{
5906 struct drm_device *dev = crtc->dev;
5907 struct drm_i915_private *dev_priv = dev->dev_private;
5908 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005909 int num_connectors = 0;
5910 bool is_lvds = false;
5911
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005912 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005913 switch (encoder->type) {
5914 case INTEL_OUTPUT_LVDS:
5915 is_lvds = true;
5916 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005917 }
5918 num_connectors++;
5919 }
5920
5921 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005922 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005923 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005924 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005925 }
5926
5927 return 120000;
5928}
5929
Daniel Vetter6ff93602013-04-19 11:24:36 +02005930static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005931{
5932 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5934 int pipe = intel_crtc->pipe;
5935 uint32_t val;
5936
Daniel Vetter78114072013-06-13 00:54:57 +02005937 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005938
Daniel Vetter965e0c42013-03-27 00:44:57 +01005939 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005940 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005941 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005942 break;
5943 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005944 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005945 break;
5946 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005947 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005948 break;
5949 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005950 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005951 break;
5952 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005953 /* Case prevented by intel_choose_pipe_bpp_dither. */
5954 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005955 }
5956
Daniel Vetterd8b32242013-04-25 17:54:44 +02005957 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005958 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5959
Daniel Vetter6ff93602013-04-19 11:24:36 +02005960 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005961 val |= PIPECONF_INTERLACED_ILK;
5962 else
5963 val |= PIPECONF_PROGRESSIVE;
5964
Daniel Vetter50f3b012013-03-27 00:44:56 +01005965 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005966 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005967
Paulo Zanonic8203562012-09-12 10:06:29 -03005968 I915_WRITE(PIPECONF(pipe), val);
5969 POSTING_READ(PIPECONF(pipe));
5970}
5971
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005972/*
5973 * Set up the pipe CSC unit.
5974 *
5975 * Currently only full range RGB to limited range RGB conversion
5976 * is supported, but eventually this should handle various
5977 * RGB<->YCbCr scenarios as well.
5978 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005979static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005980{
5981 struct drm_device *dev = crtc->dev;
5982 struct drm_i915_private *dev_priv = dev->dev_private;
5983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5984 int pipe = intel_crtc->pipe;
5985 uint16_t coeff = 0x7800; /* 1.0 */
5986
5987 /*
5988 * TODO: Check what kind of values actually come out of the pipe
5989 * with these coeff/postoff values and adjust to get the best
5990 * accuracy. Perhaps we even need to take the bpc value into
5991 * consideration.
5992 */
5993
Daniel Vetter50f3b012013-03-27 00:44:56 +01005994 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005995 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5996
5997 /*
5998 * GY/GU and RY/RU should be the other way around according
5999 * to BSpec, but reality doesn't agree. Just set them up in
6000 * a way that results in the correct picture.
6001 */
6002 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6003 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6004
6005 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6006 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6007
6008 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6009 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6010
6011 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6012 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6013 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6014
6015 if (INTEL_INFO(dev)->gen > 6) {
6016 uint16_t postoff = 0;
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 postoff = (16 * (1 << 13) / 255) & 0x1fff;
6020
6021 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6022 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6023 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6024
6025 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6026 } else {
6027 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6028
Daniel Vetter50f3b012013-03-27 00:44:56 +01006029 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006030 mode |= CSC_BLACK_SCREEN_OFFSET;
6031
6032 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6033 }
6034}
6035
Daniel Vetter6ff93602013-04-19 11:24:36 +02006036static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006037{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006038 struct drm_device *dev = crtc->dev;
6039 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006041 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006042 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006043 uint32_t val;
6044
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006045 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006046
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006047 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006048 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6049
Daniel Vetter6ff93602013-04-19 11:24:36 +02006050 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006051 val |= PIPECONF_INTERLACED_ILK;
6052 else
6053 val |= PIPECONF_PROGRESSIVE;
6054
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006055 I915_WRITE(PIPECONF(cpu_transcoder), val);
6056 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006057
6058 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6059 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006060
6061 if (IS_BROADWELL(dev)) {
6062 val = 0;
6063
6064 switch (intel_crtc->config.pipe_bpp) {
6065 case 18:
6066 val |= PIPEMISC_DITHER_6_BPC;
6067 break;
6068 case 24:
6069 val |= PIPEMISC_DITHER_8_BPC;
6070 break;
6071 case 30:
6072 val |= PIPEMISC_DITHER_10_BPC;
6073 break;
6074 case 36:
6075 val |= PIPEMISC_DITHER_12_BPC;
6076 break;
6077 default:
6078 /* Case prevented by pipe_config_set_bpp. */
6079 BUG();
6080 }
6081
6082 if (intel_crtc->config.dither)
6083 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6084
6085 I915_WRITE(PIPEMISC(pipe), val);
6086 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006087}
6088
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006089static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006090 intel_clock_t *clock,
6091 bool *has_reduced_clock,
6092 intel_clock_t *reduced_clock)
6093{
6094 struct drm_device *dev = crtc->dev;
6095 struct drm_i915_private *dev_priv = dev->dev_private;
6096 struct intel_encoder *intel_encoder;
6097 int refclk;
6098 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006099 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006100
6101 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6102 switch (intel_encoder->type) {
6103 case INTEL_OUTPUT_LVDS:
6104 is_lvds = true;
6105 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006106 }
6107 }
6108
6109 refclk = ironlake_get_refclk(crtc);
6110
6111 /*
6112 * Returns a set of divisors for the desired target clock with the given
6113 * refclk, or FALSE. The returned values represent the clock equation:
6114 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6115 */
6116 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006117 ret = dev_priv->display.find_dpll(limit, crtc,
6118 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006119 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006120 if (!ret)
6121 return false;
6122
6123 if (is_lvds && dev_priv->lvds_downclock_avail) {
6124 /*
6125 * Ensure we match the reduced clock's P to the target clock.
6126 * If the clocks don't match, we can't switch the display clock
6127 * by using the FP0/FP1. In such case we will disable the LVDS
6128 * downclock feature.
6129 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006130 *has_reduced_clock =
6131 dev_priv->display.find_dpll(limit, crtc,
6132 dev_priv->lvds_downclock,
6133 refclk, clock,
6134 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006135 }
6136
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006137 return true;
6138}
6139
Paulo Zanonid4b19312012-11-29 11:29:32 -02006140int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6141{
6142 /*
6143 * Account for spread spectrum to avoid
6144 * oversubscribing the link. Max center spread
6145 * is 2.5%; use 5% for safety's sake.
6146 */
6147 u32 bps = target_clock * bpp * 21 / 20;
6148 return bps / (link_bw * 8) + 1;
6149}
6150
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006151static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006152{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006153 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006154}
6155
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006156static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006157 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006158 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006159{
6160 struct drm_crtc *crtc = &intel_crtc->base;
6161 struct drm_device *dev = crtc->dev;
6162 struct drm_i915_private *dev_priv = dev->dev_private;
6163 struct intel_encoder *intel_encoder;
6164 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006165 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006166 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006167
6168 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6169 switch (intel_encoder->type) {
6170 case INTEL_OUTPUT_LVDS:
6171 is_lvds = true;
6172 break;
6173 case INTEL_OUTPUT_SDVO:
6174 case INTEL_OUTPUT_HDMI:
6175 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006176 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006177 }
6178
6179 num_connectors++;
6180 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006181
Chris Wilsonc1858122010-12-03 21:35:48 +00006182 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006183 factor = 21;
6184 if (is_lvds) {
6185 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006186 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006187 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006188 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006189 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006190 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006191
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006192 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006193 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006194
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006195 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6196 *fp2 |= FP_CB_TUNE;
6197
Chris Wilson5eddb702010-09-11 13:48:45 +01006198 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006199
Eric Anholta07d6782011-03-30 13:01:08 -07006200 if (is_lvds)
6201 dpll |= DPLLB_MODE_LVDS;
6202 else
6203 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006204
Daniel Vetteref1b4602013-06-01 17:17:04 +02006205 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6206 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006207
6208 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006209 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006210 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006211 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006212
Eric Anholta07d6782011-03-30 13:01:08 -07006213 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006214 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006215 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006216 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006217
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006218 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006219 case 5:
6220 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6221 break;
6222 case 7:
6223 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6224 break;
6225 case 10:
6226 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6227 break;
6228 case 14:
6229 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6230 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006231 }
6232
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006233 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006234 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006235 else
6236 dpll |= PLL_REF_INPUT_DREFCLK;
6237
Daniel Vetter959e16d2013-06-05 13:34:21 +02006238 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006239}
6240
Jesse Barnes79e53942008-11-07 14:24:08 -08006241static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006242 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006243 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006244{
6245 struct drm_device *dev = crtc->dev;
6246 struct drm_i915_private *dev_priv = dev->dev_private;
6247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6248 int pipe = intel_crtc->pipe;
6249 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006250 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006251 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006252 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006253 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006254 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006255 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006256 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006257 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006258
6259 for_each_encoder_on_crtc(dev, crtc, encoder) {
6260 switch (encoder->type) {
6261 case INTEL_OUTPUT_LVDS:
6262 is_lvds = true;
6263 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006264 }
6265
6266 num_connectors++;
6267 }
6268
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006269 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6270 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6271
Daniel Vetterff9a6752013-06-01 17:16:21 +02006272 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006273 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006274 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006275 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6276 return -EINVAL;
6277 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006278 /* Compat-code for transition, will disappear. */
6279 if (!intel_crtc->config.clock_set) {
6280 intel_crtc->config.dpll.n = clock.n;
6281 intel_crtc->config.dpll.m1 = clock.m1;
6282 intel_crtc->config.dpll.m2 = clock.m2;
6283 intel_crtc->config.dpll.p1 = clock.p1;
6284 intel_crtc->config.dpll.p2 = clock.p2;
6285 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006286
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006287 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006288 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006289 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006290 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006291 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006292
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006293 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006294 &fp, &reduced_clock,
6295 has_reduced_clock ? &fp2 : NULL);
6296
Daniel Vetter959e16d2013-06-05 13:34:21 +02006297 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006298 intel_crtc->config.dpll_hw_state.fp0 = fp;
6299 if (has_reduced_clock)
6300 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6301 else
6302 intel_crtc->config.dpll_hw_state.fp1 = fp;
6303
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006304 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006305 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006306 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6307 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006308 return -EINVAL;
6309 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006310 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006311 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006312
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006313 if (intel_crtc->config.has_dp_encoder)
6314 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006315
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006316 if (is_lvds && has_reduced_clock && i915_powersave)
6317 intel_crtc->lowfreq_avail = true;
6318 else
6319 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006320
Daniel Vetter8a654f32013-06-01 17:16:22 +02006321 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006322
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006323 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006324 intel_cpu_transcoder_set_m_n(intel_crtc,
6325 &intel_crtc->config.fdi_m_n);
6326 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006327
Daniel Vetter6ff93602013-04-19 11:24:36 +02006328 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006329
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006330 /* Set up the display plane register */
6331 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006332 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006333
Daniel Vetter94352cf2012-07-05 22:51:56 +02006334 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006335
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006336 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006337}
6338
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006339static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6340 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006341{
6342 struct drm_device *dev = crtc->base.dev;
6343 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006344 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006345
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006346 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6347 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6348 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6349 & ~TU_SIZE_MASK;
6350 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6351 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6352 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6353}
6354
6355static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6356 enum transcoder transcoder,
6357 struct intel_link_m_n *m_n)
6358{
6359 struct drm_device *dev = crtc->base.dev;
6360 struct drm_i915_private *dev_priv = dev->dev_private;
6361 enum pipe pipe = crtc->pipe;
6362
6363 if (INTEL_INFO(dev)->gen >= 5) {
6364 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6365 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6366 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6367 & ~TU_SIZE_MASK;
6368 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6369 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6370 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6371 } else {
6372 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6373 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6374 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6375 & ~TU_SIZE_MASK;
6376 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6377 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6378 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6379 }
6380}
6381
6382void intel_dp_get_m_n(struct intel_crtc *crtc,
6383 struct intel_crtc_config *pipe_config)
6384{
6385 if (crtc->config.has_pch_encoder)
6386 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6387 else
6388 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6389 &pipe_config->dp_m_n);
6390}
6391
Daniel Vetter72419202013-04-04 13:28:53 +02006392static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6393 struct intel_crtc_config *pipe_config)
6394{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006395 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6396 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006397}
6398
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006399static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6400 struct intel_crtc_config *pipe_config)
6401{
6402 struct drm_device *dev = crtc->base.dev;
6403 struct drm_i915_private *dev_priv = dev->dev_private;
6404 uint32_t tmp;
6405
6406 tmp = I915_READ(PF_CTL(crtc->pipe));
6407
6408 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006409 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006410 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6411 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006412
6413 /* We currently do not free assignements of panel fitters on
6414 * ivb/hsw (since we don't use the higher upscaling modes which
6415 * differentiates them) so just WARN about this case for now. */
6416 if (IS_GEN7(dev)) {
6417 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6418 PF_PIPE_SEL_IVB(crtc->pipe));
6419 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006420 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006421}
6422
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006423static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6424 struct intel_crtc_config *pipe_config)
6425{
6426 struct drm_device *dev = crtc->base.dev;
6427 struct drm_i915_private *dev_priv = dev->dev_private;
6428 uint32_t tmp;
6429
Daniel Vettere143a212013-07-04 12:01:15 +02006430 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006431 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006432
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006433 tmp = I915_READ(PIPECONF(crtc->pipe));
6434 if (!(tmp & PIPECONF_ENABLE))
6435 return false;
6436
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006437 switch (tmp & PIPECONF_BPC_MASK) {
6438 case PIPECONF_6BPC:
6439 pipe_config->pipe_bpp = 18;
6440 break;
6441 case PIPECONF_8BPC:
6442 pipe_config->pipe_bpp = 24;
6443 break;
6444 case PIPECONF_10BPC:
6445 pipe_config->pipe_bpp = 30;
6446 break;
6447 case PIPECONF_12BPC:
6448 pipe_config->pipe_bpp = 36;
6449 break;
6450 default:
6451 break;
6452 }
6453
Daniel Vetterab9412b2013-05-03 11:49:46 +02006454 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006455 struct intel_shared_dpll *pll;
6456
Daniel Vetter88adfff2013-03-28 10:42:01 +01006457 pipe_config->has_pch_encoder = true;
6458
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006459 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6460 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6461 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006462
6463 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006464
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006465 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006466 pipe_config->shared_dpll =
6467 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006468 } else {
6469 tmp = I915_READ(PCH_DPLL_SEL);
6470 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6471 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6472 else
6473 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6474 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006475
6476 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6477
6478 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6479 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006480
6481 tmp = pipe_config->dpll_hw_state.dpll;
6482 pipe_config->pixel_multiplier =
6483 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6484 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006485
6486 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006487 } else {
6488 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006489 }
6490
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006491 intel_get_pipe_timings(crtc, pipe_config);
6492
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006493 ironlake_get_pfit_config(crtc, pipe_config);
6494
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006495 return true;
6496}
6497
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006498static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6499{
6500 struct drm_device *dev = dev_priv->dev;
6501 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6502 struct intel_crtc *crtc;
6503 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006504 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006505
6506 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
Paulo Zanoni798183c2013-12-06 20:29:01 -02006507 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006508 pipe_name(crtc->pipe));
6509
6510 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6511 WARN(plls->spll_refcount, "SPLL enabled\n");
6512 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6513 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6514 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6515 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6516 "CPU PWM1 enabled\n");
6517 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6518 "CPU PWM2 enabled\n");
6519 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6520 "PCH PWM1 enabled\n");
6521 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6522 "Utility pin enabled\n");
6523 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6524
6525 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6526 val = I915_READ(DEIMR);
Paulo Zanoni6806e632013-11-21 13:47:24 -02006527 WARN((val | DE_PCH_EVENT_IVB) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006528 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6529 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006530 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006531 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6532 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6533}
6534
6535/*
6536 * This function implements pieces of two sequences from BSpec:
6537 * - Sequence for display software to disable LCPLL
6538 * - Sequence for display software to allow package C8+
6539 * The steps implemented here are just the steps that actually touch the LCPLL
6540 * register. Callers should take care of disabling all the display engine
6541 * functions, doing the mode unset, fixing interrupts, etc.
6542 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006543static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6544 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006545{
6546 uint32_t val;
6547
6548 assert_can_disable_lcpll(dev_priv);
6549
6550 val = I915_READ(LCPLL_CTL);
6551
6552 if (switch_to_fclk) {
6553 val |= LCPLL_CD_SOURCE_FCLK;
6554 I915_WRITE(LCPLL_CTL, val);
6555
6556 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6557 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6558 DRM_ERROR("Switching to FCLK failed\n");
6559
6560 val = I915_READ(LCPLL_CTL);
6561 }
6562
6563 val |= LCPLL_PLL_DISABLE;
6564 I915_WRITE(LCPLL_CTL, val);
6565 POSTING_READ(LCPLL_CTL);
6566
6567 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6568 DRM_ERROR("LCPLL still locked\n");
6569
6570 val = I915_READ(D_COMP);
6571 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006572 mutex_lock(&dev_priv->rps.hw_lock);
6573 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6574 DRM_ERROR("Failed to disable D_COMP\n");
6575 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006576 POSTING_READ(D_COMP);
6577 ndelay(100);
6578
6579 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6580 DRM_ERROR("D_COMP RCOMP still in progress\n");
6581
6582 if (allow_power_down) {
6583 val = I915_READ(LCPLL_CTL);
6584 val |= LCPLL_POWER_DOWN_ALLOW;
6585 I915_WRITE(LCPLL_CTL, val);
6586 POSTING_READ(LCPLL_CTL);
6587 }
6588}
6589
6590/*
6591 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6592 * source.
6593 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006594static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006595{
6596 uint32_t val;
6597
6598 val = I915_READ(LCPLL_CTL);
6599
6600 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6601 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6602 return;
6603
Paulo Zanoni215733f2013-08-19 13:18:07 -03006604 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6605 * we'll hang the machine! */
Deepak Sc8d9a592013-11-23 14:55:42 +05306606 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03006607
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006608 if (val & LCPLL_POWER_DOWN_ALLOW) {
6609 val &= ~LCPLL_POWER_DOWN_ALLOW;
6610 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006611 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006612 }
6613
6614 val = I915_READ(D_COMP);
6615 val |= D_COMP_COMP_FORCE;
6616 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006617 mutex_lock(&dev_priv->rps.hw_lock);
6618 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6619 DRM_ERROR("Failed to enable D_COMP\n");
6620 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006621 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006622
6623 val = I915_READ(LCPLL_CTL);
6624 val &= ~LCPLL_PLL_DISABLE;
6625 I915_WRITE(LCPLL_CTL, val);
6626
6627 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6628 DRM_ERROR("LCPLL not locked yet\n");
6629
6630 if (val & LCPLL_CD_SOURCE_FCLK) {
6631 val = I915_READ(LCPLL_CTL);
6632 val &= ~LCPLL_CD_SOURCE_FCLK;
6633 I915_WRITE(LCPLL_CTL, val);
6634
6635 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6636 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6637 DRM_ERROR("Switching back to LCPLL failed\n");
6638 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006639
Deepak Sc8d9a592013-11-23 14:55:42 +05306640 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006641}
6642
Paulo Zanonic67a4702013-08-19 13:18:09 -03006643void hsw_enable_pc8_work(struct work_struct *__work)
6644{
6645 struct drm_i915_private *dev_priv =
6646 container_of(to_delayed_work(__work), struct drm_i915_private,
6647 pc8.enable_work);
6648 struct drm_device *dev = dev_priv->dev;
6649 uint32_t val;
6650
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006651 WARN_ON(!HAS_PC8(dev));
6652
Paulo Zanonic67a4702013-08-19 13:18:09 -03006653 if (dev_priv->pc8.enabled)
6654 return;
6655
6656 DRM_DEBUG_KMS("Enabling package C8+\n");
6657
6658 dev_priv->pc8.enabled = true;
6659
6660 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6661 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6662 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6663 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6664 }
6665
6666 lpt_disable_clkout_dp(dev);
6667 hsw_pc8_disable_interrupts(dev);
6668 hsw_disable_lcpll(dev_priv, true, true);
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006669
6670 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006671}
6672
6673static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6674{
6675 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6676 WARN(dev_priv->pc8.disable_count < 1,
6677 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6678
6679 dev_priv->pc8.disable_count--;
6680 if (dev_priv->pc8.disable_count != 0)
6681 return;
6682
6683 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006684 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006685}
6686
6687static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6688{
6689 struct drm_device *dev = dev_priv->dev;
6690 uint32_t val;
6691
6692 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6693 WARN(dev_priv->pc8.disable_count < 0,
6694 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6695
6696 dev_priv->pc8.disable_count++;
6697 if (dev_priv->pc8.disable_count != 1)
6698 return;
6699
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006700 WARN_ON(!HAS_PC8(dev));
6701
Paulo Zanonic67a4702013-08-19 13:18:09 -03006702 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6703 if (!dev_priv->pc8.enabled)
6704 return;
6705
6706 DRM_DEBUG_KMS("Disabling package C8+\n");
6707
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006708 intel_runtime_pm_get(dev_priv);
6709
Paulo Zanonic67a4702013-08-19 13:18:09 -03006710 hsw_restore_lcpll(dev_priv);
6711 hsw_pc8_restore_interrupts(dev);
6712 lpt_init_pch_refclk(dev);
6713
6714 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6715 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6716 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6717 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6718 }
6719
6720 intel_prepare_ddi(dev);
6721 i915_gem_init_swizzling(dev);
6722 mutex_lock(&dev_priv->rps.hw_lock);
6723 gen6_update_ring_freq(dev);
6724 mutex_unlock(&dev_priv->rps.hw_lock);
6725 dev_priv->pc8.enabled = false;
6726}
6727
6728void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6729{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006730 if (!HAS_PC8(dev_priv->dev))
6731 return;
6732
Paulo Zanonic67a4702013-08-19 13:18:09 -03006733 mutex_lock(&dev_priv->pc8.lock);
6734 __hsw_enable_package_c8(dev_priv);
6735 mutex_unlock(&dev_priv->pc8.lock);
6736}
6737
6738void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6739{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006740 if (!HAS_PC8(dev_priv->dev))
6741 return;
6742
Paulo Zanonic67a4702013-08-19 13:18:09 -03006743 mutex_lock(&dev_priv->pc8.lock);
6744 __hsw_disable_package_c8(dev_priv);
6745 mutex_unlock(&dev_priv->pc8.lock);
6746}
6747
6748static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6749{
6750 struct drm_device *dev = dev_priv->dev;
6751 struct intel_crtc *crtc;
6752 uint32_t val;
6753
6754 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6755 if (crtc->base.enabled)
6756 return false;
6757
6758 /* This case is still possible since we have the i915.disable_power_well
6759 * parameter and also the KVMr or something else might be requesting the
6760 * power well. */
6761 val = I915_READ(HSW_PWR_WELL_DRIVER);
6762 if (val != 0) {
6763 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6764 return false;
6765 }
6766
6767 return true;
6768}
6769
6770/* Since we're called from modeset_global_resources there's no way to
6771 * symmetrically increase and decrease the refcount, so we use
6772 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6773 * or not.
6774 */
6775static void hsw_update_package_c8(struct drm_device *dev)
6776{
6777 struct drm_i915_private *dev_priv = dev->dev_private;
6778 bool allow;
6779
Chris Wilson7c6c2652013-11-18 18:32:37 -08006780 if (!HAS_PC8(dev_priv->dev))
6781 return;
6782
Paulo Zanonic67a4702013-08-19 13:18:09 -03006783 if (!i915_enable_pc8)
6784 return;
6785
6786 mutex_lock(&dev_priv->pc8.lock);
6787
6788 allow = hsw_can_enable_package_c8(dev_priv);
6789
6790 if (allow == dev_priv->pc8.requirements_met)
6791 goto done;
6792
6793 dev_priv->pc8.requirements_met = allow;
6794
6795 if (allow)
6796 __hsw_enable_package_c8(dev_priv);
6797 else
6798 __hsw_disable_package_c8(dev_priv);
6799
6800done:
6801 mutex_unlock(&dev_priv->pc8.lock);
6802}
6803
6804static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6805{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006806 if (!HAS_PC8(dev_priv->dev))
6807 return;
6808
Chris Wilson34581222013-11-18 18:32:36 -08006809 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006810 if (!dev_priv->pc8.gpu_idle) {
6811 dev_priv->pc8.gpu_idle = true;
Chris Wilson34581222013-11-18 18:32:36 -08006812 __hsw_enable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006813 }
Chris Wilson34581222013-11-18 18:32:36 -08006814 mutex_unlock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006815}
6816
6817static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6818{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006819 if (!HAS_PC8(dev_priv->dev))
6820 return;
6821
Chris Wilson34581222013-11-18 18:32:36 -08006822 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006823 if (dev_priv->pc8.gpu_idle) {
6824 dev_priv->pc8.gpu_idle = false;
Chris Wilson34581222013-11-18 18:32:36 -08006825 __hsw_disable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006826 }
Chris Wilson34581222013-11-18 18:32:36 -08006827 mutex_unlock(&dev_priv->pc8.lock);
Daniel Vetter94352cf2012-07-05 22:51:56 +02006828}
Eric Anholtf564048e2011-03-30 13:01:02 -07006829
Imre Deak6efdf352013-10-16 17:25:52 +03006830#define for_each_power_domain(domain, mask) \
6831 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6832 if ((1 << (domain)) & (mask))
6833
6834static unsigned long get_pipe_power_domains(struct drm_device *dev,
6835 enum pipe pipe, bool pfit_enabled)
6836{
6837 unsigned long mask;
6838 enum transcoder transcoder;
6839
6840 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6841
6842 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6843 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6844 if (pfit_enabled)
6845 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6846
6847 return mask;
6848}
6849
Imre Deakbaa70702013-10-25 17:36:48 +03006850void intel_display_set_init_power(struct drm_device *dev, bool enable)
6851{
6852 struct drm_i915_private *dev_priv = dev->dev_private;
6853
6854 if (dev_priv->power_domains.init_power_on == enable)
6855 return;
6856
6857 if (enable)
6858 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6859 else
6860 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6861
6862 dev_priv->power_domains.init_power_on = enable;
6863}
6864
Imre Deak4f074122013-10-16 17:25:51 +03006865static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006866{
Imre Deak6efdf352013-10-16 17:25:52 +03006867 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006868 struct intel_crtc *crtc;
6869
Imre Deak6efdf352013-10-16 17:25:52 +03006870 /*
6871 * First get all needed power domains, then put all unneeded, to avoid
6872 * any unnecessary toggling of the power wells.
6873 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006874 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006875 enum intel_display_power_domain domain;
6876
Jesse Barnes79e53942008-11-07 14:24:08 -08006877 if (!crtc->base.enabled)
6878 continue;
6879
Imre Deak6efdf352013-10-16 17:25:52 +03006880 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6881 crtc->pipe,
6882 crtc->config.pch_pfit.enabled);
6883
6884 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6885 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006886 }
6887
Imre Deak6efdf352013-10-16 17:25:52 +03006888 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6889 enum intel_display_power_domain domain;
6890
6891 for_each_power_domain(domain, crtc->enabled_power_domains)
6892 intel_display_power_put(dev, domain);
6893
6894 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6895 }
Imre Deakbaa70702013-10-25 17:36:48 +03006896
6897 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006898}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006899
Imre Deak4f074122013-10-16 17:25:51 +03006900static void haswell_modeset_global_resources(struct drm_device *dev)
6901{
6902 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006903 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006904}
6905
6906static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6907 int x, int y,
6908 struct drm_framebuffer *fb)
6909{
6910 struct drm_device *dev = crtc->dev;
6911 struct drm_i915_private *dev_priv = dev->dev_private;
6912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6913 int plane = intel_crtc->plane;
6914 int ret;
6915
Paulo Zanoni566b7342013-11-25 15:27:08 -02006916 if (!intel_ddi_pll_select(intel_crtc))
Chris Wilson560b85b2010-08-07 11:01:38 +01006917 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02006918 intel_ddi_pll_enable(intel_crtc);
Chris Wilson560b85b2010-08-07 11:01:38 +01006919
Chris Wilson560b85b2010-08-07 11:01:38 +01006920 if (intel_crtc->config.has_dp_encoder)
6921 intel_dp_set_m_n(intel_crtc);
6922
6923 intel_crtc->lowfreq_avail = false;
6924
6925 intel_set_pipe_timings(intel_crtc);
6926
6927 if (intel_crtc->config.has_pch_encoder) {
6928 intel_cpu_transcoder_set_m_n(intel_crtc,
6929 &intel_crtc->config.fdi_m_n);
6930 }
6931
6932 haswell_set_pipeconf(crtc);
6933
6934 intel_set_pipe_csc(crtc);
6935
6936 /* Set up the display plane register */
6937 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6938 POSTING_READ(DSPCNTR(plane));
6939
6940 ret = intel_pipe_set_base(crtc, x, y, fb);
6941
Chris Wilson560b85b2010-08-07 11:01:38 +01006942 return ret;
6943}
6944
6945static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6946 struct intel_crtc_config *pipe_config)
6947{
6948 struct drm_device *dev = crtc->base.dev;
6949 struct drm_i915_private *dev_priv = dev->dev_private;
6950 enum intel_display_power_domain pfit_domain;
6951 uint32_t tmp;
6952
6953 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6954 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6955
6956 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6957 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6958 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006959 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01006960 default:
6961 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006962 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6963 case TRANS_DDI_EDP_INPUT_A_ON:
6964 trans_edp_pipe = PIPE_A;
6965 break;
6966 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6967 trans_edp_pipe = PIPE_B;
6968 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01006969 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006970 trans_edp_pipe = PIPE_C;
6971 break;
6972 }
6973
Chris Wilson6b383a72010-09-13 13:54:26 +01006974 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006975 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6976 }
6977
6978 if (!intel_display_power_enabled(dev,
6979 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6980 return false;
6981
6982 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6983 if (!(tmp & PIPECONF_ENABLE))
6984 return false;
6985
6986 /*
6987 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6988 * DDI E. So just check whether this pipe is wired to DDI E and whether
6989 * the PCH transcoder is on.
6990 */
6991 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6992 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6993 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6994 pipe_config->has_pch_encoder = true;
6995
6996 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6997 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6998 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6999
7000 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7001 }
7002
Chris Wilson560b85b2010-08-07 11:01:38 +01007003 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007004
7005 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
7006 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01007007 ironlake_get_pfit_config(crtc, pipe_config);
7008
Jesse Barnese59150d2014-01-07 13:30:45 -08007009 if (IS_HASWELL(dev))
7010 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7011 (I915_READ(IPS_CTL) & IPS_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08007012
7013 pipe_config->pixel_multiplier = 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07007014
7015 return true;
7016}
7017
7018static int intel_crtc_mode_set(struct drm_crtc *crtc,
7019 int x, int y,
7020 struct drm_framebuffer *fb)
7021{
Eric Anholt0b701d22011-03-30 13:01:03 -07007022 struct drm_device *dev = crtc->dev;
7023 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01007024 struct intel_encoder *encoder;
Eric Anholtf564048e2011-03-30 13:01:02 -07007025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007026 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholtf564048e2011-03-30 13:01:02 -07007027 int pipe = intel_crtc->pipe;
7028 int ret;
7029
Eric Anholt0b701d22011-03-30 13:01:03 -07007030 drm_vblank_pre_modeset(dev, pipe);
7031
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007032 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7033
Jesse Barnes79e53942008-11-07 14:24:08 -08007034 drm_vblank_post_modeset(dev, pipe);
7035
Daniel Vetter9256aa12012-10-31 19:26:13 +01007036 if (ret != 0)
7037 return ret;
7038
7039 for_each_encoder_on_crtc(dev, crtc, encoder) {
7040 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7041 encoder->base.base.id,
7042 drm_get_encoder_name(&encoder->base),
7043 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02007044 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007045 }
7046
7047 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007048}
7049
Jani Nikula1a915102013-10-16 12:34:48 +03007050static struct {
7051 int clock;
7052 u32 config;
7053} hdmi_audio_clock[] = {
7054 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7055 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7056 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7057 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7058 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7059 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7060 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7061 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7062 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7063 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7064};
7065
7066/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7067static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7068{
7069 int i;
7070
7071 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7072 if (mode->clock == hdmi_audio_clock[i].clock)
7073 break;
7074 }
7075
7076 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7077 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7078 i = 1;
7079 }
7080
7081 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7082 hdmi_audio_clock[i].clock,
7083 hdmi_audio_clock[i].config);
7084
7085 return hdmi_audio_clock[i].config;
7086}
7087
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007088static bool intel_eld_uptodate(struct drm_connector *connector,
7089 int reg_eldv, uint32_t bits_eldv,
7090 int reg_elda, uint32_t bits_elda,
7091 int reg_edid)
7092{
7093 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7094 uint8_t *eld = connector->eld;
7095 uint32_t i;
7096
7097 i = I915_READ(reg_eldv);
7098 i &= bits_eldv;
7099
7100 if (!eld[0])
7101 return !i;
7102
7103 if (!i)
7104 return false;
7105
7106 i = I915_READ(reg_elda);
7107 i &= ~bits_elda;
7108 I915_WRITE(reg_elda, i);
7109
7110 for (i = 0; i < eld[2]; i++)
7111 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7112 return false;
7113
7114 return true;
7115}
7116
Wu Fengguange0dac652011-09-05 14:25:34 +08007117static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007118 struct drm_crtc *crtc,
7119 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007120{
7121 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7122 uint8_t *eld = connector->eld;
7123 uint32_t eldv;
7124 uint32_t len;
7125 uint32_t i;
7126
7127 i = I915_READ(G4X_AUD_VID_DID);
7128
7129 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7130 eldv = G4X_ELDV_DEVCL_DEVBLC;
7131 else
7132 eldv = G4X_ELDV_DEVCTG;
7133
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007134 if (intel_eld_uptodate(connector,
7135 G4X_AUD_CNTL_ST, eldv,
7136 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7137 G4X_HDMIW_HDMIEDID))
7138 return;
7139
Wu Fengguange0dac652011-09-05 14:25:34 +08007140 i = I915_READ(G4X_AUD_CNTL_ST);
7141 i &= ~(eldv | G4X_ELD_ADDR);
7142 len = (i >> 9) & 0x1f; /* ELD buffer size */
7143 I915_WRITE(G4X_AUD_CNTL_ST, i);
7144
7145 if (!eld[0])
7146 return;
7147
7148 len = min_t(uint8_t, eld[2], len);
7149 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7150 for (i = 0; i < len; i++)
7151 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7152
7153 i = I915_READ(G4X_AUD_CNTL_ST);
7154 i |= eldv;
7155 I915_WRITE(G4X_AUD_CNTL_ST, i);
7156}
7157
Wang Xingchao83358c852012-08-16 22:43:37 +08007158static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007159 struct drm_crtc *crtc,
7160 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007161{
7162 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7163 uint8_t *eld = connector->eld;
7164 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007166 uint32_t eldv;
7167 uint32_t i;
7168 int len;
7169 int pipe = to_intel_crtc(crtc)->pipe;
7170 int tmp;
7171
7172 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7173 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7174 int aud_config = HSW_AUD_CFG(pipe);
7175 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7176
7177
7178 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7179
7180 /* Audio output enable */
7181 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7182 tmp = I915_READ(aud_cntrl_st2);
7183 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7184 I915_WRITE(aud_cntrl_st2, tmp);
7185
7186 /* Wait for 1 vertical blank */
7187 intel_wait_for_vblank(dev, pipe);
7188
7189 /* Set ELD valid state */
7190 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007191 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007192 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7193 I915_WRITE(aud_cntrl_st2, tmp);
7194 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007195 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007196
7197 /* Enable HDMI mode */
7198 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007199 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007200 /* clear N_programing_enable and N_value_index */
7201 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7202 I915_WRITE(aud_config, tmp);
7203
7204 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7205
7206 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007207 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007208
7209 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7210 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7211 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7212 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007213 } else {
7214 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7215 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007216
7217 if (intel_eld_uptodate(connector,
7218 aud_cntrl_st2, eldv,
7219 aud_cntl_st, IBX_ELD_ADDRESS,
7220 hdmiw_hdmiedid))
7221 return;
7222
7223 i = I915_READ(aud_cntrl_st2);
7224 i &= ~eldv;
7225 I915_WRITE(aud_cntrl_st2, i);
7226
7227 if (!eld[0])
7228 return;
7229
7230 i = I915_READ(aud_cntl_st);
7231 i &= ~IBX_ELD_ADDRESS;
7232 I915_WRITE(aud_cntl_st, i);
7233 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7234 DRM_DEBUG_DRIVER("port num:%d\n", i);
7235
7236 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7237 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7238 for (i = 0; i < len; i++)
7239 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7240
7241 i = I915_READ(aud_cntrl_st2);
7242 i |= eldv;
7243 I915_WRITE(aud_cntrl_st2, i);
7244
7245}
7246
Wu Fengguange0dac652011-09-05 14:25:34 +08007247static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007248 struct drm_crtc *crtc,
7249 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007250{
7251 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7252 uint8_t *eld = connector->eld;
7253 uint32_t eldv;
7254 uint32_t i;
7255 int len;
7256 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007257 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007258 int aud_cntl_st;
7259 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007260 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007261
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007262 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007263 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7264 aud_config = IBX_AUD_CFG(pipe);
7265 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007266 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007267 } else if (IS_VALLEYVIEW(connector->dev)) {
7268 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7269 aud_config = VLV_AUD_CFG(pipe);
7270 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7271 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007272 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007273 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7274 aud_config = CPT_AUD_CFG(pipe);
7275 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007276 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007277 }
7278
Wang Xingchao9b138a82012-08-09 16:52:18 +08007279 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007280
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007281 if (IS_VALLEYVIEW(connector->dev)) {
7282 struct intel_encoder *intel_encoder;
7283 struct intel_digital_port *intel_dig_port;
7284
7285 intel_encoder = intel_attached_encoder(connector);
7286 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7287 i = intel_dig_port->port;
7288 } else {
7289 i = I915_READ(aud_cntl_st);
7290 i = (i >> 29) & DIP_PORT_SEL_MASK;
7291 /* DIP_Port_Select, 0x1 = PortB */
7292 }
7293
Wu Fengguange0dac652011-09-05 14:25:34 +08007294 if (!i) {
7295 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7296 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007297 eldv = IBX_ELD_VALIDB;
7298 eldv |= IBX_ELD_VALIDB << 4;
7299 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007300 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007301 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007302 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007303 }
7304
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007305 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7306 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7307 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007308 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007309 } else {
7310 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7311 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007312
7313 if (intel_eld_uptodate(connector,
7314 aud_cntrl_st2, eldv,
7315 aud_cntl_st, IBX_ELD_ADDRESS,
7316 hdmiw_hdmiedid))
7317 return;
7318
Wu Fengguange0dac652011-09-05 14:25:34 +08007319 i = I915_READ(aud_cntrl_st2);
7320 i &= ~eldv;
7321 I915_WRITE(aud_cntrl_st2, i);
7322
7323 if (!eld[0])
7324 return;
7325
Wu Fengguange0dac652011-09-05 14:25:34 +08007326 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007327 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007328 I915_WRITE(aud_cntl_st, i);
7329
7330 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7331 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7332 for (i = 0; i < len; i++)
7333 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7334
7335 i = I915_READ(aud_cntrl_st2);
7336 i |= eldv;
7337 I915_WRITE(aud_cntrl_st2, i);
7338}
7339
7340void intel_write_eld(struct drm_encoder *encoder,
7341 struct drm_display_mode *mode)
7342{
7343 struct drm_crtc *crtc = encoder->crtc;
7344 struct drm_connector *connector;
7345 struct drm_device *dev = encoder->dev;
7346 struct drm_i915_private *dev_priv = dev->dev_private;
7347
7348 connector = drm_select_eld(encoder, mode);
7349 if (!connector)
7350 return;
7351
7352 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7353 connector->base.id,
7354 drm_get_connector_name(connector),
7355 connector->encoder->base.id,
7356 drm_get_encoder_name(connector->encoder));
7357
7358 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7359
7360 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007361 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007362}
7363
Jesse Barnes79e53942008-11-07 14:24:08 -08007364static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7365{
7366 struct drm_device *dev = crtc->dev;
7367 struct drm_i915_private *dev_priv = dev->dev_private;
7368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7369 bool visible = base != 0;
7370 u32 cntl;
7371
7372 if (intel_crtc->cursor_visible == visible)
7373 return;
7374
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007375 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007376 if (visible) {
7377 /* On these chipsets we can only modify the base whilst
7378 * the cursor is disabled.
7379 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007380 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007381
7382 cntl &= ~(CURSOR_FORMAT_MASK);
7383 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7384 cntl |= CURSOR_ENABLE |
7385 CURSOR_GAMMA_ENABLE |
7386 CURSOR_FORMAT_ARGB;
7387 } else
7388 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007389 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007390
7391 intel_crtc->cursor_visible = visible;
7392}
7393
7394static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7395{
7396 struct drm_device *dev = crtc->dev;
7397 struct drm_i915_private *dev_priv = dev->dev_private;
7398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7399 int pipe = intel_crtc->pipe;
7400 bool visible = base != 0;
7401
7402 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007403 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007404 if (base) {
7405 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7406 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7407 cntl |= pipe << 28; /* Connect to correct pipe */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007408 } else {
Eric Anholtbad720f2009-10-22 16:11:14 -07007409 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007410 cntl |= CURSOR_MODE_DISABLE;
7411 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007412 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007413
7414 intel_crtc->cursor_visible = visible;
7415 }
7416 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007417 POSTING_READ(CURCNTR(pipe));
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007418 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007419 POSTING_READ(CURBASE(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007420}
7421
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007422static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7423{
7424 struct drm_device *dev = crtc->dev;
7425 struct drm_i915_private *dev_priv = dev->dev_private;
7426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7427 int pipe = intel_crtc->pipe;
7428 bool visible = base != 0;
7429
7430 if (intel_crtc->cursor_visible != visible) {
7431 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7432 if (base) {
7433 cntl &= ~CURSOR_MODE;
7434 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7435 } else {
7436 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7437 cntl |= CURSOR_MODE_DISABLE;
7438 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007439 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007440 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007441 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7442 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007443 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7444
7445 intel_crtc->cursor_visible = visible;
7446 }
7447 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007448 POSTING_READ(CURCNTR_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007449 I915_WRITE(CURBASE_IVB(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007450 POSTING_READ(CURBASE_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007451}
7452
Jesse Barnes79e53942008-11-07 14:24:08 -08007453/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007454static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7455 bool on)
7456{
7457 struct drm_device *dev = crtc->dev;
7458 struct drm_i915_private *dev_priv = dev->dev_private;
7459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7460 int pipe = intel_crtc->pipe;
7461 int x = intel_crtc->cursor_x;
7462 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007463 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007464 bool visible;
7465
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007466 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007467 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007468
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007469 if (x >= intel_crtc->config.pipe_src_w)
7470 base = 0;
7471
7472 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007473 base = 0;
7474
7475 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007476 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007477 base = 0;
7478
7479 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7480 x = -x;
7481 }
7482 pos |= x << CURSOR_X_SHIFT;
7483
7484 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007485 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007486 base = 0;
7487
7488 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7489 y = -y;
7490 }
7491 pos |= y << CURSOR_Y_SHIFT;
7492
7493 visible = base != 0;
7494 if (!visible && !intel_crtc->cursor_visible)
7495 return;
7496
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007497 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007498 I915_WRITE(CURPOS_IVB(pipe), pos);
7499 ivb_update_cursor(crtc, base);
7500 } else {
7501 I915_WRITE(CURPOS(pipe), pos);
7502 if (IS_845G(dev) || IS_I865G(dev))
7503 i845_update_cursor(crtc, base);
7504 else
7505 i9xx_update_cursor(crtc, base);
7506 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007507}
7508
Jesse Barnes79e53942008-11-07 14:24:08 -08007509static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00007510 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08007511 uint32_t handle,
7512 uint32_t width, uint32_t height)
7513{
7514 struct drm_device *dev = crtc->dev;
7515 struct drm_i915_private *dev_priv = dev->dev_private;
7516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007517 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007518 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007519 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007520
Jesse Barnes79e53942008-11-07 14:24:08 -08007521 /* if we want to turn off the cursor ignore width and height */
7522 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007523 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007524 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007525 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007526 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007527 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007528 }
7529
7530 /* Currently we only support 64x64 cursors */
7531 if (width != 64 || height != 64) {
7532 DRM_ERROR("we currently only support 64x64 cursors\n");
7533 return -EINVAL;
7534 }
7535
Chris Wilson05394f32010-11-08 19:18:58 +00007536 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007537 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007538 return -ENOENT;
7539
Chris Wilson05394f32010-11-08 19:18:58 +00007540 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007541 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007542 ret = -ENOMEM;
7543 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007544 }
7545
Dave Airlie71acb5e2008-12-30 20:31:46 +10007546 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007547 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007548 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007549 unsigned alignment;
7550
Chris Wilsond9e86c02010-11-10 16:40:20 +00007551 if (obj->tiling_mode) {
7552 DRM_ERROR("cursor cannot be tiled\n");
7553 ret = -EINVAL;
7554 goto fail_locked;
7555 }
7556
Chris Wilson693db182013-03-05 14:52:39 +00007557 /* Note that the w/a also requires 2 PTE of padding following
7558 * the bo. We currently fill all unused PTE with the shadow
7559 * page and so we should always have valid PTE following the
7560 * cursor preventing the VT-d warning.
7561 */
7562 alignment = 0;
7563 if (need_vtd_wa(dev))
7564 alignment = 64*1024;
7565
7566 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007567 if (ret) {
7568 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007569 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007570 }
7571
Chris Wilsond9e86c02010-11-10 16:40:20 +00007572 ret = i915_gem_object_put_fence(obj);
7573 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007574 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007575 goto fail_unpin;
7576 }
7577
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007578 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007579 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007580 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007581 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007582 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7583 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007584 if (ret) {
7585 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007586 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007587 }
Chris Wilson05394f32010-11-08 19:18:58 +00007588 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007589 }
7590
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007591 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007592 I915_WRITE(CURSIZE, (height << 12) | width);
7593
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007594 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007595 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007596 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007597 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007598 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7599 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007600 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007601 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007602 }
Jesse Barnes80824002009-09-10 15:28:06 -07007603
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007604 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007605
7606 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007607 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007608 intel_crtc->cursor_width = width;
7609 intel_crtc->cursor_height = height;
7610
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007611 if (intel_crtc->active)
7612 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007613
Jesse Barnes79e53942008-11-07 14:24:08 -08007614 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007615fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007616 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007617fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007618 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007619fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007620 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007621 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007622}
7623
7624static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7625{
Jesse Barnes79e53942008-11-07 14:24:08 -08007626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007627
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007628 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7629 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007630
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007631 if (intel_crtc->active)
7632 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007633
7634 return 0;
7635}
7636
Jesse Barnes79e53942008-11-07 14:24:08 -08007637static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007638 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007639{
James Simmons72034252010-08-03 01:33:19 +01007640 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007642
James Simmons72034252010-08-03 01:33:19 +01007643 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007644 intel_crtc->lut_r[i] = red[i] >> 8;
7645 intel_crtc->lut_g[i] = green[i] >> 8;
7646 intel_crtc->lut_b[i] = blue[i] >> 8;
7647 }
7648
7649 intel_crtc_load_lut(crtc);
7650}
7651
Jesse Barnes79e53942008-11-07 14:24:08 -08007652/* VESA 640x480x72Hz mode to set on the pipe */
7653static struct drm_display_mode load_detect_mode = {
7654 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7655 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7656};
7657
Chris Wilsond2dff872011-04-19 08:36:26 +01007658static struct drm_framebuffer *
7659intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007660 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007661 struct drm_i915_gem_object *obj)
7662{
7663 struct intel_framebuffer *intel_fb;
7664 int ret;
7665
7666 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7667 if (!intel_fb) {
7668 drm_gem_object_unreference_unlocked(&obj->base);
7669 return ERR_PTR(-ENOMEM);
7670 }
7671
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007672 ret = i915_mutex_lock_interruptible(dev);
7673 if (ret)
7674 goto err;
7675
Chris Wilsond2dff872011-04-19 08:36:26 +01007676 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007677 mutex_unlock(&dev->struct_mutex);
7678 if (ret)
7679 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007680
7681 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007682err:
7683 drm_gem_object_unreference_unlocked(&obj->base);
7684 kfree(intel_fb);
7685
7686 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007687}
7688
7689static u32
7690intel_framebuffer_pitch_for_width(int width, int bpp)
7691{
7692 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7693 return ALIGN(pitch, 64);
7694}
7695
7696static u32
7697intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7698{
7699 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7700 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7701}
7702
7703static struct drm_framebuffer *
7704intel_framebuffer_create_for_mode(struct drm_device *dev,
7705 struct drm_display_mode *mode,
7706 int depth, int bpp)
7707{
7708 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007709 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007710
7711 obj = i915_gem_alloc_object(dev,
7712 intel_framebuffer_size_for_mode(mode, bpp));
7713 if (obj == NULL)
7714 return ERR_PTR(-ENOMEM);
7715
7716 mode_cmd.width = mode->hdisplay;
7717 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007718 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7719 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007720 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007721
7722 return intel_framebuffer_create(dev, &mode_cmd, obj);
7723}
7724
7725static struct drm_framebuffer *
7726mode_fits_in_fbdev(struct drm_device *dev,
7727 struct drm_display_mode *mode)
7728{
Daniel Vetter4520f532013-10-09 09:18:51 +02007729#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007730 struct drm_i915_private *dev_priv = dev->dev_private;
7731 struct drm_i915_gem_object *obj;
7732 struct drm_framebuffer *fb;
7733
7734 if (dev_priv->fbdev == NULL)
7735 return NULL;
7736
7737 obj = dev_priv->fbdev->ifb.obj;
7738 if (obj == NULL)
7739 return NULL;
7740
7741 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007742 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7743 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007744 return NULL;
7745
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007746 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007747 return NULL;
7748
7749 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007750#else
7751 return NULL;
7752#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007753}
7754
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007755bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007756 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007757 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007758{
7759 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007760 struct intel_encoder *intel_encoder =
7761 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007762 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007763 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007764 struct drm_crtc *crtc = NULL;
7765 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007766 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007767 int i = -1;
7768
Chris Wilsond2dff872011-04-19 08:36:26 +01007769 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7770 connector->base.id, drm_get_connector_name(connector),
7771 encoder->base.id, drm_get_encoder_name(encoder));
7772
Jesse Barnes79e53942008-11-07 14:24:08 -08007773 /*
7774 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007775 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007776 * - if the connector already has an assigned crtc, use it (but make
7777 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007778 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007779 * - try to find the first unused crtc that can drive this connector,
7780 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007781 */
7782
7783 /* See if we already have a CRTC for this connector */
7784 if (encoder->crtc) {
7785 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007786
Daniel Vetter7b240562012-12-12 00:35:33 +01007787 mutex_lock(&crtc->mutex);
7788
Daniel Vetter24218aa2012-08-12 19:27:11 +02007789 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007790 old->load_detect_temp = false;
7791
7792 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007793 if (connector->dpms != DRM_MODE_DPMS_ON)
7794 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007795
Chris Wilson71731882011-04-19 23:10:58 +01007796 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007797 }
7798
7799 /* Find an unused one (if possible) */
7800 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7801 i++;
7802 if (!(encoder->possible_crtcs & (1 << i)))
7803 continue;
7804 if (!possible_crtc->enabled) {
7805 crtc = possible_crtc;
7806 break;
7807 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007808 }
7809
7810 /*
7811 * If we didn't find an unused CRTC, don't use any.
7812 */
7813 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007814 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7815 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007816 }
7817
Daniel Vetter7b240562012-12-12 00:35:33 +01007818 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007819 intel_encoder->new_crtc = to_intel_crtc(crtc);
7820 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007821
7822 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007823 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007824 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007825 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007826
Chris Wilson64927112011-04-20 07:25:26 +01007827 if (!mode)
7828 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007829
Chris Wilsond2dff872011-04-19 08:36:26 +01007830 /* We need a framebuffer large enough to accommodate all accesses
7831 * that the plane may generate whilst we perform load detection.
7832 * We can not rely on the fbcon either being present (we get called
7833 * during its initialisation to detect all boot displays, or it may
7834 * not even exist) or that it is large enough to satisfy the
7835 * requested mode.
7836 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007837 fb = mode_fits_in_fbdev(dev, mode);
7838 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007839 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007840 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7841 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007842 } else
7843 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007844 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007845 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007846 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007847 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007848 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007849
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007850 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007851 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007852 if (old->release_fb)
7853 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007854 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007855 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007856 }
Chris Wilson71731882011-04-19 23:10:58 +01007857
Jesse Barnes79e53942008-11-07 14:24:08 -08007858 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007859 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007860 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007861}
7862
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007863void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007864 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007865{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007866 struct intel_encoder *intel_encoder =
7867 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007868 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007869 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007870
Chris Wilsond2dff872011-04-19 08:36:26 +01007871 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7872 connector->base.id, drm_get_connector_name(connector),
7873 encoder->base.id, drm_get_encoder_name(encoder));
7874
Chris Wilson8261b192011-04-19 23:18:09 +01007875 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007876 to_intel_connector(connector)->new_encoder = NULL;
7877 intel_encoder->new_crtc = NULL;
7878 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007879
Daniel Vetter36206362012-12-10 20:42:17 +01007880 if (old->release_fb) {
7881 drm_framebuffer_unregister_private(old->release_fb);
7882 drm_framebuffer_unreference(old->release_fb);
7883 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007884
Daniel Vetter67c96402013-01-23 16:25:09 +00007885 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007886 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007887 }
7888
Eric Anholtc751ce42010-03-25 11:48:48 -07007889 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007890 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7891 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007892
7893 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007894}
7895
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007896static int i9xx_pll_refclk(struct drm_device *dev,
7897 const struct intel_crtc_config *pipe_config)
7898{
7899 struct drm_i915_private *dev_priv = dev->dev_private;
7900 u32 dpll = pipe_config->dpll_hw_state.dpll;
7901
7902 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007903 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007904 else if (HAS_PCH_SPLIT(dev))
7905 return 120000;
7906 else if (!IS_GEN2(dev))
7907 return 96000;
7908 else
7909 return 48000;
7910}
7911
Jesse Barnes79e53942008-11-07 14:24:08 -08007912/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007913static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7914 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007915{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007916 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007917 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007918 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007919 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007920 u32 fp;
7921 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007922 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007923
7924 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007925 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007926 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007927 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007928
7929 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007930 if (IS_PINEVIEW(dev)) {
7931 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7932 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007933 } else {
7934 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7935 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7936 }
7937
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007938 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007939 if (IS_PINEVIEW(dev))
7940 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7941 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007942 else
7943 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007944 DPLL_FPA01_P1_POST_DIV_SHIFT);
7945
7946 switch (dpll & DPLL_MODE_MASK) {
7947 case DPLLB_MODE_DAC_SERIAL:
7948 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7949 5 : 10;
7950 break;
7951 case DPLLB_MODE_LVDS:
7952 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7953 7 : 14;
7954 break;
7955 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007956 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007957 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007958 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007959 }
7960
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007961 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007962 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007963 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007964 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007965 } else {
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02007966 u32 lvds = I915_READ(LVDS);
7967 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08007968
7969 if (is_lvds) {
7970 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7971 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02007972
7973 if (lvds & LVDS_CLKB_POWER_UP)
7974 clock.p2 = 7;
7975 else
7976 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007977 } else {
7978 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7979 clock.p1 = 2;
7980 else {
7981 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7982 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7983 }
7984 if (dpll & PLL_P2_DIVIDE_BY_4)
7985 clock.p2 = 4;
7986 else
7987 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007988 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007989
7990 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007991 }
7992
Ville Syrjälä18442d02013-09-13 16:00:08 +03007993 /*
7994 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007995 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007996 * encoder's get_config() function.
7997 */
7998 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007999}
8000
Ville Syrjälä6878da02013-09-13 15:59:11 +03008001int intel_dotclock_calculate(int link_freq,
8002 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008003{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008004 /*
8005 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008006 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008007 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008008 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008009 *
8010 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008011 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008012 */
8013
Ville Syrjälä6878da02013-09-13 15:59:11 +03008014 if (!m_n->link_n)
8015 return 0;
8016
8017 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8018}
8019
Ville Syrjälä18442d02013-09-13 16:00:08 +03008020static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8021 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008022{
8023 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008024
8025 /* read out port_clock from the DPLL */
8026 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008027
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008028 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008029 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008030 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008031 * agree once we know their relationship in the encoder's
8032 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008033 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008034 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008035 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8036 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008037}
8038
8039/** Returns the currently programmed mode of the given pipe. */
8040struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8041 struct drm_crtc *crtc)
8042{
Jesse Barnes548f2452011-02-17 10:40:53 -08008043 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008045 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008046 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008047 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008048 int htot = I915_READ(HTOTAL(cpu_transcoder));
8049 int hsync = I915_READ(HSYNC(cpu_transcoder));
8050 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8051 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008052 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008053
8054 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8055 if (!mode)
8056 return NULL;
8057
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008058 /*
8059 * Construct a pipe_config sufficient for getting the clock info
8060 * back out of crtc_clock_get.
8061 *
8062 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8063 * to use a real value here instead.
8064 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008065 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008066 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008067 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8068 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8069 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008070 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8071
Ville Syrjälä773ae032013-09-23 17:48:20 +03008072 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008073 mode->hdisplay = (htot & 0xffff) + 1;
8074 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8075 mode->hsync_start = (hsync & 0xffff) + 1;
8076 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8077 mode->vdisplay = (vtot & 0xffff) + 1;
8078 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8079 mode->vsync_start = (vsync & 0xffff) + 1;
8080 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8081
8082 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008083
8084 return mode;
8085}
8086
Daniel Vetter3dec0092010-08-20 21:40:52 +02008087static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008088{
8089 struct drm_device *dev = crtc->dev;
8090 drm_i915_private_t *dev_priv = dev->dev_private;
8091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8092 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008093 int dpll_reg = DPLL(pipe);
8094 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008095
Eric Anholtbad720f2009-10-22 16:11:14 -07008096 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008097 return;
8098
8099 if (!dev_priv->lvds_downclock_avail)
8100 return;
8101
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008102 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008103 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008104 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008105
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008106 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008107
8108 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8109 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008110 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008111
Jesse Barnes652c3932009-08-17 13:31:43 -07008112 dpll = I915_READ(dpll_reg);
8113 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008114 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008115 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008116}
8117
8118static void intel_decrease_pllclock(struct drm_crtc *crtc)
8119{
8120 struct drm_device *dev = crtc->dev;
8121 drm_i915_private_t *dev_priv = dev->dev_private;
8122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008123
Eric Anholtbad720f2009-10-22 16:11:14 -07008124 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008125 return;
8126
8127 if (!dev_priv->lvds_downclock_avail)
8128 return;
8129
8130 /*
8131 * Since this is called by a timer, we should never get here in
8132 * the manual case.
8133 */
8134 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008135 int pipe = intel_crtc->pipe;
8136 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008137 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008138
Zhao Yakui44d98a62009-10-09 11:39:40 +08008139 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008140
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008141 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008142
Chris Wilson074b5e12012-05-02 12:07:06 +01008143 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008144 dpll |= DISPLAY_RATE_SELECT_FPA1;
8145 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008146 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008147 dpll = I915_READ(dpll_reg);
8148 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008149 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008150 }
8151
8152}
8153
Chris Wilsonf047e392012-07-21 12:31:41 +01008154void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008155{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008156 struct drm_i915_private *dev_priv = dev->dev_private;
8157
8158 hsw_package_c8_gpu_busy(dev_priv);
8159 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008160}
8161
8162void intel_mark_idle(struct drm_device *dev)
8163{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008164 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008165 struct drm_crtc *crtc;
8166
Paulo Zanonic67a4702013-08-19 13:18:09 -03008167 hsw_package_c8_gpu_idle(dev_priv);
8168
Chris Wilson725a5b52013-01-08 11:02:57 +00008169 if (!i915_powersave)
8170 return;
8171
8172 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8173 if (!crtc->fb)
8174 continue;
8175
8176 intel_decrease_pllclock(crtc);
8177 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008178
8179 if (dev_priv->info->gen >= 6)
8180 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008181}
8182
Chris Wilsonc65355b2013-06-06 16:53:41 -03008183void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8184 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008185{
8186 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008187 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008188
8189 if (!i915_powersave)
8190 return;
8191
Jesse Barnes652c3932009-08-17 13:31:43 -07008192 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008193 if (!crtc->fb)
8194 continue;
8195
Chris Wilsonc65355b2013-06-06 16:53:41 -03008196 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8197 continue;
8198
8199 intel_increase_pllclock(crtc);
8200 if (ring && intel_fbc_enabled(dev))
8201 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008202 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008203}
8204
Jesse Barnes79e53942008-11-07 14:24:08 -08008205static void intel_crtc_destroy(struct drm_crtc *crtc)
8206{
8207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008208 struct drm_device *dev = crtc->dev;
8209 struct intel_unpin_work *work;
8210 unsigned long flags;
8211
8212 spin_lock_irqsave(&dev->event_lock, flags);
8213 work = intel_crtc->unpin_work;
8214 intel_crtc->unpin_work = NULL;
8215 spin_unlock_irqrestore(&dev->event_lock, flags);
8216
8217 if (work) {
8218 cancel_work_sync(&work->work);
8219 kfree(work);
8220 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008221
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008222 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8223
Jesse Barnes79e53942008-11-07 14:24:08 -08008224 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008225
Jesse Barnes79e53942008-11-07 14:24:08 -08008226 kfree(intel_crtc);
8227}
8228
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008229static void intel_unpin_work_fn(struct work_struct *__work)
8230{
8231 struct intel_unpin_work *work =
8232 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008233 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008234
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008235 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008236 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008237 drm_gem_object_unreference(&work->pending_flip_obj->base);
8238 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008239
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008240 intel_update_fbc(dev);
8241 mutex_unlock(&dev->struct_mutex);
8242
8243 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8244 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8245
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008246 kfree(work);
8247}
8248
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008249static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008250 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008251{
8252 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8254 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008255 unsigned long flags;
8256
8257 /* Ignore early vblank irqs */
8258 if (intel_crtc == NULL)
8259 return;
8260
8261 spin_lock_irqsave(&dev->event_lock, flags);
8262 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008263
8264 /* Ensure we don't miss a work->pending update ... */
8265 smp_rmb();
8266
8267 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008268 spin_unlock_irqrestore(&dev->event_lock, flags);
8269 return;
8270 }
8271
Chris Wilsone7d841c2012-12-03 11:36:30 +00008272 /* and that the unpin work is consistent wrt ->pending. */
8273 smp_rmb();
8274
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008275 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008276
Rob Clark45a066e2012-10-08 14:50:40 -05008277 if (work->event)
8278 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008279
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008280 drm_vblank_put(dev, intel_crtc->pipe);
8281
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008282 spin_unlock_irqrestore(&dev->event_lock, flags);
8283
Daniel Vetter2c10d572012-12-20 21:24:07 +01008284 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008285
8286 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008287
8288 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008289}
8290
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008291void intel_finish_page_flip(struct drm_device *dev, int pipe)
8292{
8293 drm_i915_private_t *dev_priv = dev->dev_private;
8294 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8295
Mario Kleiner49b14a52010-12-09 07:00:07 +01008296 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008297}
8298
8299void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8300{
8301 drm_i915_private_t *dev_priv = dev->dev_private;
8302 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8303
Mario Kleiner49b14a52010-12-09 07:00:07 +01008304 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008305}
8306
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008307void intel_prepare_page_flip(struct drm_device *dev, int plane)
8308{
8309 drm_i915_private_t *dev_priv = dev->dev_private;
8310 struct intel_crtc *intel_crtc =
8311 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8312 unsigned long flags;
8313
Chris Wilsone7d841c2012-12-03 11:36:30 +00008314 /* NB: An MMIO update of the plane base pointer will also
8315 * generate a page-flip completion irq, i.e. every modeset
8316 * is also accompanied by a spurious intel_prepare_page_flip().
8317 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008318 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008319 if (intel_crtc->unpin_work)
8320 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008321 spin_unlock_irqrestore(&dev->event_lock, flags);
8322}
8323
Chris Wilsone7d841c2012-12-03 11:36:30 +00008324inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8325{
8326 /* Ensure that the work item is consistent when activating it ... */
8327 smp_wmb();
8328 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8329 /* and that it is marked active as soon as the irq could fire. */
8330 smp_wmb();
8331}
8332
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008333static int intel_gen2_queue_flip(struct drm_device *dev,
8334 struct drm_crtc *crtc,
8335 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008336 struct drm_i915_gem_object *obj,
8337 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008338{
8339 struct drm_i915_private *dev_priv = dev->dev_private;
8340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008341 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008342 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008343 int ret;
8344
Daniel Vetter6d90c952012-04-26 23:28:05 +02008345 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008346 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008347 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008348
Daniel Vetter6d90c952012-04-26 23:28:05 +02008349 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008350 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008351 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008352
8353 /* Can't queue multiple flips, so wait for the previous
8354 * one to finish before executing the next.
8355 */
8356 if (intel_crtc->plane)
8357 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8358 else
8359 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008360 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8361 intel_ring_emit(ring, MI_NOOP);
8362 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8363 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8364 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008365 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008366 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008367
8368 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008369 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008370 return 0;
8371
8372err_unpin:
8373 intel_unpin_fb_obj(obj);
8374err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008375 return ret;
8376}
8377
8378static int intel_gen3_queue_flip(struct drm_device *dev,
8379 struct drm_crtc *crtc,
8380 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008381 struct drm_i915_gem_object *obj,
8382 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008383{
8384 struct drm_i915_private *dev_priv = dev->dev_private;
8385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008386 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008387 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008388 int ret;
8389
Daniel Vetter6d90c952012-04-26 23:28:05 +02008390 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008391 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008392 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008393
Daniel Vetter6d90c952012-04-26 23:28:05 +02008394 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008395 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008396 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008397
8398 if (intel_crtc->plane)
8399 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8400 else
8401 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008402 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8403 intel_ring_emit(ring, MI_NOOP);
8404 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8405 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8406 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008407 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008408 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008409
Chris Wilsone7d841c2012-12-03 11:36:30 +00008410 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008411 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008412 return 0;
8413
8414err_unpin:
8415 intel_unpin_fb_obj(obj);
8416err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008417 return ret;
8418}
8419
8420static int intel_gen4_queue_flip(struct drm_device *dev,
8421 struct drm_crtc *crtc,
8422 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008423 struct drm_i915_gem_object *obj,
8424 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008425{
8426 struct drm_i915_private *dev_priv = dev->dev_private;
8427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8428 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008429 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008430 int ret;
8431
Daniel Vetter6d90c952012-04-26 23:28:05 +02008432 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008433 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008434 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008435
Daniel Vetter6d90c952012-04-26 23:28:05 +02008436 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008437 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008438 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008439
8440 /* i965+ uses the linear or tiled offsets from the
8441 * Display Registers (which do not change across a page-flip)
8442 * so we need only reprogram the base address.
8443 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008444 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8445 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8446 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008447 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008448 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008449 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008450
8451 /* XXX Enabling the panel-fitter across page-flip is so far
8452 * untested on non-native modes, so ignore it for now.
8453 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8454 */
8455 pf = 0;
8456 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008457 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008458
8459 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008460 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008461 return 0;
8462
8463err_unpin:
8464 intel_unpin_fb_obj(obj);
8465err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008466 return ret;
8467}
8468
8469static int intel_gen6_queue_flip(struct drm_device *dev,
8470 struct drm_crtc *crtc,
8471 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008472 struct drm_i915_gem_object *obj,
8473 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008474{
8475 struct drm_i915_private *dev_priv = dev->dev_private;
8476 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008477 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008478 uint32_t pf, pipesrc;
8479 int ret;
8480
Daniel Vetter6d90c952012-04-26 23:28:05 +02008481 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008482 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008483 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008484
Daniel Vetter6d90c952012-04-26 23:28:05 +02008485 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008486 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008487 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008488
Daniel Vetter6d90c952012-04-26 23:28:05 +02008489 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8490 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8491 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008492 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008493
Chris Wilson99d9acd2012-04-17 20:37:00 +01008494 /* Contrary to the suggestions in the documentation,
8495 * "Enable Panel Fitter" does not seem to be required when page
8496 * flipping with a non-native mode, and worse causes a normal
8497 * modeset to fail.
8498 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8499 */
8500 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008501 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008502 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008503
8504 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008505 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008506 return 0;
8507
8508err_unpin:
8509 intel_unpin_fb_obj(obj);
8510err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008511 return ret;
8512}
8513
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008514static int intel_gen7_queue_flip(struct drm_device *dev,
8515 struct drm_crtc *crtc,
8516 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008517 struct drm_i915_gem_object *obj,
8518 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008519{
8520 struct drm_i915_private *dev_priv = dev->dev_private;
8521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008522 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008523 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008524 int len, ret;
8525
8526 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008527 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008528 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008529
8530 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8531 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008532 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008533
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008534 switch(intel_crtc->plane) {
8535 case PLANE_A:
8536 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8537 break;
8538 case PLANE_B:
8539 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8540 break;
8541 case PLANE_C:
8542 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8543 break;
8544 default:
8545 WARN_ONCE(1, "unknown plane in flip command\n");
8546 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008547 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008548 }
8549
Chris Wilsonffe74d72013-08-26 20:58:12 +01008550 len = 4;
8551 if (ring->id == RCS)
8552 len += 6;
8553
8554 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008555 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008556 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008557
Chris Wilsonffe74d72013-08-26 20:58:12 +01008558 /* Unmask the flip-done completion message. Note that the bspec says that
8559 * we should do this for both the BCS and RCS, and that we must not unmask
8560 * more than one flip event at any time (or ensure that one flip message
8561 * can be sent by waiting for flip-done prior to queueing new flips).
8562 * Experimentation says that BCS works despite DERRMR masking all
8563 * flip-done completion events and that unmasking all planes at once
8564 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8565 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8566 */
8567 if (ring->id == RCS) {
8568 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8569 intel_ring_emit(ring, DERRMR);
8570 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8571 DERRMR_PIPEB_PRI_FLIP_DONE |
8572 DERRMR_PIPEC_PRI_FLIP_DONE));
8573 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8574 intel_ring_emit(ring, DERRMR);
8575 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8576 }
8577
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008578 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008579 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008580 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008581 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008582
8583 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008584 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008585 return 0;
8586
8587err_unpin:
8588 intel_unpin_fb_obj(obj);
8589err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008590 return ret;
8591}
8592
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008593static int intel_default_queue_flip(struct drm_device *dev,
8594 struct drm_crtc *crtc,
8595 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008596 struct drm_i915_gem_object *obj,
8597 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008598{
8599 return -ENODEV;
8600}
8601
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008602static int intel_crtc_page_flip(struct drm_crtc *crtc,
8603 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008604 struct drm_pending_vblank_event *event,
8605 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008606{
8607 struct drm_device *dev = crtc->dev;
8608 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008609 struct drm_framebuffer *old_fb = crtc->fb;
8610 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8612 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008613 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008614 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008615
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008616 /* Can't change pixel format via MI display flips. */
8617 if (fb->pixel_format != crtc->fb->pixel_format)
8618 return -EINVAL;
8619
8620 /*
8621 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8622 * Note that pitch changes could also affect these register.
8623 */
8624 if (INTEL_INFO(dev)->gen > 3 &&
8625 (fb->offsets[0] != crtc->fb->offsets[0] ||
8626 fb->pitches[0] != crtc->fb->pitches[0]))
8627 return -EINVAL;
8628
Daniel Vetterb14c5672013-09-19 12:18:32 +02008629 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008630 if (work == NULL)
8631 return -ENOMEM;
8632
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008633 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008634 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008635 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008636 INIT_WORK(&work->work, intel_unpin_work_fn);
8637
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008638 ret = drm_vblank_get(dev, intel_crtc->pipe);
8639 if (ret)
8640 goto free_work;
8641
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008642 /* We borrow the event spin lock for protecting unpin_work */
8643 spin_lock_irqsave(&dev->event_lock, flags);
8644 if (intel_crtc->unpin_work) {
8645 spin_unlock_irqrestore(&dev->event_lock, flags);
8646 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008647 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008648
8649 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008650 return -EBUSY;
8651 }
8652 intel_crtc->unpin_work = work;
8653 spin_unlock_irqrestore(&dev->event_lock, flags);
8654
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008655 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8656 flush_workqueue(dev_priv->wq);
8657
Chris Wilson79158102012-05-23 11:13:58 +01008658 ret = i915_mutex_lock_interruptible(dev);
8659 if (ret)
8660 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008661
Jesse Barnes75dfca82010-02-10 15:09:44 -08008662 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008663 drm_gem_object_reference(&work->old_fb_obj->base);
8664 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008665
8666 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008667
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008668 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008669
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008670 work->enable_stall_check = true;
8671
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008672 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008673 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008674
Keith Packarded8d1972013-07-22 18:49:58 -07008675 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008676 if (ret)
8677 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008678
Chris Wilson7782de32011-07-08 12:22:41 +01008679 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008680 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008681 mutex_unlock(&dev->struct_mutex);
8682
Jesse Barnese5510fa2010-07-01 16:48:37 -07008683 trace_i915_flip_request(intel_crtc->plane, obj);
8684
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008685 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008686
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008687cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008688 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008689 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008690 drm_gem_object_unreference(&work->old_fb_obj->base);
8691 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008692 mutex_unlock(&dev->struct_mutex);
8693
Chris Wilson79158102012-05-23 11:13:58 +01008694cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008695 spin_lock_irqsave(&dev->event_lock, flags);
8696 intel_crtc->unpin_work = NULL;
8697 spin_unlock_irqrestore(&dev->event_lock, flags);
8698
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008699 drm_vblank_put(dev, intel_crtc->pipe);
8700free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008701 kfree(work);
8702
8703 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008704}
8705
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008706static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008707 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8708 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008709};
8710
Daniel Vetter50f56112012-07-02 09:35:43 +02008711static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8712 struct drm_crtc *crtc)
8713{
8714 struct drm_device *dev;
8715 struct drm_crtc *tmp;
8716 int crtc_mask = 1;
8717
8718 WARN(!crtc, "checking null crtc?\n");
8719
8720 dev = crtc->dev;
8721
8722 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8723 if (tmp == crtc)
8724 break;
8725 crtc_mask <<= 1;
8726 }
8727
8728 if (encoder->possible_crtcs & crtc_mask)
8729 return true;
8730 return false;
8731}
8732
Daniel Vetter9a935852012-07-05 22:34:27 +02008733/**
8734 * intel_modeset_update_staged_output_state
8735 *
8736 * Updates the staged output configuration state, e.g. after we've read out the
8737 * current hw state.
8738 */
8739static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8740{
8741 struct intel_encoder *encoder;
8742 struct intel_connector *connector;
8743
8744 list_for_each_entry(connector, &dev->mode_config.connector_list,
8745 base.head) {
8746 connector->new_encoder =
8747 to_intel_encoder(connector->base.encoder);
8748 }
8749
8750 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8751 base.head) {
8752 encoder->new_crtc =
8753 to_intel_crtc(encoder->base.crtc);
8754 }
8755}
8756
8757/**
8758 * intel_modeset_commit_output_state
8759 *
8760 * This function copies the stage display pipe configuration to the real one.
8761 */
8762static void intel_modeset_commit_output_state(struct drm_device *dev)
8763{
8764 struct intel_encoder *encoder;
8765 struct intel_connector *connector;
8766
8767 list_for_each_entry(connector, &dev->mode_config.connector_list,
8768 base.head) {
8769 connector->base.encoder = &connector->new_encoder->base;
8770 }
8771
8772 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8773 base.head) {
8774 encoder->base.crtc = &encoder->new_crtc->base;
8775 }
8776}
8777
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008778static void
8779connected_sink_compute_bpp(struct intel_connector * connector,
8780 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008781{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008782 int bpp = pipe_config->pipe_bpp;
8783
8784 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8785 connector->base.base.id,
8786 drm_get_connector_name(&connector->base));
8787
8788 /* Don't use an invalid EDID bpc value */
8789 if (connector->base.display_info.bpc &&
8790 connector->base.display_info.bpc * 3 < bpp) {
8791 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8792 bpp, connector->base.display_info.bpc*3);
8793 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8794 }
8795
8796 /* Clamp bpp to 8 on screens without EDID 1.4 */
8797 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8798 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8799 bpp);
8800 pipe_config->pipe_bpp = 24;
8801 }
8802}
8803
8804static int
8805compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8806 struct drm_framebuffer *fb,
8807 struct intel_crtc_config *pipe_config)
8808{
8809 struct drm_device *dev = crtc->base.dev;
8810 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008811 int bpp;
8812
Daniel Vetterd42264b2013-03-28 16:38:08 +01008813 switch (fb->pixel_format) {
8814 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008815 bpp = 8*3; /* since we go through a colormap */
8816 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008817 case DRM_FORMAT_XRGB1555:
8818 case DRM_FORMAT_ARGB1555:
8819 /* checked in intel_framebuffer_init already */
8820 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8821 return -EINVAL;
8822 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008823 bpp = 6*3; /* min is 18bpp */
8824 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008825 case DRM_FORMAT_XBGR8888:
8826 case DRM_FORMAT_ABGR8888:
8827 /* checked in intel_framebuffer_init already */
8828 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8829 return -EINVAL;
8830 case DRM_FORMAT_XRGB8888:
8831 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008832 bpp = 8*3;
8833 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008834 case DRM_FORMAT_XRGB2101010:
8835 case DRM_FORMAT_ARGB2101010:
8836 case DRM_FORMAT_XBGR2101010:
8837 case DRM_FORMAT_ABGR2101010:
8838 /* checked in intel_framebuffer_init already */
8839 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008840 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008841 bpp = 10*3;
8842 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008843 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008844 default:
8845 DRM_DEBUG_KMS("unsupported depth\n");
8846 return -EINVAL;
8847 }
8848
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008849 pipe_config->pipe_bpp = bpp;
8850
8851 /* Clamp display bpp to EDID value */
8852 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008853 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008854 if (!connector->new_encoder ||
8855 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008856 continue;
8857
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008858 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008859 }
8860
8861 return bpp;
8862}
8863
Daniel Vetter644db712013-09-19 14:53:58 +02008864static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8865{
8866 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8867 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008868 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008869 mode->crtc_hdisplay, mode->crtc_hsync_start,
8870 mode->crtc_hsync_end, mode->crtc_htotal,
8871 mode->crtc_vdisplay, mode->crtc_vsync_start,
8872 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8873}
8874
Daniel Vetterc0b03412013-05-28 12:05:54 +02008875static void intel_dump_pipe_config(struct intel_crtc *crtc,
8876 struct intel_crtc_config *pipe_config,
8877 const char *context)
8878{
8879 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8880 context, pipe_name(crtc->pipe));
8881
8882 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8883 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8884 pipe_config->pipe_bpp, pipe_config->dither);
8885 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8886 pipe_config->has_pch_encoder,
8887 pipe_config->fdi_lanes,
8888 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8889 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8890 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008891 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8892 pipe_config->has_dp_encoder,
8893 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8894 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8895 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008896 DRM_DEBUG_KMS("requested mode:\n");
8897 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8898 DRM_DEBUG_KMS("adjusted mode:\n");
8899 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008900 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008901 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008902 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8903 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008904 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8905 pipe_config->gmch_pfit.control,
8906 pipe_config->gmch_pfit.pgm_ratios,
8907 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008908 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008909 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008910 pipe_config->pch_pfit.size,
8911 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008912 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008913 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008914}
8915
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008916static bool check_encoder_cloning(struct drm_crtc *crtc)
8917{
8918 int num_encoders = 0;
8919 bool uncloneable_encoders = false;
8920 struct intel_encoder *encoder;
8921
8922 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8923 base.head) {
8924 if (&encoder->new_crtc->base != crtc)
8925 continue;
8926
8927 num_encoders++;
8928 if (!encoder->cloneable)
8929 uncloneable_encoders = true;
8930 }
8931
8932 return !(num_encoders > 1 && uncloneable_encoders);
8933}
8934
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008935static struct intel_crtc_config *
8936intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008937 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008938 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008939{
8940 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008941 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008942 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008943 int plane_bpp, ret = -EINVAL;
8944 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008945
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008946 if (!check_encoder_cloning(crtc)) {
8947 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8948 return ERR_PTR(-EINVAL);
8949 }
8950
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008951 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8952 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008953 return ERR_PTR(-ENOMEM);
8954
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008955 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8956 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008957
Daniel Vettere143a212013-07-04 12:01:15 +02008958 pipe_config->cpu_transcoder =
8959 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008960 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008961
Imre Deak2960bc92013-07-30 13:36:32 +03008962 /*
8963 * Sanitize sync polarity flags based on requested ones. If neither
8964 * positive or negative polarity is requested, treat this as meaning
8965 * negative polarity.
8966 */
8967 if (!(pipe_config->adjusted_mode.flags &
8968 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8969 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8970
8971 if (!(pipe_config->adjusted_mode.flags &
8972 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8973 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8974
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008975 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8976 * plane pixel format and any sink constraints into account. Returns the
8977 * source plane bpp so that dithering can be selected on mismatches
8978 * after encoders and crtc also have had their say. */
8979 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8980 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008981 if (plane_bpp < 0)
8982 goto fail;
8983
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008984 /*
8985 * Determine the real pipe dimensions. Note that stereo modes can
8986 * increase the actual pipe size due to the frame doubling and
8987 * insertion of additional space for blanks between the frame. This
8988 * is stored in the crtc timings. We use the requested mode to do this
8989 * computation to clearly distinguish it from the adjusted mode, which
8990 * can be changed by the connectors in the below retry loop.
8991 */
8992 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8993 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8994 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8995
Daniel Vettere29c22c2013-02-21 00:00:16 +01008996encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008997 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008998 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008999 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009000
Daniel Vetter135c81b2013-07-21 21:37:09 +02009001 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009002 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009003
Daniel Vetter7758a112012-07-08 19:40:39 +02009004 /* Pass our mode to the connectors and the CRTC to give them a chance to
9005 * adjust it according to limitations or connector properties, and also
9006 * a chance to reject the mode entirely.
9007 */
9008 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9009 base.head) {
9010
9011 if (&encoder->new_crtc->base != crtc)
9012 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009013
Daniel Vetterefea6e82013-07-21 21:36:59 +02009014 if (!(encoder->compute_config(encoder, pipe_config))) {
9015 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009016 goto fail;
9017 }
9018 }
9019
Daniel Vetterff9a6752013-06-01 17:16:21 +02009020 /* Set default port clock if not overwritten by the encoder. Needs to be
9021 * done afterwards in case the encoder adjusts the mode. */
9022 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009023 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9024 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009025
Daniel Vettera43f6e02013-06-07 23:10:32 +02009026 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009027 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009028 DRM_DEBUG_KMS("CRTC fixup failed\n");
9029 goto fail;
9030 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009031
9032 if (ret == RETRY) {
9033 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9034 ret = -EINVAL;
9035 goto fail;
9036 }
9037
9038 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9039 retry = false;
9040 goto encoder_retry;
9041 }
9042
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009043 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9044 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9045 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9046
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009047 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009048fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009049 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009050 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009051}
9052
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009053/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9054 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9055static void
9056intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9057 unsigned *prepare_pipes, unsigned *disable_pipes)
9058{
9059 struct intel_crtc *intel_crtc;
9060 struct drm_device *dev = crtc->dev;
9061 struct intel_encoder *encoder;
9062 struct intel_connector *connector;
9063 struct drm_crtc *tmp_crtc;
9064
9065 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9066
9067 /* Check which crtcs have changed outputs connected to them, these need
9068 * to be part of the prepare_pipes mask. We don't (yet) support global
9069 * modeset across multiple crtcs, so modeset_pipes will only have one
9070 * bit set at most. */
9071 list_for_each_entry(connector, &dev->mode_config.connector_list,
9072 base.head) {
9073 if (connector->base.encoder == &connector->new_encoder->base)
9074 continue;
9075
9076 if (connector->base.encoder) {
9077 tmp_crtc = connector->base.encoder->crtc;
9078
9079 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9080 }
9081
9082 if (connector->new_encoder)
9083 *prepare_pipes |=
9084 1 << connector->new_encoder->new_crtc->pipe;
9085 }
9086
9087 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9088 base.head) {
9089 if (encoder->base.crtc == &encoder->new_crtc->base)
9090 continue;
9091
9092 if (encoder->base.crtc) {
9093 tmp_crtc = encoder->base.crtc;
9094
9095 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9096 }
9097
9098 if (encoder->new_crtc)
9099 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9100 }
9101
9102 /* Check for any pipes that will be fully disabled ... */
9103 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9104 base.head) {
9105 bool used = false;
9106
9107 /* Don't try to disable disabled crtcs. */
9108 if (!intel_crtc->base.enabled)
9109 continue;
9110
9111 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9112 base.head) {
9113 if (encoder->new_crtc == intel_crtc)
9114 used = true;
9115 }
9116
9117 if (!used)
9118 *disable_pipes |= 1 << intel_crtc->pipe;
9119 }
9120
9121
9122 /* set_mode is also used to update properties on life display pipes. */
9123 intel_crtc = to_intel_crtc(crtc);
9124 if (crtc->enabled)
9125 *prepare_pipes |= 1 << intel_crtc->pipe;
9126
Daniel Vetterb6c51642013-04-12 18:48:43 +02009127 /*
9128 * For simplicity do a full modeset on any pipe where the output routing
9129 * changed. We could be more clever, but that would require us to be
9130 * more careful with calling the relevant encoder->mode_set functions.
9131 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009132 if (*prepare_pipes)
9133 *modeset_pipes = *prepare_pipes;
9134
9135 /* ... and mask these out. */
9136 *modeset_pipes &= ~(*disable_pipes);
9137 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009138
9139 /*
9140 * HACK: We don't (yet) fully support global modesets. intel_set_config
9141 * obies this rule, but the modeset restore mode of
9142 * intel_modeset_setup_hw_state does not.
9143 */
9144 *modeset_pipes &= 1 << intel_crtc->pipe;
9145 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009146
9147 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9148 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009149}
9150
Daniel Vetterea9d7582012-07-10 10:42:52 +02009151static bool intel_crtc_in_use(struct drm_crtc *crtc)
9152{
9153 struct drm_encoder *encoder;
9154 struct drm_device *dev = crtc->dev;
9155
9156 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9157 if (encoder->crtc == crtc)
9158 return true;
9159
9160 return false;
9161}
9162
9163static void
9164intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9165{
9166 struct intel_encoder *intel_encoder;
9167 struct intel_crtc *intel_crtc;
9168 struct drm_connector *connector;
9169
9170 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9171 base.head) {
9172 if (!intel_encoder->base.crtc)
9173 continue;
9174
9175 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9176
9177 if (prepare_pipes & (1 << intel_crtc->pipe))
9178 intel_encoder->connectors_active = false;
9179 }
9180
9181 intel_modeset_commit_output_state(dev);
9182
9183 /* Update computed state. */
9184 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9185 base.head) {
9186 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9187 }
9188
9189 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9190 if (!connector->encoder || !connector->encoder->crtc)
9191 continue;
9192
9193 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9194
9195 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009196 struct drm_property *dpms_property =
9197 dev->mode_config.dpms_property;
9198
Daniel Vetterea9d7582012-07-10 10:42:52 +02009199 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009200 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009201 dpms_property,
9202 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009203
9204 intel_encoder = to_intel_encoder(connector->encoder);
9205 intel_encoder->connectors_active = true;
9206 }
9207 }
9208
9209}
9210
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009211static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009212{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009213 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009214
9215 if (clock1 == clock2)
9216 return true;
9217
9218 if (!clock1 || !clock2)
9219 return false;
9220
9221 diff = abs(clock1 - clock2);
9222
9223 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9224 return true;
9225
9226 return false;
9227}
9228
Daniel Vetter25c5b262012-07-08 22:08:04 +02009229#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9230 list_for_each_entry((intel_crtc), \
9231 &(dev)->mode_config.crtc_list, \
9232 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009233 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009234
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009235static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009236intel_pipe_config_compare(struct drm_device *dev,
9237 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009238 struct intel_crtc_config *pipe_config)
9239{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009240#define PIPE_CONF_CHECK_X(name) \
9241 if (current_config->name != pipe_config->name) { \
9242 DRM_ERROR("mismatch in " #name " " \
9243 "(expected 0x%08x, found 0x%08x)\n", \
9244 current_config->name, \
9245 pipe_config->name); \
9246 return false; \
9247 }
9248
Daniel Vetter08a24032013-04-19 11:25:34 +02009249#define PIPE_CONF_CHECK_I(name) \
9250 if (current_config->name != pipe_config->name) { \
9251 DRM_ERROR("mismatch in " #name " " \
9252 "(expected %i, found %i)\n", \
9253 current_config->name, \
9254 pipe_config->name); \
9255 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009256 }
9257
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009258#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9259 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009260 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009261 "(expected %i, found %i)\n", \
9262 current_config->name & (mask), \
9263 pipe_config->name & (mask)); \
9264 return false; \
9265 }
9266
Ville Syrjälä5e550652013-09-06 23:29:07 +03009267#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9268 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9269 DRM_ERROR("mismatch in " #name " " \
9270 "(expected %i, found %i)\n", \
9271 current_config->name, \
9272 pipe_config->name); \
9273 return false; \
9274 }
9275
Daniel Vetterbb760062013-06-06 14:55:52 +02009276#define PIPE_CONF_QUIRK(quirk) \
9277 ((current_config->quirks | pipe_config->quirks) & (quirk))
9278
Daniel Vettereccb1402013-05-22 00:50:22 +02009279 PIPE_CONF_CHECK_I(cpu_transcoder);
9280
Daniel Vetter08a24032013-04-19 11:25:34 +02009281 PIPE_CONF_CHECK_I(has_pch_encoder);
9282 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009283 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9284 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9285 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9286 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9287 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009288
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009289 PIPE_CONF_CHECK_I(has_dp_encoder);
9290 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9291 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9292 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9293 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9294 PIPE_CONF_CHECK_I(dp_m_n.tu);
9295
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009296 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9297 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9298 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9299 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9300 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9301 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9302
9303 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9304 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9305 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9306 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9307 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9308 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9309
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009310 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009311
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009312 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9313 DRM_MODE_FLAG_INTERLACE);
9314
Daniel Vetterbb760062013-06-06 14:55:52 +02009315 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9316 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9317 DRM_MODE_FLAG_PHSYNC);
9318 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9319 DRM_MODE_FLAG_NHSYNC);
9320 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9321 DRM_MODE_FLAG_PVSYNC);
9322 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9323 DRM_MODE_FLAG_NVSYNC);
9324 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009325
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009326 PIPE_CONF_CHECK_I(pipe_src_w);
9327 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009328
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009329 PIPE_CONF_CHECK_I(gmch_pfit.control);
9330 /* pfit ratios are autocomputed by the hw on gen4+ */
9331 if (INTEL_INFO(dev)->gen < 4)
9332 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9333 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009334 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9335 if (current_config->pch_pfit.enabled) {
9336 PIPE_CONF_CHECK_I(pch_pfit.pos);
9337 PIPE_CONF_CHECK_I(pch_pfit.size);
9338 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009339
Jesse Barnese59150d2014-01-07 13:30:45 -08009340 /* BDW+ don't expose a synchronous way to read the state */
9341 if (IS_HASWELL(dev))
9342 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009343
Ville Syrjälä282740f2013-09-04 18:30:03 +03009344 PIPE_CONF_CHECK_I(double_wide);
9345
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009346 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009347 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009348 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009349 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9350 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009351
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009352 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9353 PIPE_CONF_CHECK_I(pipe_bpp);
9354
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009355 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01009356 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009357 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9358 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03009359
Daniel Vetter66e985c2013-06-05 13:34:20 +02009360#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009361#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009362#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009363#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009364#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009365
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009366 return true;
9367}
9368
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009369static void
9370check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009371{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009372 struct intel_connector *connector;
9373
9374 list_for_each_entry(connector, &dev->mode_config.connector_list,
9375 base.head) {
9376 /* This also checks the encoder/connector hw state with the
9377 * ->get_hw_state callbacks. */
9378 intel_connector_check_state(connector);
9379
9380 WARN(&connector->new_encoder->base != connector->base.encoder,
9381 "connector's staged encoder doesn't match current encoder\n");
9382 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009383}
9384
9385static void
9386check_encoder_state(struct drm_device *dev)
9387{
9388 struct intel_encoder *encoder;
9389 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009390
9391 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9392 base.head) {
9393 bool enabled = false;
9394 bool active = false;
9395 enum pipe pipe, tracked_pipe;
9396
9397 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9398 encoder->base.base.id,
9399 drm_get_encoder_name(&encoder->base));
9400
9401 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9402 "encoder's stage crtc doesn't match current crtc\n");
9403 WARN(encoder->connectors_active && !encoder->base.crtc,
9404 "encoder's active_connectors set, but no crtc\n");
9405
9406 list_for_each_entry(connector, &dev->mode_config.connector_list,
9407 base.head) {
9408 if (connector->base.encoder != &encoder->base)
9409 continue;
9410 enabled = true;
9411 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9412 active = true;
9413 }
9414 WARN(!!encoder->base.crtc != enabled,
9415 "encoder's enabled state mismatch "
9416 "(expected %i, found %i)\n",
9417 !!encoder->base.crtc, enabled);
9418 WARN(active && !encoder->base.crtc,
9419 "active encoder with no crtc\n");
9420
9421 WARN(encoder->connectors_active != active,
9422 "encoder's computed active state doesn't match tracked active state "
9423 "(expected %i, found %i)\n", active, encoder->connectors_active);
9424
9425 active = encoder->get_hw_state(encoder, &pipe);
9426 WARN(active != encoder->connectors_active,
9427 "encoder's hw state doesn't match sw tracking "
9428 "(expected %i, found %i)\n",
9429 encoder->connectors_active, active);
9430
9431 if (!encoder->base.crtc)
9432 continue;
9433
9434 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9435 WARN(active && pipe != tracked_pipe,
9436 "active encoder's pipe doesn't match"
9437 "(expected %i, found %i)\n",
9438 tracked_pipe, pipe);
9439
9440 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009441}
9442
9443static void
9444check_crtc_state(struct drm_device *dev)
9445{
9446 drm_i915_private_t *dev_priv = dev->dev_private;
9447 struct intel_crtc *crtc;
9448 struct intel_encoder *encoder;
9449 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009450
9451 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9452 base.head) {
9453 bool enabled = false;
9454 bool active = false;
9455
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009456 memset(&pipe_config, 0, sizeof(pipe_config));
9457
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009458 DRM_DEBUG_KMS("[CRTC:%d]\n",
9459 crtc->base.base.id);
9460
9461 WARN(crtc->active && !crtc->base.enabled,
9462 "active crtc, but not enabled in sw tracking\n");
9463
9464 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9465 base.head) {
9466 if (encoder->base.crtc != &crtc->base)
9467 continue;
9468 enabled = true;
9469 if (encoder->connectors_active)
9470 active = true;
9471 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009472
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009473 WARN(active != crtc->active,
9474 "crtc's computed active state doesn't match tracked active state "
9475 "(expected %i, found %i)\n", active, crtc->active);
9476 WARN(enabled != crtc->base.enabled,
9477 "crtc's computed enabled state doesn't match tracked enabled state "
9478 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9479
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009480 active = dev_priv->display.get_pipe_config(crtc,
9481 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009482
9483 /* hw state is inconsistent with the pipe A quirk */
9484 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9485 active = crtc->active;
9486
Daniel Vetter6c49f242013-06-06 12:45:25 +02009487 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9488 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009489 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009490 if (encoder->base.crtc != &crtc->base)
9491 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +01009492 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009493 encoder->get_config(encoder, &pipe_config);
9494 }
9495
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009496 WARN(crtc->active != active,
9497 "crtc active state doesn't match with hw state "
9498 "(expected %i, found %i)\n", crtc->active, active);
9499
Daniel Vetterc0b03412013-05-28 12:05:54 +02009500 if (active &&
9501 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9502 WARN(1, "pipe state doesn't match!\n");
9503 intel_dump_pipe_config(crtc, &pipe_config,
9504 "[hw state]");
9505 intel_dump_pipe_config(crtc, &crtc->config,
9506 "[sw state]");
9507 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009508 }
9509}
9510
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009511static void
9512check_shared_dpll_state(struct drm_device *dev)
9513{
9514 drm_i915_private_t *dev_priv = dev->dev_private;
9515 struct intel_crtc *crtc;
9516 struct intel_dpll_hw_state dpll_hw_state;
9517 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009518
9519 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9520 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9521 int enabled_crtcs = 0, active_crtcs = 0;
9522 bool active;
9523
9524 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9525
9526 DRM_DEBUG_KMS("%s\n", pll->name);
9527
9528 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9529
9530 WARN(pll->active > pll->refcount,
9531 "more active pll users than references: %i vs %i\n",
9532 pll->active, pll->refcount);
9533 WARN(pll->active && !pll->on,
9534 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009535 WARN(pll->on && !pll->active,
9536 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009537 WARN(pll->on != active,
9538 "pll on state mismatch (expected %i, found %i)\n",
9539 pll->on, active);
9540
9541 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9542 base.head) {
9543 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9544 enabled_crtcs++;
9545 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9546 active_crtcs++;
9547 }
9548 WARN(pll->active != active_crtcs,
9549 "pll active crtcs mismatch (expected %i, found %i)\n",
9550 pll->active, active_crtcs);
9551 WARN(pll->refcount != enabled_crtcs,
9552 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9553 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009554
9555 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9556 sizeof(dpll_hw_state)),
9557 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009558 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009559}
9560
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009561void
9562intel_modeset_check_state(struct drm_device *dev)
9563{
9564 check_connector_state(dev);
9565 check_encoder_state(dev);
9566 check_crtc_state(dev);
9567 check_shared_dpll_state(dev);
9568}
9569
Ville Syrjälä18442d02013-09-13 16:00:08 +03009570void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9571 int dotclock)
9572{
9573 /*
9574 * FDI already provided one idea for the dotclock.
9575 * Yell if the encoder disagrees.
9576 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009577 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009578 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009579 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009580}
9581
Daniel Vetterf30da182013-04-11 20:22:50 +02009582static int __intel_set_mode(struct drm_crtc *crtc,
9583 struct drm_display_mode *mode,
9584 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009585{
9586 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009587 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009588 struct drm_display_mode *saved_mode, *saved_hwmode;
9589 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009590 struct intel_crtc *intel_crtc;
9591 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009592 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009593
Daniel Vettera1e22652013-09-21 00:35:38 +02009594 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009595 if (!saved_mode)
9596 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009597 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009598
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009599 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009600 &prepare_pipes, &disable_pipes);
9601
Tim Gardner3ac18232012-12-07 07:54:26 -07009602 *saved_hwmode = crtc->hwmode;
9603 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009604
Daniel Vetter25c5b262012-07-08 22:08:04 +02009605 /* Hack: Because we don't (yet) support global modeset on multiple
9606 * crtcs, we don't keep track of the new mode for more than one crtc.
9607 * Hence simply check whether any bit is set in modeset_pipes in all the
9608 * pieces of code that are not yet converted to deal with mutliple crtcs
9609 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009610 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009611 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009612 if (IS_ERR(pipe_config)) {
9613 ret = PTR_ERR(pipe_config);
9614 pipe_config = NULL;
9615
Tim Gardner3ac18232012-12-07 07:54:26 -07009616 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009617 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009618 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9619 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009620 }
9621
Jesse Barnes30a970c2013-11-04 13:48:12 -08009622 /*
9623 * See if the config requires any additional preparation, e.g.
9624 * to adjust global state with pipes off. We need to do this
9625 * here so we can get the modeset_pipe updated config for the new
9626 * mode set on this crtc. For other crtcs we need to use the
9627 * adjusted_mode bits in the crtc directly.
9628 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009629 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08009630 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9631 modeset_pipes, pipe_config);
9632
Ville Syrjäläc164f832013-11-05 22:34:12 +02009633 /* may have added more to prepare_pipes than we should */
9634 prepare_pipes &= ~disable_pipes;
9635 }
9636
Daniel Vetter460da9162013-03-27 00:44:51 +01009637 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9638 intel_crtc_disable(&intel_crtc->base);
9639
Daniel Vetterea9d7582012-07-10 10:42:52 +02009640 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9641 if (intel_crtc->base.enabled)
9642 dev_priv->display.crtc_disable(&intel_crtc->base);
9643 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009644
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009645 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9646 * to set it here already despite that we pass it down the callchain.
9647 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009648 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009649 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009650 /* mode_set/enable/disable functions rely on a correct pipe
9651 * config. */
9652 to_intel_crtc(crtc)->config = *pipe_config;
9653 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009654
Daniel Vetterea9d7582012-07-10 10:42:52 +02009655 /* Only after disabling all output pipelines that will be changed can we
9656 * update the the output configuration. */
9657 intel_modeset_update_state(dev, prepare_pipes);
9658
Daniel Vetter47fab732012-10-26 10:58:18 +02009659 if (dev_priv->display.modeset_global_resources)
9660 dev_priv->display.modeset_global_resources(dev);
9661
Daniel Vettera6778b32012-07-02 09:56:42 +02009662 /* Set up the DPLL and any encoders state that needs to adjust or depend
9663 * on the DPLL.
9664 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009665 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009666 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009667 x, y, fb);
9668 if (ret)
9669 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009670 }
9671
9672 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009673 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9674 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009675
Daniel Vetter25c5b262012-07-08 22:08:04 +02009676 if (modeset_pipes) {
9677 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009678 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009679
Daniel Vetter25c5b262012-07-08 22:08:04 +02009680 /* Calculate and store various constants which
9681 * are later needed by vblank and swap-completion
9682 * timestamping. They are derived from true hwmode.
9683 */
9684 drm_calc_timestamping_constants(crtc);
9685 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009686
9687 /* FIXME: add subpixel order */
9688done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009689 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009690 crtc->hwmode = *saved_hwmode;
9691 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009692 }
9693
Tim Gardner3ac18232012-12-07 07:54:26 -07009694out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009695 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009696 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009697 return ret;
9698}
9699
Damien Lespiaue7457a92013-08-08 22:28:59 +01009700static int intel_set_mode(struct drm_crtc *crtc,
9701 struct drm_display_mode *mode,
9702 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009703{
9704 int ret;
9705
9706 ret = __intel_set_mode(crtc, mode, x, y, fb);
9707
9708 if (ret == 0)
9709 intel_modeset_check_state(crtc->dev);
9710
9711 return ret;
9712}
9713
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009714void intel_crtc_restore_mode(struct drm_crtc *crtc)
9715{
9716 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9717}
9718
Daniel Vetter25c5b262012-07-08 22:08:04 +02009719#undef for_each_intel_crtc_masked
9720
Daniel Vetterd9e55602012-07-04 22:16:09 +02009721static void intel_set_config_free(struct intel_set_config *config)
9722{
9723 if (!config)
9724 return;
9725
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009726 kfree(config->save_connector_encoders);
9727 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009728 kfree(config);
9729}
9730
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009731static int intel_set_config_save_state(struct drm_device *dev,
9732 struct intel_set_config *config)
9733{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009734 struct drm_encoder *encoder;
9735 struct drm_connector *connector;
9736 int count;
9737
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009738 config->save_encoder_crtcs =
9739 kcalloc(dev->mode_config.num_encoder,
9740 sizeof(struct drm_crtc *), GFP_KERNEL);
9741 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009742 return -ENOMEM;
9743
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009744 config->save_connector_encoders =
9745 kcalloc(dev->mode_config.num_connector,
9746 sizeof(struct drm_encoder *), GFP_KERNEL);
9747 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009748 return -ENOMEM;
9749
9750 /* Copy data. Note that driver private data is not affected.
9751 * Should anything bad happen only the expected state is
9752 * restored, not the drivers personal bookkeeping.
9753 */
9754 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009755 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009756 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009757 }
9758
9759 count = 0;
9760 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009761 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009762 }
9763
9764 return 0;
9765}
9766
9767static void intel_set_config_restore_state(struct drm_device *dev,
9768 struct intel_set_config *config)
9769{
Daniel Vetter9a935852012-07-05 22:34:27 +02009770 struct intel_encoder *encoder;
9771 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009772 int count;
9773
9774 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009775 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9776 encoder->new_crtc =
9777 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009778 }
9779
9780 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009781 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9782 connector->new_encoder =
9783 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009784 }
9785}
9786
Imre Deake3de42b2013-05-03 19:44:07 +02009787static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009788is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009789{
9790 int i;
9791
Chris Wilson2e57f472013-07-17 12:14:40 +01009792 if (set->num_connectors == 0)
9793 return false;
9794
9795 if (WARN_ON(set->connectors == NULL))
9796 return false;
9797
9798 for (i = 0; i < set->num_connectors; i++)
9799 if (set->connectors[i]->encoder &&
9800 set->connectors[i]->encoder->crtc == set->crtc &&
9801 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009802 return true;
9803
9804 return false;
9805}
9806
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009807static void
9808intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9809 struct intel_set_config *config)
9810{
9811
9812 /* We should be able to check here if the fb has the same properties
9813 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009814 if (is_crtc_connector_off(set)) {
9815 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009816 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009817 /* If we have no fb then treat it as a full mode set */
9818 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009819 struct intel_crtc *intel_crtc =
9820 to_intel_crtc(set->crtc);
9821
9822 if (intel_crtc->active && i915_fastboot) {
9823 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9824 config->fb_changed = true;
9825 } else {
9826 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9827 config->mode_changed = true;
9828 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009829 } else if (set->fb == NULL) {
9830 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009831 } else if (set->fb->pixel_format !=
9832 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009833 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009834 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009835 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009836 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009837 }
9838
Daniel Vetter835c5872012-07-10 18:11:08 +02009839 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009840 config->fb_changed = true;
9841
9842 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9843 DRM_DEBUG_KMS("modes are different, full mode set\n");
9844 drm_mode_debug_printmodeline(&set->crtc->mode);
9845 drm_mode_debug_printmodeline(set->mode);
9846 config->mode_changed = true;
9847 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009848
9849 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9850 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009851}
9852
Daniel Vetter2e431052012-07-04 22:42:15 +02009853static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009854intel_modeset_stage_output_state(struct drm_device *dev,
9855 struct drm_mode_set *set,
9856 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009857{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009858 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009859 struct intel_connector *connector;
9860 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009861 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009862
Damien Lespiau9abdda72013-02-13 13:29:23 +00009863 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009864 * of connectors. For paranoia, double-check this. */
9865 WARN_ON(!set->fb && (set->num_connectors != 0));
9866 WARN_ON(set->fb && (set->num_connectors == 0));
9867
Daniel Vetter9a935852012-07-05 22:34:27 +02009868 list_for_each_entry(connector, &dev->mode_config.connector_list,
9869 base.head) {
9870 /* Otherwise traverse passed in connector list and get encoders
9871 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009872 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009873 if (set->connectors[ro] == &connector->base) {
9874 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009875 break;
9876 }
9877 }
9878
Daniel Vetter9a935852012-07-05 22:34:27 +02009879 /* If we disable the crtc, disable all its connectors. Also, if
9880 * the connector is on the changing crtc but not on the new
9881 * connector list, disable it. */
9882 if ((!set->fb || ro == set->num_connectors) &&
9883 connector->base.encoder &&
9884 connector->base.encoder->crtc == set->crtc) {
9885 connector->new_encoder = NULL;
9886
9887 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9888 connector->base.base.id,
9889 drm_get_connector_name(&connector->base));
9890 }
9891
9892
9893 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009894 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009895 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009896 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009897 }
9898 /* connector->new_encoder is now updated for all connectors. */
9899
9900 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009901 list_for_each_entry(connector, &dev->mode_config.connector_list,
9902 base.head) {
9903 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009904 continue;
9905
Daniel Vetter9a935852012-07-05 22:34:27 +02009906 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009907
9908 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009909 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009910 new_crtc = set->crtc;
9911 }
9912
9913 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009914 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9915 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009916 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009917 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009918 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9919
9920 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9921 connector->base.base.id,
9922 drm_get_connector_name(&connector->base),
9923 new_crtc->base.id);
9924 }
9925
9926 /* Check for any encoders that needs to be disabled. */
9927 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9928 base.head) {
9929 list_for_each_entry(connector,
9930 &dev->mode_config.connector_list,
9931 base.head) {
9932 if (connector->new_encoder == encoder) {
9933 WARN_ON(!connector->new_encoder->new_crtc);
9934
9935 goto next_encoder;
9936 }
9937 }
9938 encoder->new_crtc = NULL;
9939next_encoder:
9940 /* Only now check for crtc changes so we don't miss encoders
9941 * that will be disabled. */
9942 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009943 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009944 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009945 }
9946 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009947 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009948
Daniel Vetter2e431052012-07-04 22:42:15 +02009949 return 0;
9950}
9951
9952static int intel_crtc_set_config(struct drm_mode_set *set)
9953{
9954 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009955 struct drm_mode_set save_set;
9956 struct intel_set_config *config;
9957 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009958
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009959 BUG_ON(!set);
9960 BUG_ON(!set->crtc);
9961 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009962
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009963 /* Enforce sane interface api - has been abused by the fb helper. */
9964 BUG_ON(!set->mode && set->fb);
9965 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009966
Daniel Vetter2e431052012-07-04 22:42:15 +02009967 if (set->fb) {
9968 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9969 set->crtc->base.id, set->fb->base.id,
9970 (int)set->num_connectors, set->x, set->y);
9971 } else {
9972 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009973 }
9974
9975 dev = set->crtc->dev;
9976
9977 ret = -ENOMEM;
9978 config = kzalloc(sizeof(*config), GFP_KERNEL);
9979 if (!config)
9980 goto out_config;
9981
9982 ret = intel_set_config_save_state(dev, config);
9983 if (ret)
9984 goto out_config;
9985
9986 save_set.crtc = set->crtc;
9987 save_set.mode = &set->crtc->mode;
9988 save_set.x = set->crtc->x;
9989 save_set.y = set->crtc->y;
9990 save_set.fb = set->crtc->fb;
9991
9992 /* Compute whether we need a full modeset, only an fb base update or no
9993 * change at all. In the future we might also check whether only the
9994 * mode changed, e.g. for LVDS where we only change the panel fitter in
9995 * such cases. */
9996 intel_set_config_compute_mode_changes(set, config);
9997
Daniel Vetter9a935852012-07-05 22:34:27 +02009998 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009999 if (ret)
10000 goto fail;
10001
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010002 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010003 ret = intel_set_mode(set->crtc, set->mode,
10004 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010005 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010006 intel_crtc_wait_for_pending_flips(set->crtc);
10007
Daniel Vetter4f660f42012-07-02 09:47:37 +020010008 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010009 set->x, set->y, set->fb);
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010010 /*
10011 * In the fastboot case this may be our only check of the
10012 * state after boot. It would be better to only do it on
10013 * the first update, but we don't have a nice way of doing that
10014 * (and really, set_config isn't used much for high freq page
10015 * flipping, so increasing its cost here shouldn't be a big
10016 * deal).
10017 */
10018 if (i915_fastboot && ret == 0)
10019 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010020 }
10021
Chris Wilson2d05eae2013-05-03 17:36:25 +010010022 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010023 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10024 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010025fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010026 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010027
Chris Wilson2d05eae2013-05-03 17:36:25 +010010028 /* Try to restore the config */
10029 if (config->mode_changed &&
10030 intel_set_mode(save_set.crtc, save_set.mode,
10031 save_set.x, save_set.y, save_set.fb))
10032 DRM_ERROR("failed to restore config after modeset failure\n");
10033 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010034
Daniel Vetterd9e55602012-07-04 22:16:09 +020010035out_config:
10036 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010037 return ret;
10038}
10039
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010040static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010041 .cursor_set = intel_crtc_cursor_set,
10042 .cursor_move = intel_crtc_cursor_move,
10043 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010044 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010045 .destroy = intel_crtc_destroy,
10046 .page_flip = intel_crtc_page_flip,
10047};
10048
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010049static void intel_cpu_pll_init(struct drm_device *dev)
10050{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010051 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010052 intel_ddi_pll_init(dev);
10053}
10054
Daniel Vetter53589012013-06-05 13:34:16 +020010055static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10056 struct intel_shared_dpll *pll,
10057 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010058{
Daniel Vetter53589012013-06-05 13:34:16 +020010059 uint32_t val;
10060
10061 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010062 hw_state->dpll = val;
10063 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10064 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010065
10066 return val & DPLL_VCO_ENABLE;
10067}
10068
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010069static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10070 struct intel_shared_dpll *pll)
10071{
10072 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10073 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10074}
10075
Daniel Vettere7b903d2013-06-05 13:34:14 +020010076static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10077 struct intel_shared_dpll *pll)
10078{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010079 /* PCH refclock must be enabled first */
10080 assert_pch_refclk_enabled(dev_priv);
10081
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010082 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10083
10084 /* Wait for the clocks to stabilize. */
10085 POSTING_READ(PCH_DPLL(pll->id));
10086 udelay(150);
10087
10088 /* The pixel multiplier can only be updated once the
10089 * DPLL is enabled and the clocks are stable.
10090 *
10091 * So write it again.
10092 */
10093 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10094 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010095 udelay(200);
10096}
10097
10098static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10099 struct intel_shared_dpll *pll)
10100{
10101 struct drm_device *dev = dev_priv->dev;
10102 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010103
10104 /* Make sure no transcoder isn't still depending on us. */
10105 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10106 if (intel_crtc_to_shared_dpll(crtc) == pll)
10107 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10108 }
10109
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010110 I915_WRITE(PCH_DPLL(pll->id), 0);
10111 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010112 udelay(200);
10113}
10114
Daniel Vetter46edb022013-06-05 13:34:12 +020010115static char *ibx_pch_dpll_names[] = {
10116 "PCH DPLL A",
10117 "PCH DPLL B",
10118};
10119
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010120static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010121{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010122 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010123 int i;
10124
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010125 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010126
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010127 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010128 dev_priv->shared_dplls[i].id = i;
10129 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010130 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010131 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10132 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010133 dev_priv->shared_dplls[i].get_hw_state =
10134 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010135 }
10136}
10137
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010138static void intel_shared_dpll_init(struct drm_device *dev)
10139{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010140 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010141
10142 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10143 ibx_pch_dpll_init(dev);
10144 else
10145 dev_priv->num_shared_dpll = 0;
10146
10147 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10148 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
10149 dev_priv->num_shared_dpll);
10150}
10151
Hannes Ederb358d0a2008-12-18 21:18:47 +010010152static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010153{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010154 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010155 struct intel_crtc *intel_crtc;
10156 int i;
10157
Daniel Vetter955382f2013-09-19 14:05:45 +020010158 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010159 if (intel_crtc == NULL)
10160 return;
10161
10162 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10163
10164 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010165 for (i = 0; i < 256; i++) {
10166 intel_crtc->lut_r[i] = i;
10167 intel_crtc->lut_g[i] = i;
10168 intel_crtc->lut_b[i] = i;
10169 }
10170
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010171 /*
10172 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10173 * is hooked to plane B. Hence we want plane A feeding pipe B.
10174 */
Jesse Barnes80824002009-09-10 15:28:06 -070010175 intel_crtc->pipe = pipe;
10176 intel_crtc->plane = pipe;
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010177 if (IS_MOBILE(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010178 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010179 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010180 }
10181
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010182 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10183 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10184 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10185 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10186
Jesse Barnes79e53942008-11-07 14:24:08 -080010187 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010188}
10189
Jesse Barnes752aa882013-10-31 18:55:49 +020010190enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10191{
10192 struct drm_encoder *encoder = connector->base.encoder;
10193
10194 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10195
10196 if (!encoder)
10197 return INVALID_PIPE;
10198
10199 return to_intel_crtc(encoder->crtc)->pipe;
10200}
10201
Carl Worth08d7b3d2009-04-29 14:43:54 -070010202int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010203 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010204{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010205 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010206 struct drm_mode_object *drmmode_obj;
10207 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010208
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010209 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10210 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010211
Daniel Vetterc05422d2009-08-11 16:05:30 +020010212 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10213 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010214
Daniel Vetterc05422d2009-08-11 16:05:30 +020010215 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010216 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010217 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010218 }
10219
Daniel Vetterc05422d2009-08-11 16:05:30 +020010220 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10221 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010222
Daniel Vetterc05422d2009-08-11 16:05:30 +020010223 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010224}
10225
Daniel Vetter66a92782012-07-12 20:08:18 +020010226static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010227{
Daniel Vetter66a92782012-07-12 20:08:18 +020010228 struct drm_device *dev = encoder->base.dev;
10229 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010230 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010231 int entry = 0;
10232
Daniel Vetter66a92782012-07-12 20:08:18 +020010233 list_for_each_entry(source_encoder,
10234 &dev->mode_config.encoder_list, base.head) {
10235
10236 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010237 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010238
10239 /* Intel hw has only one MUX where enocoders could be cloned. */
10240 if (encoder->cloneable && source_encoder->cloneable)
10241 index_mask |= (1 << entry);
10242
Jesse Barnes79e53942008-11-07 14:24:08 -080010243 entry++;
10244 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010245
Jesse Barnes79e53942008-11-07 14:24:08 -080010246 return index_mask;
10247}
10248
Chris Wilson4d302442010-12-14 19:21:29 +000010249static bool has_edp_a(struct drm_device *dev)
10250{
10251 struct drm_i915_private *dev_priv = dev->dev_private;
10252
10253 if (!IS_MOBILE(dev))
10254 return false;
10255
10256 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10257 return false;
10258
10259 if (IS_GEN5(dev) &&
10260 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10261 return false;
10262
10263 return true;
10264}
10265
Jesse Barnes79e53942008-11-07 14:24:08 -080010266static void intel_setup_outputs(struct drm_device *dev)
10267{
Eric Anholt725e30a2009-01-22 13:01:02 -080010268 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010269 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010270 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010271
Daniel Vetterc9093352013-06-06 22:22:47 +020010272 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010273
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010274 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010275 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010276
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010277 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010278 int found;
10279
10280 /* Haswell uses DDI functions to detect digital outputs */
10281 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10282 /* DDI A only supports eDP */
10283 if (found)
10284 intel_ddi_init(dev, PORT_A);
10285
10286 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10287 * register */
10288 found = I915_READ(SFUSE_STRAP);
10289
10290 if (found & SFUSE_STRAP_DDIB_DETECTED)
10291 intel_ddi_init(dev, PORT_B);
10292 if (found & SFUSE_STRAP_DDIC_DETECTED)
10293 intel_ddi_init(dev, PORT_C);
10294 if (found & SFUSE_STRAP_DDID_DETECTED)
10295 intel_ddi_init(dev, PORT_D);
10296 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010297 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +020010298 dpd_is_edp = intel_dpd_is_edp(dev);
10299
10300 if (has_edp_a(dev))
10301 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010302
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010303 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010304 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010305 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010306 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010307 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010308 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010309 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010310 }
10311
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010312 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010313 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010314
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010315 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010316 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010317
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010318 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010319 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010320
Daniel Vetter270b3042012-10-27 15:52:05 +020010321 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010322 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010323 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010324 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10325 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10326 PORT_B);
10327 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10328 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10329 }
10330
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010331 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10332 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10333 PORT_C);
10334 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10335 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10336 PORT_C);
10337 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010338
Jani Nikula3cfca972013-08-27 15:12:26 +030010339 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010340 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010341 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010342
Paulo Zanonie2debe92013-02-18 19:00:27 -030010343 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010344 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010345 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010346 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10347 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010348 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010349 }
Ma Ling27185ae2009-08-24 13:50:23 +080010350
Imre Deake7281ea2013-05-08 13:14:08 +030010351 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010352 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010353 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010354
10355 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010356
Paulo Zanonie2debe92013-02-18 19:00:27 -030010357 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010358 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010359 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010360 }
Ma Ling27185ae2009-08-24 13:50:23 +080010361
Paulo Zanonie2debe92013-02-18 19:00:27 -030010362 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010363
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010364 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10365 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010366 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010367 }
Imre Deake7281ea2013-05-08 13:14:08 +030010368 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010369 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010370 }
Ma Ling27185ae2009-08-24 13:50:23 +080010371
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010372 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010373 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010374 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010375 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010376 intel_dvo_init(dev);
10377
Zhenyu Wang103a1962009-11-27 11:44:36 +080010378 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 intel_tv_init(dev);
10380
Chris Wilson4ef69c72010-09-09 15:14:28 +010010381 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10382 encoder->base.possible_crtcs = encoder->crtc_mask;
10383 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010384 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010385 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010386
Paulo Zanonidde86e22012-12-01 12:04:25 -020010387 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010388
10389 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010390}
10391
Chris Wilsonddfe1562013-08-06 17:43:07 +010010392void intel_framebuffer_fini(struct intel_framebuffer *fb)
10393{
10394 drm_framebuffer_cleanup(&fb->base);
Daniel Vetter80075d42013-10-09 21:23:52 +020010395 WARN_ON(!fb->obj->framebuffer_references--);
Chris Wilsonddfe1562013-08-06 17:43:07 +010010396 drm_gem_object_unreference_unlocked(&fb->obj->base);
10397}
10398
Jesse Barnes79e53942008-11-07 14:24:08 -080010399static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10400{
10401 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010402
Chris Wilsonddfe1562013-08-06 17:43:07 +010010403 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 kfree(intel_fb);
10405}
10406
10407static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010408 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010409 unsigned int *handle)
10410{
10411 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010412 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010413
Chris Wilson05394f32010-11-08 19:18:58 +000010414 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010415}
10416
10417static const struct drm_framebuffer_funcs intel_fb_funcs = {
10418 .destroy = intel_user_framebuffer_destroy,
10419 .create_handle = intel_user_framebuffer_create_handle,
10420};
10421
Dave Airlie38651672010-03-30 05:34:13 +000010422int intel_framebuffer_init(struct drm_device *dev,
10423 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010424 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010425 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010426{
Daniel Vetter53155c02013-10-09 21:55:33 +020010427 int aligned_height, tile_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010428 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010429 int ret;
10430
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010431 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10432
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010433 if (obj->tiling_mode == I915_TILING_Y) {
10434 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010435 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010436 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010437
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010438 if (mode_cmd->pitches[0] & 63) {
10439 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10440 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010441 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010442 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010443
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010444 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10445 pitch_limit = 32*1024;
10446 } else if (INTEL_INFO(dev)->gen >= 4) {
10447 if (obj->tiling_mode)
10448 pitch_limit = 16*1024;
10449 else
10450 pitch_limit = 32*1024;
10451 } else if (INTEL_INFO(dev)->gen >= 3) {
10452 if (obj->tiling_mode)
10453 pitch_limit = 8*1024;
10454 else
10455 pitch_limit = 16*1024;
10456 } else
10457 /* XXX DSPC is limited to 4k tiled */
10458 pitch_limit = 8*1024;
10459
10460 if (mode_cmd->pitches[0] > pitch_limit) {
10461 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10462 obj->tiling_mode ? "tiled" : "linear",
10463 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010464 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010465 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010466
10467 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010468 mode_cmd->pitches[0] != obj->stride) {
10469 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10470 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010471 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010472 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010473
Ville Syrjälä57779d02012-10-31 17:50:14 +020010474 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010475 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010476 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010477 case DRM_FORMAT_RGB565:
10478 case DRM_FORMAT_XRGB8888:
10479 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010480 break;
10481 case DRM_FORMAT_XRGB1555:
10482 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010483 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010484 DRM_DEBUG("unsupported pixel format: %s\n",
10485 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010486 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010487 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010488 break;
10489 case DRM_FORMAT_XBGR8888:
10490 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010491 case DRM_FORMAT_XRGB2101010:
10492 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010493 case DRM_FORMAT_XBGR2101010:
10494 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010495 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010496 DRM_DEBUG("unsupported pixel format: %s\n",
10497 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010498 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010499 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010500 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010501 case DRM_FORMAT_YUYV:
10502 case DRM_FORMAT_UYVY:
10503 case DRM_FORMAT_YVYU:
10504 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010505 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010506 DRM_DEBUG("unsupported pixel format: %s\n",
10507 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010508 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010509 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010510 break;
10511 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010512 DRM_DEBUG("unsupported pixel format: %s\n",
10513 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010514 return -EINVAL;
10515 }
10516
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010517 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10518 if (mode_cmd->offsets[0] != 0)
10519 return -EINVAL;
10520
Daniel Vetter53155c02013-10-09 21:55:33 +020010521 tile_height = IS_GEN2(dev) ? 16 : 8;
10522 aligned_height = ALIGN(mode_cmd->height,
10523 obj->tiling_mode ? tile_height : 1);
10524 /* FIXME drm helper for size checks (especially planar formats)? */
10525 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10526 return -EINVAL;
10527
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010528 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10529 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010530 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010531
Jesse Barnes79e53942008-11-07 14:24:08 -080010532 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10533 if (ret) {
10534 DRM_ERROR("framebuffer init failed %d\n", ret);
10535 return ret;
10536 }
10537
Jesse Barnes79e53942008-11-07 14:24:08 -080010538 return 0;
10539}
10540
Jesse Barnes79e53942008-11-07 14:24:08 -080010541static struct drm_framebuffer *
10542intel_user_framebuffer_create(struct drm_device *dev,
10543 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010544 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010545{
Chris Wilson05394f32010-11-08 19:18:58 +000010546 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010547
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010548 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10549 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010550 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010551 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010552
Chris Wilsond2dff872011-04-19 08:36:26 +010010553 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010554}
10555
Daniel Vetter4520f532013-10-09 09:18:51 +020010556#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010557static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010558{
10559}
10560#endif
10561
Jesse Barnes79e53942008-11-07 14:24:08 -080010562static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010563 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010564 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010565};
10566
Jesse Barnese70236a2009-09-21 10:42:27 -070010567/* Set up chip specific display functions */
10568static void intel_init_display(struct drm_device *dev)
10569{
10570 struct drm_i915_private *dev_priv = dev->dev_private;
10571
Daniel Vetteree9300b2013-06-03 22:40:22 +020010572 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10573 dev_priv->display.find_dpll = g4x_find_best_dpll;
10574 else if (IS_VALLEYVIEW(dev))
10575 dev_priv->display.find_dpll = vlv_find_best_dpll;
10576 else if (IS_PINEVIEW(dev))
10577 dev_priv->display.find_dpll = pnv_find_best_dpll;
10578 else
10579 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10580
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010581 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010582 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010583 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010584 dev_priv->display.crtc_enable = haswell_crtc_enable;
10585 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010586 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010587 dev_priv->display.update_plane = ironlake_update_plane;
10588 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010589 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010590 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010591 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10592 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010593 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010594 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010595 } else if (IS_VALLEYVIEW(dev)) {
10596 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10597 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10598 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10599 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10600 dev_priv->display.off = i9xx_crtc_off;
10601 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010602 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010603 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010604 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010605 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10606 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010607 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010608 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010609 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010610
Jesse Barnese70236a2009-09-21 10:42:27 -070010611 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010612 if (IS_VALLEYVIEW(dev))
10613 dev_priv->display.get_display_clock_speed =
10614 valleyview_get_display_clock_speed;
10615 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010616 dev_priv->display.get_display_clock_speed =
10617 i945_get_display_clock_speed;
10618 else if (IS_I915G(dev))
10619 dev_priv->display.get_display_clock_speed =
10620 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010621 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010622 dev_priv->display.get_display_clock_speed =
10623 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010624 else if (IS_PINEVIEW(dev))
10625 dev_priv->display.get_display_clock_speed =
10626 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010627 else if (IS_I915GM(dev))
10628 dev_priv->display.get_display_clock_speed =
10629 i915gm_get_display_clock_speed;
10630 else if (IS_I865G(dev))
10631 dev_priv->display.get_display_clock_speed =
10632 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010633 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010634 dev_priv->display.get_display_clock_speed =
10635 i855_get_display_clock_speed;
10636 else /* 852, 830 */
10637 dev_priv->display.get_display_clock_speed =
10638 i830_get_display_clock_speed;
10639
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010640 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010641 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010642 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010643 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010644 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010645 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010646 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010647 } else if (IS_IVYBRIDGE(dev)) {
10648 /* FIXME: detect B0+ stepping and use auto training */
10649 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010650 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010651 dev_priv->display.modeset_global_resources =
10652 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010653 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010654 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010655 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010656 dev_priv->display.modeset_global_resources =
10657 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010658 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010659 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010660 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010661 } else if (IS_VALLEYVIEW(dev)) {
10662 dev_priv->display.modeset_global_resources =
10663 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040010664 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010665 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010666
10667 /* Default just returns -ENODEV to indicate unsupported */
10668 dev_priv->display.queue_flip = intel_default_queue_flip;
10669
10670 switch (INTEL_INFO(dev)->gen) {
10671 case 2:
10672 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10673 break;
10674
10675 case 3:
10676 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10677 break;
10678
10679 case 4:
10680 case 5:
10681 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10682 break;
10683
10684 case 6:
10685 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10686 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010687 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010688 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010689 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10690 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010691 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020010692
10693 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010694}
10695
Jesse Barnesb690e962010-07-19 13:53:12 -070010696/*
10697 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10698 * resume, or other times. This quirk makes sure that's the case for
10699 * affected systems.
10700 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010701static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010702{
10703 struct drm_i915_private *dev_priv = dev->dev_private;
10704
10705 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010706 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010707}
10708
Keith Packard435793d2011-07-12 14:56:22 -070010709/*
10710 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10711 */
10712static void quirk_ssc_force_disable(struct drm_device *dev)
10713{
10714 struct drm_i915_private *dev_priv = dev->dev_private;
10715 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010716 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010717}
10718
Carsten Emde4dca20e2012-03-15 15:56:26 +010010719/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010720 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10721 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010722 */
10723static void quirk_invert_brightness(struct drm_device *dev)
10724{
10725 struct drm_i915_private *dev_priv = dev->dev_private;
10726 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010727 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010728}
10729
10730struct intel_quirk {
10731 int device;
10732 int subsystem_vendor;
10733 int subsystem_device;
10734 void (*hook)(struct drm_device *dev);
10735};
10736
Egbert Eich5f85f1762012-10-14 15:46:38 +020010737/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10738struct intel_dmi_quirk {
10739 void (*hook)(struct drm_device *dev);
10740 const struct dmi_system_id (*dmi_id_list)[];
10741};
10742
10743static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10744{
10745 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10746 return 1;
10747}
10748
10749static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10750 {
10751 .dmi_id_list = &(const struct dmi_system_id[]) {
10752 {
10753 .callback = intel_dmi_reverse_brightness,
10754 .ident = "NCR Corporation",
10755 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10756 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10757 },
10758 },
10759 { } /* terminating entry */
10760 },
10761 .hook = quirk_invert_brightness,
10762 },
10763};
10764
Ben Widawskyc43b5632012-04-16 14:07:40 -070010765static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010766 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010767 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010768
Jesse Barnesb690e962010-07-19 13:53:12 -070010769 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10770 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10771
Jesse Barnesb690e962010-07-19 13:53:12 -070010772 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10773 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10774
Chris Wilsona4945f92013-10-08 11:16:59 +010010775 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010776 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010777
10778 /* Lenovo U160 cannot use SSC on LVDS */
10779 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010780
10781 /* Sony Vaio Y cannot use SSC on LVDS */
10782 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010783
Jani Nikulaee1452d2013-09-20 15:05:30 +030010784 /*
10785 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10786 * seem to use inverted backlight PWM.
10787 */
10788 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070010789};
10790
10791static void intel_init_quirks(struct drm_device *dev)
10792{
10793 struct pci_dev *d = dev->pdev;
10794 int i;
10795
10796 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10797 struct intel_quirk *q = &intel_quirks[i];
10798
10799 if (d->device == q->device &&
10800 (d->subsystem_vendor == q->subsystem_vendor ||
10801 q->subsystem_vendor == PCI_ANY_ID) &&
10802 (d->subsystem_device == q->subsystem_device ||
10803 q->subsystem_device == PCI_ANY_ID))
10804 q->hook(dev);
10805 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010806 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10807 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10808 intel_dmi_quirks[i].hook(dev);
10809 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010810}
10811
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010812/* Disable the VGA plane that we never use */
10813static void i915_disable_vga(struct drm_device *dev)
10814{
10815 struct drm_i915_private *dev_priv = dev->dev_private;
10816 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010817 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010818
10819 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010820 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010821 sr1 = inb(VGA_SR_DATA);
10822 outb(sr1 | 1<<5, VGA_SR_DATA);
10823 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10824 udelay(300);
10825
10826 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10827 POSTING_READ(vga_reg);
10828}
10829
Daniel Vetterf8175862012-04-10 15:50:11 +020010830void intel_modeset_init_hw(struct drm_device *dev)
10831{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010832 intel_prepare_ddi(dev);
10833
Daniel Vetterf8175862012-04-10 15:50:11 +020010834 intel_init_clock_gating(dev);
10835
Jesse Barnes5382f5f352013-12-16 16:34:24 -080010836 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010837
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010838 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010839 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010840 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010841}
10842
Imre Deak7d708ee2013-04-17 14:04:50 +030010843void intel_modeset_suspend_hw(struct drm_device *dev)
10844{
10845 intel_suspend_hw(dev);
10846}
10847
Jesse Barnes79e53942008-11-07 14:24:08 -080010848void intel_modeset_init(struct drm_device *dev)
10849{
Jesse Barnes652c3932009-08-17 13:31:43 -070010850 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010851 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010852
10853 drm_mode_config_init(dev);
10854
10855 dev->mode_config.min_width = 0;
10856 dev->mode_config.min_height = 0;
10857
Dave Airlie019d96c2011-09-29 16:20:42 +010010858 dev->mode_config.preferred_depth = 24;
10859 dev->mode_config.prefer_shadow = 1;
10860
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010861 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010862
Jesse Barnesb690e962010-07-19 13:53:12 -070010863 intel_init_quirks(dev);
10864
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010865 intel_init_pm(dev);
10866
Ben Widawskye3c74752013-04-05 13:12:39 -070010867 if (INTEL_INFO(dev)->num_pipes == 0)
10868 return;
10869
Jesse Barnese70236a2009-09-21 10:42:27 -070010870 intel_init_display(dev);
10871
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010872 if (IS_GEN2(dev)) {
10873 dev->mode_config.max_width = 2048;
10874 dev->mode_config.max_height = 2048;
10875 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010876 dev->mode_config.max_width = 4096;
10877 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010878 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010879 dev->mode_config.max_width = 8192;
10880 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010881 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010882 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010883
Zhao Yakui28c97732009-10-09 11:39:41 +080010884 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010885 INTEL_INFO(dev)->num_pipes,
10886 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010887
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010888 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010889 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010890 for (j = 0; j < dev_priv->num_plane; j++) {
10891 ret = intel_plane_init(dev, i, j);
10892 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010893 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10894 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010895 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010896 }
10897
Jesse Barnesf42bb702013-12-16 16:34:23 -080010898 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080010899 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080010900
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010901 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010902 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010903
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010904 /* Just disable it once at startup */
10905 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010906 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010907
10908 /* Just in case the BIOS is doing something questionable. */
10909 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010910}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010911
Daniel Vetter24929352012-07-02 20:28:59 +020010912static void
10913intel_connector_break_all_links(struct intel_connector *connector)
10914{
10915 connector->base.dpms = DRM_MODE_DPMS_OFF;
10916 connector->base.encoder = NULL;
10917 connector->encoder->connectors_active = false;
10918 connector->encoder->base.crtc = NULL;
10919}
10920
Daniel Vetter7fad7982012-07-04 17:51:47 +020010921static void intel_enable_pipe_a(struct drm_device *dev)
10922{
10923 struct intel_connector *connector;
10924 struct drm_connector *crt = NULL;
10925 struct intel_load_detect_pipe load_detect_temp;
10926
10927 /* We can't just switch on the pipe A, we need to set things up with a
10928 * proper mode and output configuration. As a gross hack, enable pipe A
10929 * by enabling the load detect pipe once. */
10930 list_for_each_entry(connector,
10931 &dev->mode_config.connector_list,
10932 base.head) {
10933 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10934 crt = &connector->base;
10935 break;
10936 }
10937 }
10938
10939 if (!crt)
10940 return;
10941
10942 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10943 intel_release_load_detect_pipe(crt, &load_detect_temp);
10944
10945
10946}
10947
Daniel Vetterfa555832012-10-10 23:14:00 +020010948static bool
10949intel_check_plane_mapping(struct intel_crtc *crtc)
10950{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010951 struct drm_device *dev = crtc->base.dev;
10952 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010953 u32 reg, val;
10954
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010955 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010956 return true;
10957
10958 reg = DSPCNTR(!crtc->plane);
10959 val = I915_READ(reg);
10960
10961 if ((val & DISPLAY_PLANE_ENABLE) &&
10962 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10963 return false;
10964
10965 return true;
10966}
10967
Daniel Vetter24929352012-07-02 20:28:59 +020010968static void intel_sanitize_crtc(struct intel_crtc *crtc)
10969{
10970 struct drm_device *dev = crtc->base.dev;
10971 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010972 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010973
Daniel Vetter24929352012-07-02 20:28:59 +020010974 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010975 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010976 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10977
10978 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010979 * disable the crtc (and hence change the state) if it is wrong. Note
10980 * that gen4+ has a fixed plane -> pipe mapping. */
10981 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010982 struct intel_connector *connector;
10983 bool plane;
10984
Daniel Vetter24929352012-07-02 20:28:59 +020010985 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10986 crtc->base.base.id);
10987
10988 /* Pipe has the wrong plane attached and the plane is active.
10989 * Temporarily change the plane mapping and disable everything
10990 * ... */
10991 plane = crtc->plane;
10992 crtc->plane = !plane;
10993 dev_priv->display.crtc_disable(&crtc->base);
10994 crtc->plane = plane;
10995
10996 /* ... and break all links. */
10997 list_for_each_entry(connector, &dev->mode_config.connector_list,
10998 base.head) {
10999 if (connector->encoder->base.crtc != &crtc->base)
11000 continue;
11001
11002 intel_connector_break_all_links(connector);
11003 }
11004
11005 WARN_ON(crtc->active);
11006 crtc->base.enabled = false;
11007 }
Daniel Vetter24929352012-07-02 20:28:59 +020011008
Daniel Vetter7fad7982012-07-04 17:51:47 +020011009 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11010 crtc->pipe == PIPE_A && !crtc->active) {
11011 /* BIOS forgot to enable pipe A, this mostly happens after
11012 * resume. Force-enable the pipe to fix this, the update_dpms
11013 * call below we restore the pipe to the right state, but leave
11014 * the required bits on. */
11015 intel_enable_pipe_a(dev);
11016 }
11017
Daniel Vetter24929352012-07-02 20:28:59 +020011018 /* Adjust the state of the output pipe according to whether we
11019 * have active connectors/encoders. */
11020 intel_crtc_update_dpms(&crtc->base);
11021
11022 if (crtc->active != crtc->base.enabled) {
11023 struct intel_encoder *encoder;
11024
11025 /* This can happen either due to bugs in the get_hw_state
11026 * functions or because the pipe is force-enabled due to the
11027 * pipe A quirk. */
11028 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11029 crtc->base.base.id,
11030 crtc->base.enabled ? "enabled" : "disabled",
11031 crtc->active ? "enabled" : "disabled");
11032
11033 crtc->base.enabled = crtc->active;
11034
11035 /* Because we only establish the connector -> encoder ->
11036 * crtc links if something is active, this means the
11037 * crtc is now deactivated. Break the links. connector
11038 * -> encoder links are only establish when things are
11039 * actually up, hence no need to break them. */
11040 WARN_ON(crtc->active);
11041
11042 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11043 WARN_ON(encoder->connectors_active);
11044 encoder->base.crtc = NULL;
11045 }
11046 }
11047}
11048
11049static void intel_sanitize_encoder(struct intel_encoder *encoder)
11050{
11051 struct intel_connector *connector;
11052 struct drm_device *dev = encoder->base.dev;
11053
11054 /* We need to check both for a crtc link (meaning that the
11055 * encoder is active and trying to read from a pipe) and the
11056 * pipe itself being active. */
11057 bool has_active_crtc = encoder->base.crtc &&
11058 to_intel_crtc(encoder->base.crtc)->active;
11059
11060 if (encoder->connectors_active && !has_active_crtc) {
11061 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11062 encoder->base.base.id,
11063 drm_get_encoder_name(&encoder->base));
11064
11065 /* Connector is active, but has no active pipe. This is
11066 * fallout from our resume register restoring. Disable
11067 * the encoder manually again. */
11068 if (encoder->base.crtc) {
11069 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11070 encoder->base.base.id,
11071 drm_get_encoder_name(&encoder->base));
11072 encoder->disable(encoder);
11073 }
11074
11075 /* Inconsistent output/port/pipe state happens presumably due to
11076 * a bug in one of the get_hw_state functions. Or someplace else
11077 * in our code, like the register restore mess on resume. Clamp
11078 * things to off as a safer default. */
11079 list_for_each_entry(connector,
11080 &dev->mode_config.connector_list,
11081 base.head) {
11082 if (connector->encoder != encoder)
11083 continue;
11084
11085 intel_connector_break_all_links(connector);
11086 }
11087 }
11088 /* Enabled encoders without active connectors will be fixed in
11089 * the crtc fixup. */
11090}
11091
Daniel Vetter44cec742013-01-25 17:53:21 +010011092void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011093{
11094 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011095 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011096
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011097 /* This function can be called both from intel_modeset_setup_hw_state or
11098 * at a very early point in our resume sequence, where the power well
11099 * structures are not yet restored. Since this function is at a very
11100 * paranoid "someone might have enabled VGA while we were not looking"
11101 * level, just check if the power well is enabled instead of trying to
11102 * follow the "don't touch the power well if we don't need it" policy
11103 * the rest of the driver uses. */
Jesse Barnesf9e711e2013-11-25 17:15:32 +020011104 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030011105 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011106 return;
11107
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030011108 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011109 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020011110 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011111 }
11112}
11113
Daniel Vetter30e984d2013-06-05 13:34:17 +020011114static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011115{
11116 struct drm_i915_private *dev_priv = dev->dev_private;
11117 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011118 struct intel_crtc *crtc;
11119 struct intel_encoder *encoder;
11120 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011121 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011122
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011123 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11124 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011125 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011126
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011127 crtc->active = dev_priv->display.get_pipe_config(crtc,
11128 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011129
11130 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030011131 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020011132
11133 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11134 crtc->base.base.id,
11135 crtc->active ? "enabled" : "disabled");
11136 }
11137
Daniel Vetter53589012013-06-05 13:34:16 +020011138 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011139 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011140 intel_ddi_setup_hw_pll_state(dev);
11141
Daniel Vetter53589012013-06-05 13:34:16 +020011142 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11143 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11144
11145 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11146 pll->active = 0;
11147 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11148 base.head) {
11149 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11150 pll->active++;
11151 }
11152 pll->refcount = pll->active;
11153
Daniel Vetter35c95372013-07-17 06:55:04 +020011154 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11155 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011156 }
11157
Daniel Vetter24929352012-07-02 20:28:59 +020011158 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11159 base.head) {
11160 pipe = 0;
11161
11162 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011163 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11164 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011165 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011166 } else {
11167 encoder->base.crtc = NULL;
11168 }
11169
11170 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011171 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011172 encoder->base.base.id,
11173 drm_get_encoder_name(&encoder->base),
11174 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011175 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011176 }
11177
11178 list_for_each_entry(connector, &dev->mode_config.connector_list,
11179 base.head) {
11180 if (connector->get_hw_state(connector)) {
11181 connector->base.dpms = DRM_MODE_DPMS_ON;
11182 connector->encoder->connectors_active = true;
11183 connector->base.encoder = &connector->encoder->base;
11184 } else {
11185 connector->base.dpms = DRM_MODE_DPMS_OFF;
11186 connector->base.encoder = NULL;
11187 }
11188 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11189 connector->base.base.id,
11190 drm_get_connector_name(&connector->base),
11191 connector->base.encoder ? "enabled" : "disabled");
11192 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011193}
11194
11195/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11196 * and i915 state tracking structures. */
11197void intel_modeset_setup_hw_state(struct drm_device *dev,
11198 bool force_restore)
11199{
11200 struct drm_i915_private *dev_priv = dev->dev_private;
11201 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011202 struct intel_crtc *crtc;
11203 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011204 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011205
11206 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011207
Jesse Barnesbabea612013-06-26 18:57:38 +030011208 /*
11209 * Now that we have the config, copy it to each CRTC struct
11210 * Note that this could go away if we move to using crtc_config
11211 * checking everywhere.
11212 */
11213 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11214 base.head) {
11215 if (crtc->active && i915_fastboot) {
11216 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11217
11218 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11219 crtc->base.base.id);
11220 drm_mode_debug_printmodeline(&crtc->base.mode);
11221 }
11222 }
11223
Daniel Vetter24929352012-07-02 20:28:59 +020011224 /* HW state is read out, now we need to sanitize this mess. */
11225 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11226 base.head) {
11227 intel_sanitize_encoder(encoder);
11228 }
11229
11230 for_each_pipe(pipe) {
11231 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11232 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011233 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011234 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011235
Daniel Vetter35c95372013-07-17 06:55:04 +020011236 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11237 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11238
11239 if (!pll->on || pll->active)
11240 continue;
11241
11242 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11243
11244 pll->disable(dev_priv, pll);
11245 pll->on = false;
11246 }
11247
Ville Syrjälä96f90c52013-12-05 15:51:38 +020011248 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011249 ilk_wm_get_hw_state(dev);
11250
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011251 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011252 i915_redisable_vga(dev);
11253
Daniel Vetterf30da182013-04-11 20:22:50 +020011254 /*
11255 * We need to use raw interfaces for restoring state to avoid
11256 * checking (bogus) intermediate states.
11257 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011258 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011259 struct drm_crtc *crtc =
11260 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011261
11262 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11263 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011264 }
11265 } else {
11266 intel_modeset_update_staged_output_state(dev);
11267 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011268
11269 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020011270
11271 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011272}
11273
11274void intel_modeset_gem_init(struct drm_device *dev)
11275{
Chris Wilson1833b132012-05-09 11:56:28 +010011276 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011277
11278 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011279
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011280 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080011281}
11282
11283void intel_modeset_cleanup(struct drm_device *dev)
11284{
Jesse Barnes652c3932009-08-17 13:31:43 -070011285 struct drm_i915_private *dev_priv = dev->dev_private;
11286 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011287 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011288
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011289 /*
11290 * Interrupts and polling as the first thing to avoid creating havoc.
11291 * Too much stuff here (turning of rps, connectors, ...) would
11292 * experience fancy races otherwise.
11293 */
11294 drm_irq_uninstall(dev);
11295 cancel_work_sync(&dev_priv->hotplug_work);
11296 /*
11297 * Due to the hpd irq storm handling the hotplug work can re-arm the
11298 * poll handlers. Hence disable polling after hpd handling is shut down.
11299 */
Keith Packardf87ea762010-10-03 19:36:26 -070011300 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011301
Jesse Barnes652c3932009-08-17 13:31:43 -070011302 mutex_lock(&dev->struct_mutex);
11303
Jesse Barnes723bfd72010-10-07 16:01:13 -070011304 intel_unregister_dsm_handler();
11305
Jesse Barnes652c3932009-08-17 13:31:43 -070011306 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11307 /* Skip inactive CRTCs */
11308 if (!crtc->fb)
11309 continue;
11310
Daniel Vetter3dec0092010-08-20 21:40:52 +020011311 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011312 }
11313
Chris Wilson973d04f2011-07-08 12:22:37 +010011314 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011315
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011316 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011317
Daniel Vetter930ebb42012-06-29 23:32:16 +020011318 ironlake_teardown_rc6(dev);
11319
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011320 mutex_unlock(&dev->struct_mutex);
11321
Chris Wilson1630fe72011-07-08 12:22:42 +010011322 /* flush any delayed tasks or pending work */
11323 flush_scheduled_work();
11324
Jani Nikuladb31af12013-11-08 16:48:53 +020011325 /* destroy the backlight and sysfs files before encoders/connectors */
11326 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11327 intel_panel_destroy_backlight(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030011328 drm_sysfs_connector_remove(connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011329 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011330
Jesse Barnes79e53942008-11-07 14:24:08 -080011331 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011332
11333 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011334}
11335
Dave Airlie28d52042009-09-21 14:33:58 +100011336/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011337 * Return which encoder is currently attached for connector.
11338 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011339struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011340{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011341 return &intel_attached_encoder(connector)->base;
11342}
Jesse Barnes79e53942008-11-07 14:24:08 -080011343
Chris Wilsondf0e9242010-09-09 16:20:55 +010011344void intel_connector_attach_encoder(struct intel_connector *connector,
11345 struct intel_encoder *encoder)
11346{
11347 connector->encoder = encoder;
11348 drm_mode_connector_attach_encoder(&connector->base,
11349 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011350}
Dave Airlie28d52042009-09-21 14:33:58 +100011351
11352/*
11353 * set vga decode state - true == enable VGA decode
11354 */
11355int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11356{
11357 struct drm_i915_private *dev_priv = dev->dev_private;
11358 u16 gmch_ctrl;
11359
11360 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11361 if (state)
11362 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11363 else
11364 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11365 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11366 return 0;
11367}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011368
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011369struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011370
11371 u32 power_well_driver;
11372
Chris Wilson63b66e52013-08-08 15:12:06 +020011373 int num_transcoders;
11374
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011375 struct intel_cursor_error_state {
11376 u32 control;
11377 u32 position;
11378 u32 base;
11379 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011380 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011381
11382 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011383 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011384 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011385 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011386
11387 struct intel_plane_error_state {
11388 u32 control;
11389 u32 stride;
11390 u32 size;
11391 u32 pos;
11392 u32 addr;
11393 u32 surface;
11394 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011395 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011396
11397 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011398 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020011399 enum transcoder cpu_transcoder;
11400
11401 u32 conf;
11402
11403 u32 htotal;
11404 u32 hblank;
11405 u32 hsync;
11406 u32 vtotal;
11407 u32 vblank;
11408 u32 vsync;
11409 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011410};
11411
11412struct intel_display_error_state *
11413intel_display_capture_error_state(struct drm_device *dev)
11414{
Akshay Joshi0206e352011-08-16 15:34:10 -040011415 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011416 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011417 int transcoders[] = {
11418 TRANSCODER_A,
11419 TRANSCODER_B,
11420 TRANSCODER_C,
11421 TRANSCODER_EDP,
11422 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011423 int i;
11424
Chris Wilson63b66e52013-08-08 15:12:06 +020011425 if (INTEL_INFO(dev)->num_pipes == 0)
11426 return NULL;
11427
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011428 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011429 if (error == NULL)
11430 return NULL;
11431
Imre Deak190be112013-11-25 17:15:31 +020011432 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011433 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11434
Damien Lespiau52331302012-08-15 19:23:25 +010011435 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020011436 error->pipe[i].power_domain_on =
11437 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11438 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011439 continue;
11440
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011441 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11442 error->cursor[i].control = I915_READ(CURCNTR(i));
11443 error->cursor[i].position = I915_READ(CURPOS(i));
11444 error->cursor[i].base = I915_READ(CURBASE(i));
11445 } else {
11446 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11447 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11448 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11449 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011450
11451 error->plane[i].control = I915_READ(DSPCNTR(i));
11452 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011453 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011454 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011455 error->plane[i].pos = I915_READ(DSPPOS(i));
11456 }
Paulo Zanonica291362013-03-06 20:03:14 -030011457 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11458 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011459 if (INTEL_INFO(dev)->gen >= 4) {
11460 error->plane[i].surface = I915_READ(DSPSURF(i));
11461 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11462 }
11463
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011464 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011465 }
11466
11467 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11468 if (HAS_DDI(dev_priv->dev))
11469 error->num_transcoders++; /* Account for eDP. */
11470
11471 for (i = 0; i < error->num_transcoders; i++) {
11472 enum transcoder cpu_transcoder = transcoders[i];
11473
Imre Deakddf9c532013-11-27 22:02:02 +020011474 error->transcoder[i].power_domain_on =
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020011475 intel_display_power_enabled_sw(dev,
11476 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011477 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011478 continue;
11479
Chris Wilson63b66e52013-08-08 15:12:06 +020011480 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11481
11482 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11483 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11484 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11485 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11486 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11487 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11488 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011489 }
11490
11491 return error;
11492}
11493
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011494#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11495
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011496void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011497intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011498 struct drm_device *dev,
11499 struct intel_display_error_state *error)
11500{
11501 int i;
11502
Chris Wilson63b66e52013-08-08 15:12:06 +020011503 if (!error)
11504 return;
11505
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011506 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020011507 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011508 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011509 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011510 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011511 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020011512 err_printf(m, " Power: %s\n",
11513 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011514 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011515
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011516 err_printf(m, "Plane [%d]:\n", i);
11517 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11518 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011519 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011520 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11521 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011522 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011523 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011524 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011525 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011526 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11527 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011528 }
11529
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011530 err_printf(m, "Cursor [%d]:\n", i);
11531 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11532 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11533 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011534 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011535
11536 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011537 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011538 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011539 err_printf(m, " Power: %s\n",
11540 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020011541 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11542 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11543 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11544 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11545 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11546 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11547 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11548 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011549}