blob: c456768282da9b04b2e4ee050a484eb2dbbbd6e5 [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 */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001033 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001034 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)) {
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001192 for (i = 0; i < INTEL_INFO(dev)->num_sprites; i++) {
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001193 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
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001214static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001215{
1216 u32 val;
1217 bool enabled;
1218
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001219 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001220
Jesse Barnes92f25842011-01-04 15:09:34 -08001221 val = I915_READ(PCH_DREF_CONTROL);
1222 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1223 DREF_SUPERSPREAD_SOURCE_MASK));
1224 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1225}
1226
Daniel Vetterab9412b2013-05-03 11:49:46 +02001227static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1228 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001229{
1230 int reg;
1231 u32 val;
1232 bool enabled;
1233
Daniel Vetterab9412b2013-05-03 11:49:46 +02001234 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001235 val = I915_READ(reg);
1236 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001237 WARN(enabled,
1238 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1239 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001240}
1241
Keith Packard4e634382011-08-06 10:39:45 -07001242static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1243 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001244{
1245 if ((val & DP_PORT_EN) == 0)
1246 return false;
1247
1248 if (HAS_PCH_CPT(dev_priv->dev)) {
1249 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1250 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1251 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1252 return false;
1253 } else {
1254 if ((val & DP_PIPE_MASK) != (pipe << 30))
1255 return false;
1256 }
1257 return true;
1258}
1259
Keith Packard1519b992011-08-06 10:35:34 -07001260static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1261 enum pipe pipe, u32 val)
1262{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001263 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001264 return false;
1265
1266 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001267 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001268 return false;
1269 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001270 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001271 return false;
1272 }
1273 return true;
1274}
1275
1276static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1277 enum pipe pipe, u32 val)
1278{
1279 if ((val & LVDS_PORT_EN) == 0)
1280 return false;
1281
1282 if (HAS_PCH_CPT(dev_priv->dev)) {
1283 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1284 return false;
1285 } else {
1286 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1287 return false;
1288 }
1289 return true;
1290}
1291
1292static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1293 enum pipe pipe, u32 val)
1294{
1295 if ((val & ADPA_DAC_ENABLE) == 0)
1296 return false;
1297 if (HAS_PCH_CPT(dev_priv->dev)) {
1298 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1299 return false;
1300 } else {
1301 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1302 return false;
1303 }
1304 return true;
1305}
1306
Jesse Barnes291906f2011-02-02 12:28:03 -08001307static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001308 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001309{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001310 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001311 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001312 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001313 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001314
Daniel Vetter75c5da22012-09-10 21:58:29 +02001315 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1316 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001317 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001318}
1319
1320static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe, int reg)
1322{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001323 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001324 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001325 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001326 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001327
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001328 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001329 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001330 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001331}
1332
1333static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1334 enum pipe pipe)
1335{
1336 int reg;
1337 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001338
Keith Packardf0575e92011-07-25 22:12:43 -07001339 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1340 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1341 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001342
1343 reg = PCH_ADPA;
1344 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001345 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001346 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001347 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001348
1349 reg = PCH_LVDS;
1350 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001351 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001352 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001353 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001354
Paulo Zanonie2debe92013-02-18 19:00:27 -03001355 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1356 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1357 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001358}
1359
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001360static void intel_init_dpio(struct drm_device *dev)
1361{
1362 struct drm_i915_private *dev_priv = dev->dev_private;
1363
1364 if (!IS_VALLEYVIEW(dev))
1365 return;
1366
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001367 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001368}
1369
1370static void intel_reset_dpio(struct drm_device *dev)
1371{
1372 struct drm_i915_private *dev_priv = dev->dev_private;
1373
1374 if (!IS_VALLEYVIEW(dev))
1375 return;
1376
Imre Deake5cbfbf2014-01-09 17:08:16 +02001377 /*
1378 * Enable the CRI clock source so we can get at the display and the
1379 * reference clock for VGA hotplug / manual detection.
1380 */
Imre Deak404faab2014-01-09 17:08:15 +02001381 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
Imre Deake5cbfbf2014-01-09 17:08:16 +02001382 DPLL_REFA_CLK_ENABLE_VLV |
Imre Deak404faab2014-01-09 17:08:15 +02001383 DPLL_INTEGRATED_CRI_CLK_VLV);
1384
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001385 /*
1386 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1387 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1388 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1389 * b. The other bits such as sfr settings / modesel may all be set
1390 * to 0.
1391 *
1392 * This should only be done on init and resume from S3 with both
1393 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1394 */
1395 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1396}
1397
Daniel Vetter426115c2013-07-11 22:13:42 +02001398static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001399{
Daniel Vetter426115c2013-07-11 22:13:42 +02001400 struct drm_device *dev = crtc->base.dev;
1401 struct drm_i915_private *dev_priv = dev->dev_private;
1402 int reg = DPLL(crtc->pipe);
1403 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001404
Daniel Vetter426115c2013-07-11 22:13:42 +02001405 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001406
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001407 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001408 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1409
1410 /* PLL is protected by panel, make sure we can write it */
1411 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001412 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001413
Daniel Vetter426115c2013-07-11 22:13:42 +02001414 I915_WRITE(reg, dpll);
1415 POSTING_READ(reg);
1416 udelay(150);
1417
1418 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1419 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1420
1421 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1422 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001423
1424 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001425 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001426 POSTING_READ(reg);
1427 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001428 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001429 POSTING_READ(reg);
1430 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001431 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001432 POSTING_READ(reg);
1433 udelay(150); /* wait for warmup */
1434}
1435
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001436static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001437{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001438 struct drm_device *dev = crtc->base.dev;
1439 struct drm_i915_private *dev_priv = dev->dev_private;
1440 int reg = DPLL(crtc->pipe);
1441 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001442
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001443 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001444
1445 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001446 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001447
1448 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001449 if (IS_MOBILE(dev) && !IS_I830(dev))
1450 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001451
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001452 I915_WRITE(reg, dpll);
1453
1454 /* Wait for the clocks to stabilize. */
1455 POSTING_READ(reg);
1456 udelay(150);
1457
1458 if (INTEL_INFO(dev)->gen >= 4) {
1459 I915_WRITE(DPLL_MD(crtc->pipe),
1460 crtc->config.dpll_hw_state.dpll_md);
1461 } else {
1462 /* The pixel multiplier can only be updated once the
1463 * DPLL is enabled and the clocks are stable.
1464 *
1465 * So write it again.
1466 */
1467 I915_WRITE(reg, dpll);
1468 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001469
1470 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001471 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001472 POSTING_READ(reg);
1473 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001474 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001475 POSTING_READ(reg);
1476 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001477 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001478 POSTING_READ(reg);
1479 udelay(150); /* wait for warmup */
1480}
1481
1482/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001483 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001484 * @dev_priv: i915 private structure
1485 * @pipe: pipe PLL to disable
1486 *
1487 * Disable the PLL for @pipe, making sure the pipe is off first.
1488 *
1489 * Note! This is for pre-ILK only.
1490 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001491static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001492{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001493 /* Don't disable pipe A or pipe A PLLs if needed */
1494 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1495 return;
1496
1497 /* Make sure the pipe isn't still relying on us */
1498 assert_pipe_disabled(dev_priv, pipe);
1499
Daniel Vetter50b44a42013-06-05 13:34:33 +02001500 I915_WRITE(DPLL(pipe), 0);
1501 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001502}
1503
Jesse Barnesf6071162013-10-01 10:41:38 -07001504static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1505{
1506 u32 val = 0;
1507
1508 /* Make sure the pipe isn't still relying on us */
1509 assert_pipe_disabled(dev_priv, pipe);
1510
Imre Deake5cbfbf2014-01-09 17:08:16 +02001511 /*
1512 * Leave integrated clock source and reference clock enabled for pipe B.
1513 * The latter is needed for VGA hotplug / manual detection.
1514 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001515 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001516 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001517 I915_WRITE(DPLL(pipe), val);
1518 POSTING_READ(DPLL(pipe));
1519}
1520
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001521void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1522 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001523{
1524 u32 port_mask;
1525
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001526 switch (dport->port) {
1527 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001528 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001529 break;
1530 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001531 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001532 break;
1533 default:
1534 BUG();
1535 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001536
1537 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1538 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Ville Syrjäläbe46ffd2013-11-29 13:21:49 +02001539 port_name(dport->port), I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001540}
1541
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001542/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001543 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001544 * @dev_priv: i915 private structure
1545 * @pipe: pipe PLL to enable
1546 *
1547 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1548 * drives the transcoder clock.
1549 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001550static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001551{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001552 struct drm_device *dev = crtc->base.dev;
1553 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001554 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001555
Chris Wilson48da64a2012-05-13 20:16:12 +01001556 /* PCH PLLs only available on ILK, SNB and IVB */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001557 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001558 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001559 return;
1560
1561 if (WARN_ON(pll->refcount == 0))
1562 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001563
Daniel Vetter46edb022013-06-05 13:34:12 +02001564 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1565 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001566 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001567
Daniel Vettercdbd2312013-06-05 13:34:03 +02001568 if (pll->active++) {
1569 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001570 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001571 return;
1572 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001573 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001574
Daniel Vetter46edb022013-06-05 13:34:12 +02001575 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001576 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001577 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001578}
1579
Daniel Vettere2b78262013-06-07 23:10:03 +02001580static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001581{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001582 struct drm_device *dev = crtc->base.dev;
1583 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001584 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001585
Jesse Barnes92f25842011-01-04 15:09:34 -08001586 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001587 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001588 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001589 return;
1590
Chris Wilson48da64a2012-05-13 20:16:12 +01001591 if (WARN_ON(pll->refcount == 0))
1592 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001593
Daniel Vetter46edb022013-06-05 13:34:12 +02001594 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1595 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001596 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001597
Chris Wilson48da64a2012-05-13 20:16:12 +01001598 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001599 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001600 return;
1601 }
1602
Daniel Vettere9d69442013-06-05 13:34:15 +02001603 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001604 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001605 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001606 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001607
Daniel Vetter46edb022013-06-05 13:34:12 +02001608 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001609 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001610 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001611}
1612
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001613static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1614 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001615{
Daniel Vetter23670b322012-11-01 09:15:30 +01001616 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001617 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001618 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001619 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001620
1621 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001622 BUG_ON(INTEL_INFO(dev)->gen < 5);
Jesse Barnes040484a2011-01-03 12:14:26 -08001623
1624 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001625 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001626 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001627
1628 /* FDI must be feeding us bits for PCH ports */
1629 assert_fdi_tx_enabled(dev_priv, pipe);
1630 assert_fdi_rx_enabled(dev_priv, pipe);
1631
Daniel Vetter23670b322012-11-01 09:15:30 +01001632 if (HAS_PCH_CPT(dev)) {
1633 /* Workaround: Set the timing override bit before enabling the
1634 * pch transcoder. */
1635 reg = TRANS_CHICKEN2(pipe);
1636 val = I915_READ(reg);
1637 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1638 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001639 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001640
Daniel Vetterab9412b2013-05-03 11:49:46 +02001641 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001642 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001643 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001644
1645 if (HAS_PCH_IBX(dev_priv->dev)) {
1646 /*
1647 * make the BPC in transcoder be consistent with
1648 * that in pipeconf reg.
1649 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001650 val &= ~PIPECONF_BPC_MASK;
1651 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001652 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001653
1654 val &= ~TRANS_INTERLACE_MASK;
1655 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001656 if (HAS_PCH_IBX(dev_priv->dev) &&
1657 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1658 val |= TRANS_LEGACY_INTERLACED_ILK;
1659 else
1660 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001661 else
1662 val |= TRANS_PROGRESSIVE;
1663
Jesse Barnes040484a2011-01-03 12:14:26 -08001664 I915_WRITE(reg, val | TRANS_ENABLE);
1665 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001666 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001667}
1668
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001669static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001670 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001671{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001672 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001673
1674 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001675 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001676
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001677 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001678 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001679 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001680
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001681 /* Workaround: set timing override bit. */
1682 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001683 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001684 I915_WRITE(_TRANSA_CHICKEN2, val);
1685
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001686 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001687 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001688
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001689 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1690 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001691 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001692 else
1693 val |= TRANS_PROGRESSIVE;
1694
Daniel Vetterab9412b2013-05-03 11:49:46 +02001695 I915_WRITE(LPT_TRANSCONF, val);
1696 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001697 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001698}
1699
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001700static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1701 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001702{
Daniel Vetter23670b322012-11-01 09:15:30 +01001703 struct drm_device *dev = dev_priv->dev;
1704 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001705
1706 /* FDI relies on the transcoder */
1707 assert_fdi_tx_disabled(dev_priv, pipe);
1708 assert_fdi_rx_disabled(dev_priv, pipe);
1709
Jesse Barnes291906f2011-02-02 12:28:03 -08001710 /* Ports must be off as well */
1711 assert_pch_ports_disabled(dev_priv, pipe);
1712
Daniel Vetterab9412b2013-05-03 11:49:46 +02001713 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001714 val = I915_READ(reg);
1715 val &= ~TRANS_ENABLE;
1716 I915_WRITE(reg, val);
1717 /* wait for PCH transcoder off, transcoder state */
1718 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001719 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001720
1721 if (!HAS_PCH_IBX(dev)) {
1722 /* Workaround: Clear the timing override chicken bit again. */
1723 reg = TRANS_CHICKEN2(pipe);
1724 val = I915_READ(reg);
1725 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1726 I915_WRITE(reg, val);
1727 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001728}
1729
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001730static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001731{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001732 u32 val;
1733
Daniel Vetterab9412b2013-05-03 11:49:46 +02001734 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001735 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001736 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001737 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001738 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001739 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001740
1741 /* Workaround: clear timing override bit. */
1742 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001743 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001744 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001745}
1746
1747/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001748 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001749 * @dev_priv: i915 private structure
1750 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001751 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001752 *
1753 * Enable @pipe, making sure that various hardware specific requirements
1754 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1755 *
1756 * @pipe should be %PIPE_A or %PIPE_B.
1757 *
1758 * Will wait until the pipe is actually running (i.e. first vblank) before
1759 * returning.
1760 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001761static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Paulo Zanoni851855d2013-12-19 19:12:29 -02001762 bool pch_port, bool dsi, bool wait_for_vblank)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001763{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001764 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1765 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001766 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001767 int reg;
1768 u32 val;
1769
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001770 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001771 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001772 assert_sprites_disabled(dev_priv, pipe);
1773
Paulo Zanoni681e5812012-12-06 11:12:38 -02001774 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001775 pch_transcoder = TRANSCODER_A;
1776 else
1777 pch_transcoder = pipe;
1778
Jesse Barnesb24e7172011-01-04 15:09:30 -08001779 /*
1780 * A pipe without a PLL won't actually be able to drive bits from
1781 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1782 * need the check.
1783 */
1784 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001785 if (dsi)
1786 assert_dsi_pll_enabled(dev_priv);
1787 else
1788 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001789 else {
1790 if (pch_port) {
1791 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001792 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001793 assert_fdi_tx_pll_enabled(dev_priv,
1794 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001795 }
1796 /* FIXME: assert CPU port conditions for SNB+ */
1797 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001798
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001799 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001800 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001801 if (val & PIPECONF_ENABLE)
1802 return;
1803
1804 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001805 POSTING_READ(reg);
1806 if (wait_for_vblank)
1807 intel_wait_for_vblank(dev_priv->dev, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001808}
1809
1810/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001811 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001812 * @dev_priv: i915 private structure
1813 * @pipe: pipe to disable
1814 *
1815 * Disable @pipe, making sure that various hardware specific requirements
1816 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1817 *
1818 * @pipe should be %PIPE_A or %PIPE_B.
1819 *
1820 * Will wait until the pipe has shut down before returning.
1821 */
1822static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1823 enum pipe pipe)
1824{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001825 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1826 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001827 int reg;
1828 u32 val;
1829
1830 /*
1831 * Make sure planes won't keep trying to pump pixels to us,
1832 * or we might hang the display.
1833 */
1834 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001835 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001836 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001837
1838 /* Don't disable pipe A or pipe A PLLs if needed */
1839 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1840 return;
1841
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001842 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001843 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001844 if ((val & PIPECONF_ENABLE) == 0)
1845 return;
1846
1847 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001848 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1849}
1850
Keith Packardd74362c2011-07-28 14:47:14 -07001851/*
1852 * Plane regs are double buffered, going from enabled->disabled needs a
1853 * trigger in order to latch. The display address reg provides this.
1854 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001855void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1856 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001857{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001858 struct drm_device *dev = dev_priv->dev;
1859 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001860
1861 I915_WRITE(reg, I915_READ(reg));
1862 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001863}
1864
Jesse Barnesb24e7172011-01-04 15:09:30 -08001865/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001866 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001867 * @dev_priv: i915 private structure
1868 * @plane: plane to enable
1869 * @pipe: pipe being fed
1870 *
1871 * Enable @plane on @pipe, making sure that @pipe is running first.
1872 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001873static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1874 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001875{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001876 struct intel_crtc *intel_crtc =
1877 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001878 int reg;
1879 u32 val;
1880
1881 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1882 assert_pipe_enabled(dev_priv, pipe);
1883
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001884 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001885
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001886 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001887
Jesse Barnesb24e7172011-01-04 15:09:30 -08001888 reg = DSPCNTR(plane);
1889 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001890 if (val & DISPLAY_PLANE_ENABLE)
1891 return;
1892
1893 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001894 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001895 intel_wait_for_vblank(dev_priv->dev, pipe);
1896}
1897
Jesse Barnesb24e7172011-01-04 15:09:30 -08001898/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001899 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001900 * @dev_priv: i915 private structure
1901 * @plane: plane to disable
1902 * @pipe: pipe consuming the data
1903 *
1904 * Disable @plane; should be an independent operation.
1905 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001906static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1907 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001908{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001909 struct intel_crtc *intel_crtc =
1910 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001911 int reg;
1912 u32 val;
1913
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001914 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001915
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001916 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001917
Jesse Barnesb24e7172011-01-04 15:09:30 -08001918 reg = DSPCNTR(plane);
1919 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001920 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1921 return;
1922
1923 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001924 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001925 intel_wait_for_vblank(dev_priv->dev, pipe);
1926}
1927
Chris Wilson693db182013-03-05 14:52:39 +00001928static bool need_vtd_wa(struct drm_device *dev)
1929{
1930#ifdef CONFIG_INTEL_IOMMU
1931 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1932 return true;
1933#endif
1934 return false;
1935}
1936
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001937static int intel_align_height(struct drm_device *dev, int height, bool tiled)
1938{
1939 int tile_height;
1940
1941 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
1942 return ALIGN(height, tile_height);
1943}
1944
Chris Wilson127bd2a2010-07-23 23:32:05 +01001945int
Chris Wilson48b956c2010-09-14 12:50:34 +01001946intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001947 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001948 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001949{
Chris Wilsonce453d82011-02-21 14:43:56 +00001950 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001951 u32 alignment;
1952 int ret;
1953
Chris Wilson05394f32010-11-08 19:18:58 +00001954 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001955 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001956 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1957 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001958 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001959 alignment = 4 * 1024;
1960 else
1961 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001962 break;
1963 case I915_TILING_X:
1964 /* pin() will align the object as required by fence */
1965 alignment = 0;
1966 break;
1967 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001968 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001969 return -EINVAL;
1970 default:
1971 BUG();
1972 }
1973
Chris Wilson693db182013-03-05 14:52:39 +00001974 /* Note that the w/a also requires 64 PTE of padding following the
1975 * bo. We currently fill all unused PTE with the shadow page and so
1976 * we should always have valid PTE following the scanout preventing
1977 * the VT-d warning.
1978 */
1979 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1980 alignment = 256 * 1024;
1981
Chris Wilsonce453d82011-02-21 14:43:56 +00001982 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001983 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001984 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001985 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001986
1987 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1988 * fence, whereas 965+ only requires a fence if using
1989 * framebuffer compression. For simplicity, we always install
1990 * a fence as the cost is not that onerous.
1991 */
Chris Wilson06d98132012-04-17 15:31:24 +01001992 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001993 if (ret)
1994 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001995
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001996 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001997
Chris Wilsonce453d82011-02-21 14:43:56 +00001998 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001999 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002000
2001err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002002 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002003err_interruptible:
2004 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002005 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002006}
2007
Chris Wilson1690e1e2011-12-14 13:57:08 +01002008void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2009{
2010 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002011 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002012}
2013
Daniel Vetterc2c75132012-07-05 12:17:30 +02002014/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2015 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002016unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2017 unsigned int tiling_mode,
2018 unsigned int cpp,
2019 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002020{
Chris Wilsonbc752862013-02-21 20:04:31 +00002021 if (tiling_mode != I915_TILING_NONE) {
2022 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002023
Chris Wilsonbc752862013-02-21 20:04:31 +00002024 tile_rows = *y / 8;
2025 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002026
Chris Wilsonbc752862013-02-21 20:04:31 +00002027 tiles = *x / (512/cpp);
2028 *x %= 512/cpp;
2029
2030 return tile_rows * pitch * 8 + tiles * 4096;
2031 } else {
2032 unsigned int offset;
2033
2034 offset = *y * pitch + *x * cpp;
2035 *y = 0;
2036 *x = (offset & 4095) / cpp;
2037 return offset & -4096;
2038 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002039}
2040
Jesse Barnes17638cd2011-06-24 12:19:23 -07002041static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2042 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002043{
2044 struct drm_device *dev = crtc->dev;
2045 struct drm_i915_private *dev_priv = dev->dev_private;
2046 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2047 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002048 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002049 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002050 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002051 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002052 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002053
2054 switch (plane) {
2055 case 0:
2056 case 1:
2057 break;
2058 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002059 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002060 return -EINVAL;
2061 }
2062
2063 intel_fb = to_intel_framebuffer(fb);
2064 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002065
Chris Wilson5eddb702010-09-11 13:48:45 +01002066 reg = DSPCNTR(plane);
2067 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002068 /* Mask out pixel format bits in case we change it */
2069 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002070 switch (fb->pixel_format) {
2071 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002072 dspcntr |= DISPPLANE_8BPP;
2073 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002074 case DRM_FORMAT_XRGB1555:
2075 case DRM_FORMAT_ARGB1555:
2076 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002077 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002078 case DRM_FORMAT_RGB565:
2079 dspcntr |= DISPPLANE_BGRX565;
2080 break;
2081 case DRM_FORMAT_XRGB8888:
2082 case DRM_FORMAT_ARGB8888:
2083 dspcntr |= DISPPLANE_BGRX888;
2084 break;
2085 case DRM_FORMAT_XBGR8888:
2086 case DRM_FORMAT_ABGR8888:
2087 dspcntr |= DISPPLANE_RGBX888;
2088 break;
2089 case DRM_FORMAT_XRGB2101010:
2090 case DRM_FORMAT_ARGB2101010:
2091 dspcntr |= DISPPLANE_BGRX101010;
2092 break;
2093 case DRM_FORMAT_XBGR2101010:
2094 case DRM_FORMAT_ABGR2101010:
2095 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002096 break;
2097 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002098 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002099 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002100
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002101 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002102 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002103 dspcntr |= DISPPLANE_TILED;
2104 else
2105 dspcntr &= ~DISPPLANE_TILED;
2106 }
2107
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002108 if (IS_G4X(dev))
2109 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2110
Chris Wilson5eddb702010-09-11 13:48:45 +01002111 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002112
Daniel Vettere506a0c2012-07-05 12:17:29 +02002113 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002114
Daniel Vetterc2c75132012-07-05 12:17:30 +02002115 if (INTEL_INFO(dev)->gen >= 4) {
2116 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002117 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2118 fb->bits_per_pixel / 8,
2119 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002120 linear_offset -= intel_crtc->dspaddr_offset;
2121 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002122 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002123 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002124
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002125 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2126 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2127 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002128 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002129 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002130 I915_WRITE(DSPSURF(plane),
2131 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002132 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002133 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002134 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002135 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002136 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002137
Jesse Barnes17638cd2011-06-24 12:19:23 -07002138 return 0;
2139}
2140
2141static int ironlake_update_plane(struct drm_crtc *crtc,
2142 struct drm_framebuffer *fb, int x, int y)
2143{
2144 struct drm_device *dev = crtc->dev;
2145 struct drm_i915_private *dev_priv = dev->dev_private;
2146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2147 struct intel_framebuffer *intel_fb;
2148 struct drm_i915_gem_object *obj;
2149 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002150 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002151 u32 dspcntr;
2152 u32 reg;
2153
2154 switch (plane) {
2155 case 0:
2156 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002157 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002158 break;
2159 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002160 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002161 return -EINVAL;
2162 }
2163
2164 intel_fb = to_intel_framebuffer(fb);
2165 obj = intel_fb->obj;
2166
2167 reg = DSPCNTR(plane);
2168 dspcntr = I915_READ(reg);
2169 /* Mask out pixel format bits in case we change it */
2170 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002171 switch (fb->pixel_format) {
2172 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002173 dspcntr |= DISPPLANE_8BPP;
2174 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002175 case DRM_FORMAT_RGB565:
2176 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002177 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002178 case DRM_FORMAT_XRGB8888:
2179 case DRM_FORMAT_ARGB8888:
2180 dspcntr |= DISPPLANE_BGRX888;
2181 break;
2182 case DRM_FORMAT_XBGR8888:
2183 case DRM_FORMAT_ABGR8888:
2184 dspcntr |= DISPPLANE_RGBX888;
2185 break;
2186 case DRM_FORMAT_XRGB2101010:
2187 case DRM_FORMAT_ARGB2101010:
2188 dspcntr |= DISPPLANE_BGRX101010;
2189 break;
2190 case DRM_FORMAT_XBGR2101010:
2191 case DRM_FORMAT_ABGR2101010:
2192 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002193 break;
2194 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002195 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002196 }
2197
2198 if (obj->tiling_mode != I915_TILING_NONE)
2199 dspcntr |= DISPPLANE_TILED;
2200 else
2201 dspcntr &= ~DISPPLANE_TILED;
2202
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002203 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002204 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2205 else
2206 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002207
2208 I915_WRITE(reg, dspcntr);
2209
Daniel Vettere506a0c2012-07-05 12:17:29 +02002210 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002211 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002212 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2213 fb->bits_per_pixel / 8,
2214 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002215 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002216
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002217 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2218 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2219 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002220 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002221 I915_WRITE(DSPSURF(plane),
2222 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002223 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002224 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2225 } else {
2226 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2227 I915_WRITE(DSPLINOFF(plane), linear_offset);
2228 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002229 POSTING_READ(reg);
2230
2231 return 0;
2232}
2233
2234/* Assume fb object is pinned & idle & fenced and just update base pointers */
2235static int
2236intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2237 int x, int y, enum mode_set_atomic state)
2238{
2239 struct drm_device *dev = crtc->dev;
2240 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002241
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002242 if (dev_priv->display.disable_fbc)
2243 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002244 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002245
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002246 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002247}
2248
Ville Syrjälä96a02912013-02-18 19:08:49 +02002249void intel_display_handle_reset(struct drm_device *dev)
2250{
2251 struct drm_i915_private *dev_priv = dev->dev_private;
2252 struct drm_crtc *crtc;
2253
2254 /*
2255 * Flips in the rings have been nuked by the reset,
2256 * so complete all pending flips so that user space
2257 * will get its events and not get stuck.
2258 *
2259 * Also update the base address of all primary
2260 * planes to the the last fb to make sure we're
2261 * showing the correct fb after a reset.
2262 *
2263 * Need to make two loops over the crtcs so that we
2264 * don't try to grab a crtc mutex before the
2265 * pending_flip_queue really got woken up.
2266 */
2267
2268 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2270 enum plane plane = intel_crtc->plane;
2271
2272 intel_prepare_page_flip(dev, plane);
2273 intel_finish_page_flip_plane(dev, plane);
2274 }
2275
2276 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2278
2279 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002280 /*
2281 * FIXME: Once we have proper support for primary planes (and
2282 * disabling them without disabling the entire crtc) allow again
2283 * a NULL crtc->fb.
2284 */
2285 if (intel_crtc->active && crtc->fb)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002286 dev_priv->display.update_plane(crtc, crtc->fb,
2287 crtc->x, crtc->y);
2288 mutex_unlock(&crtc->mutex);
2289 }
2290}
2291
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002292static int
Chris Wilson14667a42012-04-03 17:58:35 +01002293intel_finish_fb(struct drm_framebuffer *old_fb)
2294{
2295 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2296 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2297 bool was_interruptible = dev_priv->mm.interruptible;
2298 int ret;
2299
Chris Wilson14667a42012-04-03 17:58:35 +01002300 /* Big Hammer, we also need to ensure that any pending
2301 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2302 * current scanout is retired before unpinning the old
2303 * framebuffer.
2304 *
2305 * This should only fail upon a hung GPU, in which case we
2306 * can safely continue.
2307 */
2308 dev_priv->mm.interruptible = false;
2309 ret = i915_gem_object_finish_gpu(obj);
2310 dev_priv->mm.interruptible = was_interruptible;
2311
2312 return ret;
2313}
2314
Ville Syrjälä198598d2012-10-31 17:50:24 +02002315static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2316{
2317 struct drm_device *dev = crtc->dev;
2318 struct drm_i915_master_private *master_priv;
2319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2320
2321 if (!dev->primary->master)
2322 return;
2323
2324 master_priv = dev->primary->master->driver_priv;
2325 if (!master_priv->sarea_priv)
2326 return;
2327
2328 switch (intel_crtc->pipe) {
2329 case 0:
2330 master_priv->sarea_priv->pipeA_x = x;
2331 master_priv->sarea_priv->pipeA_y = y;
2332 break;
2333 case 1:
2334 master_priv->sarea_priv->pipeB_x = x;
2335 master_priv->sarea_priv->pipeB_y = y;
2336 break;
2337 default:
2338 break;
2339 }
2340}
2341
Chris Wilson14667a42012-04-03 17:58:35 +01002342static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002343intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002344 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002345{
2346 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002347 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002349 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002350 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002351
2352 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002353 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002354 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002355 return 0;
2356 }
2357
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002358 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002359 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2360 plane_name(intel_crtc->plane),
2361 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002362 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002363 }
2364
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002365 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002366 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002367 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002368 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002369 if (ret != 0) {
2370 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002371 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002372 return ret;
2373 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002374
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002375 /*
2376 * Update pipe size and adjust fitter if needed: the reason for this is
2377 * that in compute_mode_changes we check the native mode (not the pfit
2378 * mode) to see if we can flip rather than do a full mode set. In the
2379 * fastboot case, we'll flip, but if we don't update the pipesrc and
2380 * pfit state, we'll end up with a big fb scanned out into the wrong
2381 * sized surface.
2382 *
2383 * To fix this properly, we need to hoist the checks up into
2384 * compute_mode_changes (or above), check the actual pfit state and
2385 * whether the platform allows pfit disable with pipe active, and only
2386 * then update the pipesrc and pfit state, even on the flip path.
2387 */
Jani Nikulad330a952014-01-21 11:24:25 +02002388 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002389 const struct drm_display_mode *adjusted_mode =
2390 &intel_crtc->config.adjusted_mode;
2391
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002392 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002393 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2394 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002395 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002396 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2397 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2398 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2399 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2400 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2401 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002402 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2403 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002404 }
2405
Daniel Vetter94352cf2012-07-05 22:51:56 +02002406 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002407 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002408 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002409 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002410 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002411 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002412 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002413
Daniel Vetter94352cf2012-07-05 22:51:56 +02002414 old_fb = crtc->fb;
2415 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002416 crtc->x = x;
2417 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002418
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002419 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002420 if (intel_crtc->active && old_fb != fb)
2421 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002422 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002423 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002424
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002425 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002426 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002427 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002428
Ville Syrjälä198598d2012-10-31 17:50:24 +02002429 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002430
2431 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002432}
2433
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002434static void intel_fdi_normal_train(struct drm_crtc *crtc)
2435{
2436 struct drm_device *dev = crtc->dev;
2437 struct drm_i915_private *dev_priv = dev->dev_private;
2438 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2439 int pipe = intel_crtc->pipe;
2440 u32 reg, temp;
2441
2442 /* enable normal train */
2443 reg = FDI_TX_CTL(pipe);
2444 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002445 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002446 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2447 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002448 } else {
2449 temp &= ~FDI_LINK_TRAIN_NONE;
2450 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002451 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002452 I915_WRITE(reg, temp);
2453
2454 reg = FDI_RX_CTL(pipe);
2455 temp = I915_READ(reg);
2456 if (HAS_PCH_CPT(dev)) {
2457 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2458 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2459 } else {
2460 temp &= ~FDI_LINK_TRAIN_NONE;
2461 temp |= FDI_LINK_TRAIN_NONE;
2462 }
2463 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2464
2465 /* wait one idle pattern time */
2466 POSTING_READ(reg);
2467 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002468
2469 /* IVB wants error correction enabled */
2470 if (IS_IVYBRIDGE(dev))
2471 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2472 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002473}
2474
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002475static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002476{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002477 return crtc->base.enabled && crtc->active &&
2478 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002479}
2480
Daniel Vetter01a415f2012-10-27 15:58:40 +02002481static void ivb_modeset_global_resources(struct drm_device *dev)
2482{
2483 struct drm_i915_private *dev_priv = dev->dev_private;
2484 struct intel_crtc *pipe_B_crtc =
2485 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2486 struct intel_crtc *pipe_C_crtc =
2487 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2488 uint32_t temp;
2489
Daniel Vetter1e833f42013-02-19 22:31:57 +01002490 /*
2491 * When everything is off disable fdi C so that we could enable fdi B
2492 * with all lanes. Note that we don't care about enabled pipes without
2493 * an enabled pch encoder.
2494 */
2495 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2496 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002497 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2498 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2499
2500 temp = I915_READ(SOUTH_CHICKEN1);
2501 temp &= ~FDI_BC_BIFURCATION_SELECT;
2502 DRM_DEBUG_KMS("disabling fdi C rx\n");
2503 I915_WRITE(SOUTH_CHICKEN1, temp);
2504 }
2505}
2506
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002507/* The FDI link training functions for ILK/Ibexpeak. */
2508static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2509{
2510 struct drm_device *dev = crtc->dev;
2511 struct drm_i915_private *dev_priv = dev->dev_private;
2512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2513 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002514 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002517 /* FDI needs bits from pipe & plane first */
2518 assert_pipe_enabled(dev_priv, pipe);
2519 assert_plane_enabled(dev_priv, plane);
2520
Adam Jacksone1a44742010-06-25 15:32:14 -04002521 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2522 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002523 reg = FDI_RX_IMR(pipe);
2524 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002525 temp &= ~FDI_RX_SYMBOL_LOCK;
2526 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002527 I915_WRITE(reg, temp);
2528 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002529 udelay(150);
2530
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 reg = FDI_TX_CTL(pipe);
2533 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002534 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2535 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 temp &= ~FDI_LINK_TRAIN_NONE;
2537 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539
Chris Wilson5eddb702010-09-11 13:48:45 +01002540 reg = FDI_RX_CTL(pipe);
2541 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002542 temp &= ~FDI_LINK_TRAIN_NONE;
2543 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002544 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2545
2546 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547 udelay(150);
2548
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002549 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002550 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2551 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2552 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002553
Chris Wilson5eddb702010-09-11 13:48:45 +01002554 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002555 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2558
2559 if ((temp & FDI_RX_BIT_LOCK)) {
2560 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562 break;
2563 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002564 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002565 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002567
2568 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002569 reg = FDI_TX_CTL(pipe);
2570 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571 temp &= ~FDI_LINK_TRAIN_NONE;
2572 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002573 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574
Chris Wilson5eddb702010-09-11 13:48:45 +01002575 reg = FDI_RX_CTL(pipe);
2576 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002577 temp &= ~FDI_LINK_TRAIN_NONE;
2578 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002579 I915_WRITE(reg, temp);
2580
2581 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002582 udelay(150);
2583
Chris Wilson5eddb702010-09-11 13:48:45 +01002584 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002585 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2588
2589 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002590 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002591 DRM_DEBUG_KMS("FDI train 2 done.\n");
2592 break;
2593 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002594 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002595 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597
2598 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002599
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002600}
2601
Akshay Joshi0206e352011-08-16 15:34:10 -04002602static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002603 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2604 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2605 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2606 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2607};
2608
2609/* The FDI link training functions for SNB/Cougarpoint. */
2610static void gen6_fdi_link_train(struct drm_crtc *crtc)
2611{
2612 struct drm_device *dev = crtc->dev;
2613 struct drm_i915_private *dev_priv = dev->dev_private;
2614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2615 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002616 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617
Adam Jacksone1a44742010-06-25 15:32:14 -04002618 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2619 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002620 reg = FDI_RX_IMR(pipe);
2621 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002622 temp &= ~FDI_RX_SYMBOL_LOCK;
2623 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002624 I915_WRITE(reg, temp);
2625
2626 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002627 udelay(150);
2628
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002629 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002630 reg = FDI_TX_CTL(pipe);
2631 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002632 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2633 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002634 temp &= ~FDI_LINK_TRAIN_NONE;
2635 temp |= FDI_LINK_TRAIN_PATTERN_1;
2636 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2637 /* SNB-B */
2638 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002639 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002640
Daniel Vetterd74cf322012-10-26 10:58:13 +02002641 I915_WRITE(FDI_RX_MISC(pipe),
2642 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2643
Chris Wilson5eddb702010-09-11 13:48:45 +01002644 reg = FDI_RX_CTL(pipe);
2645 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002646 if (HAS_PCH_CPT(dev)) {
2647 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2648 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2649 } else {
2650 temp &= ~FDI_LINK_TRAIN_NONE;
2651 temp |= FDI_LINK_TRAIN_PATTERN_1;
2652 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002653 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2654
2655 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002656 udelay(150);
2657
Akshay Joshi0206e352011-08-16 15:34:10 -04002658 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002659 reg = FDI_TX_CTL(pipe);
2660 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002661 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2662 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002663 I915_WRITE(reg, temp);
2664
2665 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002666 udelay(500);
2667
Sean Paulfa37d392012-03-02 12:53:39 -05002668 for (retry = 0; retry < 5; retry++) {
2669 reg = FDI_RX_IIR(pipe);
2670 temp = I915_READ(reg);
2671 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2672 if (temp & FDI_RX_BIT_LOCK) {
2673 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2674 DRM_DEBUG_KMS("FDI train 1 done.\n");
2675 break;
2676 }
2677 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002678 }
Sean Paulfa37d392012-03-02 12:53:39 -05002679 if (retry < 5)
2680 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002681 }
2682 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002683 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002684
2685 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002686 reg = FDI_TX_CTL(pipe);
2687 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002688 temp &= ~FDI_LINK_TRAIN_NONE;
2689 temp |= FDI_LINK_TRAIN_PATTERN_2;
2690 if (IS_GEN6(dev)) {
2691 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2692 /* SNB-B */
2693 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2694 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002695 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002696
Chris Wilson5eddb702010-09-11 13:48:45 +01002697 reg = FDI_RX_CTL(pipe);
2698 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002699 if (HAS_PCH_CPT(dev)) {
2700 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2701 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2702 } else {
2703 temp &= ~FDI_LINK_TRAIN_NONE;
2704 temp |= FDI_LINK_TRAIN_PATTERN_2;
2705 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002706 I915_WRITE(reg, temp);
2707
2708 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002709 udelay(150);
2710
Akshay Joshi0206e352011-08-16 15:34:10 -04002711 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002712 reg = FDI_TX_CTL(pipe);
2713 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002714 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2715 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002716 I915_WRITE(reg, temp);
2717
2718 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002719 udelay(500);
2720
Sean Paulfa37d392012-03-02 12:53:39 -05002721 for (retry = 0; retry < 5; retry++) {
2722 reg = FDI_RX_IIR(pipe);
2723 temp = I915_READ(reg);
2724 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2725 if (temp & FDI_RX_SYMBOL_LOCK) {
2726 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2727 DRM_DEBUG_KMS("FDI train 2 done.\n");
2728 break;
2729 }
2730 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002731 }
Sean Paulfa37d392012-03-02 12:53:39 -05002732 if (retry < 5)
2733 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002734 }
2735 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002736 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002737
2738 DRM_DEBUG_KMS("FDI train done.\n");
2739}
2740
Jesse Barnes357555c2011-04-28 15:09:55 -07002741/* Manual link training for Ivy Bridge A0 parts */
2742static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2743{
2744 struct drm_device *dev = crtc->dev;
2745 struct drm_i915_private *dev_priv = dev->dev_private;
2746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2747 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002748 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002749
2750 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2751 for train result */
2752 reg = FDI_RX_IMR(pipe);
2753 temp = I915_READ(reg);
2754 temp &= ~FDI_RX_SYMBOL_LOCK;
2755 temp &= ~FDI_RX_BIT_LOCK;
2756 I915_WRITE(reg, temp);
2757
2758 POSTING_READ(reg);
2759 udelay(150);
2760
Daniel Vetter01a415f2012-10-27 15:58:40 +02002761 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2762 I915_READ(FDI_RX_IIR(pipe)));
2763
Jesse Barnes139ccd32013-08-19 11:04:55 -07002764 /* Try each vswing and preemphasis setting twice before moving on */
2765 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2766 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002767 reg = FDI_TX_CTL(pipe);
2768 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002769 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2770 temp &= ~FDI_TX_ENABLE;
2771 I915_WRITE(reg, temp);
2772
2773 reg = FDI_RX_CTL(pipe);
2774 temp = I915_READ(reg);
2775 temp &= ~FDI_LINK_TRAIN_AUTO;
2776 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2777 temp &= ~FDI_RX_ENABLE;
2778 I915_WRITE(reg, temp);
2779
2780 /* enable CPU FDI TX and PCH FDI RX */
2781 reg = FDI_TX_CTL(pipe);
2782 temp = I915_READ(reg);
2783 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2784 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2785 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002786 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002787 temp |= snb_b_fdi_train_param[j/2];
2788 temp |= FDI_COMPOSITE_SYNC;
2789 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2790
2791 I915_WRITE(FDI_RX_MISC(pipe),
2792 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2793
2794 reg = FDI_RX_CTL(pipe);
2795 temp = I915_READ(reg);
2796 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2797 temp |= FDI_COMPOSITE_SYNC;
2798 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2799
2800 POSTING_READ(reg);
2801 udelay(1); /* should be 0.5us */
2802
2803 for (i = 0; i < 4; i++) {
2804 reg = FDI_RX_IIR(pipe);
2805 temp = I915_READ(reg);
2806 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2807
2808 if (temp & FDI_RX_BIT_LOCK ||
2809 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2810 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2811 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2812 i);
2813 break;
2814 }
2815 udelay(1); /* should be 0.5us */
2816 }
2817 if (i == 4) {
2818 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2819 continue;
2820 }
2821
2822 /* Train 2 */
2823 reg = FDI_TX_CTL(pipe);
2824 temp = I915_READ(reg);
2825 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2826 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2827 I915_WRITE(reg, temp);
2828
2829 reg = FDI_RX_CTL(pipe);
2830 temp = I915_READ(reg);
2831 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2832 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002833 I915_WRITE(reg, temp);
2834
2835 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002836 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002837
Jesse Barnes139ccd32013-08-19 11:04:55 -07002838 for (i = 0; i < 4; i++) {
2839 reg = FDI_RX_IIR(pipe);
2840 temp = I915_READ(reg);
2841 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002842
Jesse Barnes139ccd32013-08-19 11:04:55 -07002843 if (temp & FDI_RX_SYMBOL_LOCK ||
2844 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2845 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2846 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2847 i);
2848 goto train_done;
2849 }
2850 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002851 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002852 if (i == 4)
2853 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002854 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002855
Jesse Barnes139ccd32013-08-19 11:04:55 -07002856train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002857 DRM_DEBUG_KMS("FDI train done.\n");
2858}
2859
Daniel Vetter88cefb62012-08-12 19:27:14 +02002860static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002861{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002862 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002863 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002864 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002865 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002866
Jesse Barnesc64e3112010-09-10 11:27:03 -07002867
Jesse Barnes0e23b992010-09-10 11:10:00 -07002868 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002869 reg = FDI_RX_CTL(pipe);
2870 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002871 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2872 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002873 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002874 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2875
2876 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002877 udelay(200);
2878
2879 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002880 temp = I915_READ(reg);
2881 I915_WRITE(reg, temp | FDI_PCDCLK);
2882
2883 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002884 udelay(200);
2885
Paulo Zanoni20749732012-11-23 15:30:38 -02002886 /* Enable CPU FDI TX PLL, always on for Ironlake */
2887 reg = FDI_TX_CTL(pipe);
2888 temp = I915_READ(reg);
2889 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2890 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002891
Paulo Zanoni20749732012-11-23 15:30:38 -02002892 POSTING_READ(reg);
2893 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002894 }
2895}
2896
Daniel Vetter88cefb62012-08-12 19:27:14 +02002897static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2898{
2899 struct drm_device *dev = intel_crtc->base.dev;
2900 struct drm_i915_private *dev_priv = dev->dev_private;
2901 int pipe = intel_crtc->pipe;
2902 u32 reg, temp;
2903
2904 /* Switch from PCDclk to Rawclk */
2905 reg = FDI_RX_CTL(pipe);
2906 temp = I915_READ(reg);
2907 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2908
2909 /* Disable CPU FDI TX PLL */
2910 reg = FDI_TX_CTL(pipe);
2911 temp = I915_READ(reg);
2912 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2913
2914 POSTING_READ(reg);
2915 udelay(100);
2916
2917 reg = FDI_RX_CTL(pipe);
2918 temp = I915_READ(reg);
2919 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2920
2921 /* Wait for the clocks to turn off. */
2922 POSTING_READ(reg);
2923 udelay(100);
2924}
2925
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002926static void ironlake_fdi_disable(struct drm_crtc *crtc)
2927{
2928 struct drm_device *dev = crtc->dev;
2929 struct drm_i915_private *dev_priv = dev->dev_private;
2930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2931 int pipe = intel_crtc->pipe;
2932 u32 reg, temp;
2933
2934 /* disable CPU FDI tx and PCH FDI rx */
2935 reg = FDI_TX_CTL(pipe);
2936 temp = I915_READ(reg);
2937 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2938 POSTING_READ(reg);
2939
2940 reg = FDI_RX_CTL(pipe);
2941 temp = I915_READ(reg);
2942 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002943 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002944 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2945
2946 POSTING_READ(reg);
2947 udelay(100);
2948
2949 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002950 if (HAS_PCH_IBX(dev)) {
2951 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002952 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002953
2954 /* still set train pattern 1 */
2955 reg = FDI_TX_CTL(pipe);
2956 temp = I915_READ(reg);
2957 temp &= ~FDI_LINK_TRAIN_NONE;
2958 temp |= FDI_LINK_TRAIN_PATTERN_1;
2959 I915_WRITE(reg, temp);
2960
2961 reg = FDI_RX_CTL(pipe);
2962 temp = I915_READ(reg);
2963 if (HAS_PCH_CPT(dev)) {
2964 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2965 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2966 } else {
2967 temp &= ~FDI_LINK_TRAIN_NONE;
2968 temp |= FDI_LINK_TRAIN_PATTERN_1;
2969 }
2970 /* BPC in FDI rx is consistent with that in PIPECONF */
2971 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002972 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002973 I915_WRITE(reg, temp);
2974
2975 POSTING_READ(reg);
2976 udelay(100);
2977}
2978
Chris Wilson5bb61642012-09-27 21:25:58 +01002979static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2980{
2981 struct drm_device *dev = crtc->dev;
2982 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002984 unsigned long flags;
2985 bool pending;
2986
Ville Syrjälä10d83732013-01-29 18:13:34 +02002987 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2988 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002989 return false;
2990
2991 spin_lock_irqsave(&dev->event_lock, flags);
2992 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2993 spin_unlock_irqrestore(&dev->event_lock, flags);
2994
2995 return pending;
2996}
2997
Chris Wilson5dce5b932014-01-20 10:17:36 +00002998bool intel_has_pending_fb_unpin(struct drm_device *dev)
2999{
3000 struct intel_crtc *crtc;
3001
3002 /* Note that we don't need to be called with mode_config.lock here
3003 * as our list of CRTC objects is static for the lifetime of the
3004 * device and so cannot disappear as we iterate. Similarly, we can
3005 * happily treat the predicates as racy, atomic checks as userspace
3006 * cannot claim and pin a new fb without at least acquring the
3007 * struct_mutex and so serialising with us.
3008 */
3009 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3010 if (atomic_read(&crtc->unpin_work_count) == 0)
3011 continue;
3012
3013 if (crtc->unpin_work)
3014 intel_wait_for_vblank(dev, crtc->pipe);
3015
3016 return true;
3017 }
3018
3019 return false;
3020}
3021
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003022static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3023{
Chris Wilson0f911282012-04-17 10:05:38 +01003024 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003025 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003026
3027 if (crtc->fb == NULL)
3028 return;
3029
Daniel Vetter2c10d572012-12-20 21:24:07 +01003030 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3031
Chris Wilson5bb61642012-09-27 21:25:58 +01003032 wait_event(dev_priv->pending_flip_queue,
3033 !intel_crtc_has_pending_flip(crtc));
3034
Chris Wilson0f911282012-04-17 10:05:38 +01003035 mutex_lock(&dev->struct_mutex);
3036 intel_finish_fb(crtc->fb);
3037 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003038}
3039
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003040/* Program iCLKIP clock to the desired frequency */
3041static void lpt_program_iclkip(struct drm_crtc *crtc)
3042{
3043 struct drm_device *dev = crtc->dev;
3044 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003045 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003046 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3047 u32 temp;
3048
Daniel Vetter09153002012-12-12 14:06:44 +01003049 mutex_lock(&dev_priv->dpio_lock);
3050
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003051 /* It is necessary to ungate the pixclk gate prior to programming
3052 * the divisors, and gate it back when it is done.
3053 */
3054 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3055
3056 /* Disable SSCCTL */
3057 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003058 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3059 SBI_SSCCTL_DISABLE,
3060 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003061
3062 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003063 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003064 auxdiv = 1;
3065 divsel = 0x41;
3066 phaseinc = 0x20;
3067 } else {
3068 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003069 * but the adjusted_mode->crtc_clock in in KHz. To get the
3070 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003071 * convert the virtual clock precision to KHz here for higher
3072 * precision.
3073 */
3074 u32 iclk_virtual_root_freq = 172800 * 1000;
3075 u32 iclk_pi_range = 64;
3076 u32 desired_divisor, msb_divisor_value, pi_value;
3077
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003078 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003079 msb_divisor_value = desired_divisor / iclk_pi_range;
3080 pi_value = desired_divisor % iclk_pi_range;
3081
3082 auxdiv = 0;
3083 divsel = msb_divisor_value - 2;
3084 phaseinc = pi_value;
3085 }
3086
3087 /* This should not happen with any sane values */
3088 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3089 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3090 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3091 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3092
3093 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 +03003094 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003095 auxdiv,
3096 divsel,
3097 phasedir,
3098 phaseinc);
3099
3100 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003101 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003102 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3103 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3104 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3105 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3106 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3107 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003108 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003109
3110 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003111 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003112 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3113 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003114 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003115
3116 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003117 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003118 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003119 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003120
3121 /* Wait for initialization time */
3122 udelay(24);
3123
3124 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003125
3126 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003127}
3128
Daniel Vetter275f01b22013-05-03 11:49:47 +02003129static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3130 enum pipe pch_transcoder)
3131{
3132 struct drm_device *dev = crtc->base.dev;
3133 struct drm_i915_private *dev_priv = dev->dev_private;
3134 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3135
3136 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3137 I915_READ(HTOTAL(cpu_transcoder)));
3138 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3139 I915_READ(HBLANK(cpu_transcoder)));
3140 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3141 I915_READ(HSYNC(cpu_transcoder)));
3142
3143 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3144 I915_READ(VTOTAL(cpu_transcoder)));
3145 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3146 I915_READ(VBLANK(cpu_transcoder)));
3147 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3148 I915_READ(VSYNC(cpu_transcoder)));
3149 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3150 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3151}
3152
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003153static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3154{
3155 struct drm_i915_private *dev_priv = dev->dev_private;
3156 uint32_t temp;
3157
3158 temp = I915_READ(SOUTH_CHICKEN1);
3159 if (temp & FDI_BC_BIFURCATION_SELECT)
3160 return;
3161
3162 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3163 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3164
3165 temp |= FDI_BC_BIFURCATION_SELECT;
3166 DRM_DEBUG_KMS("enabling fdi C rx\n");
3167 I915_WRITE(SOUTH_CHICKEN1, temp);
3168 POSTING_READ(SOUTH_CHICKEN1);
3169}
3170
3171static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3172{
3173 struct drm_device *dev = intel_crtc->base.dev;
3174 struct drm_i915_private *dev_priv = dev->dev_private;
3175
3176 switch (intel_crtc->pipe) {
3177 case PIPE_A:
3178 break;
3179 case PIPE_B:
3180 if (intel_crtc->config.fdi_lanes > 2)
3181 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3182 else
3183 cpt_enable_fdi_bc_bifurcation(dev);
3184
3185 break;
3186 case PIPE_C:
3187 cpt_enable_fdi_bc_bifurcation(dev);
3188
3189 break;
3190 default:
3191 BUG();
3192 }
3193}
3194
Jesse Barnesf67a5592011-01-05 10:31:48 -08003195/*
3196 * Enable PCH resources required for PCH ports:
3197 * - PCH PLLs
3198 * - FDI training & RX/TX
3199 * - update transcoder timings
3200 * - DP transcoding bits
3201 * - transcoder
3202 */
3203static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003204{
3205 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003206 struct drm_i915_private *dev_priv = dev->dev_private;
3207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3208 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003209 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003210
Daniel Vetterab9412b2013-05-03 11:49:46 +02003211 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003212
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003213 if (IS_IVYBRIDGE(dev))
3214 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3215
Daniel Vettercd986ab2012-10-26 10:58:12 +02003216 /* Write the TU size bits before fdi link training, so that error
3217 * detection works. */
3218 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3219 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3220
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003221 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003222 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003223
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003224 /* We need to program the right clock selection before writing the pixel
3225 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003226 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003227 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003228
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003229 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003230 temp |= TRANS_DPLL_ENABLE(pipe);
3231 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003232 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003233 temp |= sel;
3234 else
3235 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003236 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003237 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003238
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003239 /* XXX: pch pll's can be enabled any time before we enable the PCH
3240 * transcoder, and we actually should do this to not upset any PCH
3241 * transcoder that already use the clock when we share it.
3242 *
3243 * Note that enable_shared_dpll tries to do the right thing, but
3244 * get_shared_dpll unconditionally resets the pll - we need that to have
3245 * the right LVDS enable sequence. */
3246 ironlake_enable_shared_dpll(intel_crtc);
3247
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003248 /* set transcoder timing, panel must allow it */
3249 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003250 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003251
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003252 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003253
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003254 /* For PCH DP, enable TRANS_DP_CTL */
3255 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003256 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3257 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003258 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003259 reg = TRANS_DP_CTL(pipe);
3260 temp = I915_READ(reg);
3261 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003262 TRANS_DP_SYNC_MASK |
3263 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003264 temp |= (TRANS_DP_OUTPUT_ENABLE |
3265 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003266 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003267
3268 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003269 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003270 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003271 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003272
3273 switch (intel_trans_dp_port_sel(crtc)) {
3274 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003275 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003276 break;
3277 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003278 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003279 break;
3280 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003281 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003282 break;
3283 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003284 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003285 }
3286
Chris Wilson5eddb702010-09-11 13:48:45 +01003287 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003288 }
3289
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003290 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003291}
3292
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003293static void lpt_pch_enable(struct drm_crtc *crtc)
3294{
3295 struct drm_device *dev = crtc->dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
3297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003298 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003299
Daniel Vetterab9412b2013-05-03 11:49:46 +02003300 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003301
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003302 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003303
Paulo Zanoni0540e482012-10-31 18:12:40 -02003304 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003305 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003306
Paulo Zanoni937bb612012-10-31 18:12:47 -02003307 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003308}
3309
Daniel Vettere2b78262013-06-07 23:10:03 +02003310static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003311{
Daniel Vettere2b78262013-06-07 23:10:03 +02003312 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003313
3314 if (pll == NULL)
3315 return;
3316
3317 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003318 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003319 return;
3320 }
3321
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003322 if (--pll->refcount == 0) {
3323 WARN_ON(pll->on);
3324 WARN_ON(pll->active);
3325 }
3326
Daniel Vettera43f6e02013-06-07 23:10:32 +02003327 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003328}
3329
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003330static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003331{
Daniel Vettere2b78262013-06-07 23:10:03 +02003332 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3333 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3334 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003335
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003336 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003337 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3338 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003339 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003340 }
3341
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003342 if (HAS_PCH_IBX(dev_priv->dev)) {
3343 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003344 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003345 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003346
Daniel Vetter46edb022013-06-05 13:34:12 +02003347 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3348 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003349
3350 goto found;
3351 }
3352
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003353 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3354 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003355
3356 /* Only want to check enabled timings first */
3357 if (pll->refcount == 0)
3358 continue;
3359
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003360 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3361 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003362 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003363 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003364 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003365
3366 goto found;
3367 }
3368 }
3369
3370 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003371 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3372 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003373 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003374 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3375 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003376 goto found;
3377 }
3378 }
3379
3380 return NULL;
3381
3382found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003383 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003384 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3385 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003386
Daniel Vettercdbd2312013-06-05 13:34:03 +02003387 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003388 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3389 sizeof(pll->hw_state));
3390
Daniel Vetter46edb022013-06-05 13:34:12 +02003391 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003392 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003393 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003394
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003395 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003396 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003397 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003398
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003399 return pll;
3400}
3401
Daniel Vettera1520312013-05-03 11:49:50 +02003402static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003403{
3404 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003405 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003406 u32 temp;
3407
3408 temp = I915_READ(dslreg);
3409 udelay(500);
3410 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003411 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003412 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003413 }
3414}
3415
Jesse Barnesb074cec2013-04-25 12:55:02 -07003416static void ironlake_pfit_enable(struct intel_crtc *crtc)
3417{
3418 struct drm_device *dev = crtc->base.dev;
3419 struct drm_i915_private *dev_priv = dev->dev_private;
3420 int pipe = crtc->pipe;
3421
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003422 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003423 /* Force use of hard-coded filter coefficients
3424 * as some pre-programmed values are broken,
3425 * e.g. x201.
3426 */
3427 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3428 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3429 PF_PIPE_SEL_IVB(pipe));
3430 else
3431 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3432 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3433 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003434 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003435}
3436
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003437static void intel_enable_planes(struct drm_crtc *crtc)
3438{
3439 struct drm_device *dev = crtc->dev;
3440 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3441 struct intel_plane *intel_plane;
3442
3443 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3444 if (intel_plane->pipe == pipe)
3445 intel_plane_restore(&intel_plane->base);
3446}
3447
3448static void intel_disable_planes(struct drm_crtc *crtc)
3449{
3450 struct drm_device *dev = crtc->dev;
3451 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3452 struct intel_plane *intel_plane;
3453
3454 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3455 if (intel_plane->pipe == pipe)
3456 intel_plane_disable(&intel_plane->base);
3457}
3458
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003459void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003460{
3461 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3462
3463 if (!crtc->config.ips_enabled)
3464 return;
3465
3466 /* We can only enable IPS after we enable a plane and wait for a vblank.
3467 * We guarantee that the plane is enabled by calling intel_enable_ips
3468 * only after intel_enable_plane. And intel_enable_plane already waits
3469 * for a vblank, so all we need to do here is to enable the IPS bit. */
3470 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003471 if (IS_BROADWELL(crtc->base.dev)) {
3472 mutex_lock(&dev_priv->rps.hw_lock);
3473 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3474 mutex_unlock(&dev_priv->rps.hw_lock);
3475 /* Quoting Art Runyan: "its not safe to expect any particular
3476 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003477 * mailbox." Moreover, the mailbox may return a bogus state,
3478 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003479 */
3480 } else {
3481 I915_WRITE(IPS_CTL, IPS_ENABLE);
3482 /* The bit only becomes 1 in the next vblank, so this wait here
3483 * is essentially intel_wait_for_vblank. If we don't have this
3484 * and don't wait for vblanks until the end of crtc_enable, then
3485 * the HW state readout code will complain that the expected
3486 * IPS_CTL value is not the one we read. */
3487 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3488 DRM_ERROR("Timed out waiting for IPS enable\n");
3489 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003490}
3491
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003492void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003493{
3494 struct drm_device *dev = crtc->base.dev;
3495 struct drm_i915_private *dev_priv = dev->dev_private;
3496
3497 if (!crtc->config.ips_enabled)
3498 return;
3499
3500 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003501 if (IS_BROADWELL(crtc->base.dev)) {
3502 mutex_lock(&dev_priv->rps.hw_lock);
3503 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3504 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnese59150d2014-01-07 13:30:45 -08003505 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003506 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003507 POSTING_READ(IPS_CTL);
3508 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003509
3510 /* We need to wait for a vblank before we can disable the plane. */
3511 intel_wait_for_vblank(dev, crtc->pipe);
3512}
3513
3514/** Loads the palette/gamma unit for the CRTC with the prepared values */
3515static void intel_crtc_load_lut(struct drm_crtc *crtc)
3516{
3517 struct drm_device *dev = crtc->dev;
3518 struct drm_i915_private *dev_priv = dev->dev_private;
3519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3520 enum pipe pipe = intel_crtc->pipe;
3521 int palreg = PALETTE(pipe);
3522 int i;
3523 bool reenable_ips = false;
3524
3525 /* The clocks have to be on to load the palette. */
3526 if (!crtc->enabled || !intel_crtc->active)
3527 return;
3528
3529 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3530 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3531 assert_dsi_pll_enabled(dev_priv);
3532 else
3533 assert_pll_enabled(dev_priv, pipe);
3534 }
3535
3536 /* use legacy palette for Ironlake */
3537 if (HAS_PCH_SPLIT(dev))
3538 palreg = LGC_PALETTE(pipe);
3539
3540 /* Workaround : Do not read or write the pipe palette/gamma data while
3541 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3542 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003543 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003544 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3545 GAMMA_MODE_MODE_SPLIT)) {
3546 hsw_disable_ips(intel_crtc);
3547 reenable_ips = true;
3548 }
3549
3550 for (i = 0; i < 256; i++) {
3551 I915_WRITE(palreg + 4 * i,
3552 (intel_crtc->lut_r[i] << 16) |
3553 (intel_crtc->lut_g[i] << 8) |
3554 intel_crtc->lut_b[i]);
3555 }
3556
3557 if (reenable_ips)
3558 hsw_enable_ips(intel_crtc);
3559}
3560
Jesse Barnesf67a5592011-01-05 10:31:48 -08003561static void ironlake_crtc_enable(struct drm_crtc *crtc)
3562{
3563 struct drm_device *dev = crtc->dev;
3564 struct drm_i915_private *dev_priv = dev->dev_private;
3565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003566 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003567 int pipe = intel_crtc->pipe;
3568 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003569
Daniel Vetter08a48462012-07-02 11:43:47 +02003570 WARN_ON(!crtc->enabled);
3571
Jesse Barnesf67a5592011-01-05 10:31:48 -08003572 if (intel_crtc->active)
3573 return;
3574
3575 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003576
3577 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3578 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3579
Daniel Vetterf6736a12013-06-05 13:34:30 +02003580 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003581 if (encoder->pre_enable)
3582 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003583
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003584 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003585 /* Note: FDI PLL enabling _must_ be done before we enable the
3586 * cpu pipes, hence this is separate from all the other fdi/pch
3587 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003588 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003589 } else {
3590 assert_fdi_tx_disabled(dev_priv, pipe);
3591 assert_fdi_rx_disabled(dev_priv, pipe);
3592 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003593
Jesse Barnesb074cec2013-04-25 12:55:02 -07003594 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003595
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003596 /*
3597 * On ILK+ LUT must be loaded before the pipe is running but with
3598 * clocks enabled
3599 */
3600 intel_crtc_load_lut(crtc);
3601
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003602 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003603 intel_enable_pipe(dev_priv, pipe,
Paulo Zanoni851855d2013-12-19 19:12:29 -02003604 intel_crtc->config.has_pch_encoder, false, true);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003605 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003606 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003607 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003608
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003609 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003610 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003611
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003612 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003613 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003614 mutex_unlock(&dev->struct_mutex);
3615
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003616 for_each_encoder_on_crtc(dev, crtc, encoder)
3617 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003618
3619 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003620 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003621
3622 /*
3623 * There seems to be a race in PCH platform hw (at least on some
3624 * outputs) where an enabled pipe still completes any pageflip right
3625 * away (as if the pipe is off) instead of waiting for vblank. As soon
3626 * as the first vblank happend, everything works as expected. Hence just
3627 * wait for one vblank before returning to avoid strange things
3628 * happening.
3629 */
3630 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003631}
3632
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003633/* IPS only exists on ULT machines and is tied to pipe A. */
3634static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3635{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003636 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003637}
3638
Ville Syrjälädda9a662013-09-19 17:00:37 -03003639static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3640{
3641 struct drm_device *dev = crtc->dev;
3642 struct drm_i915_private *dev_priv = dev->dev_private;
3643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3644 int pipe = intel_crtc->pipe;
3645 int plane = intel_crtc->plane;
3646
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003647 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003648 intel_enable_planes(crtc);
3649 intel_crtc_update_cursor(crtc, true);
3650
3651 hsw_enable_ips(intel_crtc);
3652
3653 mutex_lock(&dev->struct_mutex);
3654 intel_update_fbc(dev);
3655 mutex_unlock(&dev->struct_mutex);
3656}
3657
3658static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3659{
3660 struct drm_device *dev = crtc->dev;
3661 struct drm_i915_private *dev_priv = dev->dev_private;
3662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3663 int pipe = intel_crtc->pipe;
3664 int plane = intel_crtc->plane;
3665
3666 intel_crtc_wait_for_pending_flips(crtc);
3667 drm_vblank_off(dev, pipe);
3668
3669 /* FBC must be disabled before disabling the plane on HSW. */
3670 if (dev_priv->fbc.plane == plane)
3671 intel_disable_fbc(dev);
3672
3673 hsw_disable_ips(intel_crtc);
3674
3675 intel_crtc_update_cursor(crtc, false);
3676 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003677 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003678}
3679
Paulo Zanonie4916942013-09-20 16:21:19 -03003680/*
3681 * This implements the workaround described in the "notes" section of the mode
3682 * set sequence documentation. When going from no pipes or single pipe to
3683 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3684 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3685 */
3686static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3687{
3688 struct drm_device *dev = crtc->base.dev;
3689 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3690
3691 /* We want to get the other_active_crtc only if there's only 1 other
3692 * active crtc. */
3693 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3694 if (!crtc_it->active || crtc_it == crtc)
3695 continue;
3696
3697 if (other_active_crtc)
3698 return;
3699
3700 other_active_crtc = crtc_it;
3701 }
3702 if (!other_active_crtc)
3703 return;
3704
3705 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3706 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3707}
3708
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003709static void haswell_crtc_enable(struct drm_crtc *crtc)
3710{
3711 struct drm_device *dev = crtc->dev;
3712 struct drm_i915_private *dev_priv = dev->dev_private;
3713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3714 struct intel_encoder *encoder;
3715 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003716
3717 WARN_ON(!crtc->enabled);
3718
3719 if (intel_crtc->active)
3720 return;
3721
3722 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003723
3724 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3725 if (intel_crtc->config.has_pch_encoder)
3726 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3727
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003728 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003729 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003730
3731 for_each_encoder_on_crtc(dev, crtc, encoder)
3732 if (encoder->pre_enable)
3733 encoder->pre_enable(encoder);
3734
Paulo Zanoni1f544382012-10-24 11:32:00 -02003735 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003736
Jesse Barnesb074cec2013-04-25 12:55:02 -07003737 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003738
3739 /*
3740 * On ILK+ LUT must be loaded before the pipe is running but with
3741 * clocks enabled
3742 */
3743 intel_crtc_load_lut(crtc);
3744
Paulo Zanoni1f544382012-10-24 11:32:00 -02003745 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003746 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003747
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003748 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003749 intel_enable_pipe(dev_priv, pipe,
Paulo Zanoni851855d2013-12-19 19:12:29 -02003750 intel_crtc->config.has_pch_encoder, false, true);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003751
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003752 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003753 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003754
Jani Nikula8807e552013-08-30 19:40:32 +03003755 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003756 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003757 intel_opregion_notify_encoder(encoder, true);
3758 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003759
Paulo Zanonie4916942013-09-20 16:21:19 -03003760 /* If we change the relative order between pipe/planes enabling, we need
3761 * to change the workaround. */
3762 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003763 haswell_crtc_enable_planes(crtc);
3764
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003765 /*
3766 * There seems to be a race in PCH platform hw (at least on some
3767 * outputs) where an enabled pipe still completes any pageflip right
3768 * away (as if the pipe is off) instead of waiting for vblank. As soon
3769 * as the first vblank happend, everything works as expected. Hence just
3770 * wait for one vblank before returning to avoid strange things
3771 * happening.
3772 */
3773 intel_wait_for_vblank(dev, intel_crtc->pipe);
3774}
3775
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003776static void ironlake_pfit_disable(struct intel_crtc *crtc)
3777{
3778 struct drm_device *dev = crtc->base.dev;
3779 struct drm_i915_private *dev_priv = dev->dev_private;
3780 int pipe = crtc->pipe;
3781
3782 /* To avoid upsetting the power well on haswell only disable the pfit if
3783 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003784 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003785 I915_WRITE(PF_CTL(pipe), 0);
3786 I915_WRITE(PF_WIN_POS(pipe), 0);
3787 I915_WRITE(PF_WIN_SZ(pipe), 0);
3788 }
3789}
3790
Jesse Barnes6be4a602010-09-10 10:26:01 -07003791static void ironlake_crtc_disable(struct drm_crtc *crtc)
3792{
3793 struct drm_device *dev = crtc->dev;
3794 struct drm_i915_private *dev_priv = dev->dev_private;
3795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003796 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003797 int pipe = intel_crtc->pipe;
3798 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003799 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003800
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003801
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003802 if (!intel_crtc->active)
3803 return;
3804
Daniel Vetterea9d7582012-07-10 10:42:52 +02003805 for_each_encoder_on_crtc(dev, crtc, encoder)
3806 encoder->disable(encoder);
3807
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003808 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003809 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003810
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003811 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003812 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003813
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003814 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003815 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003816 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003817
Daniel Vetterd925c592013-06-05 13:34:04 +02003818 if (intel_crtc->config.has_pch_encoder)
3819 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3820
Jesse Barnesb24e7172011-01-04 15:09:30 -08003821 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003822
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003823 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003824
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003825 for_each_encoder_on_crtc(dev, crtc, encoder)
3826 if (encoder->post_disable)
3827 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003828
Daniel Vetterd925c592013-06-05 13:34:04 +02003829 if (intel_crtc->config.has_pch_encoder) {
3830 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003831
Daniel Vetterd925c592013-06-05 13:34:04 +02003832 ironlake_disable_pch_transcoder(dev_priv, pipe);
3833 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003834
Daniel Vetterd925c592013-06-05 13:34:04 +02003835 if (HAS_PCH_CPT(dev)) {
3836 /* disable TRANS_DP_CTL */
3837 reg = TRANS_DP_CTL(pipe);
3838 temp = I915_READ(reg);
3839 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3840 TRANS_DP_PORT_SEL_MASK);
3841 temp |= TRANS_DP_PORT_SEL_NONE;
3842 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003843
Daniel Vetterd925c592013-06-05 13:34:04 +02003844 /* disable DPLL_SEL */
3845 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003846 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003847 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003848 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003849
3850 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003851 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003852
3853 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003854 }
3855
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003856 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003857 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003858
3859 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003860 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003861 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003862}
3863
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003864static void haswell_crtc_disable(struct drm_crtc *crtc)
3865{
3866 struct drm_device *dev = crtc->dev;
3867 struct drm_i915_private *dev_priv = dev->dev_private;
3868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3869 struct intel_encoder *encoder;
3870 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003871 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003872
3873 if (!intel_crtc->active)
3874 return;
3875
Ville Syrjälädda9a662013-09-19 17:00:37 -03003876 haswell_crtc_disable_planes(crtc);
3877
Jani Nikula8807e552013-08-30 19:40:32 +03003878 for_each_encoder_on_crtc(dev, crtc, encoder) {
3879 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003880 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003881 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003882
Paulo Zanoni86642812013-04-12 17:57:57 -03003883 if (intel_crtc->config.has_pch_encoder)
3884 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003885 intel_disable_pipe(dev_priv, pipe);
3886
Paulo Zanoniad80a812012-10-24 16:06:19 -02003887 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003888
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003889 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003890
Paulo Zanoni1f544382012-10-24 11:32:00 -02003891 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003892
3893 for_each_encoder_on_crtc(dev, crtc, encoder)
3894 if (encoder->post_disable)
3895 encoder->post_disable(encoder);
3896
Daniel Vetter88adfff2013-03-28 10:42:01 +01003897 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003898 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003899 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003900 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003901 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003902
3903 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003904 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003905
3906 mutex_lock(&dev->struct_mutex);
3907 intel_update_fbc(dev);
3908 mutex_unlock(&dev->struct_mutex);
3909}
3910
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003911static void ironlake_crtc_off(struct drm_crtc *crtc)
3912{
3913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003914 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003915}
3916
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003917static void haswell_crtc_off(struct drm_crtc *crtc)
3918{
3919 intel_ddi_put_crtc_pll(crtc);
3920}
3921
Daniel Vetter02e792f2009-09-15 22:57:34 +02003922static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3923{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003924 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003925 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003926 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003927
Chris Wilson23f09ce2010-08-12 13:53:37 +01003928 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003929 dev_priv->mm.interruptible = false;
3930 (void) intel_overlay_switch_off(intel_crtc->overlay);
3931 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003932 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003933 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003934
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003935 /* Let userspace switch the overlay on again. In most cases userspace
3936 * has to recompute where to put it anyway.
3937 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003938}
3939
Egbert Eich61bc95c2013-03-04 09:24:38 -05003940/**
3941 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3942 * cursor plane briefly if not already running after enabling the display
3943 * plane.
3944 * This workaround avoids occasional blank screens when self refresh is
3945 * enabled.
3946 */
3947static void
3948g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3949{
3950 u32 cntl = I915_READ(CURCNTR(pipe));
3951
3952 if ((cntl & CURSOR_MODE) == 0) {
3953 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3954
3955 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3956 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3957 intel_wait_for_vblank(dev_priv->dev, pipe);
3958 I915_WRITE(CURCNTR(pipe), cntl);
3959 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3960 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3961 }
3962}
3963
Jesse Barnes2dd24552013-04-25 12:55:01 -07003964static void i9xx_pfit_enable(struct intel_crtc *crtc)
3965{
3966 struct drm_device *dev = crtc->base.dev;
3967 struct drm_i915_private *dev_priv = dev->dev_private;
3968 struct intel_crtc_config *pipe_config = &crtc->config;
3969
Daniel Vetter328d8e82013-05-08 10:36:31 +02003970 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003971 return;
3972
Daniel Vetterc0b03412013-05-28 12:05:54 +02003973 /*
3974 * The panel fitter should only be adjusted whilst the pipe is disabled,
3975 * according to register description and PRM.
3976 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003977 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3978 assert_pipe_disabled(dev_priv, crtc->pipe);
3979
Jesse Barnesb074cec2013-04-25 12:55:02 -07003980 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3981 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003982
3983 /* Border color in case we don't scale up to the full screen. Black by
3984 * default, change to something else for debugging. */
3985 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003986}
3987
Jesse Barnes586f49d2013-11-04 16:06:59 -08003988int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003989{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003990 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003991
Jesse Barnes586f49d2013-11-04 16:06:59 -08003992 /* Obtain SKU information */
3993 mutex_lock(&dev_priv->dpio_lock);
3994 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3995 CCK_FUSE_HPLL_FREQ_MASK;
3996 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003997
Jesse Barnes586f49d2013-11-04 16:06:59 -08003998 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003999}
4000
4001/* Adjust CDclk dividers to allow high res or save power if possible */
4002static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4003{
4004 struct drm_i915_private *dev_priv = dev->dev_private;
4005 u32 val, cmd;
4006
4007 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4008 cmd = 2;
4009 else if (cdclk == 266)
4010 cmd = 1;
4011 else
4012 cmd = 0;
4013
4014 mutex_lock(&dev_priv->rps.hw_lock);
4015 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4016 val &= ~DSPFREQGUAR_MASK;
4017 val |= (cmd << DSPFREQGUAR_SHIFT);
4018 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4019 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4020 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4021 50)) {
4022 DRM_ERROR("timed out waiting for CDclk change\n");
4023 }
4024 mutex_unlock(&dev_priv->rps.hw_lock);
4025
4026 if (cdclk == 400) {
4027 u32 divider, vco;
4028
4029 vco = valleyview_get_vco(dev_priv);
4030 divider = ((vco << 1) / cdclk) - 1;
4031
4032 mutex_lock(&dev_priv->dpio_lock);
4033 /* adjust cdclk divider */
4034 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4035 val &= ~0xf;
4036 val |= divider;
4037 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4038 mutex_unlock(&dev_priv->dpio_lock);
4039 }
4040
4041 mutex_lock(&dev_priv->dpio_lock);
4042 /* adjust self-refresh exit latency value */
4043 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4044 val &= ~0x7f;
4045
4046 /*
4047 * For high bandwidth configs, we set a higher latency in the bunit
4048 * so that the core display fetch happens in time to avoid underruns.
4049 */
4050 if (cdclk == 400)
4051 val |= 4500 / 250; /* 4.5 usec */
4052 else
4053 val |= 3000 / 250; /* 3.0 usec */
4054 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4055 mutex_unlock(&dev_priv->dpio_lock);
4056
4057 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4058 intel_i2c_reset(dev);
4059}
4060
4061static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4062{
4063 int cur_cdclk, vco;
4064 int divider;
4065
4066 vco = valleyview_get_vco(dev_priv);
4067
4068 mutex_lock(&dev_priv->dpio_lock);
4069 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4070 mutex_unlock(&dev_priv->dpio_lock);
4071
4072 divider &= 0xf;
4073
4074 cur_cdclk = (vco << 1) / (divider + 1);
4075
4076 return cur_cdclk;
4077}
4078
4079static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4080 int max_pixclk)
4081{
4082 int cur_cdclk;
4083
4084 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4085
4086 /*
4087 * Really only a few cases to deal with, as only 4 CDclks are supported:
4088 * 200MHz
4089 * 267MHz
4090 * 320MHz
4091 * 400MHz
4092 * So we check to see whether we're above 90% of the lower bin and
4093 * adjust if needed.
4094 */
4095 if (max_pixclk > 288000) {
4096 return 400;
4097 } else if (max_pixclk > 240000) {
4098 return 320;
4099 } else
4100 return 266;
4101 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4102}
4103
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004104/* compute the max pixel clock for new configuration */
4105static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004106{
4107 struct drm_device *dev = dev_priv->dev;
4108 struct intel_crtc *intel_crtc;
4109 int max_pixclk = 0;
4110
4111 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4112 base.head) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004113 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004114 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004115 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004116 }
4117
4118 return max_pixclk;
4119}
4120
4121static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004122 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004123{
4124 struct drm_i915_private *dev_priv = dev->dev_private;
4125 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004126 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004127 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4128
4129 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4130 return;
4131
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004132 /* disable/enable all currently active pipes while we change cdclk */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004133 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4134 base.head)
4135 if (intel_crtc->base.enabled)
4136 *prepare_pipes |= (1 << intel_crtc->pipe);
4137}
4138
4139static void valleyview_modeset_global_resources(struct drm_device *dev)
4140{
4141 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004142 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004143 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4144 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4145
4146 if (req_cdclk != cur_cdclk)
4147 valleyview_set_cdclk(dev, req_cdclk);
4148}
4149
Jesse Barnes89b667f2013-04-18 14:51:36 -07004150static void valleyview_crtc_enable(struct drm_crtc *crtc)
4151{
4152 struct drm_device *dev = crtc->dev;
4153 struct drm_i915_private *dev_priv = dev->dev_private;
4154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4155 struct intel_encoder *encoder;
4156 int pipe = intel_crtc->pipe;
4157 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004158 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004159
4160 WARN_ON(!crtc->enabled);
4161
4162 if (intel_crtc->active)
4163 return;
4164
4165 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004166
Jesse Barnes89b667f2013-04-18 14:51:36 -07004167 for_each_encoder_on_crtc(dev, crtc, encoder)
4168 if (encoder->pre_pll_enable)
4169 encoder->pre_pll_enable(encoder);
4170
Jani Nikula23538ef2013-08-27 15:12:22 +03004171 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4172
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004173 if (!is_dsi)
4174 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004175
4176 for_each_encoder_on_crtc(dev, crtc, encoder)
4177 if (encoder->pre_enable)
4178 encoder->pre_enable(encoder);
4179
Jesse Barnes2dd24552013-04-25 12:55:01 -07004180 i9xx_pfit_enable(intel_crtc);
4181
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004182 intel_crtc_load_lut(crtc);
4183
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004184 intel_update_watermarks(crtc);
Paulo Zanoni851855d2013-12-19 19:12:29 -02004185 intel_enable_pipe(dev_priv, pipe, false, is_dsi, true);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004186 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004187 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004188 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004189 intel_crtc_update_cursor(crtc, true);
4190
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004191 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004192
4193 for_each_encoder_on_crtc(dev, crtc, encoder)
4194 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004195}
4196
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004197static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004198{
4199 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004200 struct drm_i915_private *dev_priv = dev->dev_private;
4201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004202 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004203 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004204 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004205
Daniel Vetter08a48462012-07-02 11:43:47 +02004206 WARN_ON(!crtc->enabled);
4207
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004208 if (intel_crtc->active)
4209 return;
4210
4211 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004212
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004213 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004214 if (encoder->pre_enable)
4215 encoder->pre_enable(encoder);
4216
Daniel Vetterf6736a12013-06-05 13:34:30 +02004217 i9xx_enable_pll(intel_crtc);
4218
Jesse Barnes2dd24552013-04-25 12:55:01 -07004219 i9xx_pfit_enable(intel_crtc);
4220
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004221 intel_crtc_load_lut(crtc);
4222
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004223 intel_update_watermarks(crtc);
Paulo Zanoni851855d2013-12-19 19:12:29 -02004224 intel_enable_pipe(dev_priv, pipe, false, false, true);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004225 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004226 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004227 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004228 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004229 if (IS_G4X(dev))
4230 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004231 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004232
4233 /* Give the overlay scaler a chance to enable if it's on this pipe */
4234 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004235
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004236 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004237
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004238 for_each_encoder_on_crtc(dev, crtc, encoder)
4239 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004240}
4241
Daniel Vetter87476d62013-04-11 16:29:06 +02004242static void i9xx_pfit_disable(struct intel_crtc *crtc)
4243{
4244 struct drm_device *dev = crtc->base.dev;
4245 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004246
4247 if (!crtc->config.gmch_pfit.control)
4248 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004249
4250 assert_pipe_disabled(dev_priv, crtc->pipe);
4251
Daniel Vetter328d8e82013-05-08 10:36:31 +02004252 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4253 I915_READ(PFIT_CONTROL));
4254 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004255}
4256
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004257static void i9xx_crtc_disable(struct drm_crtc *crtc)
4258{
4259 struct drm_device *dev = crtc->dev;
4260 struct drm_i915_private *dev_priv = dev->dev_private;
4261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004262 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004263 int pipe = intel_crtc->pipe;
4264 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004265
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004266 if (!intel_crtc->active)
4267 return;
4268
Daniel Vetterea9d7582012-07-10 10:42:52 +02004269 for_each_encoder_on_crtc(dev, crtc, encoder)
4270 encoder->disable(encoder);
4271
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004272 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004273 intel_crtc_wait_for_pending_flips(crtc);
4274 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004275
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004276 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004277 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004278
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004279 intel_crtc_dpms_overlay(intel_crtc, false);
4280 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004281 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004282 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004283
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004284 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08004285 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004286
Daniel Vetter87476d62013-04-11 16:29:06 +02004287 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004288
Jesse Barnes89b667f2013-04-18 14:51:36 -07004289 for_each_encoder_on_crtc(dev, crtc, encoder)
4290 if (encoder->post_disable)
4291 encoder->post_disable(encoder);
4292
Jesse Barnesf6071162013-10-01 10:41:38 -07004293 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4294 vlv_disable_pll(dev_priv, pipe);
4295 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004296 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004297
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004298 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004299 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004300
Chris Wilson6b383a72010-09-13 13:54:26 +01004301 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004302}
4303
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004304static void i9xx_crtc_off(struct drm_crtc *crtc)
4305{
4306}
4307
Daniel Vetter976f8a22012-07-08 22:34:21 +02004308static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4309 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004310{
4311 struct drm_device *dev = crtc->dev;
4312 struct drm_i915_master_private *master_priv;
4313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4314 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004315
4316 if (!dev->primary->master)
4317 return;
4318
4319 master_priv = dev->primary->master->driver_priv;
4320 if (!master_priv->sarea_priv)
4321 return;
4322
Jesse Barnes79e53942008-11-07 14:24:08 -08004323 switch (pipe) {
4324 case 0:
4325 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4326 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4327 break;
4328 case 1:
4329 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4330 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4331 break;
4332 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004333 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004334 break;
4335 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004336}
4337
Daniel Vetter976f8a22012-07-08 22:34:21 +02004338/**
4339 * Sets the power management mode of the pipe and plane.
4340 */
4341void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004342{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004343 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004344 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004345 struct intel_encoder *intel_encoder;
4346 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004347
Daniel Vetter976f8a22012-07-08 22:34:21 +02004348 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4349 enable |= intel_encoder->connectors_active;
4350
4351 if (enable)
4352 dev_priv->display.crtc_enable(crtc);
4353 else
4354 dev_priv->display.crtc_disable(crtc);
4355
4356 intel_crtc_update_sarea(crtc, enable);
4357}
4358
Daniel Vetter976f8a22012-07-08 22:34:21 +02004359static void intel_crtc_disable(struct drm_crtc *crtc)
4360{
4361 struct drm_device *dev = crtc->dev;
4362 struct drm_connector *connector;
4363 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004365
4366 /* crtc should still be enabled when we disable it. */
4367 WARN_ON(!crtc->enabled);
4368
4369 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004370 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004371 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004372 dev_priv->display.off(crtc);
4373
Chris Wilson931872f2012-01-16 23:01:13 +00004374 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004375 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004376 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004377
4378 if (crtc->fb) {
4379 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004380 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004381 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004382 crtc->fb = NULL;
4383 }
4384
4385 /* Update computed state. */
4386 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4387 if (!connector->encoder || !connector->encoder->crtc)
4388 continue;
4389
4390 if (connector->encoder->crtc != crtc)
4391 continue;
4392
4393 connector->dpms = DRM_MODE_DPMS_OFF;
4394 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004395 }
4396}
4397
Chris Wilsonea5b2132010-08-04 13:50:23 +01004398void intel_encoder_destroy(struct drm_encoder *encoder)
4399{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004400 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004401
Chris Wilsonea5b2132010-08-04 13:50:23 +01004402 drm_encoder_cleanup(encoder);
4403 kfree(intel_encoder);
4404}
4405
Damien Lespiau92373292013-08-08 22:28:57 +01004406/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004407 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4408 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004409static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004410{
4411 if (mode == DRM_MODE_DPMS_ON) {
4412 encoder->connectors_active = true;
4413
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004414 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004415 } else {
4416 encoder->connectors_active = false;
4417
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004418 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004419 }
4420}
4421
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004422/* Cross check the actual hw state with our own modeset state tracking (and it's
4423 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004424static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004425{
4426 if (connector->get_hw_state(connector)) {
4427 struct intel_encoder *encoder = connector->encoder;
4428 struct drm_crtc *crtc;
4429 bool encoder_enabled;
4430 enum pipe pipe;
4431
4432 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4433 connector->base.base.id,
4434 drm_get_connector_name(&connector->base));
4435
4436 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4437 "wrong connector dpms state\n");
4438 WARN(connector->base.encoder != &encoder->base,
4439 "active connector not linked to encoder\n");
4440 WARN(!encoder->connectors_active,
4441 "encoder->connectors_active not set\n");
4442
4443 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4444 WARN(!encoder_enabled, "encoder not enabled\n");
4445 if (WARN_ON(!encoder->base.crtc))
4446 return;
4447
4448 crtc = encoder->base.crtc;
4449
4450 WARN(!crtc->enabled, "crtc not enabled\n");
4451 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4452 WARN(pipe != to_intel_crtc(crtc)->pipe,
4453 "encoder active on the wrong pipe\n");
4454 }
4455}
4456
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004457/* Even simpler default implementation, if there's really no special case to
4458 * consider. */
4459void intel_connector_dpms(struct drm_connector *connector, int mode)
4460{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004461 /* All the simple cases only support two dpms states. */
4462 if (mode != DRM_MODE_DPMS_ON)
4463 mode = DRM_MODE_DPMS_OFF;
4464
4465 if (mode == connector->dpms)
4466 return;
4467
4468 connector->dpms = mode;
4469
4470 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004471 if (connector->encoder)
4472 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004473
Daniel Vetterb9805142012-08-31 17:37:33 +02004474 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004475}
4476
Daniel Vetterf0947c32012-07-02 13:10:34 +02004477/* Simple connector->get_hw_state implementation for encoders that support only
4478 * one connector and no cloning and hence the encoder state determines the state
4479 * of the connector. */
4480bool intel_connector_get_hw_state(struct intel_connector *connector)
4481{
Daniel Vetter24929352012-07-02 20:28:59 +02004482 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004483 struct intel_encoder *encoder = connector->encoder;
4484
4485 return encoder->get_hw_state(encoder, &pipe);
4486}
4487
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004488static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4489 struct intel_crtc_config *pipe_config)
4490{
4491 struct drm_i915_private *dev_priv = dev->dev_private;
4492 struct intel_crtc *pipe_B_crtc =
4493 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4494
4495 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4496 pipe_name(pipe), pipe_config->fdi_lanes);
4497 if (pipe_config->fdi_lanes > 4) {
4498 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4499 pipe_name(pipe), pipe_config->fdi_lanes);
4500 return false;
4501 }
4502
Paulo Zanonibafb6552013-11-02 21:07:44 -07004503 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004504 if (pipe_config->fdi_lanes > 2) {
4505 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4506 pipe_config->fdi_lanes);
4507 return false;
4508 } else {
4509 return true;
4510 }
4511 }
4512
4513 if (INTEL_INFO(dev)->num_pipes == 2)
4514 return true;
4515
4516 /* Ivybridge 3 pipe is really complicated */
4517 switch (pipe) {
4518 case PIPE_A:
4519 return true;
4520 case PIPE_B:
4521 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4522 pipe_config->fdi_lanes > 2) {
4523 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4524 pipe_name(pipe), pipe_config->fdi_lanes);
4525 return false;
4526 }
4527 return true;
4528 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004529 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004530 pipe_B_crtc->config.fdi_lanes <= 2) {
4531 if (pipe_config->fdi_lanes > 2) {
4532 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4533 pipe_name(pipe), pipe_config->fdi_lanes);
4534 return false;
4535 }
4536 } else {
4537 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4538 return false;
4539 }
4540 return true;
4541 default:
4542 BUG();
4543 }
4544}
4545
Daniel Vettere29c22c2013-02-21 00:00:16 +01004546#define RETRY 1
4547static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4548 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004549{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004550 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004551 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004552 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004553 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004554
Daniel Vettere29c22c2013-02-21 00:00:16 +01004555retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004556 /* FDI is a binary signal running at ~2.7GHz, encoding
4557 * each output octet as 10 bits. The actual frequency
4558 * is stored as a divider into a 100MHz clock, and the
4559 * mode pixel clock is stored in units of 1KHz.
4560 * Hence the bw of each lane in terms of the mode signal
4561 * is:
4562 */
4563 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4564
Damien Lespiau241bfc32013-09-25 16:45:37 +01004565 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004566
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004567 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004568 pipe_config->pipe_bpp);
4569
4570 pipe_config->fdi_lanes = lane;
4571
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004572 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004573 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004574
Daniel Vettere29c22c2013-02-21 00:00:16 +01004575 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4576 intel_crtc->pipe, pipe_config);
4577 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4578 pipe_config->pipe_bpp -= 2*3;
4579 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4580 pipe_config->pipe_bpp);
4581 needs_recompute = true;
4582 pipe_config->bw_constrained = true;
4583
4584 goto retry;
4585 }
4586
4587 if (needs_recompute)
4588 return RETRY;
4589
4590 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004591}
4592
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004593static void hsw_compute_ips_config(struct intel_crtc *crtc,
4594 struct intel_crtc_config *pipe_config)
4595{
Jani Nikulad330a952014-01-21 11:24:25 +02004596 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004597 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004598 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004599}
4600
Daniel Vettera43f6e02013-06-07 23:10:32 +02004601static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004602 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004603{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004604 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004605 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004606
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004607 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004608 if (INTEL_INFO(dev)->gen < 4) {
4609 struct drm_i915_private *dev_priv = dev->dev_private;
4610 int clock_limit =
4611 dev_priv->display.get_display_clock_speed(dev);
4612
4613 /*
4614 * Enable pixel doubling when the dot clock
4615 * is > 90% of the (display) core speed.
4616 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004617 * GDG double wide on either pipe,
4618 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004619 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004620 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004621 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004622 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004623 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004624 }
4625
Damien Lespiau241bfc32013-09-25 16:45:37 +01004626 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004627 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004628 }
Chris Wilson89749352010-09-12 18:25:19 +01004629
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004630 /*
4631 * Pipe horizontal size must be even in:
4632 * - DVO ganged mode
4633 * - LVDS dual channel mode
4634 * - Double wide pipe
4635 */
4636 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4637 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4638 pipe_config->pipe_src_w &= ~1;
4639
Damien Lespiau8693a822013-05-03 18:48:11 +01004640 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4641 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004642 */
4643 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4644 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004645 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004646
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004647 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004648 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004649 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004650 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4651 * for lvds. */
4652 pipe_config->pipe_bpp = 8*3;
4653 }
4654
Damien Lespiauf5adf942013-06-24 18:29:34 +01004655 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004656 hsw_compute_ips_config(crtc, pipe_config);
4657
4658 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4659 * clock survives for now. */
4660 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4661 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004662
Daniel Vetter877d48d2013-04-19 11:24:43 +02004663 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004664 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004665
Daniel Vettere29c22c2013-02-21 00:00:16 +01004666 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004667}
4668
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004669static int valleyview_get_display_clock_speed(struct drm_device *dev)
4670{
4671 return 400000; /* FIXME */
4672}
4673
Jesse Barnese70236a2009-09-21 10:42:27 -07004674static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004675{
Jesse Barnese70236a2009-09-21 10:42:27 -07004676 return 400000;
4677}
Jesse Barnes79e53942008-11-07 14:24:08 -08004678
Jesse Barnese70236a2009-09-21 10:42:27 -07004679static int i915_get_display_clock_speed(struct drm_device *dev)
4680{
4681 return 333000;
4682}
Jesse Barnes79e53942008-11-07 14:24:08 -08004683
Jesse Barnese70236a2009-09-21 10:42:27 -07004684static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4685{
4686 return 200000;
4687}
Jesse Barnes79e53942008-11-07 14:24:08 -08004688
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004689static int pnv_get_display_clock_speed(struct drm_device *dev)
4690{
4691 u16 gcfgc = 0;
4692
4693 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4694
4695 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4696 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4697 return 267000;
4698 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4699 return 333000;
4700 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4701 return 444000;
4702 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4703 return 200000;
4704 default:
4705 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4706 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4707 return 133000;
4708 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4709 return 167000;
4710 }
4711}
4712
Jesse Barnese70236a2009-09-21 10:42:27 -07004713static int i915gm_get_display_clock_speed(struct drm_device *dev)
4714{
4715 u16 gcfgc = 0;
4716
4717 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4718
4719 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004720 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004721 else {
4722 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4723 case GC_DISPLAY_CLOCK_333_MHZ:
4724 return 333000;
4725 default:
4726 case GC_DISPLAY_CLOCK_190_200_MHZ:
4727 return 190000;
4728 }
4729 }
4730}
Jesse Barnes79e53942008-11-07 14:24:08 -08004731
Jesse Barnese70236a2009-09-21 10:42:27 -07004732static int i865_get_display_clock_speed(struct drm_device *dev)
4733{
4734 return 266000;
4735}
4736
4737static int i855_get_display_clock_speed(struct drm_device *dev)
4738{
4739 u16 hpllcc = 0;
4740 /* Assume that the hardware is in the high speed state. This
4741 * should be the default.
4742 */
4743 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4744 case GC_CLOCK_133_200:
4745 case GC_CLOCK_100_200:
4746 return 200000;
4747 case GC_CLOCK_166_250:
4748 return 250000;
4749 case GC_CLOCK_100_133:
4750 return 133000;
4751 }
4752
4753 /* Shouldn't happen */
4754 return 0;
4755}
4756
4757static int i830_get_display_clock_speed(struct drm_device *dev)
4758{
4759 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004760}
4761
Zhenyu Wang2c072452009-06-05 15:38:42 +08004762static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004763intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004764{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004765 while (*num > DATA_LINK_M_N_MASK ||
4766 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004767 *num >>= 1;
4768 *den >>= 1;
4769 }
4770}
4771
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004772static void compute_m_n(unsigned int m, unsigned int n,
4773 uint32_t *ret_m, uint32_t *ret_n)
4774{
4775 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4776 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4777 intel_reduce_m_n_ratio(ret_m, ret_n);
4778}
4779
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004780void
4781intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4782 int pixel_clock, int link_clock,
4783 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004784{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004785 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004786
4787 compute_m_n(bits_per_pixel * pixel_clock,
4788 link_clock * nlanes * 8,
4789 &m_n->gmch_m, &m_n->gmch_n);
4790
4791 compute_m_n(pixel_clock, link_clock,
4792 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004793}
4794
Chris Wilsona7615032011-01-12 17:04:08 +00004795static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4796{
Jani Nikulad330a952014-01-21 11:24:25 +02004797 if (i915.panel_use_ssc >= 0)
4798 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004799 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004800 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004801}
4802
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004803static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4804{
4805 struct drm_device *dev = crtc->dev;
4806 struct drm_i915_private *dev_priv = dev->dev_private;
4807 int refclk;
4808
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004809 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004810 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004811 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004812 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02004813 refclk = dev_priv->vbt.lvds_ssc_freq;
4814 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004815 } else if (!IS_GEN2(dev)) {
4816 refclk = 96000;
4817 } else {
4818 refclk = 48000;
4819 }
4820
4821 return refclk;
4822}
4823
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004824static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004825{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004826 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004827}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004828
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004829static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4830{
4831 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004832}
4833
Daniel Vetterf47709a2013-03-28 10:42:02 +01004834static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004835 intel_clock_t *reduced_clock)
4836{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004837 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004838 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004839 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004840 u32 fp, fp2 = 0;
4841
4842 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004843 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004844 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004845 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004846 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004847 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004848 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004849 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004850 }
4851
4852 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004853 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004854
Daniel Vetterf47709a2013-03-28 10:42:02 +01004855 crtc->lowfreq_avail = false;
4856 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02004857 reduced_clock && i915.powersave) {
Jesse Barnesa7516a02011-12-15 12:30:37 -08004858 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004859 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004860 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004861 } else {
4862 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004863 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004864 }
4865}
4866
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004867static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4868 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004869{
4870 u32 reg_val;
4871
4872 /*
4873 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4874 * and set it to a reasonable value instead.
4875 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004876 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004877 reg_val &= 0xffffff00;
4878 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004879 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004880
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004881 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004882 reg_val &= 0x8cffffff;
4883 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004884 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004885
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004886 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004887 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004888 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004889
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004890 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004891 reg_val &= 0x00ffffff;
4892 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004893 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004894}
4895
Daniel Vetterb5518422013-05-03 11:49:48 +02004896static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4897 struct intel_link_m_n *m_n)
4898{
4899 struct drm_device *dev = crtc->base.dev;
4900 struct drm_i915_private *dev_priv = dev->dev_private;
4901 int pipe = crtc->pipe;
4902
Daniel Vettere3b95f12013-05-03 11:49:49 +02004903 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4904 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4905 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4906 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004907}
4908
4909static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4910 struct intel_link_m_n *m_n)
4911{
4912 struct drm_device *dev = crtc->base.dev;
4913 struct drm_i915_private *dev_priv = dev->dev_private;
4914 int pipe = crtc->pipe;
4915 enum transcoder transcoder = crtc->config.cpu_transcoder;
4916
4917 if (INTEL_INFO(dev)->gen >= 5) {
4918 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4919 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4920 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4921 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4922 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004923 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4924 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4925 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4926 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004927 }
4928}
4929
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004930static void intel_dp_set_m_n(struct intel_crtc *crtc)
4931{
4932 if (crtc->config.has_pch_encoder)
4933 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4934 else
4935 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4936}
4937
Daniel Vetterf47709a2013-03-28 10:42:02 +01004938static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004939{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004940 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004941 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004942 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004943 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004944 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004945 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004946
Daniel Vetter09153002012-12-12 14:06:44 +01004947 mutex_lock(&dev_priv->dpio_lock);
4948
Daniel Vetterf47709a2013-03-28 10:42:02 +01004949 bestn = crtc->config.dpll.n;
4950 bestm1 = crtc->config.dpll.m1;
4951 bestm2 = crtc->config.dpll.m2;
4952 bestp1 = crtc->config.dpll.p1;
4953 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004954
Jesse Barnes89b667f2013-04-18 14:51:36 -07004955 /* See eDP HDMI DPIO driver vbios notes doc */
4956
4957 /* PLL B needs special handling */
4958 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004959 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004960
4961 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004963
4964 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004965 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004966 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004967 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004968
4969 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004970 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004971
4972 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004973 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4974 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4975 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004976 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004977
4978 /*
4979 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4980 * but we don't support that).
4981 * Note: don't use the DAC post divider as it seems unstable.
4982 */
4983 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004984 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004985
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004986 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004988
Jesse Barnes89b667f2013-04-18 14:51:36 -07004989 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004990 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004991 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004992 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004993 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004994 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004995 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004996 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004997 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004998
Jesse Barnes89b667f2013-04-18 14:51:36 -07004999 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5000 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5001 /* Use SSC source */
5002 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005003 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005004 0x0df40000);
5005 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005006 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005007 0x0df70000);
5008 } else { /* HDMI or VGA */
5009 /* Use bend source */
5010 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005011 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005012 0x0df70000);
5013 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005014 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005015 0x0df40000);
5016 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005017
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005018 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005019 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5020 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5021 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5022 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005023 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005024
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005025 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005026
Imre Deake5cbfbf2014-01-09 17:08:16 +02005027 /*
5028 * Enable DPIO clock input. We should never disable the reference
5029 * clock for pipe B, since VGA hotplug / manual detection depends
5030 * on it.
5031 */
Jesse Barnes89b667f2013-04-18 14:51:36 -07005032 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5033 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07005034 /* We should never disable this, set it here for state tracking */
5035 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005036 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005037 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005038 crtc->config.dpll_hw_state.dpll = dpll;
5039
Daniel Vetteref1b4602013-06-01 17:17:04 +02005040 dpll_md = (crtc->config.pixel_multiplier - 1)
5041 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005042 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5043
Daniel Vetterf47709a2013-03-28 10:42:02 +01005044 if (crtc->config.has_dp_encoder)
5045 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305046
Daniel Vetter09153002012-12-12 14:06:44 +01005047 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005048}
5049
Daniel Vetterf47709a2013-03-28 10:42:02 +01005050static void i9xx_update_pll(struct intel_crtc *crtc,
5051 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005052 int num_connectors)
5053{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005054 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005055 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005056 u32 dpll;
5057 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005058 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005059
Daniel Vetterf47709a2013-03-28 10:42:02 +01005060 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305061
Daniel Vetterf47709a2013-03-28 10:42:02 +01005062 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5063 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005064
5065 dpll = DPLL_VGA_MODE_DIS;
5066
Daniel Vetterf47709a2013-03-28 10:42:02 +01005067 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005068 dpll |= DPLLB_MODE_LVDS;
5069 else
5070 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005071
Daniel Vetteref1b4602013-06-01 17:17:04 +02005072 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005073 dpll |= (crtc->config.pixel_multiplier - 1)
5074 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005075 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005076
5077 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005078 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005079
Daniel Vetterf47709a2013-03-28 10:42:02 +01005080 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005081 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005082
5083 /* compute bitmask from p1 value */
5084 if (IS_PINEVIEW(dev))
5085 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5086 else {
5087 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5088 if (IS_G4X(dev) && reduced_clock)
5089 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5090 }
5091 switch (clock->p2) {
5092 case 5:
5093 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5094 break;
5095 case 7:
5096 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5097 break;
5098 case 10:
5099 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5100 break;
5101 case 14:
5102 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5103 break;
5104 }
5105 if (INTEL_INFO(dev)->gen >= 4)
5106 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5107
Daniel Vetter09ede542013-04-30 14:01:45 +02005108 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005109 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005110 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005111 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5112 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5113 else
5114 dpll |= PLL_REF_INPUT_DREFCLK;
5115
5116 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005117 crtc->config.dpll_hw_state.dpll = dpll;
5118
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005119 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005120 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5121 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005122 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005123 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005124
5125 if (crtc->config.has_dp_encoder)
5126 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005127}
5128
Daniel Vetterf47709a2013-03-28 10:42:02 +01005129static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005130 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005131 int num_connectors)
5132{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005133 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005134 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005135 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005136 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005137
Daniel Vetterf47709a2013-03-28 10:42:02 +01005138 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305139
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005140 dpll = DPLL_VGA_MODE_DIS;
5141
Daniel Vetterf47709a2013-03-28 10:42:02 +01005142 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005143 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5144 } else {
5145 if (clock->p1 == 2)
5146 dpll |= PLL_P1_DIVIDE_BY_TWO;
5147 else
5148 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5149 if (clock->p2 == 4)
5150 dpll |= PLL_P2_DIVIDE_BY_4;
5151 }
5152
Daniel Vetter4a33e482013-07-06 12:52:05 +02005153 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5154 dpll |= DPLL_DVO_2X_MODE;
5155
Daniel Vetterf47709a2013-03-28 10:42:02 +01005156 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005157 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5158 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5159 else
5160 dpll |= PLL_REF_INPUT_DREFCLK;
5161
5162 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005163 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005164}
5165
Daniel Vetter8a654f32013-06-01 17:16:22 +02005166static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005167{
5168 struct drm_device *dev = intel_crtc->base.dev;
5169 struct drm_i915_private *dev_priv = dev->dev_private;
5170 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005171 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005172 struct drm_display_mode *adjusted_mode =
5173 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005174 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5175
5176 /* We need to be careful not to changed the adjusted mode, for otherwise
5177 * the hw state checker will get angry at the mismatch. */
5178 crtc_vtotal = adjusted_mode->crtc_vtotal;
5179 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005180
5181 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5182 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005183 crtc_vtotal -= 1;
5184 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005185 vsyncshift = adjusted_mode->crtc_hsync_start
5186 - adjusted_mode->crtc_htotal / 2;
5187 } else {
5188 vsyncshift = 0;
5189 }
5190
5191 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005192 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005193
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005194 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005195 (adjusted_mode->crtc_hdisplay - 1) |
5196 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005197 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005198 (adjusted_mode->crtc_hblank_start - 1) |
5199 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005200 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005201 (adjusted_mode->crtc_hsync_start - 1) |
5202 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5203
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005204 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005205 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005206 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005207 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005208 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005209 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005210 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005211 (adjusted_mode->crtc_vsync_start - 1) |
5212 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5213
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005214 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5215 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5216 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5217 * bits. */
5218 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5219 (pipe == PIPE_B || pipe == PIPE_C))
5220 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5221
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005222 /* pipesrc controls the size that is scaled from, which should
5223 * always be the user's requested size.
5224 */
5225 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005226 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5227 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005228}
5229
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005230static void intel_get_pipe_timings(struct intel_crtc *crtc,
5231 struct intel_crtc_config *pipe_config)
5232{
5233 struct drm_device *dev = crtc->base.dev;
5234 struct drm_i915_private *dev_priv = dev->dev_private;
5235 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5236 uint32_t tmp;
5237
5238 tmp = I915_READ(HTOTAL(cpu_transcoder));
5239 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5240 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5241 tmp = I915_READ(HBLANK(cpu_transcoder));
5242 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5243 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5244 tmp = I915_READ(HSYNC(cpu_transcoder));
5245 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5246 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5247
5248 tmp = I915_READ(VTOTAL(cpu_transcoder));
5249 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5250 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5251 tmp = I915_READ(VBLANK(cpu_transcoder));
5252 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5253 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5254 tmp = I915_READ(VSYNC(cpu_transcoder));
5255 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5256 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5257
5258 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5259 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5260 pipe_config->adjusted_mode.crtc_vtotal += 1;
5261 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5262 }
5263
5264 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005265 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5266 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5267
5268 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5269 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005270}
5271
Jesse Barnesbabea612013-06-26 18:57:38 +03005272static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5273 struct intel_crtc_config *pipe_config)
5274{
5275 struct drm_crtc *crtc = &intel_crtc->base;
5276
5277 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5278 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5279 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5280 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5281
5282 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5283 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5284 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5285 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5286
5287 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5288
Damien Lespiau241bfc32013-09-25 16:45:37 +01005289 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005290 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5291}
5292
Daniel Vetter84b046f2013-02-19 18:48:54 +01005293static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5294{
5295 struct drm_device *dev = intel_crtc->base.dev;
5296 struct drm_i915_private *dev_priv = dev->dev_private;
5297 uint32_t pipeconf;
5298
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005299 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005300
Daniel Vetter67c72a12013-09-24 11:46:14 +02005301 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5302 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5303 pipeconf |= PIPECONF_ENABLE;
5304
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005305 if (intel_crtc->config.double_wide)
5306 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005307
Daniel Vetterff9ce462013-04-24 14:57:17 +02005308 /* only g4x and later have fancy bpc/dither controls */
5309 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005310 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5311 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5312 pipeconf |= PIPECONF_DITHER_EN |
5313 PIPECONF_DITHER_TYPE_SP;
5314
5315 switch (intel_crtc->config.pipe_bpp) {
5316 case 18:
5317 pipeconf |= PIPECONF_6BPC;
5318 break;
5319 case 24:
5320 pipeconf |= PIPECONF_8BPC;
5321 break;
5322 case 30:
5323 pipeconf |= PIPECONF_10BPC;
5324 break;
5325 default:
5326 /* Case prevented by intel_choose_pipe_bpp_dither. */
5327 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005328 }
5329 }
5330
5331 if (HAS_PIPE_CXSR(dev)) {
5332 if (intel_crtc->lowfreq_avail) {
5333 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5334 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5335 } else {
5336 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005337 }
5338 }
5339
Daniel Vetter84b046f2013-02-19 18:48:54 +01005340 if (!IS_GEN2(dev) &&
5341 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5342 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5343 else
5344 pipeconf |= PIPECONF_PROGRESSIVE;
5345
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005346 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5347 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005348
Daniel Vetter84b046f2013-02-19 18:48:54 +01005349 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5350 POSTING_READ(PIPECONF(intel_crtc->pipe));
5351}
5352
Eric Anholtf564048e2011-03-30 13:01:02 -07005353static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005354 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005355 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005356{
5357 struct drm_device *dev = crtc->dev;
5358 struct drm_i915_private *dev_priv = dev->dev_private;
5359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5360 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005361 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005362 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005363 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005364 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005365 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005366 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005367 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005368 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005369 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005370
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005371 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005372 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005373 case INTEL_OUTPUT_LVDS:
5374 is_lvds = true;
5375 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005376 case INTEL_OUTPUT_DSI:
5377 is_dsi = true;
5378 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005379 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005380
Eric Anholtc751ce42010-03-25 11:48:48 -07005381 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005382 }
5383
Jani Nikulaf2335332013-09-13 11:03:09 +03005384 if (is_dsi)
5385 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005386
Jani Nikulaf2335332013-09-13 11:03:09 +03005387 if (!intel_crtc->config.clock_set) {
5388 refclk = i9xx_get_refclk(crtc, num_connectors);
5389
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005390 /*
5391 * Returns a set of divisors for the desired target clock with
5392 * the given refclk, or FALSE. The returned values represent
5393 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5394 * 2) / p1 / p2.
5395 */
5396 limit = intel_limit(crtc, refclk);
5397 ok = dev_priv->display.find_dpll(limit, crtc,
5398 intel_crtc->config.port_clock,
5399 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005400 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005401 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5402 return -EINVAL;
5403 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005404
Jani Nikulaf2335332013-09-13 11:03:09 +03005405 if (is_lvds && dev_priv->lvds_downclock_avail) {
5406 /*
5407 * Ensure we match the reduced clock's P to the target
5408 * clock. If the clocks don't match, we can't switch
5409 * the display clock by using the FP0/FP1. In such case
5410 * we will disable the LVDS downclock feature.
5411 */
5412 has_reduced_clock =
5413 dev_priv->display.find_dpll(limit, crtc,
5414 dev_priv->lvds_downclock,
5415 refclk, &clock,
5416 &reduced_clock);
5417 }
5418 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005419 intel_crtc->config.dpll.n = clock.n;
5420 intel_crtc->config.dpll.m1 = clock.m1;
5421 intel_crtc->config.dpll.m2 = clock.m2;
5422 intel_crtc->config.dpll.p1 = clock.p1;
5423 intel_crtc->config.dpll.p2 = clock.p2;
5424 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005425
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005426 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005427 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305428 has_reduced_clock ? &reduced_clock : NULL,
5429 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005430 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005431 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005432 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005433 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005434 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005435 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005436 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005437
Jani Nikulaf2335332013-09-13 11:03:09 +03005438skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005439 /* Set up the display plane register */
5440 dspcntr = DISPPLANE_GAMMA_ENABLE;
5441
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005442 if (!IS_VALLEYVIEW(dev)) {
5443 if (pipe == 0)
5444 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5445 else
5446 dspcntr |= DISPPLANE_SEL_PIPE_B;
5447 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005448
Daniel Vetter8a654f32013-06-01 17:16:22 +02005449 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005450
5451 /* pipesrc and dspsize control the size that is scaled from,
5452 * which should always be the user's requested size.
5453 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005454 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005455 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5456 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005457 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005458
Daniel Vetter84b046f2013-02-19 18:48:54 +01005459 i9xx_set_pipeconf(intel_crtc);
5460
Eric Anholtf564048e2011-03-30 13:01:02 -07005461 I915_WRITE(DSPCNTR(plane), dspcntr);
5462 POSTING_READ(DSPCNTR(plane));
5463
Daniel Vetter94352cf2012-07-05 22:51:56 +02005464 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005465
Eric Anholtf564048e2011-03-30 13:01:02 -07005466 return ret;
5467}
5468
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005469static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5470 struct intel_crtc_config *pipe_config)
5471{
5472 struct drm_device *dev = crtc->base.dev;
5473 struct drm_i915_private *dev_priv = dev->dev_private;
5474 uint32_t tmp;
5475
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02005476 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
5477 return;
5478
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005479 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005480 if (!(tmp & PFIT_ENABLE))
5481 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005482
Daniel Vetter06922822013-07-11 13:35:40 +02005483 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005484 if (INTEL_INFO(dev)->gen < 4) {
5485 if (crtc->pipe != PIPE_B)
5486 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005487 } else {
5488 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5489 return;
5490 }
5491
Daniel Vetter06922822013-07-11 13:35:40 +02005492 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005493 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5494 if (INTEL_INFO(dev)->gen < 5)
5495 pipe_config->gmch_pfit.lvds_border_bits =
5496 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5497}
5498
Jesse Barnesacbec812013-09-20 11:29:32 -07005499static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5500 struct intel_crtc_config *pipe_config)
5501{
5502 struct drm_device *dev = crtc->base.dev;
5503 struct drm_i915_private *dev_priv = dev->dev_private;
5504 int pipe = pipe_config->cpu_transcoder;
5505 intel_clock_t clock;
5506 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005507 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005508
5509 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005510 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005511 mutex_unlock(&dev_priv->dpio_lock);
5512
5513 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5514 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5515 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5516 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5517 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5518
Ville Syrjäläf6466282013-10-14 14:50:31 +03005519 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005520
Ville Syrjäläf6466282013-10-14 14:50:31 +03005521 /* clock.dot is the fast clock */
5522 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005523}
5524
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005525static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5526 struct intel_crtc_config *pipe_config)
5527{
5528 struct drm_device *dev = crtc->base.dev;
5529 struct drm_i915_private *dev_priv = dev->dev_private;
5530 uint32_t tmp;
5531
Daniel Vettere143a212013-07-04 12:01:15 +02005532 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005533 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005534
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005535 tmp = I915_READ(PIPECONF(crtc->pipe));
5536 if (!(tmp & PIPECONF_ENABLE))
5537 return false;
5538
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005539 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5540 switch (tmp & PIPECONF_BPC_MASK) {
5541 case PIPECONF_6BPC:
5542 pipe_config->pipe_bpp = 18;
5543 break;
5544 case PIPECONF_8BPC:
5545 pipe_config->pipe_bpp = 24;
5546 break;
5547 case PIPECONF_10BPC:
5548 pipe_config->pipe_bpp = 30;
5549 break;
5550 default:
5551 break;
5552 }
5553 }
5554
Ville Syrjälä282740f2013-09-04 18:30:03 +03005555 if (INTEL_INFO(dev)->gen < 4)
5556 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5557
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005558 intel_get_pipe_timings(crtc, pipe_config);
5559
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005560 i9xx_get_pfit_config(crtc, pipe_config);
5561
Daniel Vetter6c49f242013-06-06 12:45:25 +02005562 if (INTEL_INFO(dev)->gen >= 4) {
5563 tmp = I915_READ(DPLL_MD(crtc->pipe));
5564 pipe_config->pixel_multiplier =
5565 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5566 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005567 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005568 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5569 tmp = I915_READ(DPLL(crtc->pipe));
5570 pipe_config->pixel_multiplier =
5571 ((tmp & SDVO_MULTIPLIER_MASK)
5572 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5573 } else {
5574 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5575 * port and will be fixed up in the encoder->get_config
5576 * function. */
5577 pipe_config->pixel_multiplier = 1;
5578 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005579 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5580 if (!IS_VALLEYVIEW(dev)) {
5581 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5582 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005583 } else {
5584 /* Mask out read-only status bits. */
5585 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5586 DPLL_PORTC_READY_MASK |
5587 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005588 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005589
Jesse Barnesacbec812013-09-20 11:29:32 -07005590 if (IS_VALLEYVIEW(dev))
5591 vlv_crtc_clock_get(crtc, pipe_config);
5592 else
5593 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005594
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005595 return true;
5596}
5597
Paulo Zanonidde86e22012-12-01 12:04:25 -02005598static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005599{
5600 struct drm_i915_private *dev_priv = dev->dev_private;
5601 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005602 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005603 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005604 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005605 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005606 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005607 bool has_ck505 = false;
5608 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005609
5610 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005611 list_for_each_entry(encoder, &mode_config->encoder_list,
5612 base.head) {
5613 switch (encoder->type) {
5614 case INTEL_OUTPUT_LVDS:
5615 has_panel = true;
5616 has_lvds = true;
5617 break;
5618 case INTEL_OUTPUT_EDP:
5619 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005620 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005621 has_cpu_edp = true;
5622 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005623 }
5624 }
5625
Keith Packard99eb6a02011-09-26 14:29:12 -07005626 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005627 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005628 can_ssc = has_ck505;
5629 } else {
5630 has_ck505 = false;
5631 can_ssc = true;
5632 }
5633
Imre Deak2de69052013-05-08 13:14:04 +03005634 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5635 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005636
5637 /* Ironlake: try to setup display ref clock before DPLL
5638 * enabling. This is only under driver's control after
5639 * PCH B stepping, previous chipset stepping should be
5640 * ignoring this setting.
5641 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005642 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005643
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005644 /* As we must carefully and slowly disable/enable each source in turn,
5645 * compute the final state we want first and check if we need to
5646 * make any changes at all.
5647 */
5648 final = val;
5649 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005650 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005651 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005652 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005653 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5654
5655 final &= ~DREF_SSC_SOURCE_MASK;
5656 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5657 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005658
Keith Packard199e5d72011-09-22 12:01:57 -07005659 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005660 final |= DREF_SSC_SOURCE_ENABLE;
5661
5662 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5663 final |= DREF_SSC1_ENABLE;
5664
5665 if (has_cpu_edp) {
5666 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5667 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5668 else
5669 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5670 } else
5671 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5672 } else {
5673 final |= DREF_SSC_SOURCE_DISABLE;
5674 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5675 }
5676
5677 if (final == val)
5678 return;
5679
5680 /* Always enable nonspread source */
5681 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5682
5683 if (has_ck505)
5684 val |= DREF_NONSPREAD_CK505_ENABLE;
5685 else
5686 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5687
5688 if (has_panel) {
5689 val &= ~DREF_SSC_SOURCE_MASK;
5690 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005691
Keith Packard199e5d72011-09-22 12:01:57 -07005692 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005693 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005694 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005695 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005696 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005697 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005698
5699 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005700 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005701 POSTING_READ(PCH_DREF_CONTROL);
5702 udelay(200);
5703
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005704 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005705
5706 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005707 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005708 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005709 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005710 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005711 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005712 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005713 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005714 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005715 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005716
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005717 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005718 POSTING_READ(PCH_DREF_CONTROL);
5719 udelay(200);
5720 } else {
5721 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5722
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005723 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005724
5725 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005726 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005727
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005728 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005729 POSTING_READ(PCH_DREF_CONTROL);
5730 udelay(200);
5731
5732 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005733 val &= ~DREF_SSC_SOURCE_MASK;
5734 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005735
5736 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005737 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005738
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005739 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005740 POSTING_READ(PCH_DREF_CONTROL);
5741 udelay(200);
5742 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005743
5744 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005745}
5746
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005747static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005748{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005749 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005750
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005751 tmp = I915_READ(SOUTH_CHICKEN2);
5752 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5753 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005754
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005755 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5756 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5757 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005758
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005759 tmp = I915_READ(SOUTH_CHICKEN2);
5760 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5761 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005762
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005763 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5764 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5765 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005766}
5767
5768/* WaMPhyProgramming:hsw */
5769static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5770{
5771 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005772
5773 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5774 tmp &= ~(0xFF << 24);
5775 tmp |= (0x12 << 24);
5776 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5777
Paulo Zanonidde86e22012-12-01 12:04:25 -02005778 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5779 tmp |= (1 << 11);
5780 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5781
5782 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5783 tmp |= (1 << 11);
5784 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5785
Paulo Zanonidde86e22012-12-01 12:04:25 -02005786 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5787 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5788 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5789
5790 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5791 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5792 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5793
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005794 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5795 tmp &= ~(7 << 13);
5796 tmp |= (5 << 13);
5797 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005798
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005799 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5800 tmp &= ~(7 << 13);
5801 tmp |= (5 << 13);
5802 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005803
5804 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5805 tmp &= ~0xFF;
5806 tmp |= 0x1C;
5807 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5808
5809 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5810 tmp &= ~0xFF;
5811 tmp |= 0x1C;
5812 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5813
5814 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5815 tmp &= ~(0xFF << 16);
5816 tmp |= (0x1C << 16);
5817 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5818
5819 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5820 tmp &= ~(0xFF << 16);
5821 tmp |= (0x1C << 16);
5822 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5823
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005824 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5825 tmp |= (1 << 27);
5826 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005827
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005828 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5829 tmp |= (1 << 27);
5830 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005831
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005832 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5833 tmp &= ~(0xF << 28);
5834 tmp |= (4 << 28);
5835 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005836
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005837 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5838 tmp &= ~(0xF << 28);
5839 tmp |= (4 << 28);
5840 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005841}
5842
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005843/* Implements 3 different sequences from BSpec chapter "Display iCLK
5844 * Programming" based on the parameters passed:
5845 * - Sequence to enable CLKOUT_DP
5846 * - Sequence to enable CLKOUT_DP without spread
5847 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5848 */
5849static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5850 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005851{
5852 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005853 uint32_t reg, tmp;
5854
5855 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5856 with_spread = true;
5857 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5858 with_fdi, "LP PCH doesn't have FDI\n"))
5859 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005860
5861 mutex_lock(&dev_priv->dpio_lock);
5862
5863 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5864 tmp &= ~SBI_SSCCTL_DISABLE;
5865 tmp |= SBI_SSCCTL_PATHALT;
5866 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5867
5868 udelay(24);
5869
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005870 if (with_spread) {
5871 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5872 tmp &= ~SBI_SSCCTL_PATHALT;
5873 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005874
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005875 if (with_fdi) {
5876 lpt_reset_fdi_mphy(dev_priv);
5877 lpt_program_fdi_mphy(dev_priv);
5878 }
5879 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005880
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005881 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5882 SBI_GEN0 : SBI_DBUFF0;
5883 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5884 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5885 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005886
5887 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005888}
5889
Paulo Zanoni47701c32013-07-23 11:19:25 -03005890/* Sequence to disable CLKOUT_DP */
5891static void lpt_disable_clkout_dp(struct drm_device *dev)
5892{
5893 struct drm_i915_private *dev_priv = dev->dev_private;
5894 uint32_t reg, tmp;
5895
5896 mutex_lock(&dev_priv->dpio_lock);
5897
5898 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5899 SBI_GEN0 : SBI_DBUFF0;
5900 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5901 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5902 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5903
5904 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5905 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5906 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5907 tmp |= SBI_SSCCTL_PATHALT;
5908 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5909 udelay(32);
5910 }
5911 tmp |= SBI_SSCCTL_DISABLE;
5912 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5913 }
5914
5915 mutex_unlock(&dev_priv->dpio_lock);
5916}
5917
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005918static void lpt_init_pch_refclk(struct drm_device *dev)
5919{
5920 struct drm_mode_config *mode_config = &dev->mode_config;
5921 struct intel_encoder *encoder;
5922 bool has_vga = false;
5923
5924 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5925 switch (encoder->type) {
5926 case INTEL_OUTPUT_ANALOG:
5927 has_vga = true;
5928 break;
5929 }
5930 }
5931
Paulo Zanoni47701c32013-07-23 11:19:25 -03005932 if (has_vga)
5933 lpt_enable_clkout_dp(dev, true, true);
5934 else
5935 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005936}
5937
Paulo Zanonidde86e22012-12-01 12:04:25 -02005938/*
5939 * Initialize reference clocks when the driver loads
5940 */
5941void intel_init_pch_refclk(struct drm_device *dev)
5942{
5943 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5944 ironlake_init_pch_refclk(dev);
5945 else if (HAS_PCH_LPT(dev))
5946 lpt_init_pch_refclk(dev);
5947}
5948
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005949static int ironlake_get_refclk(struct drm_crtc *crtc)
5950{
5951 struct drm_device *dev = crtc->dev;
5952 struct drm_i915_private *dev_priv = dev->dev_private;
5953 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005954 int num_connectors = 0;
5955 bool is_lvds = false;
5956
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005957 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005958 switch (encoder->type) {
5959 case INTEL_OUTPUT_LVDS:
5960 is_lvds = true;
5961 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005962 }
5963 num_connectors++;
5964 }
5965
5966 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005967 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005968 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005969 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005970 }
5971
5972 return 120000;
5973}
5974
Daniel Vetter6ff93602013-04-19 11:24:36 +02005975static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005976{
5977 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5979 int pipe = intel_crtc->pipe;
5980 uint32_t val;
5981
Daniel Vetter78114072013-06-13 00:54:57 +02005982 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005983
Daniel Vetter965e0c42013-03-27 00:44:57 +01005984 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005985 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005986 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005987 break;
5988 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005989 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005990 break;
5991 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005992 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005993 break;
5994 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005995 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005996 break;
5997 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005998 /* Case prevented by intel_choose_pipe_bpp_dither. */
5999 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006000 }
6001
Daniel Vetterd8b32242013-04-25 17:54:44 +02006002 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006003 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6004
Daniel Vetter6ff93602013-04-19 11:24:36 +02006005 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006006 val |= PIPECONF_INTERLACED_ILK;
6007 else
6008 val |= PIPECONF_PROGRESSIVE;
6009
Daniel Vetter50f3b012013-03-27 00:44:56 +01006010 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006011 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006012
Paulo Zanonic8203562012-09-12 10:06:29 -03006013 I915_WRITE(PIPECONF(pipe), val);
6014 POSTING_READ(PIPECONF(pipe));
6015}
6016
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006017/*
6018 * Set up the pipe CSC unit.
6019 *
6020 * Currently only full range RGB to limited range RGB conversion
6021 * is supported, but eventually this should handle various
6022 * RGB<->YCbCr scenarios as well.
6023 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006024static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006025{
6026 struct drm_device *dev = crtc->dev;
6027 struct drm_i915_private *dev_priv = dev->dev_private;
6028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6029 int pipe = intel_crtc->pipe;
6030 uint16_t coeff = 0x7800; /* 1.0 */
6031
6032 /*
6033 * TODO: Check what kind of values actually come out of the pipe
6034 * with these coeff/postoff values and adjust to get the best
6035 * accuracy. Perhaps we even need to take the bpc value into
6036 * consideration.
6037 */
6038
Daniel Vetter50f3b012013-03-27 00:44:56 +01006039 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006040 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6041
6042 /*
6043 * GY/GU and RY/RU should be the other way around according
6044 * to BSpec, but reality doesn't agree. Just set them up in
6045 * a way that results in the correct picture.
6046 */
6047 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6048 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6049
6050 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6051 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6052
6053 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6054 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6055
6056 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6057 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6058 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6059
6060 if (INTEL_INFO(dev)->gen > 6) {
6061 uint16_t postoff = 0;
6062
Daniel Vetter50f3b012013-03-27 00:44:56 +01006063 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006064 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006065
6066 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6067 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6068 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6069
6070 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6071 } else {
6072 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6073
Daniel Vetter50f3b012013-03-27 00:44:56 +01006074 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006075 mode |= CSC_BLACK_SCREEN_OFFSET;
6076
6077 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6078 }
6079}
6080
Daniel Vetter6ff93602013-04-19 11:24:36 +02006081static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006082{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006083 struct drm_device *dev = crtc->dev;
6084 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006086 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006087 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006088 uint32_t val;
6089
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006090 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006091
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006092 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006093 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6094
Daniel Vetter6ff93602013-04-19 11:24:36 +02006095 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006096 val |= PIPECONF_INTERLACED_ILK;
6097 else
6098 val |= PIPECONF_PROGRESSIVE;
6099
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006100 I915_WRITE(PIPECONF(cpu_transcoder), val);
6101 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006102
6103 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6104 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006105
6106 if (IS_BROADWELL(dev)) {
6107 val = 0;
6108
6109 switch (intel_crtc->config.pipe_bpp) {
6110 case 18:
6111 val |= PIPEMISC_DITHER_6_BPC;
6112 break;
6113 case 24:
6114 val |= PIPEMISC_DITHER_8_BPC;
6115 break;
6116 case 30:
6117 val |= PIPEMISC_DITHER_10_BPC;
6118 break;
6119 case 36:
6120 val |= PIPEMISC_DITHER_12_BPC;
6121 break;
6122 default:
6123 /* Case prevented by pipe_config_set_bpp. */
6124 BUG();
6125 }
6126
6127 if (intel_crtc->config.dither)
6128 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6129
6130 I915_WRITE(PIPEMISC(pipe), val);
6131 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006132}
6133
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006134static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006135 intel_clock_t *clock,
6136 bool *has_reduced_clock,
6137 intel_clock_t *reduced_clock)
6138{
6139 struct drm_device *dev = crtc->dev;
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141 struct intel_encoder *intel_encoder;
6142 int refclk;
6143 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006144 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006145
6146 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6147 switch (intel_encoder->type) {
6148 case INTEL_OUTPUT_LVDS:
6149 is_lvds = true;
6150 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006151 }
6152 }
6153
6154 refclk = ironlake_get_refclk(crtc);
6155
6156 /*
6157 * Returns a set of divisors for the desired target clock with the given
6158 * refclk, or FALSE. The returned values represent the clock equation:
6159 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6160 */
6161 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006162 ret = dev_priv->display.find_dpll(limit, crtc,
6163 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006164 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006165 if (!ret)
6166 return false;
6167
6168 if (is_lvds && dev_priv->lvds_downclock_avail) {
6169 /*
6170 * Ensure we match the reduced clock's P to the target clock.
6171 * If the clocks don't match, we can't switch the display clock
6172 * by using the FP0/FP1. In such case we will disable the LVDS
6173 * downclock feature.
6174 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006175 *has_reduced_clock =
6176 dev_priv->display.find_dpll(limit, crtc,
6177 dev_priv->lvds_downclock,
6178 refclk, clock,
6179 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006180 }
6181
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006182 return true;
6183}
6184
Paulo Zanonid4b19312012-11-29 11:29:32 -02006185int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6186{
6187 /*
6188 * Account for spread spectrum to avoid
6189 * oversubscribing the link. Max center spread
6190 * is 2.5%; use 5% for safety's sake.
6191 */
6192 u32 bps = target_clock * bpp * 21 / 20;
6193 return bps / (link_bw * 8) + 1;
6194}
6195
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006196static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006197{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006198 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006199}
6200
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006201static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006202 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006203 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006204{
6205 struct drm_crtc *crtc = &intel_crtc->base;
6206 struct drm_device *dev = crtc->dev;
6207 struct drm_i915_private *dev_priv = dev->dev_private;
6208 struct intel_encoder *intel_encoder;
6209 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006210 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006211 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006212
6213 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6214 switch (intel_encoder->type) {
6215 case INTEL_OUTPUT_LVDS:
6216 is_lvds = true;
6217 break;
6218 case INTEL_OUTPUT_SDVO:
6219 case INTEL_OUTPUT_HDMI:
6220 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006221 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006222 }
6223
6224 num_connectors++;
6225 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006226
Chris Wilsonc1858122010-12-03 21:35:48 +00006227 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006228 factor = 21;
6229 if (is_lvds) {
6230 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006231 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006232 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006233 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006234 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006235 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006236
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006237 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006238 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006239
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006240 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6241 *fp2 |= FP_CB_TUNE;
6242
Chris Wilson5eddb702010-09-11 13:48:45 +01006243 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006244
Eric Anholta07d6782011-03-30 13:01:08 -07006245 if (is_lvds)
6246 dpll |= DPLLB_MODE_LVDS;
6247 else
6248 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006249
Daniel Vetteref1b4602013-06-01 17:17:04 +02006250 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6251 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006252
6253 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006254 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006255 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006256 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006257
Eric Anholta07d6782011-03-30 13:01:08 -07006258 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006259 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006260 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006261 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006262
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006263 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006264 case 5:
6265 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6266 break;
6267 case 7:
6268 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6269 break;
6270 case 10:
6271 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6272 break;
6273 case 14:
6274 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6275 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006276 }
6277
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006278 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006279 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006280 else
6281 dpll |= PLL_REF_INPUT_DREFCLK;
6282
Daniel Vetter959e16d2013-06-05 13:34:21 +02006283 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006284}
6285
Jesse Barnes79e53942008-11-07 14:24:08 -08006286static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006287 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006288 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006289{
6290 struct drm_device *dev = crtc->dev;
6291 struct drm_i915_private *dev_priv = dev->dev_private;
6292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6293 int pipe = intel_crtc->pipe;
6294 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006295 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006296 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006297 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006298 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006299 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006300 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006301 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006302 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006303
6304 for_each_encoder_on_crtc(dev, crtc, encoder) {
6305 switch (encoder->type) {
6306 case INTEL_OUTPUT_LVDS:
6307 is_lvds = true;
6308 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006309 }
6310
6311 num_connectors++;
6312 }
6313
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006314 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6315 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6316
Daniel Vetterff9a6752013-06-01 17:16:21 +02006317 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006318 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006319 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006320 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6321 return -EINVAL;
6322 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006323 /* Compat-code for transition, will disappear. */
6324 if (!intel_crtc->config.clock_set) {
6325 intel_crtc->config.dpll.n = clock.n;
6326 intel_crtc->config.dpll.m1 = clock.m1;
6327 intel_crtc->config.dpll.m2 = clock.m2;
6328 intel_crtc->config.dpll.p1 = clock.p1;
6329 intel_crtc->config.dpll.p2 = clock.p2;
6330 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006331
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006332 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006333 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006334 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006335 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006336 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006337
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006338 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006339 &fp, &reduced_clock,
6340 has_reduced_clock ? &fp2 : NULL);
6341
Daniel Vetter959e16d2013-06-05 13:34:21 +02006342 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006343 intel_crtc->config.dpll_hw_state.fp0 = fp;
6344 if (has_reduced_clock)
6345 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6346 else
6347 intel_crtc->config.dpll_hw_state.fp1 = fp;
6348
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006349 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006350 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006351 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6352 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006353 return -EINVAL;
6354 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006355 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006356 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006357
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006358 if (intel_crtc->config.has_dp_encoder)
6359 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006360
Jani Nikulad330a952014-01-21 11:24:25 +02006361 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006362 intel_crtc->lowfreq_avail = true;
6363 else
6364 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006365
Daniel Vetter8a654f32013-06-01 17:16:22 +02006366 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006367
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006368 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006369 intel_cpu_transcoder_set_m_n(intel_crtc,
6370 &intel_crtc->config.fdi_m_n);
6371 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006372
Daniel Vetter6ff93602013-04-19 11:24:36 +02006373 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006374
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006375 /* Set up the display plane register */
6376 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006377 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006378
Daniel Vetter94352cf2012-07-05 22:51:56 +02006379 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006380
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006381 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006382}
6383
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006384static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6385 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006386{
6387 struct drm_device *dev = crtc->base.dev;
6388 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006389 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006390
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006391 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6392 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6393 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6394 & ~TU_SIZE_MASK;
6395 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6396 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6397 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6398}
6399
6400static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6401 enum transcoder transcoder,
6402 struct intel_link_m_n *m_n)
6403{
6404 struct drm_device *dev = crtc->base.dev;
6405 struct drm_i915_private *dev_priv = dev->dev_private;
6406 enum pipe pipe = crtc->pipe;
6407
6408 if (INTEL_INFO(dev)->gen >= 5) {
6409 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6410 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6411 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6412 & ~TU_SIZE_MASK;
6413 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6414 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6415 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6416 } else {
6417 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6418 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6419 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6420 & ~TU_SIZE_MASK;
6421 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6422 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6423 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6424 }
6425}
6426
6427void intel_dp_get_m_n(struct intel_crtc *crtc,
6428 struct intel_crtc_config *pipe_config)
6429{
6430 if (crtc->config.has_pch_encoder)
6431 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6432 else
6433 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6434 &pipe_config->dp_m_n);
6435}
6436
Daniel Vetter72419202013-04-04 13:28:53 +02006437static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6438 struct intel_crtc_config *pipe_config)
6439{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006440 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6441 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006442}
6443
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006444static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6445 struct intel_crtc_config *pipe_config)
6446{
6447 struct drm_device *dev = crtc->base.dev;
6448 struct drm_i915_private *dev_priv = dev->dev_private;
6449 uint32_t tmp;
6450
6451 tmp = I915_READ(PF_CTL(crtc->pipe));
6452
6453 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006454 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006455 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6456 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006457
6458 /* We currently do not free assignements of panel fitters on
6459 * ivb/hsw (since we don't use the higher upscaling modes which
6460 * differentiates them) so just WARN about this case for now. */
6461 if (IS_GEN7(dev)) {
6462 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6463 PF_PIPE_SEL_IVB(crtc->pipe));
6464 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006465 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006466}
6467
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006468static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6469 struct intel_crtc_config *pipe_config)
6470{
6471 struct drm_device *dev = crtc->base.dev;
6472 struct drm_i915_private *dev_priv = dev->dev_private;
6473 uint32_t tmp;
6474
Daniel Vettere143a212013-07-04 12:01:15 +02006475 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006476 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006477
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006478 tmp = I915_READ(PIPECONF(crtc->pipe));
6479 if (!(tmp & PIPECONF_ENABLE))
6480 return false;
6481
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006482 switch (tmp & PIPECONF_BPC_MASK) {
6483 case PIPECONF_6BPC:
6484 pipe_config->pipe_bpp = 18;
6485 break;
6486 case PIPECONF_8BPC:
6487 pipe_config->pipe_bpp = 24;
6488 break;
6489 case PIPECONF_10BPC:
6490 pipe_config->pipe_bpp = 30;
6491 break;
6492 case PIPECONF_12BPC:
6493 pipe_config->pipe_bpp = 36;
6494 break;
6495 default:
6496 break;
6497 }
6498
Daniel Vetterab9412b2013-05-03 11:49:46 +02006499 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006500 struct intel_shared_dpll *pll;
6501
Daniel Vetter88adfff2013-03-28 10:42:01 +01006502 pipe_config->has_pch_encoder = true;
6503
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006504 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6505 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6506 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006507
6508 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006509
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006510 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006511 pipe_config->shared_dpll =
6512 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006513 } else {
6514 tmp = I915_READ(PCH_DPLL_SEL);
6515 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6516 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6517 else
6518 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6519 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006520
6521 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6522
6523 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6524 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006525
6526 tmp = pipe_config->dpll_hw_state.dpll;
6527 pipe_config->pixel_multiplier =
6528 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6529 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006530
6531 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006532 } else {
6533 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006534 }
6535
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006536 intel_get_pipe_timings(crtc, pipe_config);
6537
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006538 ironlake_get_pfit_config(crtc, pipe_config);
6539
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006540 return true;
6541}
6542
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006543static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6544{
6545 struct drm_device *dev = dev_priv->dev;
6546 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6547 struct intel_crtc *crtc;
6548 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006549 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006550
6551 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
Paulo Zanoni798183c2013-12-06 20:29:01 -02006552 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006553 pipe_name(crtc->pipe));
6554
6555 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6556 WARN(plls->spll_refcount, "SPLL enabled\n");
6557 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6558 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6559 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6560 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6561 "CPU PWM1 enabled\n");
6562 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6563 "CPU PWM2 enabled\n");
6564 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6565 "PCH PWM1 enabled\n");
6566 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6567 "Utility pin enabled\n");
6568 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6569
6570 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6571 val = I915_READ(DEIMR);
Paulo Zanoni6806e632013-11-21 13:47:24 -02006572 WARN((val | DE_PCH_EVENT_IVB) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006573 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6574 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006575 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006576 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6577 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6578}
6579
6580/*
6581 * This function implements pieces of two sequences from BSpec:
6582 * - Sequence for display software to disable LCPLL
6583 * - Sequence for display software to allow package C8+
6584 * The steps implemented here are just the steps that actually touch the LCPLL
6585 * register. Callers should take care of disabling all the display engine
6586 * functions, doing the mode unset, fixing interrupts, etc.
6587 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006588static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6589 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006590{
6591 uint32_t val;
6592
6593 assert_can_disable_lcpll(dev_priv);
6594
6595 val = I915_READ(LCPLL_CTL);
6596
6597 if (switch_to_fclk) {
6598 val |= LCPLL_CD_SOURCE_FCLK;
6599 I915_WRITE(LCPLL_CTL, val);
6600
6601 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6602 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6603 DRM_ERROR("Switching to FCLK failed\n");
6604
6605 val = I915_READ(LCPLL_CTL);
6606 }
6607
6608 val |= LCPLL_PLL_DISABLE;
6609 I915_WRITE(LCPLL_CTL, val);
6610 POSTING_READ(LCPLL_CTL);
6611
6612 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6613 DRM_ERROR("LCPLL still locked\n");
6614
6615 val = I915_READ(D_COMP);
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 disable D_COMP\n");
6620 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006621 POSTING_READ(D_COMP);
6622 ndelay(100);
6623
6624 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6625 DRM_ERROR("D_COMP RCOMP still in progress\n");
6626
6627 if (allow_power_down) {
6628 val = I915_READ(LCPLL_CTL);
6629 val |= LCPLL_POWER_DOWN_ALLOW;
6630 I915_WRITE(LCPLL_CTL, val);
6631 POSTING_READ(LCPLL_CTL);
6632 }
6633}
6634
6635/*
6636 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6637 * source.
6638 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006639static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006640{
6641 uint32_t val;
6642
6643 val = I915_READ(LCPLL_CTL);
6644
6645 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6646 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6647 return;
6648
Paulo Zanoni215733f2013-08-19 13:18:07 -03006649 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6650 * we'll hang the machine! */
Daniel Vetter0d9d3492014-01-16 22:06:30 +01006651 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03006652
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006653 if (val & LCPLL_POWER_DOWN_ALLOW) {
6654 val &= ~LCPLL_POWER_DOWN_ALLOW;
6655 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006656 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006657 }
6658
6659 val = I915_READ(D_COMP);
6660 val |= D_COMP_COMP_FORCE;
6661 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006662 mutex_lock(&dev_priv->rps.hw_lock);
6663 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6664 DRM_ERROR("Failed to enable D_COMP\n");
6665 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006666 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006667
6668 val = I915_READ(LCPLL_CTL);
6669 val &= ~LCPLL_PLL_DISABLE;
6670 I915_WRITE(LCPLL_CTL, val);
6671
6672 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6673 DRM_ERROR("LCPLL not locked yet\n");
6674
6675 if (val & LCPLL_CD_SOURCE_FCLK) {
6676 val = I915_READ(LCPLL_CTL);
6677 val &= ~LCPLL_CD_SOURCE_FCLK;
6678 I915_WRITE(LCPLL_CTL, val);
6679
6680 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6681 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6682 DRM_ERROR("Switching back to LCPLL failed\n");
6683 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006684
Daniel Vetter0d9d3492014-01-16 22:06:30 +01006685 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006686}
6687
Paulo Zanonic67a4702013-08-19 13:18:09 -03006688void hsw_enable_pc8_work(struct work_struct *__work)
6689{
6690 struct drm_i915_private *dev_priv =
6691 container_of(to_delayed_work(__work), struct drm_i915_private,
6692 pc8.enable_work);
6693 struct drm_device *dev = dev_priv->dev;
6694 uint32_t val;
6695
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006696 WARN_ON(!HAS_PC8(dev));
6697
Paulo Zanonic67a4702013-08-19 13:18:09 -03006698 if (dev_priv->pc8.enabled)
6699 return;
6700
6701 DRM_DEBUG_KMS("Enabling package C8+\n");
6702
6703 dev_priv->pc8.enabled = true;
6704
6705 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6706 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6707 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6708 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6709 }
6710
6711 lpt_disable_clkout_dp(dev);
6712 hsw_pc8_disable_interrupts(dev);
6713 hsw_disable_lcpll(dev_priv, true, true);
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006714
6715 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006716}
6717
6718static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6719{
6720 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6721 WARN(dev_priv->pc8.disable_count < 1,
6722 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6723
6724 dev_priv->pc8.disable_count--;
6725 if (dev_priv->pc8.disable_count != 0)
6726 return;
6727
6728 schedule_delayed_work(&dev_priv->pc8.enable_work,
Jani Nikulad330a952014-01-21 11:24:25 +02006729 msecs_to_jiffies(i915.pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006730}
6731
6732static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6733{
6734 struct drm_device *dev = dev_priv->dev;
6735 uint32_t val;
6736
6737 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6738 WARN(dev_priv->pc8.disable_count < 0,
6739 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6740
6741 dev_priv->pc8.disable_count++;
6742 if (dev_priv->pc8.disable_count != 1)
6743 return;
6744
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006745 WARN_ON(!HAS_PC8(dev));
6746
Paulo Zanonic67a4702013-08-19 13:18:09 -03006747 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6748 if (!dev_priv->pc8.enabled)
6749 return;
6750
6751 DRM_DEBUG_KMS("Disabling package C8+\n");
6752
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006753 intel_runtime_pm_get(dev_priv);
6754
Paulo Zanonic67a4702013-08-19 13:18:09 -03006755 hsw_restore_lcpll(dev_priv);
6756 hsw_pc8_restore_interrupts(dev);
6757 lpt_init_pch_refclk(dev);
6758
6759 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6760 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6761 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6762 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6763 }
6764
6765 intel_prepare_ddi(dev);
6766 i915_gem_init_swizzling(dev);
6767 mutex_lock(&dev_priv->rps.hw_lock);
6768 gen6_update_ring_freq(dev);
6769 mutex_unlock(&dev_priv->rps.hw_lock);
6770 dev_priv->pc8.enabled = false;
6771}
6772
6773void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6774{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006775 if (!HAS_PC8(dev_priv->dev))
6776 return;
6777
Paulo Zanonic67a4702013-08-19 13:18:09 -03006778 mutex_lock(&dev_priv->pc8.lock);
6779 __hsw_enable_package_c8(dev_priv);
6780 mutex_unlock(&dev_priv->pc8.lock);
6781}
6782
6783void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6784{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006785 if (!HAS_PC8(dev_priv->dev))
6786 return;
6787
Paulo Zanonic67a4702013-08-19 13:18:09 -03006788 mutex_lock(&dev_priv->pc8.lock);
6789 __hsw_disable_package_c8(dev_priv);
6790 mutex_unlock(&dev_priv->pc8.lock);
6791}
6792
6793static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6794{
6795 struct drm_device *dev = dev_priv->dev;
6796 struct intel_crtc *crtc;
6797 uint32_t val;
6798
6799 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6800 if (crtc->base.enabled)
6801 return false;
6802
6803 /* This case is still possible since we have the i915.disable_power_well
6804 * parameter and also the KVMr or something else might be requesting the
6805 * power well. */
6806 val = I915_READ(HSW_PWR_WELL_DRIVER);
6807 if (val != 0) {
6808 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6809 return false;
6810 }
6811
6812 return true;
6813}
6814
6815/* Since we're called from modeset_global_resources there's no way to
6816 * symmetrically increase and decrease the refcount, so we use
6817 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6818 * or not.
6819 */
6820static void hsw_update_package_c8(struct drm_device *dev)
6821{
6822 struct drm_i915_private *dev_priv = dev->dev_private;
6823 bool allow;
6824
Chris Wilson7c6c2652013-11-18 18:32:37 -08006825 if (!HAS_PC8(dev_priv->dev))
6826 return;
6827
Jani Nikulad330a952014-01-21 11:24:25 +02006828 if (!i915.enable_pc8)
Paulo Zanonic67a4702013-08-19 13:18:09 -03006829 return;
6830
6831 mutex_lock(&dev_priv->pc8.lock);
6832
6833 allow = hsw_can_enable_package_c8(dev_priv);
6834
6835 if (allow == dev_priv->pc8.requirements_met)
6836 goto done;
6837
6838 dev_priv->pc8.requirements_met = allow;
6839
6840 if (allow)
6841 __hsw_enable_package_c8(dev_priv);
6842 else
6843 __hsw_disable_package_c8(dev_priv);
6844
6845done:
6846 mutex_unlock(&dev_priv->pc8.lock);
6847}
6848
6849static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6850{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006851 if (!HAS_PC8(dev_priv->dev))
6852 return;
6853
Chris Wilson34581222013-11-18 18:32:36 -08006854 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006855 if (!dev_priv->pc8.gpu_idle) {
6856 dev_priv->pc8.gpu_idle = true;
Chris Wilson34581222013-11-18 18:32:36 -08006857 __hsw_enable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006858 }
Chris Wilson34581222013-11-18 18:32:36 -08006859 mutex_unlock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006860}
6861
6862static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6863{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006864 if (!HAS_PC8(dev_priv->dev))
6865 return;
6866
Chris Wilson34581222013-11-18 18:32:36 -08006867 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006868 if (dev_priv->pc8.gpu_idle) {
6869 dev_priv->pc8.gpu_idle = false;
Chris Wilson34581222013-11-18 18:32:36 -08006870 __hsw_disable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006871 }
Chris Wilson34581222013-11-18 18:32:36 -08006872 mutex_unlock(&dev_priv->pc8.lock);
Daniel Vetter94352cf2012-07-05 22:51:56 +02006873}
Eric Anholtf564048e2011-03-30 13:01:02 -07006874
Imre Deak6efdf352013-10-16 17:25:52 +03006875#define for_each_power_domain(domain, mask) \
6876 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6877 if ((1 << (domain)) & (mask))
6878
6879static unsigned long get_pipe_power_domains(struct drm_device *dev,
6880 enum pipe pipe, bool pfit_enabled)
6881{
6882 unsigned long mask;
6883 enum transcoder transcoder;
6884
6885 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6886
6887 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6888 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6889 if (pfit_enabled)
6890 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6891
6892 return mask;
6893}
6894
Imre Deakbaa70702013-10-25 17:36:48 +03006895void intel_display_set_init_power(struct drm_device *dev, bool enable)
6896{
6897 struct drm_i915_private *dev_priv = dev->dev_private;
6898
6899 if (dev_priv->power_domains.init_power_on == enable)
6900 return;
6901
6902 if (enable)
6903 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6904 else
6905 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6906
6907 dev_priv->power_domains.init_power_on = enable;
6908}
6909
Imre Deak4f074122013-10-16 17:25:51 +03006910static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006911{
Imre Deak6efdf352013-10-16 17:25:52 +03006912 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006913 struct intel_crtc *crtc;
6914
Imre Deak6efdf352013-10-16 17:25:52 +03006915 /*
6916 * First get all needed power domains, then put all unneeded, to avoid
6917 * any unnecessary toggling of the power wells.
6918 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006919 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006920 enum intel_display_power_domain domain;
6921
Jesse Barnes79e53942008-11-07 14:24:08 -08006922 if (!crtc->base.enabled)
6923 continue;
6924
Imre Deak6efdf352013-10-16 17:25:52 +03006925 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6926 crtc->pipe,
6927 crtc->config.pch_pfit.enabled);
6928
6929 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6930 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006931 }
6932
Imre Deak6efdf352013-10-16 17:25:52 +03006933 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6934 enum intel_display_power_domain domain;
6935
6936 for_each_power_domain(domain, crtc->enabled_power_domains)
6937 intel_display_power_put(dev, domain);
6938
6939 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6940 }
Imre Deakbaa70702013-10-25 17:36:48 +03006941
6942 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006943}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006944
Imre Deak4f074122013-10-16 17:25:51 +03006945static void haswell_modeset_global_resources(struct drm_device *dev)
6946{
6947 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006948 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006949}
6950
6951static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6952 int x, int y,
6953 struct drm_framebuffer *fb)
6954{
6955 struct drm_device *dev = crtc->dev;
6956 struct drm_i915_private *dev_priv = dev->dev_private;
6957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6958 int plane = intel_crtc->plane;
6959 int ret;
6960
Paulo Zanoni566b7342013-11-25 15:27:08 -02006961 if (!intel_ddi_pll_select(intel_crtc))
Chris Wilson560b85b2010-08-07 11:01:38 +01006962 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02006963 intel_ddi_pll_enable(intel_crtc);
Chris Wilson560b85b2010-08-07 11:01:38 +01006964
Chris Wilson560b85b2010-08-07 11:01:38 +01006965 if (intel_crtc->config.has_dp_encoder)
6966 intel_dp_set_m_n(intel_crtc);
6967
6968 intel_crtc->lowfreq_avail = false;
6969
6970 intel_set_pipe_timings(intel_crtc);
6971
6972 if (intel_crtc->config.has_pch_encoder) {
6973 intel_cpu_transcoder_set_m_n(intel_crtc,
6974 &intel_crtc->config.fdi_m_n);
6975 }
6976
6977 haswell_set_pipeconf(crtc);
6978
6979 intel_set_pipe_csc(crtc);
6980
6981 /* Set up the display plane register */
6982 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6983 POSTING_READ(DSPCNTR(plane));
6984
6985 ret = intel_pipe_set_base(crtc, x, y, fb);
6986
Chris Wilson560b85b2010-08-07 11:01:38 +01006987 return ret;
6988}
6989
6990static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6991 struct intel_crtc_config *pipe_config)
6992{
6993 struct drm_device *dev = crtc->base.dev;
6994 struct drm_i915_private *dev_priv = dev->dev_private;
6995 enum intel_display_power_domain pfit_domain;
6996 uint32_t tmp;
6997
6998 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6999 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7000
7001 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7002 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7003 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007004 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01007005 default:
7006 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007007 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7008 case TRANS_DDI_EDP_INPUT_A_ON:
7009 trans_edp_pipe = PIPE_A;
7010 break;
7011 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7012 trans_edp_pipe = PIPE_B;
7013 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01007014 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007015 trans_edp_pipe = PIPE_C;
7016 break;
7017 }
7018
Chris Wilson6b383a72010-09-13 13:54:26 +01007019 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007020 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7021 }
7022
7023 if (!intel_display_power_enabled(dev,
7024 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
7025 return false;
7026
7027 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
7028 if (!(tmp & PIPECONF_ENABLE))
7029 return false;
7030
7031 /*
7032 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7033 * DDI E. So just check whether this pipe is wired to DDI E and whether
7034 * the PCH transcoder is on.
7035 */
7036 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7037 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
7038 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7039 pipe_config->has_pch_encoder = true;
7040
7041 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7042 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7043 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7044
7045 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7046 }
7047
Chris Wilson560b85b2010-08-07 11:01:38 +01007048 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007049
7050 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
7051 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01007052 ironlake_get_pfit_config(crtc, pipe_config);
7053
Jesse Barnese59150d2014-01-07 13:30:45 -08007054 if (IS_HASWELL(dev))
7055 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7056 (I915_READ(IPS_CTL) & IPS_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08007057
7058 pipe_config->pixel_multiplier = 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07007059
7060 return true;
7061}
7062
7063static int intel_crtc_mode_set(struct drm_crtc *crtc,
7064 int x, int y,
7065 struct drm_framebuffer *fb)
7066{
Eric Anholt0b701d22011-03-30 13:01:03 -07007067 struct drm_device *dev = crtc->dev;
7068 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01007069 struct intel_encoder *encoder;
Eric Anholtf564048e2011-03-30 13:01:02 -07007070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007071 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholtf564048e2011-03-30 13:01:02 -07007072 int pipe = intel_crtc->pipe;
7073 int ret;
7074
Eric Anholt0b701d22011-03-30 13:01:03 -07007075 drm_vblank_pre_modeset(dev, pipe);
7076
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007077 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7078
Jesse Barnes79e53942008-11-07 14:24:08 -08007079 drm_vblank_post_modeset(dev, pipe);
7080
Daniel Vetter9256aa12012-10-31 19:26:13 +01007081 if (ret != 0)
7082 return ret;
7083
7084 for_each_encoder_on_crtc(dev, crtc, encoder) {
7085 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7086 encoder->base.base.id,
7087 drm_get_encoder_name(&encoder->base),
7088 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02007089 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007090 }
7091
7092 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007093}
7094
Jani Nikula1a915102013-10-16 12:34:48 +03007095static struct {
7096 int clock;
7097 u32 config;
7098} hdmi_audio_clock[] = {
7099 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7100 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7101 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7102 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7103 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7104 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7105 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7106 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7107 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7108 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7109};
7110
7111/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7112static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7113{
7114 int i;
7115
7116 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7117 if (mode->clock == hdmi_audio_clock[i].clock)
7118 break;
7119 }
7120
7121 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7122 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7123 i = 1;
7124 }
7125
7126 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7127 hdmi_audio_clock[i].clock,
7128 hdmi_audio_clock[i].config);
7129
7130 return hdmi_audio_clock[i].config;
7131}
7132
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007133static bool intel_eld_uptodate(struct drm_connector *connector,
7134 int reg_eldv, uint32_t bits_eldv,
7135 int reg_elda, uint32_t bits_elda,
7136 int reg_edid)
7137{
7138 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7139 uint8_t *eld = connector->eld;
7140 uint32_t i;
7141
7142 i = I915_READ(reg_eldv);
7143 i &= bits_eldv;
7144
7145 if (!eld[0])
7146 return !i;
7147
7148 if (!i)
7149 return false;
7150
7151 i = I915_READ(reg_elda);
7152 i &= ~bits_elda;
7153 I915_WRITE(reg_elda, i);
7154
7155 for (i = 0; i < eld[2]; i++)
7156 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7157 return false;
7158
7159 return true;
7160}
7161
Wu Fengguange0dac652011-09-05 14:25:34 +08007162static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007163 struct drm_crtc *crtc,
7164 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007165{
7166 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7167 uint8_t *eld = connector->eld;
7168 uint32_t eldv;
7169 uint32_t len;
7170 uint32_t i;
7171
7172 i = I915_READ(G4X_AUD_VID_DID);
7173
7174 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7175 eldv = G4X_ELDV_DEVCL_DEVBLC;
7176 else
7177 eldv = G4X_ELDV_DEVCTG;
7178
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007179 if (intel_eld_uptodate(connector,
7180 G4X_AUD_CNTL_ST, eldv,
7181 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7182 G4X_HDMIW_HDMIEDID))
7183 return;
7184
Wu Fengguange0dac652011-09-05 14:25:34 +08007185 i = I915_READ(G4X_AUD_CNTL_ST);
7186 i &= ~(eldv | G4X_ELD_ADDR);
7187 len = (i >> 9) & 0x1f; /* ELD buffer size */
7188 I915_WRITE(G4X_AUD_CNTL_ST, i);
7189
7190 if (!eld[0])
7191 return;
7192
7193 len = min_t(uint8_t, eld[2], len);
7194 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7195 for (i = 0; i < len; i++)
7196 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7197
7198 i = I915_READ(G4X_AUD_CNTL_ST);
7199 i |= eldv;
7200 I915_WRITE(G4X_AUD_CNTL_ST, i);
7201}
7202
Wang Xingchao83358c852012-08-16 22:43:37 +08007203static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007204 struct drm_crtc *crtc,
7205 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007206{
7207 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7208 uint8_t *eld = connector->eld;
7209 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007211 uint32_t eldv;
7212 uint32_t i;
7213 int len;
7214 int pipe = to_intel_crtc(crtc)->pipe;
7215 int tmp;
7216
7217 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7218 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7219 int aud_config = HSW_AUD_CFG(pipe);
7220 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7221
7222
7223 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7224
7225 /* Audio output enable */
7226 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7227 tmp = I915_READ(aud_cntrl_st2);
7228 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7229 I915_WRITE(aud_cntrl_st2, tmp);
7230
7231 /* Wait for 1 vertical blank */
7232 intel_wait_for_vblank(dev, pipe);
7233
7234 /* Set ELD valid state */
7235 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007236 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007237 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7238 I915_WRITE(aud_cntrl_st2, tmp);
7239 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007240 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007241
7242 /* Enable HDMI mode */
7243 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007244 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007245 /* clear N_programing_enable and N_value_index */
7246 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7247 I915_WRITE(aud_config, tmp);
7248
7249 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7250
7251 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007252 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007253
7254 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7255 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7256 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7257 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007258 } else {
7259 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7260 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007261
7262 if (intel_eld_uptodate(connector,
7263 aud_cntrl_st2, eldv,
7264 aud_cntl_st, IBX_ELD_ADDRESS,
7265 hdmiw_hdmiedid))
7266 return;
7267
7268 i = I915_READ(aud_cntrl_st2);
7269 i &= ~eldv;
7270 I915_WRITE(aud_cntrl_st2, i);
7271
7272 if (!eld[0])
7273 return;
7274
7275 i = I915_READ(aud_cntl_st);
7276 i &= ~IBX_ELD_ADDRESS;
7277 I915_WRITE(aud_cntl_st, i);
7278 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7279 DRM_DEBUG_DRIVER("port num:%d\n", i);
7280
7281 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7282 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7283 for (i = 0; i < len; i++)
7284 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7285
7286 i = I915_READ(aud_cntrl_st2);
7287 i |= eldv;
7288 I915_WRITE(aud_cntrl_st2, i);
7289
7290}
7291
Wu Fengguange0dac652011-09-05 14:25:34 +08007292static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007293 struct drm_crtc *crtc,
7294 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007295{
7296 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7297 uint8_t *eld = connector->eld;
7298 uint32_t eldv;
7299 uint32_t i;
7300 int len;
7301 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007302 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007303 int aud_cntl_st;
7304 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007305 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007306
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007307 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007308 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7309 aud_config = IBX_AUD_CFG(pipe);
7310 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007311 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007312 } else if (IS_VALLEYVIEW(connector->dev)) {
7313 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7314 aud_config = VLV_AUD_CFG(pipe);
7315 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7316 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007317 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007318 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7319 aud_config = CPT_AUD_CFG(pipe);
7320 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007321 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007322 }
7323
Wang Xingchao9b138a82012-08-09 16:52:18 +08007324 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007325
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007326 if (IS_VALLEYVIEW(connector->dev)) {
7327 struct intel_encoder *intel_encoder;
7328 struct intel_digital_port *intel_dig_port;
7329
7330 intel_encoder = intel_attached_encoder(connector);
7331 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7332 i = intel_dig_port->port;
7333 } else {
7334 i = I915_READ(aud_cntl_st);
7335 i = (i >> 29) & DIP_PORT_SEL_MASK;
7336 /* DIP_Port_Select, 0x1 = PortB */
7337 }
7338
Wu Fengguange0dac652011-09-05 14:25:34 +08007339 if (!i) {
7340 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7341 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007342 eldv = IBX_ELD_VALIDB;
7343 eldv |= IBX_ELD_VALIDB << 4;
7344 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007345 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007346 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007347 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007348 }
7349
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007350 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7351 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7352 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007353 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007354 } else {
7355 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7356 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007357
7358 if (intel_eld_uptodate(connector,
7359 aud_cntrl_st2, eldv,
7360 aud_cntl_st, IBX_ELD_ADDRESS,
7361 hdmiw_hdmiedid))
7362 return;
7363
Wu Fengguange0dac652011-09-05 14:25:34 +08007364 i = I915_READ(aud_cntrl_st2);
7365 i &= ~eldv;
7366 I915_WRITE(aud_cntrl_st2, i);
7367
7368 if (!eld[0])
7369 return;
7370
Wu Fengguange0dac652011-09-05 14:25:34 +08007371 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007372 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007373 I915_WRITE(aud_cntl_st, i);
7374
7375 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7376 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7377 for (i = 0; i < len; i++)
7378 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7379
7380 i = I915_READ(aud_cntrl_st2);
7381 i |= eldv;
7382 I915_WRITE(aud_cntrl_st2, i);
7383}
7384
7385void intel_write_eld(struct drm_encoder *encoder,
7386 struct drm_display_mode *mode)
7387{
7388 struct drm_crtc *crtc = encoder->crtc;
7389 struct drm_connector *connector;
7390 struct drm_device *dev = encoder->dev;
7391 struct drm_i915_private *dev_priv = dev->dev_private;
7392
7393 connector = drm_select_eld(encoder, mode);
7394 if (!connector)
7395 return;
7396
7397 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7398 connector->base.id,
7399 drm_get_connector_name(connector),
7400 connector->encoder->base.id,
7401 drm_get_encoder_name(connector->encoder));
7402
7403 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7404
7405 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007406 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007407}
7408
Jesse Barnes79e53942008-11-07 14:24:08 -08007409static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7410{
7411 struct drm_device *dev = crtc->dev;
7412 struct drm_i915_private *dev_priv = dev->dev_private;
7413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7414 bool visible = base != 0;
7415 u32 cntl;
7416
7417 if (intel_crtc->cursor_visible == visible)
7418 return;
7419
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007420 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007421 if (visible) {
7422 /* On these chipsets we can only modify the base whilst
7423 * the cursor is disabled.
7424 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007425 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007426
7427 cntl &= ~(CURSOR_FORMAT_MASK);
7428 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7429 cntl |= CURSOR_ENABLE |
7430 CURSOR_GAMMA_ENABLE |
7431 CURSOR_FORMAT_ARGB;
7432 } else
7433 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007434 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007435
7436 intel_crtc->cursor_visible = visible;
7437}
7438
7439static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7440{
7441 struct drm_device *dev = crtc->dev;
7442 struct drm_i915_private *dev_priv = dev->dev_private;
7443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7444 int pipe = intel_crtc->pipe;
7445 bool visible = base != 0;
7446
7447 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007448 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007449 if (base) {
7450 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7451 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7452 cntl |= pipe << 28; /* Connect to correct pipe */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007453 } else {
Eric Anholtbad720f2009-10-22 16:11:14 -07007454 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007455 cntl |= CURSOR_MODE_DISABLE;
7456 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007457 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007458
7459 intel_crtc->cursor_visible = visible;
7460 }
7461 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007462 POSTING_READ(CURCNTR(pipe));
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007463 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007464 POSTING_READ(CURBASE(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007465}
7466
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007467static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7468{
7469 struct drm_device *dev = crtc->dev;
7470 struct drm_i915_private *dev_priv = dev->dev_private;
7471 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7472 int pipe = intel_crtc->pipe;
7473 bool visible = base != 0;
7474
7475 if (intel_crtc->cursor_visible != visible) {
7476 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7477 if (base) {
7478 cntl &= ~CURSOR_MODE;
7479 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7480 } else {
7481 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7482 cntl |= CURSOR_MODE_DISABLE;
7483 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007484 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007485 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007486 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7487 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007488 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7489
7490 intel_crtc->cursor_visible = visible;
7491 }
7492 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007493 POSTING_READ(CURCNTR_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007494 I915_WRITE(CURBASE_IVB(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007495 POSTING_READ(CURBASE_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007496}
7497
Jesse Barnes79e53942008-11-07 14:24:08 -08007498/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007499static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7500 bool on)
7501{
7502 struct drm_device *dev = crtc->dev;
7503 struct drm_i915_private *dev_priv = dev->dev_private;
7504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7505 int pipe = intel_crtc->pipe;
7506 int x = intel_crtc->cursor_x;
7507 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007508 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007509 bool visible;
7510
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007511 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007512 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007513
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007514 if (x >= intel_crtc->config.pipe_src_w)
7515 base = 0;
7516
7517 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007518 base = 0;
7519
7520 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007521 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007522 base = 0;
7523
7524 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7525 x = -x;
7526 }
7527 pos |= x << CURSOR_X_SHIFT;
7528
7529 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007530 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007531 base = 0;
7532
7533 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7534 y = -y;
7535 }
7536 pos |= y << CURSOR_Y_SHIFT;
7537
7538 visible = base != 0;
7539 if (!visible && !intel_crtc->cursor_visible)
7540 return;
7541
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007542 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007543 I915_WRITE(CURPOS_IVB(pipe), pos);
7544 ivb_update_cursor(crtc, base);
7545 } else {
7546 I915_WRITE(CURPOS(pipe), pos);
7547 if (IS_845G(dev) || IS_I865G(dev))
7548 i845_update_cursor(crtc, base);
7549 else
7550 i9xx_update_cursor(crtc, base);
7551 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007552}
7553
Jesse Barnes79e53942008-11-07 14:24:08 -08007554static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00007555 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08007556 uint32_t handle,
7557 uint32_t width, uint32_t height)
7558{
7559 struct drm_device *dev = crtc->dev;
7560 struct drm_i915_private *dev_priv = dev->dev_private;
7561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007562 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007563 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007564 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007565
Jesse Barnes79e53942008-11-07 14:24:08 -08007566 /* if we want to turn off the cursor ignore width and height */
7567 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007568 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007569 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007570 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007571 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007572 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007573 }
7574
7575 /* Currently we only support 64x64 cursors */
7576 if (width != 64 || height != 64) {
7577 DRM_ERROR("we currently only support 64x64 cursors\n");
7578 return -EINVAL;
7579 }
7580
Chris Wilson05394f32010-11-08 19:18:58 +00007581 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007582 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007583 return -ENOENT;
7584
Chris Wilson05394f32010-11-08 19:18:58 +00007585 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007586 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007587 ret = -ENOMEM;
7588 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007589 }
7590
Dave Airlie71acb5e2008-12-30 20:31:46 +10007591 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007592 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00007593 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007594 unsigned alignment;
7595
Chris Wilsond9e86c02010-11-10 16:40:20 +00007596 if (obj->tiling_mode) {
7597 DRM_ERROR("cursor cannot be tiled\n");
7598 ret = -EINVAL;
7599 goto fail_locked;
7600 }
7601
Chris Wilson693db182013-03-05 14:52:39 +00007602 /* Note that the w/a also requires 2 PTE of padding following
7603 * the bo. We currently fill all unused PTE with the shadow
7604 * page and so we should always have valid PTE following the
7605 * cursor preventing the VT-d warning.
7606 */
7607 alignment = 0;
7608 if (need_vtd_wa(dev))
7609 alignment = 64*1024;
7610
7611 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007612 if (ret) {
7613 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007614 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007615 }
7616
Chris Wilsond9e86c02010-11-10 16:40:20 +00007617 ret = i915_gem_object_put_fence(obj);
7618 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007619 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007620 goto fail_unpin;
7621 }
7622
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007623 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007624 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007625 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007626 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007627 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7628 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007629 if (ret) {
7630 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007631 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007632 }
Chris Wilson05394f32010-11-08 19:18:58 +00007633 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007634 }
7635
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007636 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007637 I915_WRITE(CURSIZE, (height << 12) | width);
7638
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007639 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007640 if (intel_crtc->cursor_bo) {
Damien Lespiau3d13ef22014-02-07 19:12:47 +00007641 if (INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007642 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007643 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7644 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007645 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007646 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007647 }
Jesse Barnes80824002009-09-10 15:28:06 -07007648
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007649 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007650
7651 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007652 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007653 intel_crtc->cursor_width = width;
7654 intel_crtc->cursor_height = height;
7655
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007656 if (intel_crtc->active)
7657 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007658
Jesse Barnes79e53942008-11-07 14:24:08 -08007659 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007660fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007661 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007662fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007663 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007664fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007665 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007666 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007667}
7668
7669static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7670{
Jesse Barnes79e53942008-11-07 14:24:08 -08007671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007672
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007673 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7674 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007675
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007676 if (intel_crtc->active)
7677 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007678
7679 return 0;
7680}
7681
Jesse Barnes79e53942008-11-07 14:24:08 -08007682static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007683 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007684{
James Simmons72034252010-08-03 01:33:19 +01007685 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007687
James Simmons72034252010-08-03 01:33:19 +01007688 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007689 intel_crtc->lut_r[i] = red[i] >> 8;
7690 intel_crtc->lut_g[i] = green[i] >> 8;
7691 intel_crtc->lut_b[i] = blue[i] >> 8;
7692 }
7693
7694 intel_crtc_load_lut(crtc);
7695}
7696
Jesse Barnes79e53942008-11-07 14:24:08 -08007697/* VESA 640x480x72Hz mode to set on the pipe */
7698static struct drm_display_mode load_detect_mode = {
7699 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7700 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7701};
7702
Daniel Vettera8bb6812014-02-10 18:00:39 +01007703static int intel_framebuffer_init(struct drm_device *dev,
7704 struct intel_framebuffer *ifb,
7705 struct drm_mode_fb_cmd2 *mode_cmd,
7706 struct drm_i915_gem_object *obj);
7707
7708struct drm_framebuffer *
7709__intel_framebuffer_create(struct drm_device *dev,
7710 struct drm_mode_fb_cmd2 *mode_cmd,
7711 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01007712{
7713 struct intel_framebuffer *intel_fb;
7714 int ret;
7715
7716 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7717 if (!intel_fb) {
7718 drm_gem_object_unreference_unlocked(&obj->base);
7719 return ERR_PTR(-ENOMEM);
7720 }
7721
7722 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007723 if (ret)
7724 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007725
7726 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007727err:
7728 drm_gem_object_unreference_unlocked(&obj->base);
7729 kfree(intel_fb);
7730
7731 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007732}
7733
Daniel Vettera8bb6812014-02-10 18:00:39 +01007734struct drm_framebuffer *
7735intel_framebuffer_create(struct drm_device *dev,
7736 struct drm_mode_fb_cmd2 *mode_cmd,
7737 struct drm_i915_gem_object *obj)
7738{
7739 struct drm_framebuffer *fb;
7740 int ret;
7741
7742 ret = i915_mutex_lock_interruptible(dev);
7743 if (ret)
7744 return ERR_PTR(ret);
7745 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
7746 mutex_unlock(&dev->struct_mutex);
7747
7748 return fb;
7749}
7750
Chris Wilsond2dff872011-04-19 08:36:26 +01007751static u32
7752intel_framebuffer_pitch_for_width(int width, int bpp)
7753{
7754 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7755 return ALIGN(pitch, 64);
7756}
7757
7758static u32
7759intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7760{
7761 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7762 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7763}
7764
7765static struct drm_framebuffer *
7766intel_framebuffer_create_for_mode(struct drm_device *dev,
7767 struct drm_display_mode *mode,
7768 int depth, int bpp)
7769{
7770 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007771 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007772
7773 obj = i915_gem_alloc_object(dev,
7774 intel_framebuffer_size_for_mode(mode, bpp));
7775 if (obj == NULL)
7776 return ERR_PTR(-ENOMEM);
7777
7778 mode_cmd.width = mode->hdisplay;
7779 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007780 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7781 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007782 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007783
7784 return intel_framebuffer_create(dev, &mode_cmd, obj);
7785}
7786
7787static struct drm_framebuffer *
7788mode_fits_in_fbdev(struct drm_device *dev,
7789 struct drm_display_mode *mode)
7790{
Daniel Vetter4520f532013-10-09 09:18:51 +02007791#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007792 struct drm_i915_private *dev_priv = dev->dev_private;
7793 struct drm_i915_gem_object *obj;
7794 struct drm_framebuffer *fb;
7795
7796 if (dev_priv->fbdev == NULL)
7797 return NULL;
7798
Jesse Barnes8bcd4552014-02-07 12:10:38 -08007799 obj = dev_priv->fbdev->fb->obj;
Chris Wilsond2dff872011-04-19 08:36:26 +01007800 if (obj == NULL)
7801 return NULL;
7802
Jesse Barnes8bcd4552014-02-07 12:10:38 -08007803 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007804 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7805 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007806 return NULL;
7807
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007808 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007809 return NULL;
7810
7811 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007812#else
7813 return NULL;
7814#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007815}
7816
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007817bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007818 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007819 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007820{
7821 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007822 struct intel_encoder *intel_encoder =
7823 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007824 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007825 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007826 struct drm_crtc *crtc = NULL;
7827 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007828 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007829 int i = -1;
7830
Chris Wilsond2dff872011-04-19 08:36:26 +01007831 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7832 connector->base.id, drm_get_connector_name(connector),
7833 encoder->base.id, drm_get_encoder_name(encoder));
7834
Jesse Barnes79e53942008-11-07 14:24:08 -08007835 /*
7836 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007837 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007838 * - if the connector already has an assigned crtc, use it (but make
7839 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007840 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007841 * - try to find the first unused crtc that can drive this connector,
7842 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007843 */
7844
7845 /* See if we already have a CRTC for this connector */
7846 if (encoder->crtc) {
7847 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007848
Daniel Vetter7b240562012-12-12 00:35:33 +01007849 mutex_lock(&crtc->mutex);
7850
Daniel Vetter24218aa2012-08-12 19:27:11 +02007851 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007852 old->load_detect_temp = false;
7853
7854 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007855 if (connector->dpms != DRM_MODE_DPMS_ON)
7856 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007857
Chris Wilson71731882011-04-19 23:10:58 +01007858 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007859 }
7860
7861 /* Find an unused one (if possible) */
7862 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7863 i++;
7864 if (!(encoder->possible_crtcs & (1 << i)))
7865 continue;
7866 if (!possible_crtc->enabled) {
7867 crtc = possible_crtc;
7868 break;
7869 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007870 }
7871
7872 /*
7873 * If we didn't find an unused CRTC, don't use any.
7874 */
7875 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007876 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7877 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007878 }
7879
Daniel Vetter7b240562012-12-12 00:35:33 +01007880 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007881 intel_encoder->new_crtc = to_intel_crtc(crtc);
7882 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007883
7884 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02007885 intel_crtc->new_enabled = true;
7886 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02007887 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007888 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007889 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007890
Chris Wilson64927112011-04-20 07:25:26 +01007891 if (!mode)
7892 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007893
Chris Wilsond2dff872011-04-19 08:36:26 +01007894 /* We need a framebuffer large enough to accommodate all accesses
7895 * that the plane may generate whilst we perform load detection.
7896 * We can not rely on the fbcon either being present (we get called
7897 * during its initialisation to detect all boot displays, or it may
7898 * not even exist) or that it is large enough to satisfy the
7899 * requested mode.
7900 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007901 fb = mode_fits_in_fbdev(dev, mode);
7902 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007903 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007904 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7905 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007906 } else
7907 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007908 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007909 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02007910 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007911 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007912
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007913 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007914 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007915 if (old->release_fb)
7916 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02007917 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007918 }
Chris Wilson71731882011-04-19 23:10:58 +01007919
Jesse Barnes79e53942008-11-07 14:24:08 -08007920 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007921 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007922 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02007923
7924 fail:
7925 intel_crtc->new_enabled = crtc->enabled;
7926 if (intel_crtc->new_enabled)
7927 intel_crtc->new_config = &intel_crtc->config;
7928 else
7929 intel_crtc->new_config = NULL;
7930 mutex_unlock(&crtc->mutex);
7931 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007932}
7933
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007934void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007935 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007936{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007937 struct intel_encoder *intel_encoder =
7938 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007939 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007940 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02007941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007942
Chris Wilsond2dff872011-04-19 08:36:26 +01007943 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7944 connector->base.id, drm_get_connector_name(connector),
7945 encoder->base.id, drm_get_encoder_name(encoder));
7946
Chris Wilson8261b192011-04-19 23:18:09 +01007947 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007948 to_intel_connector(connector)->new_encoder = NULL;
7949 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02007950 intel_crtc->new_enabled = false;
7951 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02007952 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007953
Daniel Vetter36206362012-12-10 20:42:17 +01007954 if (old->release_fb) {
7955 drm_framebuffer_unregister_private(old->release_fb);
7956 drm_framebuffer_unreference(old->release_fb);
7957 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007958
Daniel Vetter67c96402013-01-23 16:25:09 +00007959 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007960 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007961 }
7962
Eric Anholtc751ce42010-03-25 11:48:48 -07007963 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007964 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7965 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007966
7967 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007968}
7969
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007970static int i9xx_pll_refclk(struct drm_device *dev,
7971 const struct intel_crtc_config *pipe_config)
7972{
7973 struct drm_i915_private *dev_priv = dev->dev_private;
7974 u32 dpll = pipe_config->dpll_hw_state.dpll;
7975
7976 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007977 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007978 else if (HAS_PCH_SPLIT(dev))
7979 return 120000;
7980 else if (!IS_GEN2(dev))
7981 return 96000;
7982 else
7983 return 48000;
7984}
7985
Jesse Barnes79e53942008-11-07 14:24:08 -08007986/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007987static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7988 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007989{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007990 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007991 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007992 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007993 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007994 u32 fp;
7995 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007996 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007997
7998 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007999 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008000 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008001 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008002
8003 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008004 if (IS_PINEVIEW(dev)) {
8005 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8006 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008007 } else {
8008 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8009 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8010 }
8011
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008012 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008013 if (IS_PINEVIEW(dev))
8014 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8015 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008016 else
8017 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008018 DPLL_FPA01_P1_POST_DIV_SHIFT);
8019
8020 switch (dpll & DPLL_MODE_MASK) {
8021 case DPLLB_MODE_DAC_SERIAL:
8022 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8023 5 : 10;
8024 break;
8025 case DPLLB_MODE_LVDS:
8026 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8027 7 : 14;
8028 break;
8029 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008030 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008031 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008032 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008033 }
8034
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008035 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008036 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008037 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008038 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008039 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008040 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008041 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008042
8043 if (is_lvds) {
8044 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8045 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008046
8047 if (lvds & LVDS_CLKB_POWER_UP)
8048 clock.p2 = 7;
8049 else
8050 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008051 } else {
8052 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8053 clock.p1 = 2;
8054 else {
8055 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8056 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8057 }
8058 if (dpll & PLL_P2_DIVIDE_BY_4)
8059 clock.p2 = 4;
8060 else
8061 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008062 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008063
8064 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008065 }
8066
Ville Syrjälä18442d02013-09-13 16:00:08 +03008067 /*
8068 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008069 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008070 * encoder's get_config() function.
8071 */
8072 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008073}
8074
Ville Syrjälä6878da02013-09-13 15:59:11 +03008075int intel_dotclock_calculate(int link_freq,
8076 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008077{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008078 /*
8079 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008080 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008081 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008082 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008083 *
8084 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008085 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008086 */
8087
Ville Syrjälä6878da02013-09-13 15:59:11 +03008088 if (!m_n->link_n)
8089 return 0;
8090
8091 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8092}
8093
Ville Syrjälä18442d02013-09-13 16:00:08 +03008094static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8095 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008096{
8097 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008098
8099 /* read out port_clock from the DPLL */
8100 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008101
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008102 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008103 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008104 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008105 * agree once we know their relationship in the encoder's
8106 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008107 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008108 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008109 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8110 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008111}
8112
8113/** Returns the currently programmed mode of the given pipe. */
8114struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8115 struct drm_crtc *crtc)
8116{
Jesse Barnes548f2452011-02-17 10:40:53 -08008117 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008119 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008120 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008121 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008122 int htot = I915_READ(HTOTAL(cpu_transcoder));
8123 int hsync = I915_READ(HSYNC(cpu_transcoder));
8124 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8125 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008126 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008127
8128 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8129 if (!mode)
8130 return NULL;
8131
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008132 /*
8133 * Construct a pipe_config sufficient for getting the clock info
8134 * back out of crtc_clock_get.
8135 *
8136 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8137 * to use a real value here instead.
8138 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008139 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008140 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008141 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8142 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8143 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008144 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8145
Ville Syrjälä773ae032013-09-23 17:48:20 +03008146 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008147 mode->hdisplay = (htot & 0xffff) + 1;
8148 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8149 mode->hsync_start = (hsync & 0xffff) + 1;
8150 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8151 mode->vdisplay = (vtot & 0xffff) + 1;
8152 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8153 mode->vsync_start = (vsync & 0xffff) + 1;
8154 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8155
8156 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008157
8158 return mode;
8159}
8160
Daniel Vetter3dec0092010-08-20 21:40:52 +02008161static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008162{
8163 struct drm_device *dev = crtc->dev;
8164 drm_i915_private_t *dev_priv = dev->dev_private;
8165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8166 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008167 int dpll_reg = DPLL(pipe);
8168 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008169
Eric Anholtbad720f2009-10-22 16:11:14 -07008170 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008171 return;
8172
8173 if (!dev_priv->lvds_downclock_avail)
8174 return;
8175
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008176 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008177 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008178 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008179
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008180 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008181
8182 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8183 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008184 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008185
Jesse Barnes652c3932009-08-17 13:31:43 -07008186 dpll = I915_READ(dpll_reg);
8187 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008188 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008189 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008190}
8191
8192static void intel_decrease_pllclock(struct drm_crtc *crtc)
8193{
8194 struct drm_device *dev = crtc->dev;
8195 drm_i915_private_t *dev_priv = dev->dev_private;
8196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008197
Eric Anholtbad720f2009-10-22 16:11:14 -07008198 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008199 return;
8200
8201 if (!dev_priv->lvds_downclock_avail)
8202 return;
8203
8204 /*
8205 * Since this is called by a timer, we should never get here in
8206 * the manual case.
8207 */
8208 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008209 int pipe = intel_crtc->pipe;
8210 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008211 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008212
Zhao Yakui44d98a62009-10-09 11:39:40 +08008213 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008214
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008215 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008216
Chris Wilson074b5e12012-05-02 12:07:06 +01008217 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008218 dpll |= DISPLAY_RATE_SELECT_FPA1;
8219 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008220 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008221 dpll = I915_READ(dpll_reg);
8222 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008223 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008224 }
8225
8226}
8227
Chris Wilsonf047e392012-07-21 12:31:41 +01008228void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008229{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008230 struct drm_i915_private *dev_priv = dev->dev_private;
8231
8232 hsw_package_c8_gpu_busy(dev_priv);
8233 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008234}
8235
8236void intel_mark_idle(struct drm_device *dev)
8237{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008238 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008239 struct drm_crtc *crtc;
8240
Paulo Zanonic67a4702013-08-19 13:18:09 -03008241 hsw_package_c8_gpu_idle(dev_priv);
8242
Jani Nikulad330a952014-01-21 11:24:25 +02008243 if (!i915.powersave)
Chris Wilson725a5b52013-01-08 11:02:57 +00008244 return;
8245
8246 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8247 if (!crtc->fb)
8248 continue;
8249
8250 intel_decrease_pllclock(crtc);
8251 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008252
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008253 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008254 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008255}
8256
Chris Wilsonc65355b2013-06-06 16:53:41 -03008257void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8258 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008259{
8260 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008261 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008262
Jani Nikulad330a952014-01-21 11:24:25 +02008263 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07008264 return;
8265
Jesse Barnes652c3932009-08-17 13:31:43 -07008266 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008267 if (!crtc->fb)
8268 continue;
8269
Chris Wilsonc65355b2013-06-06 16:53:41 -03008270 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8271 continue;
8272
8273 intel_increase_pllclock(crtc);
8274 if (ring && intel_fbc_enabled(dev))
8275 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008276 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008277}
8278
Jesse Barnes79e53942008-11-07 14:24:08 -08008279static void intel_crtc_destroy(struct drm_crtc *crtc)
8280{
8281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008282 struct drm_device *dev = crtc->dev;
8283 struct intel_unpin_work *work;
8284 unsigned long flags;
8285
8286 spin_lock_irqsave(&dev->event_lock, flags);
8287 work = intel_crtc->unpin_work;
8288 intel_crtc->unpin_work = NULL;
8289 spin_unlock_irqrestore(&dev->event_lock, flags);
8290
8291 if (work) {
8292 cancel_work_sync(&work->work);
8293 kfree(work);
8294 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008295
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008296 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8297
Jesse Barnes79e53942008-11-07 14:24:08 -08008298 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008299
Jesse Barnes79e53942008-11-07 14:24:08 -08008300 kfree(intel_crtc);
8301}
8302
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008303static void intel_unpin_work_fn(struct work_struct *__work)
8304{
8305 struct intel_unpin_work *work =
8306 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008307 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008308
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008309 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008310 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008311 drm_gem_object_unreference(&work->pending_flip_obj->base);
8312 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008313
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008314 intel_update_fbc(dev);
8315 mutex_unlock(&dev->struct_mutex);
8316
8317 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8318 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8319
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008320 kfree(work);
8321}
8322
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008323static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008324 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008325{
8326 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8328 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008329 unsigned long flags;
8330
8331 /* Ignore early vblank irqs */
8332 if (intel_crtc == NULL)
8333 return;
8334
8335 spin_lock_irqsave(&dev->event_lock, flags);
8336 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008337
8338 /* Ensure we don't miss a work->pending update ... */
8339 smp_rmb();
8340
8341 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008342 spin_unlock_irqrestore(&dev->event_lock, flags);
8343 return;
8344 }
8345
Chris Wilsone7d841c2012-12-03 11:36:30 +00008346 /* and that the unpin work is consistent wrt ->pending. */
8347 smp_rmb();
8348
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008349 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008350
Rob Clark45a066e2012-10-08 14:50:40 -05008351 if (work->event)
8352 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008353
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008354 drm_vblank_put(dev, intel_crtc->pipe);
8355
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008356 spin_unlock_irqrestore(&dev->event_lock, flags);
8357
Daniel Vetter2c10d572012-12-20 21:24:07 +01008358 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008359
8360 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008361
8362 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008363}
8364
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008365void intel_finish_page_flip(struct drm_device *dev, int pipe)
8366{
8367 drm_i915_private_t *dev_priv = dev->dev_private;
8368 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8369
Mario Kleiner49b14a52010-12-09 07:00:07 +01008370 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008371}
8372
8373void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8374{
8375 drm_i915_private_t *dev_priv = dev->dev_private;
8376 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8377
Mario Kleiner49b14a52010-12-09 07:00:07 +01008378 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008379}
8380
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008381void intel_prepare_page_flip(struct drm_device *dev, int plane)
8382{
8383 drm_i915_private_t *dev_priv = dev->dev_private;
8384 struct intel_crtc *intel_crtc =
8385 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8386 unsigned long flags;
8387
Chris Wilsone7d841c2012-12-03 11:36:30 +00008388 /* NB: An MMIO update of the plane base pointer will also
8389 * generate a page-flip completion irq, i.e. every modeset
8390 * is also accompanied by a spurious intel_prepare_page_flip().
8391 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008392 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008393 if (intel_crtc->unpin_work)
8394 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008395 spin_unlock_irqrestore(&dev->event_lock, flags);
8396}
8397
Chris Wilsone7d841c2012-12-03 11:36:30 +00008398inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8399{
8400 /* Ensure that the work item is consistent when activating it ... */
8401 smp_wmb();
8402 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8403 /* and that it is marked active as soon as the irq could fire. */
8404 smp_wmb();
8405}
8406
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008407static int intel_gen2_queue_flip(struct drm_device *dev,
8408 struct drm_crtc *crtc,
8409 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008410 struct drm_i915_gem_object *obj,
8411 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008412{
8413 struct drm_i915_private *dev_priv = dev->dev_private;
8414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008415 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008416 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008417 int ret;
8418
Daniel Vetter6d90c952012-04-26 23:28:05 +02008419 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008420 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008421 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008422
Daniel Vetter6d90c952012-04-26 23:28:05 +02008423 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008424 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008425 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008426
8427 /* Can't queue multiple flips, so wait for the previous
8428 * one to finish before executing the next.
8429 */
8430 if (intel_crtc->plane)
8431 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8432 else
8433 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008434 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8435 intel_ring_emit(ring, MI_NOOP);
8436 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8437 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8438 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008439 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008440 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008441
8442 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008443 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008444 return 0;
8445
8446err_unpin:
8447 intel_unpin_fb_obj(obj);
8448err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008449 return ret;
8450}
8451
8452static int intel_gen3_queue_flip(struct drm_device *dev,
8453 struct drm_crtc *crtc,
8454 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008455 struct drm_i915_gem_object *obj,
8456 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008457{
8458 struct drm_i915_private *dev_priv = dev->dev_private;
8459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008460 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008461 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008462 int ret;
8463
Daniel Vetter6d90c952012-04-26 23:28:05 +02008464 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008465 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008466 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008467
Daniel Vetter6d90c952012-04-26 23:28:05 +02008468 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008469 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008470 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008471
8472 if (intel_crtc->plane)
8473 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8474 else
8475 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008476 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8477 intel_ring_emit(ring, MI_NOOP);
8478 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8479 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8480 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008481 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008482 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008483
Chris Wilsone7d841c2012-12-03 11:36:30 +00008484 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008485 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008486 return 0;
8487
8488err_unpin:
8489 intel_unpin_fb_obj(obj);
8490err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008491 return ret;
8492}
8493
8494static int intel_gen4_queue_flip(struct drm_device *dev,
8495 struct drm_crtc *crtc,
8496 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008497 struct drm_i915_gem_object *obj,
8498 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008499{
8500 struct drm_i915_private *dev_priv = dev->dev_private;
8501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8502 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008503 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008504 int ret;
8505
Daniel Vetter6d90c952012-04-26 23:28:05 +02008506 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008507 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008508 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008509
Daniel Vetter6d90c952012-04-26 23:28:05 +02008510 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008511 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008512 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008513
8514 /* i965+ uses the linear or tiled offsets from the
8515 * Display Registers (which do not change across a page-flip)
8516 * so we need only reprogram the base address.
8517 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008518 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8519 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8520 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008521 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008522 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008523 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008524
8525 /* XXX Enabling the panel-fitter across page-flip is so far
8526 * untested on non-native modes, so ignore it for now.
8527 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8528 */
8529 pf = 0;
8530 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008531 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008532
8533 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008534 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008535 return 0;
8536
8537err_unpin:
8538 intel_unpin_fb_obj(obj);
8539err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008540 return ret;
8541}
8542
8543static int intel_gen6_queue_flip(struct drm_device *dev,
8544 struct drm_crtc *crtc,
8545 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008546 struct drm_i915_gem_object *obj,
8547 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008548{
8549 struct drm_i915_private *dev_priv = dev->dev_private;
8550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008551 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008552 uint32_t pf, pipesrc;
8553 int ret;
8554
Daniel Vetter6d90c952012-04-26 23:28:05 +02008555 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008556 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008557 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008558
Daniel Vetter6d90c952012-04-26 23:28:05 +02008559 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008560 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008561 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008562
Daniel Vetter6d90c952012-04-26 23:28:05 +02008563 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8564 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8565 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008566 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008567
Chris Wilson99d9acd2012-04-17 20:37:00 +01008568 /* Contrary to the suggestions in the documentation,
8569 * "Enable Panel Fitter" does not seem to be required when page
8570 * flipping with a non-native mode, and worse causes a normal
8571 * modeset to fail.
8572 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8573 */
8574 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008575 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008576 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008577
8578 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008579 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008580 return 0;
8581
8582err_unpin:
8583 intel_unpin_fb_obj(obj);
8584err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008585 return ret;
8586}
8587
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008588static int intel_gen7_queue_flip(struct drm_device *dev,
8589 struct drm_crtc *crtc,
8590 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008591 struct drm_i915_gem_object *obj,
8592 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008593{
8594 struct drm_i915_private *dev_priv = dev->dev_private;
8595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008596 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008597 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008598 int len, ret;
8599
8600 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008601 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008602 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008603
8604 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8605 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008606 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008607
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008608 switch(intel_crtc->plane) {
8609 case PLANE_A:
8610 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8611 break;
8612 case PLANE_B:
8613 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8614 break;
8615 case PLANE_C:
8616 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8617 break;
8618 default:
8619 WARN_ONCE(1, "unknown plane in flip command\n");
8620 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008621 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008622 }
8623
Chris Wilsonffe74d72013-08-26 20:58:12 +01008624 len = 4;
8625 if (ring->id == RCS)
8626 len += 6;
8627
8628 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008629 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008630 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008631
Chris Wilsonffe74d72013-08-26 20:58:12 +01008632 /* Unmask the flip-done completion message. Note that the bspec says that
8633 * we should do this for both the BCS and RCS, and that we must not unmask
8634 * more than one flip event at any time (or ensure that one flip message
8635 * can be sent by waiting for flip-done prior to queueing new flips).
8636 * Experimentation says that BCS works despite DERRMR masking all
8637 * flip-done completion events and that unmasking all planes at once
8638 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8639 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8640 */
8641 if (ring->id == RCS) {
8642 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8643 intel_ring_emit(ring, DERRMR);
8644 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8645 DERRMR_PIPEB_PRI_FLIP_DONE |
8646 DERRMR_PIPEC_PRI_FLIP_DONE));
Ville Syrjälä22613c92013-11-29 13:13:42 +02008647 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
8648 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008649 intel_ring_emit(ring, DERRMR);
8650 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8651 }
8652
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008653 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008654 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008655 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008656 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008657
8658 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008659 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008660 return 0;
8661
8662err_unpin:
8663 intel_unpin_fb_obj(obj);
8664err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008665 return ret;
8666}
8667
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008668static int intel_default_queue_flip(struct drm_device *dev,
8669 struct drm_crtc *crtc,
8670 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008671 struct drm_i915_gem_object *obj,
8672 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008673{
8674 return -ENODEV;
8675}
8676
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008677static int intel_crtc_page_flip(struct drm_crtc *crtc,
8678 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008679 struct drm_pending_vblank_event *event,
8680 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008681{
8682 struct drm_device *dev = crtc->dev;
8683 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008684 struct drm_framebuffer *old_fb = crtc->fb;
8685 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8687 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008688 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008689 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008690
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008691 /* Can't change pixel format via MI display flips. */
8692 if (fb->pixel_format != crtc->fb->pixel_format)
8693 return -EINVAL;
8694
8695 /*
8696 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8697 * Note that pitch changes could also affect these register.
8698 */
8699 if (INTEL_INFO(dev)->gen > 3 &&
8700 (fb->offsets[0] != crtc->fb->offsets[0] ||
8701 fb->pitches[0] != crtc->fb->pitches[0]))
8702 return -EINVAL;
8703
Daniel Vetterb14c5672013-09-19 12:18:32 +02008704 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008705 if (work == NULL)
8706 return -ENOMEM;
8707
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008708 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008709 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008710 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008711 INIT_WORK(&work->work, intel_unpin_work_fn);
8712
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008713 ret = drm_vblank_get(dev, intel_crtc->pipe);
8714 if (ret)
8715 goto free_work;
8716
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008717 /* We borrow the event spin lock for protecting unpin_work */
8718 spin_lock_irqsave(&dev->event_lock, flags);
8719 if (intel_crtc->unpin_work) {
8720 spin_unlock_irqrestore(&dev->event_lock, flags);
8721 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008722 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008723
8724 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008725 return -EBUSY;
8726 }
8727 intel_crtc->unpin_work = work;
8728 spin_unlock_irqrestore(&dev->event_lock, flags);
8729
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008730 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8731 flush_workqueue(dev_priv->wq);
8732
Chris Wilson79158102012-05-23 11:13:58 +01008733 ret = i915_mutex_lock_interruptible(dev);
8734 if (ret)
8735 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008736
Jesse Barnes75dfca82010-02-10 15:09:44 -08008737 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008738 drm_gem_object_reference(&work->old_fb_obj->base);
8739 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008740
8741 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008742
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008743 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008744
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008745 work->enable_stall_check = true;
8746
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008747 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008748 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008749
Keith Packarded8d1972013-07-22 18:49:58 -07008750 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008751 if (ret)
8752 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008753
Chris Wilson7782de32011-07-08 12:22:41 +01008754 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008755 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008756 mutex_unlock(&dev->struct_mutex);
8757
Jesse Barnese5510fa2010-07-01 16:48:37 -07008758 trace_i915_flip_request(intel_crtc->plane, obj);
8759
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008760 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008761
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008762cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008763 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008764 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008765 drm_gem_object_unreference(&work->old_fb_obj->base);
8766 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008767 mutex_unlock(&dev->struct_mutex);
8768
Chris Wilson79158102012-05-23 11:13:58 +01008769cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008770 spin_lock_irqsave(&dev->event_lock, flags);
8771 intel_crtc->unpin_work = NULL;
8772 spin_unlock_irqrestore(&dev->event_lock, flags);
8773
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008774 drm_vblank_put(dev, intel_crtc->pipe);
8775free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008776 kfree(work);
8777
8778 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008779}
8780
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008781static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008782 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8783 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008784};
8785
Daniel Vetter9a935852012-07-05 22:34:27 +02008786/**
8787 * intel_modeset_update_staged_output_state
8788 *
8789 * Updates the staged output configuration state, e.g. after we've read out the
8790 * current hw state.
8791 */
8792static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8793{
Ville Syrjälä76688512014-01-10 11:28:06 +02008794 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008795 struct intel_encoder *encoder;
8796 struct intel_connector *connector;
8797
8798 list_for_each_entry(connector, &dev->mode_config.connector_list,
8799 base.head) {
8800 connector->new_encoder =
8801 to_intel_encoder(connector->base.encoder);
8802 }
8803
8804 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8805 base.head) {
8806 encoder->new_crtc =
8807 to_intel_crtc(encoder->base.crtc);
8808 }
Ville Syrjälä76688512014-01-10 11:28:06 +02008809
8810 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8811 base.head) {
8812 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02008813
8814 if (crtc->new_enabled)
8815 crtc->new_config = &crtc->config;
8816 else
8817 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02008818 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008819}
8820
8821/**
8822 * intel_modeset_commit_output_state
8823 *
8824 * This function copies the stage display pipe configuration to the real one.
8825 */
8826static void intel_modeset_commit_output_state(struct drm_device *dev)
8827{
Ville Syrjälä76688512014-01-10 11:28:06 +02008828 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008829 struct intel_encoder *encoder;
8830 struct intel_connector *connector;
8831
8832 list_for_each_entry(connector, &dev->mode_config.connector_list,
8833 base.head) {
8834 connector->base.encoder = &connector->new_encoder->base;
8835 }
8836
8837 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8838 base.head) {
8839 encoder->base.crtc = &encoder->new_crtc->base;
8840 }
Ville Syrjälä76688512014-01-10 11:28:06 +02008841
8842 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8843 base.head) {
8844 crtc->base.enabled = crtc->new_enabled;
8845 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008846}
8847
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008848static void
8849connected_sink_compute_bpp(struct intel_connector * connector,
8850 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008851{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008852 int bpp = pipe_config->pipe_bpp;
8853
8854 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8855 connector->base.base.id,
8856 drm_get_connector_name(&connector->base));
8857
8858 /* Don't use an invalid EDID bpc value */
8859 if (connector->base.display_info.bpc &&
8860 connector->base.display_info.bpc * 3 < bpp) {
8861 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8862 bpp, connector->base.display_info.bpc*3);
8863 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8864 }
8865
8866 /* Clamp bpp to 8 on screens without EDID 1.4 */
8867 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8868 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8869 bpp);
8870 pipe_config->pipe_bpp = 24;
8871 }
8872}
8873
8874static int
8875compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8876 struct drm_framebuffer *fb,
8877 struct intel_crtc_config *pipe_config)
8878{
8879 struct drm_device *dev = crtc->base.dev;
8880 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008881 int bpp;
8882
Daniel Vetterd42264b2013-03-28 16:38:08 +01008883 switch (fb->pixel_format) {
8884 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008885 bpp = 8*3; /* since we go through a colormap */
8886 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008887 case DRM_FORMAT_XRGB1555:
8888 case DRM_FORMAT_ARGB1555:
8889 /* checked in intel_framebuffer_init already */
8890 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8891 return -EINVAL;
8892 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008893 bpp = 6*3; /* min is 18bpp */
8894 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008895 case DRM_FORMAT_XBGR8888:
8896 case DRM_FORMAT_ABGR8888:
8897 /* checked in intel_framebuffer_init already */
8898 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8899 return -EINVAL;
8900 case DRM_FORMAT_XRGB8888:
8901 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008902 bpp = 8*3;
8903 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008904 case DRM_FORMAT_XRGB2101010:
8905 case DRM_FORMAT_ARGB2101010:
8906 case DRM_FORMAT_XBGR2101010:
8907 case DRM_FORMAT_ABGR2101010:
8908 /* checked in intel_framebuffer_init already */
8909 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008910 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008911 bpp = 10*3;
8912 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008913 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008914 default:
8915 DRM_DEBUG_KMS("unsupported depth\n");
8916 return -EINVAL;
8917 }
8918
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008919 pipe_config->pipe_bpp = bpp;
8920
8921 /* Clamp display bpp to EDID value */
8922 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008923 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008924 if (!connector->new_encoder ||
8925 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008926 continue;
8927
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008928 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008929 }
8930
8931 return bpp;
8932}
8933
Daniel Vetter644db712013-09-19 14:53:58 +02008934static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8935{
8936 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8937 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008938 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008939 mode->crtc_hdisplay, mode->crtc_hsync_start,
8940 mode->crtc_hsync_end, mode->crtc_htotal,
8941 mode->crtc_vdisplay, mode->crtc_vsync_start,
8942 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8943}
8944
Daniel Vetterc0b03412013-05-28 12:05:54 +02008945static void intel_dump_pipe_config(struct intel_crtc *crtc,
8946 struct intel_crtc_config *pipe_config,
8947 const char *context)
8948{
8949 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8950 context, pipe_name(crtc->pipe));
8951
8952 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8953 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8954 pipe_config->pipe_bpp, pipe_config->dither);
8955 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8956 pipe_config->has_pch_encoder,
8957 pipe_config->fdi_lanes,
8958 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8959 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8960 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008961 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8962 pipe_config->has_dp_encoder,
8963 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8964 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8965 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008966 DRM_DEBUG_KMS("requested mode:\n");
8967 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8968 DRM_DEBUG_KMS("adjusted mode:\n");
8969 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008970 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008971 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008972 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8973 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008974 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8975 pipe_config->gmch_pfit.control,
8976 pipe_config->gmch_pfit.pgm_ratios,
8977 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008978 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008979 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008980 pipe_config->pch_pfit.size,
8981 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008982 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008983 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008984}
8985
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008986static bool check_encoder_cloning(struct drm_crtc *crtc)
8987{
8988 int num_encoders = 0;
8989 bool uncloneable_encoders = false;
8990 struct intel_encoder *encoder;
8991
8992 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8993 base.head) {
8994 if (&encoder->new_crtc->base != crtc)
8995 continue;
8996
8997 num_encoders++;
8998 if (!encoder->cloneable)
8999 uncloneable_encoders = true;
9000 }
9001
9002 return !(num_encoders > 1 && uncloneable_encoders);
9003}
9004
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009005static struct intel_crtc_config *
9006intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009007 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009008 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009009{
9010 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009011 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009012 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009013 int plane_bpp, ret = -EINVAL;
9014 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009015
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009016 if (!check_encoder_cloning(crtc)) {
9017 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9018 return ERR_PTR(-EINVAL);
9019 }
9020
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009021 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9022 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009023 return ERR_PTR(-ENOMEM);
9024
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009025 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9026 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009027
Daniel Vettere143a212013-07-04 12:01:15 +02009028 pipe_config->cpu_transcoder =
9029 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009030 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009031
Imre Deak2960bc92013-07-30 13:36:32 +03009032 /*
9033 * Sanitize sync polarity flags based on requested ones. If neither
9034 * positive or negative polarity is requested, treat this as meaning
9035 * negative polarity.
9036 */
9037 if (!(pipe_config->adjusted_mode.flags &
9038 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9039 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9040
9041 if (!(pipe_config->adjusted_mode.flags &
9042 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9043 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9044
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009045 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9046 * plane pixel format and any sink constraints into account. Returns the
9047 * source plane bpp so that dithering can be selected on mismatches
9048 * after encoders and crtc also have had their say. */
9049 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9050 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009051 if (plane_bpp < 0)
9052 goto fail;
9053
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009054 /*
9055 * Determine the real pipe dimensions. Note that stereo modes can
9056 * increase the actual pipe size due to the frame doubling and
9057 * insertion of additional space for blanks between the frame. This
9058 * is stored in the crtc timings. We use the requested mode to do this
9059 * computation to clearly distinguish it from the adjusted mode, which
9060 * can be changed by the connectors in the below retry loop.
9061 */
9062 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9063 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9064 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9065
Daniel Vettere29c22c2013-02-21 00:00:16 +01009066encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009067 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009068 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009069 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009070
Daniel Vetter135c81b2013-07-21 21:37:09 +02009071 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009072 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009073
Daniel Vetter7758a112012-07-08 19:40:39 +02009074 /* Pass our mode to the connectors and the CRTC to give them a chance to
9075 * adjust it according to limitations or connector properties, and also
9076 * a chance to reject the mode entirely.
9077 */
9078 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9079 base.head) {
9080
9081 if (&encoder->new_crtc->base != crtc)
9082 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009083
Daniel Vetterefea6e82013-07-21 21:36:59 +02009084 if (!(encoder->compute_config(encoder, pipe_config))) {
9085 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009086 goto fail;
9087 }
9088 }
9089
Daniel Vetterff9a6752013-06-01 17:16:21 +02009090 /* Set default port clock if not overwritten by the encoder. Needs to be
9091 * done afterwards in case the encoder adjusts the mode. */
9092 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009093 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9094 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009095
Daniel Vettera43f6e02013-06-07 23:10:32 +02009096 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009097 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009098 DRM_DEBUG_KMS("CRTC fixup failed\n");
9099 goto fail;
9100 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009101
9102 if (ret == RETRY) {
9103 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9104 ret = -EINVAL;
9105 goto fail;
9106 }
9107
9108 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9109 retry = false;
9110 goto encoder_retry;
9111 }
9112
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009113 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9114 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9115 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9116
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009117 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009118fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009119 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009120 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009121}
9122
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009123/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9124 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9125static void
9126intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9127 unsigned *prepare_pipes, unsigned *disable_pipes)
9128{
9129 struct intel_crtc *intel_crtc;
9130 struct drm_device *dev = crtc->dev;
9131 struct intel_encoder *encoder;
9132 struct intel_connector *connector;
9133 struct drm_crtc *tmp_crtc;
9134
9135 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9136
9137 /* Check which crtcs have changed outputs connected to them, these need
9138 * to be part of the prepare_pipes mask. We don't (yet) support global
9139 * modeset across multiple crtcs, so modeset_pipes will only have one
9140 * bit set at most. */
9141 list_for_each_entry(connector, &dev->mode_config.connector_list,
9142 base.head) {
9143 if (connector->base.encoder == &connector->new_encoder->base)
9144 continue;
9145
9146 if (connector->base.encoder) {
9147 tmp_crtc = connector->base.encoder->crtc;
9148
9149 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9150 }
9151
9152 if (connector->new_encoder)
9153 *prepare_pipes |=
9154 1 << connector->new_encoder->new_crtc->pipe;
9155 }
9156
9157 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9158 base.head) {
9159 if (encoder->base.crtc == &encoder->new_crtc->base)
9160 continue;
9161
9162 if (encoder->base.crtc) {
9163 tmp_crtc = encoder->base.crtc;
9164
9165 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9166 }
9167
9168 if (encoder->new_crtc)
9169 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9170 }
9171
Ville Syrjälä76688512014-01-10 11:28:06 +02009172 /* Check for pipes that will be enabled/disabled ... */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009173 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9174 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009175 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009176 continue;
9177
Ville Syrjälä76688512014-01-10 11:28:06 +02009178 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009179 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +02009180 else
9181 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009182 }
9183
9184
9185 /* set_mode is also used to update properties on life display pipes. */
9186 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +02009187 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009188 *prepare_pipes |= 1 << intel_crtc->pipe;
9189
Daniel Vetterb6c51642013-04-12 18:48:43 +02009190 /*
9191 * For simplicity do a full modeset on any pipe where the output routing
9192 * changed. We could be more clever, but that would require us to be
9193 * more careful with calling the relevant encoder->mode_set functions.
9194 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009195 if (*prepare_pipes)
9196 *modeset_pipes = *prepare_pipes;
9197
9198 /* ... and mask these out. */
9199 *modeset_pipes &= ~(*disable_pipes);
9200 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009201
9202 /*
9203 * HACK: We don't (yet) fully support global modesets. intel_set_config
9204 * obies this rule, but the modeset restore mode of
9205 * intel_modeset_setup_hw_state does not.
9206 */
9207 *modeset_pipes &= 1 << intel_crtc->pipe;
9208 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009209
9210 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9211 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009212}
9213
Daniel Vetterea9d7582012-07-10 10:42:52 +02009214static bool intel_crtc_in_use(struct drm_crtc *crtc)
9215{
9216 struct drm_encoder *encoder;
9217 struct drm_device *dev = crtc->dev;
9218
9219 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9220 if (encoder->crtc == crtc)
9221 return true;
9222
9223 return false;
9224}
9225
9226static void
9227intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9228{
9229 struct intel_encoder *intel_encoder;
9230 struct intel_crtc *intel_crtc;
9231 struct drm_connector *connector;
9232
9233 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9234 base.head) {
9235 if (!intel_encoder->base.crtc)
9236 continue;
9237
9238 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9239
9240 if (prepare_pipes & (1 << intel_crtc->pipe))
9241 intel_encoder->connectors_active = false;
9242 }
9243
9244 intel_modeset_commit_output_state(dev);
9245
Ville Syrjälä76688512014-01-10 11:28:06 +02009246 /* Double check state. */
Daniel Vetterea9d7582012-07-10 10:42:52 +02009247 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9248 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009249 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009250 WARN_ON(intel_crtc->new_config &&
9251 intel_crtc->new_config != &intel_crtc->config);
9252 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009253 }
9254
9255 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9256 if (!connector->encoder || !connector->encoder->crtc)
9257 continue;
9258
9259 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9260
9261 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009262 struct drm_property *dpms_property =
9263 dev->mode_config.dpms_property;
9264
Daniel Vetterea9d7582012-07-10 10:42:52 +02009265 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009266 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009267 dpms_property,
9268 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009269
9270 intel_encoder = to_intel_encoder(connector->encoder);
9271 intel_encoder->connectors_active = true;
9272 }
9273 }
9274
9275}
9276
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009277static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009278{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009279 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009280
9281 if (clock1 == clock2)
9282 return true;
9283
9284 if (!clock1 || !clock2)
9285 return false;
9286
9287 diff = abs(clock1 - clock2);
9288
9289 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9290 return true;
9291
9292 return false;
9293}
9294
Daniel Vetter25c5b262012-07-08 22:08:04 +02009295#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9296 list_for_each_entry((intel_crtc), \
9297 &(dev)->mode_config.crtc_list, \
9298 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009299 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009300
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009301static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009302intel_pipe_config_compare(struct drm_device *dev,
9303 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009304 struct intel_crtc_config *pipe_config)
9305{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009306#define PIPE_CONF_CHECK_X(name) \
9307 if (current_config->name != pipe_config->name) { \
9308 DRM_ERROR("mismatch in " #name " " \
9309 "(expected 0x%08x, found 0x%08x)\n", \
9310 current_config->name, \
9311 pipe_config->name); \
9312 return false; \
9313 }
9314
Daniel Vetter08a24032013-04-19 11:25:34 +02009315#define PIPE_CONF_CHECK_I(name) \
9316 if (current_config->name != pipe_config->name) { \
9317 DRM_ERROR("mismatch in " #name " " \
9318 "(expected %i, found %i)\n", \
9319 current_config->name, \
9320 pipe_config->name); \
9321 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009322 }
9323
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009324#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9325 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009326 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009327 "(expected %i, found %i)\n", \
9328 current_config->name & (mask), \
9329 pipe_config->name & (mask)); \
9330 return false; \
9331 }
9332
Ville Syrjälä5e550652013-09-06 23:29:07 +03009333#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9334 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9335 DRM_ERROR("mismatch in " #name " " \
9336 "(expected %i, found %i)\n", \
9337 current_config->name, \
9338 pipe_config->name); \
9339 return false; \
9340 }
9341
Daniel Vetterbb760062013-06-06 14:55:52 +02009342#define PIPE_CONF_QUIRK(quirk) \
9343 ((current_config->quirks | pipe_config->quirks) & (quirk))
9344
Daniel Vettereccb1402013-05-22 00:50:22 +02009345 PIPE_CONF_CHECK_I(cpu_transcoder);
9346
Daniel Vetter08a24032013-04-19 11:25:34 +02009347 PIPE_CONF_CHECK_I(has_pch_encoder);
9348 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009349 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9350 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9351 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9352 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9353 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009354
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009355 PIPE_CONF_CHECK_I(has_dp_encoder);
9356 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9357 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9358 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9359 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9360 PIPE_CONF_CHECK_I(dp_m_n.tu);
9361
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009362 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9363 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9364 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9365 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9366 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9367 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9368
9369 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9370 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9371 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9372 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9373 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9374 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9375
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009376 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009377
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009378 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9379 DRM_MODE_FLAG_INTERLACE);
9380
Daniel Vetterbb760062013-06-06 14:55:52 +02009381 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9382 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9383 DRM_MODE_FLAG_PHSYNC);
9384 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9385 DRM_MODE_FLAG_NHSYNC);
9386 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9387 DRM_MODE_FLAG_PVSYNC);
9388 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9389 DRM_MODE_FLAG_NVSYNC);
9390 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009391
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009392 PIPE_CONF_CHECK_I(pipe_src_w);
9393 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009394
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009395 PIPE_CONF_CHECK_I(gmch_pfit.control);
9396 /* pfit ratios are autocomputed by the hw on gen4+ */
9397 if (INTEL_INFO(dev)->gen < 4)
9398 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9399 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009400 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9401 if (current_config->pch_pfit.enabled) {
9402 PIPE_CONF_CHECK_I(pch_pfit.pos);
9403 PIPE_CONF_CHECK_I(pch_pfit.size);
9404 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009405
Jesse Barnese59150d2014-01-07 13:30:45 -08009406 /* BDW+ don't expose a synchronous way to read the state */
9407 if (IS_HASWELL(dev))
9408 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009409
Ville Syrjälä282740f2013-09-04 18:30:03 +03009410 PIPE_CONF_CHECK_I(double_wide);
9411
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009412 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009413 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009414 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009415 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9416 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009417
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009418 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9419 PIPE_CONF_CHECK_I(pipe_bpp);
9420
Jesse Barnesa9a7e982014-01-20 14:18:04 -08009421 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
9422 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +03009423
Daniel Vetter66e985c2013-06-05 13:34:20 +02009424#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009425#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009426#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009427#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009428#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009429
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009430 return true;
9431}
9432
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009433static void
9434check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009435{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009436 struct intel_connector *connector;
9437
9438 list_for_each_entry(connector, &dev->mode_config.connector_list,
9439 base.head) {
9440 /* This also checks the encoder/connector hw state with the
9441 * ->get_hw_state callbacks. */
9442 intel_connector_check_state(connector);
9443
9444 WARN(&connector->new_encoder->base != connector->base.encoder,
9445 "connector's staged encoder doesn't match current encoder\n");
9446 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009447}
9448
9449static void
9450check_encoder_state(struct drm_device *dev)
9451{
9452 struct intel_encoder *encoder;
9453 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009454
9455 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9456 base.head) {
9457 bool enabled = false;
9458 bool active = false;
9459 enum pipe pipe, tracked_pipe;
9460
9461 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9462 encoder->base.base.id,
9463 drm_get_encoder_name(&encoder->base));
9464
9465 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9466 "encoder's stage crtc doesn't match current crtc\n");
9467 WARN(encoder->connectors_active && !encoder->base.crtc,
9468 "encoder's active_connectors set, but no crtc\n");
9469
9470 list_for_each_entry(connector, &dev->mode_config.connector_list,
9471 base.head) {
9472 if (connector->base.encoder != &encoder->base)
9473 continue;
9474 enabled = true;
9475 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9476 active = true;
9477 }
9478 WARN(!!encoder->base.crtc != enabled,
9479 "encoder's enabled state mismatch "
9480 "(expected %i, found %i)\n",
9481 !!encoder->base.crtc, enabled);
9482 WARN(active && !encoder->base.crtc,
9483 "active encoder with no crtc\n");
9484
9485 WARN(encoder->connectors_active != active,
9486 "encoder's computed active state doesn't match tracked active state "
9487 "(expected %i, found %i)\n", active, encoder->connectors_active);
9488
9489 active = encoder->get_hw_state(encoder, &pipe);
9490 WARN(active != encoder->connectors_active,
9491 "encoder's hw state doesn't match sw tracking "
9492 "(expected %i, found %i)\n",
9493 encoder->connectors_active, active);
9494
9495 if (!encoder->base.crtc)
9496 continue;
9497
9498 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9499 WARN(active && pipe != tracked_pipe,
9500 "active encoder's pipe doesn't match"
9501 "(expected %i, found %i)\n",
9502 tracked_pipe, pipe);
9503
9504 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009505}
9506
9507static void
9508check_crtc_state(struct drm_device *dev)
9509{
9510 drm_i915_private_t *dev_priv = dev->dev_private;
9511 struct intel_crtc *crtc;
9512 struct intel_encoder *encoder;
9513 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009514
9515 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9516 base.head) {
9517 bool enabled = false;
9518 bool active = false;
9519
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009520 memset(&pipe_config, 0, sizeof(pipe_config));
9521
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009522 DRM_DEBUG_KMS("[CRTC:%d]\n",
9523 crtc->base.base.id);
9524
9525 WARN(crtc->active && !crtc->base.enabled,
9526 "active crtc, but not enabled in sw tracking\n");
9527
9528 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9529 base.head) {
9530 if (encoder->base.crtc != &crtc->base)
9531 continue;
9532 enabled = true;
9533 if (encoder->connectors_active)
9534 active = true;
9535 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009536
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009537 WARN(active != crtc->active,
9538 "crtc's computed active state doesn't match tracked active state "
9539 "(expected %i, found %i)\n", active, crtc->active);
9540 WARN(enabled != crtc->base.enabled,
9541 "crtc's computed enabled state doesn't match tracked enabled state "
9542 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9543
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009544 active = dev_priv->display.get_pipe_config(crtc,
9545 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009546
9547 /* hw state is inconsistent with the pipe A quirk */
9548 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9549 active = crtc->active;
9550
Daniel Vetter6c49f242013-06-06 12:45:25 +02009551 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9552 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009553 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009554 if (encoder->base.crtc != &crtc->base)
9555 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +01009556 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009557 encoder->get_config(encoder, &pipe_config);
9558 }
9559
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009560 WARN(crtc->active != active,
9561 "crtc active state doesn't match with hw state "
9562 "(expected %i, found %i)\n", crtc->active, active);
9563
Daniel Vetterc0b03412013-05-28 12:05:54 +02009564 if (active &&
9565 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9566 WARN(1, "pipe state doesn't match!\n");
9567 intel_dump_pipe_config(crtc, &pipe_config,
9568 "[hw state]");
9569 intel_dump_pipe_config(crtc, &crtc->config,
9570 "[sw state]");
9571 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009572 }
9573}
9574
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009575static void
9576check_shared_dpll_state(struct drm_device *dev)
9577{
9578 drm_i915_private_t *dev_priv = dev->dev_private;
9579 struct intel_crtc *crtc;
9580 struct intel_dpll_hw_state dpll_hw_state;
9581 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009582
9583 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9584 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9585 int enabled_crtcs = 0, active_crtcs = 0;
9586 bool active;
9587
9588 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9589
9590 DRM_DEBUG_KMS("%s\n", pll->name);
9591
9592 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9593
9594 WARN(pll->active > pll->refcount,
9595 "more active pll users than references: %i vs %i\n",
9596 pll->active, pll->refcount);
9597 WARN(pll->active && !pll->on,
9598 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009599 WARN(pll->on && !pll->active,
9600 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009601 WARN(pll->on != active,
9602 "pll on state mismatch (expected %i, found %i)\n",
9603 pll->on, active);
9604
9605 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9606 base.head) {
9607 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9608 enabled_crtcs++;
9609 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9610 active_crtcs++;
9611 }
9612 WARN(pll->active != active_crtcs,
9613 "pll active crtcs mismatch (expected %i, found %i)\n",
9614 pll->active, active_crtcs);
9615 WARN(pll->refcount != enabled_crtcs,
9616 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9617 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009618
9619 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9620 sizeof(dpll_hw_state)),
9621 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009622 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009623}
9624
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009625void
9626intel_modeset_check_state(struct drm_device *dev)
9627{
9628 check_connector_state(dev);
9629 check_encoder_state(dev);
9630 check_crtc_state(dev);
9631 check_shared_dpll_state(dev);
9632}
9633
Ville Syrjälä18442d02013-09-13 16:00:08 +03009634void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9635 int dotclock)
9636{
9637 /*
9638 * FDI already provided one idea for the dotclock.
9639 * Yell if the encoder disagrees.
9640 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009641 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009642 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009643 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009644}
9645
Daniel Vetterf30da182013-04-11 20:22:50 +02009646static int __intel_set_mode(struct drm_crtc *crtc,
9647 struct drm_display_mode *mode,
9648 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009649{
9650 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009651 drm_i915_private_t *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +03009652 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009653 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009654 struct intel_crtc *intel_crtc;
9655 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009656 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009657
Ville Syrjälä4b4b9232013-10-26 17:59:30 +03009658 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009659 if (!saved_mode)
9660 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +02009661
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009662 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009663 &prepare_pipes, &disable_pipes);
9664
Tim Gardner3ac18232012-12-07 07:54:26 -07009665 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009666
Daniel Vetter25c5b262012-07-08 22:08:04 +02009667 /* Hack: Because we don't (yet) support global modeset on multiple
9668 * crtcs, we don't keep track of the new mode for more than one crtc.
9669 * Hence simply check whether any bit is set in modeset_pipes in all the
9670 * pieces of code that are not yet converted to deal with mutliple crtcs
9671 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009672 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009673 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009674 if (IS_ERR(pipe_config)) {
9675 ret = PTR_ERR(pipe_config);
9676 pipe_config = NULL;
9677
Tim Gardner3ac18232012-12-07 07:54:26 -07009678 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009679 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009680 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9681 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +02009682 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +02009683 }
9684
Jesse Barnes30a970c2013-11-04 13:48:12 -08009685 /*
9686 * See if the config requires any additional preparation, e.g.
9687 * to adjust global state with pipes off. We need to do this
9688 * here so we can get the modeset_pipe updated config for the new
9689 * mode set on this crtc. For other crtcs we need to use the
9690 * adjusted_mode bits in the crtc directly.
9691 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009692 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02009693 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -08009694
Ville Syrjäläc164f832013-11-05 22:34:12 +02009695 /* may have added more to prepare_pipes than we should */
9696 prepare_pipes &= ~disable_pipes;
9697 }
9698
Daniel Vetter460da9162013-03-27 00:44:51 +01009699 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9700 intel_crtc_disable(&intel_crtc->base);
9701
Daniel Vetterea9d7582012-07-10 10:42:52 +02009702 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9703 if (intel_crtc->base.enabled)
9704 dev_priv->display.crtc_disable(&intel_crtc->base);
9705 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009706
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009707 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9708 * to set it here already despite that we pass it down the callchain.
9709 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009710 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009711 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009712 /* mode_set/enable/disable functions rely on a correct pipe
9713 * config. */
9714 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +02009715 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +02009716
9717 /*
9718 * Calculate and store various constants which
9719 * are later needed by vblank and swap-completion
9720 * timestamping. They are derived from true hwmode.
9721 */
9722 drm_calc_timestamping_constants(crtc,
9723 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009724 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009725
Daniel Vetterea9d7582012-07-10 10:42:52 +02009726 /* Only after disabling all output pipelines that will be changed can we
9727 * update the the output configuration. */
9728 intel_modeset_update_state(dev, prepare_pipes);
9729
Daniel Vetter47fab732012-10-26 10:58:18 +02009730 if (dev_priv->display.modeset_global_resources)
9731 dev_priv->display.modeset_global_resources(dev);
9732
Daniel Vettera6778b32012-07-02 09:56:42 +02009733 /* Set up the DPLL and any encoders state that needs to adjust or depend
9734 * on the DPLL.
9735 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009736 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009737 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009738 x, y, fb);
9739 if (ret)
9740 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009741 }
9742
9743 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009744 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9745 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009746
Daniel Vettera6778b32012-07-02 09:56:42 +02009747 /* FIXME: add subpixel order */
9748done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +03009749 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -07009750 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009751
Tim Gardner3ac18232012-12-07 07:54:26 -07009752out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009753 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009754 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009755 return ret;
9756}
9757
Damien Lespiaue7457a92013-08-08 22:28:59 +01009758static int intel_set_mode(struct drm_crtc *crtc,
9759 struct drm_display_mode *mode,
9760 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009761{
9762 int ret;
9763
9764 ret = __intel_set_mode(crtc, mode, x, y, fb);
9765
9766 if (ret == 0)
9767 intel_modeset_check_state(crtc->dev);
9768
9769 return ret;
9770}
9771
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009772void intel_crtc_restore_mode(struct drm_crtc *crtc)
9773{
9774 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9775}
9776
Daniel Vetter25c5b262012-07-08 22:08:04 +02009777#undef for_each_intel_crtc_masked
9778
Daniel Vetterd9e55602012-07-04 22:16:09 +02009779static void intel_set_config_free(struct intel_set_config *config)
9780{
9781 if (!config)
9782 return;
9783
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009784 kfree(config->save_connector_encoders);
9785 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +02009786 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009787 kfree(config);
9788}
9789
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009790static int intel_set_config_save_state(struct drm_device *dev,
9791 struct intel_set_config *config)
9792{
Ville Syrjälä76688512014-01-10 11:28:06 +02009793 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009794 struct drm_encoder *encoder;
9795 struct drm_connector *connector;
9796 int count;
9797
Ville Syrjälä76688512014-01-10 11:28:06 +02009798 config->save_crtc_enabled =
9799 kcalloc(dev->mode_config.num_crtc,
9800 sizeof(bool), GFP_KERNEL);
9801 if (!config->save_crtc_enabled)
9802 return -ENOMEM;
9803
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009804 config->save_encoder_crtcs =
9805 kcalloc(dev->mode_config.num_encoder,
9806 sizeof(struct drm_crtc *), GFP_KERNEL);
9807 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009808 return -ENOMEM;
9809
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009810 config->save_connector_encoders =
9811 kcalloc(dev->mode_config.num_connector,
9812 sizeof(struct drm_encoder *), GFP_KERNEL);
9813 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009814 return -ENOMEM;
9815
9816 /* Copy data. Note that driver private data is not affected.
9817 * Should anything bad happen only the expected state is
9818 * restored, not the drivers personal bookkeeping.
9819 */
9820 count = 0;
Ville Syrjälä76688512014-01-10 11:28:06 +02009821 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9822 config->save_crtc_enabled[count++] = crtc->enabled;
9823 }
9824
9825 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009826 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009827 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009828 }
9829
9830 count = 0;
9831 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009832 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009833 }
9834
9835 return 0;
9836}
9837
9838static void intel_set_config_restore_state(struct drm_device *dev,
9839 struct intel_set_config *config)
9840{
Ville Syrjälä76688512014-01-10 11:28:06 +02009841 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009842 struct intel_encoder *encoder;
9843 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009844 int count;
9845
9846 count = 0;
Ville Syrjälä76688512014-01-10 11:28:06 +02009847 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9848 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009849
9850 if (crtc->new_enabled)
9851 crtc->new_config = &crtc->config;
9852 else
9853 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009854 }
9855
9856 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009857 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9858 encoder->new_crtc =
9859 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009860 }
9861
9862 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009863 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9864 connector->new_encoder =
9865 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009866 }
9867}
9868
Imre Deake3de42b2013-05-03 19:44:07 +02009869static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009870is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009871{
9872 int i;
9873
Chris Wilson2e57f472013-07-17 12:14:40 +01009874 if (set->num_connectors == 0)
9875 return false;
9876
9877 if (WARN_ON(set->connectors == NULL))
9878 return false;
9879
9880 for (i = 0; i < set->num_connectors; i++)
9881 if (set->connectors[i]->encoder &&
9882 set->connectors[i]->encoder->crtc == set->crtc &&
9883 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009884 return true;
9885
9886 return false;
9887}
9888
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009889static void
9890intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9891 struct intel_set_config *config)
9892{
9893
9894 /* We should be able to check here if the fb has the same properties
9895 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009896 if (is_crtc_connector_off(set)) {
9897 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009898 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009899 /* If we have no fb then treat it as a full mode set */
9900 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009901 struct intel_crtc *intel_crtc =
9902 to_intel_crtc(set->crtc);
9903
Jani Nikulad330a952014-01-21 11:24:25 +02009904 if (intel_crtc->active && i915.fastboot) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009905 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9906 config->fb_changed = true;
9907 } else {
9908 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9909 config->mode_changed = true;
9910 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009911 } else if (set->fb == NULL) {
9912 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009913 } else if (set->fb->pixel_format !=
9914 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009915 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009916 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009917 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009918 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009919 }
9920
Daniel Vetter835c5872012-07-10 18:11:08 +02009921 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009922 config->fb_changed = true;
9923
9924 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9925 DRM_DEBUG_KMS("modes are different, full mode set\n");
9926 drm_mode_debug_printmodeline(&set->crtc->mode);
9927 drm_mode_debug_printmodeline(set->mode);
9928 config->mode_changed = true;
9929 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009930
9931 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9932 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009933}
9934
Daniel Vetter2e431052012-07-04 22:42:15 +02009935static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009936intel_modeset_stage_output_state(struct drm_device *dev,
9937 struct drm_mode_set *set,
9938 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009939{
Daniel Vetter9a935852012-07-05 22:34:27 +02009940 struct intel_connector *connector;
9941 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +02009942 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009943 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009944
Damien Lespiau9abdda72013-02-13 13:29:23 +00009945 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009946 * of connectors. For paranoia, double-check this. */
9947 WARN_ON(!set->fb && (set->num_connectors != 0));
9948 WARN_ON(set->fb && (set->num_connectors == 0));
9949
Daniel Vetter9a935852012-07-05 22:34:27 +02009950 list_for_each_entry(connector, &dev->mode_config.connector_list,
9951 base.head) {
9952 /* Otherwise traverse passed in connector list and get encoders
9953 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009954 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009955 if (set->connectors[ro] == &connector->base) {
9956 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009957 break;
9958 }
9959 }
9960
Daniel Vetter9a935852012-07-05 22:34:27 +02009961 /* If we disable the crtc, disable all its connectors. Also, if
9962 * the connector is on the changing crtc but not on the new
9963 * connector list, disable it. */
9964 if ((!set->fb || ro == set->num_connectors) &&
9965 connector->base.encoder &&
9966 connector->base.encoder->crtc == set->crtc) {
9967 connector->new_encoder = NULL;
9968
9969 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9970 connector->base.base.id,
9971 drm_get_connector_name(&connector->base));
9972 }
9973
9974
9975 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009976 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009977 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009978 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009979 }
9980 /* connector->new_encoder is now updated for all connectors. */
9981
9982 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009983 list_for_each_entry(connector, &dev->mode_config.connector_list,
9984 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009985 struct drm_crtc *new_crtc;
9986
Daniel Vetter9a935852012-07-05 22:34:27 +02009987 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009988 continue;
9989
Daniel Vetter9a935852012-07-05 22:34:27 +02009990 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009991
9992 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009993 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009994 new_crtc = set->crtc;
9995 }
9996
9997 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +01009998 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
9999 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010000 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020010001 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010002 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10003
10004 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10005 connector->base.base.id,
10006 drm_get_connector_name(&connector->base),
10007 new_crtc->base.id);
10008 }
10009
10010 /* Check for any encoders that needs to be disabled. */
10011 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10012 base.head) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010013 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010014 list_for_each_entry(connector,
10015 &dev->mode_config.connector_list,
10016 base.head) {
10017 if (connector->new_encoder == encoder) {
10018 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010019 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020010020 }
10021 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010022
10023 if (num_connectors == 0)
10024 encoder->new_crtc = NULL;
10025 else if (num_connectors > 1)
10026 return -EINVAL;
10027
Daniel Vetter9a935852012-07-05 22:34:27 +020010028 /* Only now check for crtc changes so we don't miss encoders
10029 * that will be disabled. */
10030 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010031 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010032 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010033 }
10034 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010035 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010036
Ville Syrjälä76688512014-01-10 11:28:06 +020010037 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10038 base.head) {
10039 crtc->new_enabled = false;
10040
10041 list_for_each_entry(encoder,
10042 &dev->mode_config.encoder_list,
10043 base.head) {
10044 if (encoder->new_crtc == crtc) {
10045 crtc->new_enabled = true;
10046 break;
10047 }
10048 }
10049
10050 if (crtc->new_enabled != crtc->base.enabled) {
10051 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10052 crtc->new_enabled ? "en" : "dis");
10053 config->mode_changed = true;
10054 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010055
10056 if (crtc->new_enabled)
10057 crtc->new_config = &crtc->config;
10058 else
10059 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010060 }
10061
Daniel Vetter2e431052012-07-04 22:42:15 +020010062 return 0;
10063}
10064
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010065static void disable_crtc_nofb(struct intel_crtc *crtc)
10066{
10067 struct drm_device *dev = crtc->base.dev;
10068 struct intel_encoder *encoder;
10069 struct intel_connector *connector;
10070
10071 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10072 pipe_name(crtc->pipe));
10073
10074 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10075 if (connector->new_encoder &&
10076 connector->new_encoder->new_crtc == crtc)
10077 connector->new_encoder = NULL;
10078 }
10079
10080 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10081 if (encoder->new_crtc == crtc)
10082 encoder->new_crtc = NULL;
10083 }
10084
10085 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010086 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010087}
10088
Daniel Vetter2e431052012-07-04 22:42:15 +020010089static int intel_crtc_set_config(struct drm_mode_set *set)
10090{
10091 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020010092 struct drm_mode_set save_set;
10093 struct intel_set_config *config;
10094 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020010095
Daniel Vetter8d3e3752012-07-05 16:09:09 +020010096 BUG_ON(!set);
10097 BUG_ON(!set->crtc);
10098 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020010099
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010010100 /* Enforce sane interface api - has been abused by the fb helper. */
10101 BUG_ON(!set->mode && set->fb);
10102 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020010103
Daniel Vetter2e431052012-07-04 22:42:15 +020010104 if (set->fb) {
10105 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10106 set->crtc->base.id, set->fb->base.id,
10107 (int)set->num_connectors, set->x, set->y);
10108 } else {
10109 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020010110 }
10111
10112 dev = set->crtc->dev;
10113
10114 ret = -ENOMEM;
10115 config = kzalloc(sizeof(*config), GFP_KERNEL);
10116 if (!config)
10117 goto out_config;
10118
10119 ret = intel_set_config_save_state(dev, config);
10120 if (ret)
10121 goto out_config;
10122
10123 save_set.crtc = set->crtc;
10124 save_set.mode = &set->crtc->mode;
10125 save_set.x = set->crtc->x;
10126 save_set.y = set->crtc->y;
10127 save_set.fb = set->crtc->fb;
10128
10129 /* Compute whether we need a full modeset, only an fb base update or no
10130 * change at all. In the future we might also check whether only the
10131 * mode changed, e.g. for LVDS where we only change the panel fitter in
10132 * such cases. */
10133 intel_set_config_compute_mode_changes(set, config);
10134
Daniel Vetter9a935852012-07-05 22:34:27 +020010135 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020010136 if (ret)
10137 goto fail;
10138
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010139 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010140 ret = intel_set_mode(set->crtc, set->mode,
10141 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010142 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010143 intel_crtc_wait_for_pending_flips(set->crtc);
10144
Daniel Vetter4f660f42012-07-02 09:47:37 +020010145 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010146 set->x, set->y, set->fb);
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010147 /*
10148 * In the fastboot case this may be our only check of the
10149 * state after boot. It would be better to only do it on
10150 * the first update, but we don't have a nice way of doing that
10151 * (and really, set_config isn't used much for high freq page
10152 * flipping, so increasing its cost here shouldn't be a big
10153 * deal).
10154 */
Jani Nikulad330a952014-01-21 11:24:25 +020010155 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010156 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010157 }
10158
Chris Wilson2d05eae2013-05-03 17:36:25 +010010159 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010160 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10161 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010162fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010163 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010164
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010165 /*
10166 * HACK: if the pipe was on, but we didn't have a framebuffer,
10167 * force the pipe off to avoid oopsing in the modeset code
10168 * due to fb==NULL. This should only happen during boot since
10169 * we don't yet reconstruct the FB from the hardware state.
10170 */
10171 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10172 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10173
Chris Wilson2d05eae2013-05-03 17:36:25 +010010174 /* Try to restore the config */
10175 if (config->mode_changed &&
10176 intel_set_mode(save_set.crtc, save_set.mode,
10177 save_set.x, save_set.y, save_set.fb))
10178 DRM_ERROR("failed to restore config after modeset failure\n");
10179 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010180
Daniel Vetterd9e55602012-07-04 22:16:09 +020010181out_config:
10182 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010183 return ret;
10184}
10185
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010186static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010187 .cursor_set = intel_crtc_cursor_set,
10188 .cursor_move = intel_crtc_cursor_move,
10189 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010190 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010191 .destroy = intel_crtc_destroy,
10192 .page_flip = intel_crtc_page_flip,
10193};
10194
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010195static void intel_cpu_pll_init(struct drm_device *dev)
10196{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010197 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010198 intel_ddi_pll_init(dev);
10199}
10200
Daniel Vetter53589012013-06-05 13:34:16 +020010201static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10202 struct intel_shared_dpll *pll,
10203 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010204{
Daniel Vetter53589012013-06-05 13:34:16 +020010205 uint32_t val;
10206
10207 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010208 hw_state->dpll = val;
10209 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10210 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010211
10212 return val & DPLL_VCO_ENABLE;
10213}
10214
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010215static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10216 struct intel_shared_dpll *pll)
10217{
10218 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10219 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10220}
10221
Daniel Vettere7b903d2013-06-05 13:34:14 +020010222static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10223 struct intel_shared_dpll *pll)
10224{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010225 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020010226 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020010227
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010228 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10229
10230 /* Wait for the clocks to stabilize. */
10231 POSTING_READ(PCH_DPLL(pll->id));
10232 udelay(150);
10233
10234 /* The pixel multiplier can only be updated once the
10235 * DPLL is enabled and the clocks are stable.
10236 *
10237 * So write it again.
10238 */
10239 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10240 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010241 udelay(200);
10242}
10243
10244static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10245 struct intel_shared_dpll *pll)
10246{
10247 struct drm_device *dev = dev_priv->dev;
10248 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010249
10250 /* Make sure no transcoder isn't still depending on us. */
10251 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10252 if (intel_crtc_to_shared_dpll(crtc) == pll)
10253 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10254 }
10255
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010256 I915_WRITE(PCH_DPLL(pll->id), 0);
10257 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010258 udelay(200);
10259}
10260
Daniel Vetter46edb022013-06-05 13:34:12 +020010261static char *ibx_pch_dpll_names[] = {
10262 "PCH DPLL A",
10263 "PCH DPLL B",
10264};
10265
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010266static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010267{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010268 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010269 int i;
10270
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010271 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010272
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010273 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010274 dev_priv->shared_dplls[i].id = i;
10275 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010276 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010277 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10278 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010279 dev_priv->shared_dplls[i].get_hw_state =
10280 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010281 }
10282}
10283
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010284static void intel_shared_dpll_init(struct drm_device *dev)
10285{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010286 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010287
10288 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10289 ibx_pch_dpll_init(dev);
10290 else
10291 dev_priv->num_shared_dpll = 0;
10292
10293 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010294}
10295
Hannes Ederb358d0a2008-12-18 21:18:47 +010010296static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010297{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010298 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010299 struct intel_crtc *intel_crtc;
10300 int i;
10301
Daniel Vetter955382f2013-09-19 14:05:45 +020010302 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010303 if (intel_crtc == NULL)
10304 return;
10305
10306 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10307
10308 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010309 for (i = 0; i < 256; i++) {
10310 intel_crtc->lut_r[i] = i;
10311 intel_crtc->lut_g[i] = i;
10312 intel_crtc->lut_b[i] = i;
10313 }
10314
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010315 /*
10316 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10317 * is hooked to plane B. Hence we want plane A feeding pipe B.
10318 */
Jesse Barnes80824002009-09-10 15:28:06 -070010319 intel_crtc->pipe = pipe;
10320 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010010321 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010322 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010323 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010324 }
10325
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010326 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10327 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10328 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10329 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10330
Jesse Barnes79e53942008-11-07 14:24:08 -080010331 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010332}
10333
Jesse Barnes752aa882013-10-31 18:55:49 +020010334enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10335{
10336 struct drm_encoder *encoder = connector->base.encoder;
10337
10338 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10339
10340 if (!encoder)
10341 return INVALID_PIPE;
10342
10343 return to_intel_crtc(encoder->crtc)->pipe;
10344}
10345
Carl Worth08d7b3d2009-04-29 14:43:54 -070010346int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010347 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010348{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010349 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010350 struct drm_mode_object *drmmode_obj;
10351 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010352
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010353 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10354 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010355
Daniel Vetterc05422d2009-08-11 16:05:30 +020010356 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10357 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010358
Daniel Vetterc05422d2009-08-11 16:05:30 +020010359 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010360 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010361 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010362 }
10363
Daniel Vetterc05422d2009-08-11 16:05:30 +020010364 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10365 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010366
Daniel Vetterc05422d2009-08-11 16:05:30 +020010367 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010368}
10369
Daniel Vetter66a92782012-07-12 20:08:18 +020010370static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010371{
Daniel Vetter66a92782012-07-12 20:08:18 +020010372 struct drm_device *dev = encoder->base.dev;
10373 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010374 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010375 int entry = 0;
10376
Daniel Vetter66a92782012-07-12 20:08:18 +020010377 list_for_each_entry(source_encoder,
10378 &dev->mode_config.encoder_list, base.head) {
10379
10380 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010381 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010382
10383 /* Intel hw has only one MUX where enocoders could be cloned. */
10384 if (encoder->cloneable && source_encoder->cloneable)
10385 index_mask |= (1 << entry);
10386
Jesse Barnes79e53942008-11-07 14:24:08 -080010387 entry++;
10388 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010389
Jesse Barnes79e53942008-11-07 14:24:08 -080010390 return index_mask;
10391}
10392
Chris Wilson4d302442010-12-14 19:21:29 +000010393static bool has_edp_a(struct drm_device *dev)
10394{
10395 struct drm_i915_private *dev_priv = dev->dev_private;
10396
10397 if (!IS_MOBILE(dev))
10398 return false;
10399
10400 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10401 return false;
10402
Damien Lespiaue3589902014-02-07 19:12:50 +000010403 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000010404 return false;
10405
10406 return true;
10407}
10408
Damien Lespiauba0fbca2014-01-08 14:18:23 +000010409const char *intel_output_name(int output)
10410{
10411 static const char *names[] = {
10412 [INTEL_OUTPUT_UNUSED] = "Unused",
10413 [INTEL_OUTPUT_ANALOG] = "Analog",
10414 [INTEL_OUTPUT_DVO] = "DVO",
10415 [INTEL_OUTPUT_SDVO] = "SDVO",
10416 [INTEL_OUTPUT_LVDS] = "LVDS",
10417 [INTEL_OUTPUT_TVOUT] = "TV",
10418 [INTEL_OUTPUT_HDMI] = "HDMI",
10419 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
10420 [INTEL_OUTPUT_EDP] = "eDP",
10421 [INTEL_OUTPUT_DSI] = "DSI",
10422 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
10423 };
10424
10425 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
10426 return "Invalid";
10427
10428 return names[output];
10429}
10430
Jesse Barnes79e53942008-11-07 14:24:08 -080010431static void intel_setup_outputs(struct drm_device *dev)
10432{
Eric Anholt725e30a2009-01-22 13:01:02 -080010433 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010434 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010435 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010436
Daniel Vetterc9093352013-06-06 22:22:47 +020010437 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010438
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010439 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010440 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010441
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010442 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010443 int found;
10444
10445 /* Haswell uses DDI functions to detect digital outputs */
10446 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10447 /* DDI A only supports eDP */
10448 if (found)
10449 intel_ddi_init(dev, PORT_A);
10450
10451 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10452 * register */
10453 found = I915_READ(SFUSE_STRAP);
10454
10455 if (found & SFUSE_STRAP_DDIB_DETECTED)
10456 intel_ddi_init(dev, PORT_B);
10457 if (found & SFUSE_STRAP_DDIC_DETECTED)
10458 intel_ddi_init(dev, PORT_C);
10459 if (found & SFUSE_STRAP_DDID_DETECTED)
10460 intel_ddi_init(dev, PORT_D);
10461 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010462 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020010463 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020010464
10465 if (has_edp_a(dev))
10466 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010467
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010468 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010469 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010470 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010471 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010472 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010473 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010474 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010475 }
10476
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010477 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010478 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010479
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010480 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010481 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010482
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010483 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010484 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010485
Daniel Vetter270b3042012-10-27 15:52:05 +020010486 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010487 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010488 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010489 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10490 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10491 PORT_B);
10492 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10493 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10494 }
10495
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010496 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10497 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10498 PORT_C);
10499 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020010500 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010501 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010502
Jani Nikula3cfca972013-08-27 15:12:26 +030010503 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010504 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010505 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010506
Paulo Zanonie2debe92013-02-18 19:00:27 -030010507 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010508 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010509 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010510 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10511 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010512 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010513 }
Ma Ling27185ae2009-08-24 13:50:23 +080010514
Imre Deake7281ea2013-05-08 13:14:08 +030010515 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010516 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010517 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010518
10519 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010520
Paulo Zanonie2debe92013-02-18 19:00:27 -030010521 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010522 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010523 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010524 }
Ma Ling27185ae2009-08-24 13:50:23 +080010525
Paulo Zanonie2debe92013-02-18 19:00:27 -030010526 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010527
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010528 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10529 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010530 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010531 }
Imre Deake7281ea2013-05-08 13:14:08 +030010532 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010533 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010534 }
Ma Ling27185ae2009-08-24 13:50:23 +080010535
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010536 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010537 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010538 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010539 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010540 intel_dvo_init(dev);
10541
Zhenyu Wang103a1962009-11-27 11:44:36 +080010542 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010543 intel_tv_init(dev);
10544
Chris Wilson4ef69c72010-09-09 15:14:28 +010010545 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10546 encoder->base.possible_crtcs = encoder->crtc_mask;
10547 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010548 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010549 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010550
Paulo Zanonidde86e22012-12-01 12:04:25 -020010551 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010552
10553 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010554}
10555
10556static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10557{
10558 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010559
Daniel Vetteref2d6332014-02-10 18:00:38 +010010560 drm_framebuffer_cleanup(fb);
10561 WARN_ON(!intel_fb->obj->framebuffer_references--);
10562 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010563 kfree(intel_fb);
10564}
10565
10566static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010567 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010568 unsigned int *handle)
10569{
10570 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010571 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010572
Chris Wilson05394f32010-11-08 19:18:58 +000010573 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010574}
10575
10576static const struct drm_framebuffer_funcs intel_fb_funcs = {
10577 .destroy = intel_user_framebuffer_destroy,
10578 .create_handle = intel_user_framebuffer_create_handle,
10579};
10580
Dave Airlie38651672010-03-30 05:34:13 +000010581int intel_framebuffer_init(struct drm_device *dev,
10582 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010583 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010584 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010585{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080010586 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010587 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010588 int ret;
10589
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010590 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10591
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010592 if (obj->tiling_mode == I915_TILING_Y) {
10593 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010594 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010595 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010596
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010597 if (mode_cmd->pitches[0] & 63) {
10598 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10599 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010600 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010601 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010602
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010603 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10604 pitch_limit = 32*1024;
10605 } else if (INTEL_INFO(dev)->gen >= 4) {
10606 if (obj->tiling_mode)
10607 pitch_limit = 16*1024;
10608 else
10609 pitch_limit = 32*1024;
10610 } else if (INTEL_INFO(dev)->gen >= 3) {
10611 if (obj->tiling_mode)
10612 pitch_limit = 8*1024;
10613 else
10614 pitch_limit = 16*1024;
10615 } else
10616 /* XXX DSPC is limited to 4k tiled */
10617 pitch_limit = 8*1024;
10618
10619 if (mode_cmd->pitches[0] > pitch_limit) {
10620 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10621 obj->tiling_mode ? "tiled" : "linear",
10622 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010623 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010624 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010625
10626 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010627 mode_cmd->pitches[0] != obj->stride) {
10628 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10629 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010630 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010631 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010632
Ville Syrjälä57779d02012-10-31 17:50:14 +020010633 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010634 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010635 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010636 case DRM_FORMAT_RGB565:
10637 case DRM_FORMAT_XRGB8888:
10638 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010639 break;
10640 case DRM_FORMAT_XRGB1555:
10641 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010642 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010643 DRM_DEBUG("unsupported pixel format: %s\n",
10644 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010645 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010646 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010647 break;
10648 case DRM_FORMAT_XBGR8888:
10649 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010650 case DRM_FORMAT_XRGB2101010:
10651 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010652 case DRM_FORMAT_XBGR2101010:
10653 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010654 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010655 DRM_DEBUG("unsupported pixel format: %s\n",
10656 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010657 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010658 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010659 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010660 case DRM_FORMAT_YUYV:
10661 case DRM_FORMAT_UYVY:
10662 case DRM_FORMAT_YVYU:
10663 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010664 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010665 DRM_DEBUG("unsupported pixel format: %s\n",
10666 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010667 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010668 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010669 break;
10670 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010671 DRM_DEBUG("unsupported pixel format: %s\n",
10672 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010673 return -EINVAL;
10674 }
10675
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010676 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10677 if (mode_cmd->offsets[0] != 0)
10678 return -EINVAL;
10679
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080010680 aligned_height = intel_align_height(dev, mode_cmd->height,
10681 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020010682 /* FIXME drm helper for size checks (especially planar formats)? */
10683 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10684 return -EINVAL;
10685
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010686 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10687 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010688 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010689
Jesse Barnes79e53942008-11-07 14:24:08 -080010690 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10691 if (ret) {
10692 DRM_ERROR("framebuffer init failed %d\n", ret);
10693 return ret;
10694 }
10695
Jesse Barnes79e53942008-11-07 14:24:08 -080010696 return 0;
10697}
10698
Jesse Barnes79e53942008-11-07 14:24:08 -080010699static struct drm_framebuffer *
10700intel_user_framebuffer_create(struct drm_device *dev,
10701 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010702 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010703{
Chris Wilson05394f32010-11-08 19:18:58 +000010704 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010705
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010706 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10707 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010708 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010709 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010710
Chris Wilsond2dff872011-04-19 08:36:26 +010010711 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010712}
10713
Daniel Vetter4520f532013-10-09 09:18:51 +020010714#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010715static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010716{
10717}
10718#endif
10719
Jesse Barnes79e53942008-11-07 14:24:08 -080010720static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010721 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010722 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010723};
10724
Jesse Barnese70236a2009-09-21 10:42:27 -070010725/* Set up chip specific display functions */
10726static void intel_init_display(struct drm_device *dev)
10727{
10728 struct drm_i915_private *dev_priv = dev->dev_private;
10729
Daniel Vetteree9300b2013-06-03 22:40:22 +020010730 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10731 dev_priv->display.find_dpll = g4x_find_best_dpll;
10732 else if (IS_VALLEYVIEW(dev))
10733 dev_priv->display.find_dpll = vlv_find_best_dpll;
10734 else if (IS_PINEVIEW(dev))
10735 dev_priv->display.find_dpll = pnv_find_best_dpll;
10736 else
10737 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10738
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010739 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010740 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010741 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010742 dev_priv->display.crtc_enable = haswell_crtc_enable;
10743 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010744 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010745 dev_priv->display.update_plane = ironlake_update_plane;
10746 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010747 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010748 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010749 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10750 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010751 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010752 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010753 } else if (IS_VALLEYVIEW(dev)) {
10754 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10755 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10756 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10757 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10758 dev_priv->display.off = i9xx_crtc_off;
10759 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010760 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010761 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010762 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010763 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10764 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010765 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010766 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010767 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010768
Jesse Barnese70236a2009-09-21 10:42:27 -070010769 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010770 if (IS_VALLEYVIEW(dev))
10771 dev_priv->display.get_display_clock_speed =
10772 valleyview_get_display_clock_speed;
10773 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010774 dev_priv->display.get_display_clock_speed =
10775 i945_get_display_clock_speed;
10776 else if (IS_I915G(dev))
10777 dev_priv->display.get_display_clock_speed =
10778 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010779 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010780 dev_priv->display.get_display_clock_speed =
10781 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010782 else if (IS_PINEVIEW(dev))
10783 dev_priv->display.get_display_clock_speed =
10784 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010785 else if (IS_I915GM(dev))
10786 dev_priv->display.get_display_clock_speed =
10787 i915gm_get_display_clock_speed;
10788 else if (IS_I865G(dev))
10789 dev_priv->display.get_display_clock_speed =
10790 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010791 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010792 dev_priv->display.get_display_clock_speed =
10793 i855_get_display_clock_speed;
10794 else /* 852, 830 */
10795 dev_priv->display.get_display_clock_speed =
10796 i830_get_display_clock_speed;
10797
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010798 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010799 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010800 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010801 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010802 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010803 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010804 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010805 } else if (IS_IVYBRIDGE(dev)) {
10806 /* FIXME: detect B0+ stepping and use auto training */
10807 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010808 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010809 dev_priv->display.modeset_global_resources =
10810 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010811 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010812 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010813 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010814 dev_priv->display.modeset_global_resources =
10815 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010816 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010817 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010818 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010819 } else if (IS_VALLEYVIEW(dev)) {
10820 dev_priv->display.modeset_global_resources =
10821 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040010822 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010823 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010824
10825 /* Default just returns -ENODEV to indicate unsupported */
10826 dev_priv->display.queue_flip = intel_default_queue_flip;
10827
10828 switch (INTEL_INFO(dev)->gen) {
10829 case 2:
10830 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10831 break;
10832
10833 case 3:
10834 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10835 break;
10836
10837 case 4:
10838 case 5:
10839 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10840 break;
10841
10842 case 6:
10843 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10844 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010845 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010846 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010847 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10848 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010849 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020010850
10851 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010852}
10853
Jesse Barnesb690e962010-07-19 13:53:12 -070010854/*
10855 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10856 * resume, or other times. This quirk makes sure that's the case for
10857 * affected systems.
10858 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010859static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010860{
10861 struct drm_i915_private *dev_priv = dev->dev_private;
10862
10863 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010864 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010865}
10866
Keith Packard435793d2011-07-12 14:56:22 -070010867/*
10868 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10869 */
10870static void quirk_ssc_force_disable(struct drm_device *dev)
10871{
10872 struct drm_i915_private *dev_priv = dev->dev_private;
10873 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010874 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010875}
10876
Carsten Emde4dca20e2012-03-15 15:56:26 +010010877/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010878 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10879 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010880 */
10881static void quirk_invert_brightness(struct drm_device *dev)
10882{
10883 struct drm_i915_private *dev_priv = dev->dev_private;
10884 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010885 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010886}
10887
10888struct intel_quirk {
10889 int device;
10890 int subsystem_vendor;
10891 int subsystem_device;
10892 void (*hook)(struct drm_device *dev);
10893};
10894
Egbert Eich5f85f1762012-10-14 15:46:38 +020010895/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10896struct intel_dmi_quirk {
10897 void (*hook)(struct drm_device *dev);
10898 const struct dmi_system_id (*dmi_id_list)[];
10899};
10900
10901static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10902{
10903 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10904 return 1;
10905}
10906
10907static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10908 {
10909 .dmi_id_list = &(const struct dmi_system_id[]) {
10910 {
10911 .callback = intel_dmi_reverse_brightness,
10912 .ident = "NCR Corporation",
10913 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10914 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10915 },
10916 },
10917 { } /* terminating entry */
10918 },
10919 .hook = quirk_invert_brightness,
10920 },
10921};
10922
Ben Widawskyc43b5632012-04-16 14:07:40 -070010923static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010924 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010925 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010926
Jesse Barnesb690e962010-07-19 13:53:12 -070010927 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10928 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10929
Jesse Barnesb690e962010-07-19 13:53:12 -070010930 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10931 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10932
Chris Wilsona4945f92013-10-08 11:16:59 +010010933 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010934 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010935
10936 /* Lenovo U160 cannot use SSC on LVDS */
10937 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010938
10939 /* Sony Vaio Y cannot use SSC on LVDS */
10940 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010941
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010010942 /* Acer Aspire 5734Z must invert backlight brightness */
10943 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
10944
10945 /* Acer/eMachines G725 */
10946 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
10947
10948 /* Acer/eMachines e725 */
10949 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
10950
10951 /* Acer/Packard Bell NCL20 */
10952 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
10953
10954 /* Acer Aspire 4736Z */
10955 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020010956
10957 /* Acer Aspire 5336 */
10958 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070010959};
10960
10961static void intel_init_quirks(struct drm_device *dev)
10962{
10963 struct pci_dev *d = dev->pdev;
10964 int i;
10965
10966 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10967 struct intel_quirk *q = &intel_quirks[i];
10968
10969 if (d->device == q->device &&
10970 (d->subsystem_vendor == q->subsystem_vendor ||
10971 q->subsystem_vendor == PCI_ANY_ID) &&
10972 (d->subsystem_device == q->subsystem_device ||
10973 q->subsystem_device == PCI_ANY_ID))
10974 q->hook(dev);
10975 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010976 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10977 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10978 intel_dmi_quirks[i].hook(dev);
10979 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010980}
10981
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010982/* Disable the VGA plane that we never use */
10983static void i915_disable_vga(struct drm_device *dev)
10984{
10985 struct drm_i915_private *dev_priv = dev->dev_private;
10986 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010987 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010988
Ville Syrjälä2b37c612014-01-22 21:32:38 +020010989 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010990 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010991 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010992 sr1 = inb(VGA_SR_DATA);
10993 outb(sr1 | 1<<5, VGA_SR_DATA);
10994 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10995 udelay(300);
10996
10997 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10998 POSTING_READ(vga_reg);
10999}
11000
Daniel Vetterf8175862012-04-10 15:50:11 +020011001void intel_modeset_init_hw(struct drm_device *dev)
11002{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030011003 intel_prepare_ddi(dev);
11004
Daniel Vetterf8175862012-04-10 15:50:11 +020011005 intel_init_clock_gating(dev);
11006
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011007 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070011008
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020011009 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011010 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020011011 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020011012}
11013
Imre Deak7d708ee2013-04-17 14:04:50 +030011014void intel_modeset_suspend_hw(struct drm_device *dev)
11015{
11016 intel_suspend_hw(dev);
11017}
11018
Jesse Barnes79e53942008-11-07 14:24:08 -080011019void intel_modeset_init(struct drm_device *dev)
11020{
Jesse Barnes652c3932009-08-17 13:31:43 -070011021 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011022 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011023
11024 drm_mode_config_init(dev);
11025
11026 dev->mode_config.min_width = 0;
11027 dev->mode_config.min_height = 0;
11028
Dave Airlie019d96c2011-09-29 16:20:42 +010011029 dev->mode_config.preferred_depth = 24;
11030 dev->mode_config.prefer_shadow = 1;
11031
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020011032 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080011033
Jesse Barnesb690e962010-07-19 13:53:12 -070011034 intel_init_quirks(dev);
11035
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030011036 intel_init_pm(dev);
11037
Ben Widawskye3c74752013-04-05 13:12:39 -070011038 if (INTEL_INFO(dev)->num_pipes == 0)
11039 return;
11040
Jesse Barnese70236a2009-09-21 10:42:27 -070011041 intel_init_display(dev);
11042
Chris Wilsona6c45cf2010-09-17 00:32:17 +010011043 if (IS_GEN2(dev)) {
11044 dev->mode_config.max_width = 2048;
11045 dev->mode_config.max_height = 2048;
11046 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070011047 dev->mode_config.max_width = 4096;
11048 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080011049 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010011050 dev->mode_config.max_width = 8192;
11051 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080011052 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080011053 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011054
Zhao Yakui28c97732009-10-09 11:39:41 +080011055 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011056 INTEL_INFO(dev)->num_pipes,
11057 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080011058
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010011059 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080011060 intel_crtc_init(dev, i);
Damien Lespiau22d3fd462014-02-07 19:12:49 +000011061 for (j = 0; j < INTEL_INFO(dev)->num_sprites; j++) {
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011062 ret = intel_plane_init(dev, i, j);
11063 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030011064 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
11065 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011066 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011067 }
11068
Jesse Barnesf42bb702013-12-16 16:34:23 -080011069 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011070 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080011071
Paulo Zanoni79f689a2012-10-05 12:05:52 -030011072 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011073 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011074
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011075 /* Just disable it once at startup */
11076 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011077 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000011078
11079 /* Just in case the BIOS is doing something questionable. */
11080 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011081}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080011082
Daniel Vetter24929352012-07-02 20:28:59 +020011083static void
11084intel_connector_break_all_links(struct intel_connector *connector)
11085{
11086 connector->base.dpms = DRM_MODE_DPMS_OFF;
11087 connector->base.encoder = NULL;
11088 connector->encoder->connectors_active = false;
11089 connector->encoder->base.crtc = NULL;
11090}
11091
Daniel Vetter7fad7982012-07-04 17:51:47 +020011092static void intel_enable_pipe_a(struct drm_device *dev)
11093{
11094 struct intel_connector *connector;
11095 struct drm_connector *crt = NULL;
11096 struct intel_load_detect_pipe load_detect_temp;
11097
11098 /* We can't just switch on the pipe A, we need to set things up with a
11099 * proper mode and output configuration. As a gross hack, enable pipe A
11100 * by enabling the load detect pipe once. */
11101 list_for_each_entry(connector,
11102 &dev->mode_config.connector_list,
11103 base.head) {
11104 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
11105 crt = &connector->base;
11106 break;
11107 }
11108 }
11109
11110 if (!crt)
11111 return;
11112
11113 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
11114 intel_release_load_detect_pipe(crt, &load_detect_temp);
11115
11116
11117}
11118
Daniel Vetterfa555832012-10-10 23:14:00 +020011119static bool
11120intel_check_plane_mapping(struct intel_crtc *crtc)
11121{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011122 struct drm_device *dev = crtc->base.dev;
11123 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020011124 u32 reg, val;
11125
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011126 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020011127 return true;
11128
11129 reg = DSPCNTR(!crtc->plane);
11130 val = I915_READ(reg);
11131
11132 if ((val & DISPLAY_PLANE_ENABLE) &&
11133 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
11134 return false;
11135
11136 return true;
11137}
11138
Daniel Vetter24929352012-07-02 20:28:59 +020011139static void intel_sanitize_crtc(struct intel_crtc *crtc)
11140{
11141 struct drm_device *dev = crtc->base.dev;
11142 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020011143 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020011144
Daniel Vetter24929352012-07-02 20:28:59 +020011145 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020011146 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020011147 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
11148
11149 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020011150 * disable the crtc (and hence change the state) if it is wrong. Note
11151 * that gen4+ has a fixed plane -> pipe mapping. */
11152 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020011153 struct intel_connector *connector;
11154 bool plane;
11155
Daniel Vetter24929352012-07-02 20:28:59 +020011156 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
11157 crtc->base.base.id);
11158
11159 /* Pipe has the wrong plane attached and the plane is active.
11160 * Temporarily change the plane mapping and disable everything
11161 * ... */
11162 plane = crtc->plane;
11163 crtc->plane = !plane;
11164 dev_priv->display.crtc_disable(&crtc->base);
11165 crtc->plane = plane;
11166
11167 /* ... and break all links. */
11168 list_for_each_entry(connector, &dev->mode_config.connector_list,
11169 base.head) {
11170 if (connector->encoder->base.crtc != &crtc->base)
11171 continue;
11172
11173 intel_connector_break_all_links(connector);
11174 }
11175
11176 WARN_ON(crtc->active);
11177 crtc->base.enabled = false;
11178 }
Daniel Vetter24929352012-07-02 20:28:59 +020011179
Daniel Vetter7fad7982012-07-04 17:51:47 +020011180 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11181 crtc->pipe == PIPE_A && !crtc->active) {
11182 /* BIOS forgot to enable pipe A, this mostly happens after
11183 * resume. Force-enable the pipe to fix this, the update_dpms
11184 * call below we restore the pipe to the right state, but leave
11185 * the required bits on. */
11186 intel_enable_pipe_a(dev);
11187 }
11188
Daniel Vetter24929352012-07-02 20:28:59 +020011189 /* Adjust the state of the output pipe according to whether we
11190 * have active connectors/encoders. */
11191 intel_crtc_update_dpms(&crtc->base);
11192
11193 if (crtc->active != crtc->base.enabled) {
11194 struct intel_encoder *encoder;
11195
11196 /* This can happen either due to bugs in the get_hw_state
11197 * functions or because the pipe is force-enabled due to the
11198 * pipe A quirk. */
11199 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11200 crtc->base.base.id,
11201 crtc->base.enabled ? "enabled" : "disabled",
11202 crtc->active ? "enabled" : "disabled");
11203
11204 crtc->base.enabled = crtc->active;
11205
11206 /* Because we only establish the connector -> encoder ->
11207 * crtc links if something is active, this means the
11208 * crtc is now deactivated. Break the links. connector
11209 * -> encoder links are only establish when things are
11210 * actually up, hence no need to break them. */
11211 WARN_ON(crtc->active);
11212
11213 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11214 WARN_ON(encoder->connectors_active);
11215 encoder->base.crtc = NULL;
11216 }
11217 }
11218}
11219
11220static void intel_sanitize_encoder(struct intel_encoder *encoder)
11221{
11222 struct intel_connector *connector;
11223 struct drm_device *dev = encoder->base.dev;
11224
11225 /* We need to check both for a crtc link (meaning that the
11226 * encoder is active and trying to read from a pipe) and the
11227 * pipe itself being active. */
11228 bool has_active_crtc = encoder->base.crtc &&
11229 to_intel_crtc(encoder->base.crtc)->active;
11230
11231 if (encoder->connectors_active && !has_active_crtc) {
11232 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11233 encoder->base.base.id,
11234 drm_get_encoder_name(&encoder->base));
11235
11236 /* Connector is active, but has no active pipe. This is
11237 * fallout from our resume register restoring. Disable
11238 * the encoder manually again. */
11239 if (encoder->base.crtc) {
11240 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11241 encoder->base.base.id,
11242 drm_get_encoder_name(&encoder->base));
11243 encoder->disable(encoder);
11244 }
11245
11246 /* Inconsistent output/port/pipe state happens presumably due to
11247 * a bug in one of the get_hw_state functions. Or someplace else
11248 * in our code, like the register restore mess on resume. Clamp
11249 * things to off as a safer default. */
11250 list_for_each_entry(connector,
11251 &dev->mode_config.connector_list,
11252 base.head) {
11253 if (connector->encoder != encoder)
11254 continue;
11255
11256 intel_connector_break_all_links(connector);
11257 }
11258 }
11259 /* Enabled encoders without active connectors will be fixed in
11260 * the crtc fixup. */
11261}
11262
Daniel Vetter44cec742013-01-25 17:53:21 +010011263void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011264{
11265 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011266 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011267
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011268 /* This function can be called both from intel_modeset_setup_hw_state or
11269 * at a very early point in our resume sequence, where the power well
11270 * structures are not yet restored. Since this function is at a very
11271 * paranoid "someone might have enabled VGA while we were not looking"
11272 * level, just check if the power well is enabled instead of trying to
11273 * follow the "don't touch the power well if we don't need it" policy
11274 * the rest of the driver uses. */
Jesse Barnesf9e711e2013-11-25 17:15:32 +020011275 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030011276 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011277 return;
11278
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030011279 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011280 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020011281 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011282 }
11283}
11284
Daniel Vetter30e984d2013-06-05 13:34:17 +020011285static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011286{
11287 struct drm_i915_private *dev_priv = dev->dev_private;
11288 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011289 struct intel_crtc *crtc;
11290 struct intel_encoder *encoder;
11291 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011292 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011293
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011294 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11295 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011296 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011297
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011298 crtc->active = dev_priv->display.get_pipe_config(crtc,
11299 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011300
11301 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030011302 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020011303
11304 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11305 crtc->base.base.id,
11306 crtc->active ? "enabled" : "disabled");
11307 }
11308
Daniel Vetter53589012013-06-05 13:34:16 +020011309 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011310 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011311 intel_ddi_setup_hw_pll_state(dev);
11312
Daniel Vetter53589012013-06-05 13:34:16 +020011313 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11314 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11315
11316 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11317 pll->active = 0;
11318 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11319 base.head) {
11320 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11321 pll->active++;
11322 }
11323 pll->refcount = pll->active;
11324
Daniel Vetter35c95372013-07-17 06:55:04 +020011325 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11326 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011327 }
11328
Daniel Vetter24929352012-07-02 20:28:59 +020011329 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11330 base.head) {
11331 pipe = 0;
11332
11333 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011334 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11335 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011336 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011337 } else {
11338 encoder->base.crtc = NULL;
11339 }
11340
11341 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011342 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011343 encoder->base.base.id,
11344 drm_get_encoder_name(&encoder->base),
11345 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011346 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011347 }
11348
11349 list_for_each_entry(connector, &dev->mode_config.connector_list,
11350 base.head) {
11351 if (connector->get_hw_state(connector)) {
11352 connector->base.dpms = DRM_MODE_DPMS_ON;
11353 connector->encoder->connectors_active = true;
11354 connector->base.encoder = &connector->encoder->base;
11355 } else {
11356 connector->base.dpms = DRM_MODE_DPMS_OFF;
11357 connector->base.encoder = NULL;
11358 }
11359 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11360 connector->base.base.id,
11361 drm_get_connector_name(&connector->base),
11362 connector->base.encoder ? "enabled" : "disabled");
11363 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011364}
11365
11366/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11367 * and i915 state tracking structures. */
11368void intel_modeset_setup_hw_state(struct drm_device *dev,
11369 bool force_restore)
11370{
11371 struct drm_i915_private *dev_priv = dev->dev_private;
11372 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011373 struct intel_crtc *crtc;
11374 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011375 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011376
11377 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011378
Jesse Barnesbabea612013-06-26 18:57:38 +030011379 /*
11380 * Now that we have the config, copy it to each CRTC struct
11381 * Note that this could go away if we move to using crtc_config
11382 * checking everywhere.
11383 */
11384 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11385 base.head) {
Jani Nikulad330a952014-01-21 11:24:25 +020011386 if (crtc->active && i915.fastboot) {
Jesse Barnesbabea612013-06-26 18:57:38 +030011387 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11388
11389 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11390 crtc->base.base.id);
11391 drm_mode_debug_printmodeline(&crtc->base.mode);
11392 }
11393 }
11394
Daniel Vetter24929352012-07-02 20:28:59 +020011395 /* HW state is read out, now we need to sanitize this mess. */
11396 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11397 base.head) {
11398 intel_sanitize_encoder(encoder);
11399 }
11400
11401 for_each_pipe(pipe) {
11402 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11403 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011404 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011405 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011406
Daniel Vetter35c95372013-07-17 06:55:04 +020011407 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11408 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11409
11410 if (!pll->on || pll->active)
11411 continue;
11412
11413 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11414
11415 pll->disable(dev_priv, pll);
11416 pll->on = false;
11417 }
11418
Ville Syrjälä96f90c52013-12-05 15:51:38 +020011419 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011420 ilk_wm_get_hw_state(dev);
11421
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011422 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011423 i915_redisable_vga(dev);
11424
Daniel Vetterf30da182013-04-11 20:22:50 +020011425 /*
11426 * We need to use raw interfaces for restoring state to avoid
11427 * checking (bogus) intermediate states.
11428 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011429 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011430 struct drm_crtc *crtc =
11431 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011432
11433 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11434 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011435 }
11436 } else {
11437 intel_modeset_update_staged_output_state(dev);
11438 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011439
11440 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011441}
11442
11443void intel_modeset_gem_init(struct drm_device *dev)
11444{
Chris Wilson1833b132012-05-09 11:56:28 +010011445 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011446
11447 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011448
Ville Syrjälä7ad228b2014-01-07 16:15:36 +020011449 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011450 intel_modeset_setup_hw_state(dev, false);
Ville Syrjälä7ad228b2014-01-07 16:15:36 +020011451 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080011452}
11453
11454void intel_modeset_cleanup(struct drm_device *dev)
11455{
Jesse Barnes652c3932009-08-17 13:31:43 -070011456 struct drm_i915_private *dev_priv = dev->dev_private;
11457 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011458 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011459
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011460 /*
11461 * Interrupts and polling as the first thing to avoid creating havoc.
11462 * Too much stuff here (turning of rps, connectors, ...) would
11463 * experience fancy races otherwise.
11464 */
11465 drm_irq_uninstall(dev);
11466 cancel_work_sync(&dev_priv->hotplug_work);
11467 /*
11468 * Due to the hpd irq storm handling the hotplug work can re-arm the
11469 * poll handlers. Hence disable polling after hpd handling is shut down.
11470 */
Keith Packardf87ea762010-10-03 19:36:26 -070011471 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011472
Jesse Barnes652c3932009-08-17 13:31:43 -070011473 mutex_lock(&dev->struct_mutex);
11474
Jesse Barnes723bfd72010-10-07 16:01:13 -070011475 intel_unregister_dsm_handler();
11476
Jesse Barnes652c3932009-08-17 13:31:43 -070011477 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11478 /* Skip inactive CRTCs */
11479 if (!crtc->fb)
11480 continue;
11481
Daniel Vetter3dec0092010-08-20 21:40:52 +020011482 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011483 }
11484
Chris Wilson973d04f2011-07-08 12:22:37 +010011485 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011486
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011487 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011488
Daniel Vetter930ebb42012-06-29 23:32:16 +020011489 ironlake_teardown_rc6(dev);
11490
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011491 mutex_unlock(&dev->struct_mutex);
11492
Chris Wilson1630fe72011-07-08 12:22:42 +010011493 /* flush any delayed tasks or pending work */
11494 flush_scheduled_work();
11495
Jani Nikuladb31af12013-11-08 16:48:53 +020011496 /* destroy the backlight and sysfs files before encoders/connectors */
11497 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11498 intel_panel_destroy_backlight(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030011499 drm_sysfs_connector_remove(connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011500 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011501
Jesse Barnes79e53942008-11-07 14:24:08 -080011502 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011503
11504 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011505}
11506
Dave Airlie28d52042009-09-21 14:33:58 +100011507/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011508 * Return which encoder is currently attached for connector.
11509 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011510struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011511{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011512 return &intel_attached_encoder(connector)->base;
11513}
Jesse Barnes79e53942008-11-07 14:24:08 -080011514
Chris Wilsondf0e9242010-09-09 16:20:55 +010011515void intel_connector_attach_encoder(struct intel_connector *connector,
11516 struct intel_encoder *encoder)
11517{
11518 connector->encoder = encoder;
11519 drm_mode_connector_attach_encoder(&connector->base,
11520 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011521}
Dave Airlie28d52042009-09-21 14:33:58 +100011522
11523/*
11524 * set vga decode state - true == enable VGA decode
11525 */
11526int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11527{
11528 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000011529 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100011530 u16 gmch_ctrl;
11531
Chris Wilson75fa0412014-02-07 18:37:02 -020011532 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
11533 DRM_ERROR("failed to read control word\n");
11534 return -EIO;
11535 }
11536
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020011537 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
11538 return 0;
11539
Dave Airlie28d52042009-09-21 14:33:58 +100011540 if (state)
11541 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11542 else
11543 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020011544
11545 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
11546 DRM_ERROR("failed to write control word\n");
11547 return -EIO;
11548 }
11549
Dave Airlie28d52042009-09-21 14:33:58 +100011550 return 0;
11551}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011552
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011553struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011554
11555 u32 power_well_driver;
11556
Chris Wilson63b66e52013-08-08 15:12:06 +020011557 int num_transcoders;
11558
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011559 struct intel_cursor_error_state {
11560 u32 control;
11561 u32 position;
11562 u32 base;
11563 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011564 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011565
11566 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011567 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011568 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011569 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011570
11571 struct intel_plane_error_state {
11572 u32 control;
11573 u32 stride;
11574 u32 size;
11575 u32 pos;
11576 u32 addr;
11577 u32 surface;
11578 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011579 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011580
11581 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011582 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020011583 enum transcoder cpu_transcoder;
11584
11585 u32 conf;
11586
11587 u32 htotal;
11588 u32 hblank;
11589 u32 hsync;
11590 u32 vtotal;
11591 u32 vblank;
11592 u32 vsync;
11593 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011594};
11595
11596struct intel_display_error_state *
11597intel_display_capture_error_state(struct drm_device *dev)
11598{
Akshay Joshi0206e352011-08-16 15:34:10 -040011599 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011600 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011601 int transcoders[] = {
11602 TRANSCODER_A,
11603 TRANSCODER_B,
11604 TRANSCODER_C,
11605 TRANSCODER_EDP,
11606 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011607 int i;
11608
Chris Wilson63b66e52013-08-08 15:12:06 +020011609 if (INTEL_INFO(dev)->num_pipes == 0)
11610 return NULL;
11611
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011612 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011613 if (error == NULL)
11614 return NULL;
11615
Imre Deak190be112013-11-25 17:15:31 +020011616 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011617 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11618
Damien Lespiau52331302012-08-15 19:23:25 +010011619 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020011620 error->pipe[i].power_domain_on =
11621 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11622 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011623 continue;
11624
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011625 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11626 error->cursor[i].control = I915_READ(CURCNTR(i));
11627 error->cursor[i].position = I915_READ(CURPOS(i));
11628 error->cursor[i].base = I915_READ(CURBASE(i));
11629 } else {
11630 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11631 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11632 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11633 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011634
11635 error->plane[i].control = I915_READ(DSPCNTR(i));
11636 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011637 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011638 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011639 error->plane[i].pos = I915_READ(DSPPOS(i));
11640 }
Paulo Zanonica291362013-03-06 20:03:14 -030011641 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11642 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011643 if (INTEL_INFO(dev)->gen >= 4) {
11644 error->plane[i].surface = I915_READ(DSPSURF(i));
11645 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11646 }
11647
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011648 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011649 }
11650
11651 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11652 if (HAS_DDI(dev_priv->dev))
11653 error->num_transcoders++; /* Account for eDP. */
11654
11655 for (i = 0; i < error->num_transcoders; i++) {
11656 enum transcoder cpu_transcoder = transcoders[i];
11657
Imre Deakddf9c532013-11-27 22:02:02 +020011658 error->transcoder[i].power_domain_on =
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020011659 intel_display_power_enabled_sw(dev,
11660 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011661 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011662 continue;
11663
Chris Wilson63b66e52013-08-08 15:12:06 +020011664 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11665
11666 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11667 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11668 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11669 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11670 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11671 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11672 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011673 }
11674
11675 return error;
11676}
11677
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011678#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11679
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011680void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011681intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011682 struct drm_device *dev,
11683 struct intel_display_error_state *error)
11684{
11685 int i;
11686
Chris Wilson63b66e52013-08-08 15:12:06 +020011687 if (!error)
11688 return;
11689
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011690 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020011691 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011692 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011693 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011694 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011695 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020011696 err_printf(m, " Power: %s\n",
11697 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011698 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011699
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011700 err_printf(m, "Plane [%d]:\n", i);
11701 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11702 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011703 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011704 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11705 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011706 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011707 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011708 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011709 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011710 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11711 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011712 }
11713
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011714 err_printf(m, "Cursor [%d]:\n", i);
11715 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11716 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11717 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011718 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011719
11720 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011721 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011722 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011723 err_printf(m, " Power: %s\n",
11724 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020011725 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11726 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11727 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11728 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11729 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11730 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11731 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11732 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011733}