blob: c4d3f439168625bfe634f521f9b133c7ff9392a9 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Daniel Vetter3dec0092010-08-20 21:40:52 +020044static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010045static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080046
Jesse Barnesf1f644d2013-06-27 00:39:25 +030047static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030049static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030051
Damien Lespiaue7457a92013-08-08 22:28:59 +010052static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
54
55
Jesse Barnes79e53942008-11-07 14:24:08 -080056typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040057 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_range_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int dot_limit;
62 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080063} intel_p2_t;
64
Ma Lingd4906092009-03-18 20:13:27 +080065typedef struct intel_limit intel_limit_t;
66struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 intel_range_t dot, vco, n, m, m1, m2, p, p1;
68 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080069};
Jesse Barnes79e53942008-11-07 14:24:08 -080070
Daniel Vetterd2acd212012-10-20 20:57:43 +020071int
72intel_pch_rawclk(struct drm_device *dev)
73{
74 struct drm_i915_private *dev_priv = dev->dev_private;
75
76 WARN_ON(!HAS_PCH_SPLIT(dev));
77
78 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
79}
80
Chris Wilson021357a2010-09-07 20:54:59 +010081static inline u32 /* units of 100MHz */
82intel_fdi_link_freq(struct drm_device *dev)
83{
Chris Wilson8b99e682010-10-13 09:59:17 +010084 if (IS_GEN5(dev)) {
85 struct drm_i915_private *dev_priv = dev->dev_private;
86 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 } else
88 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010089}
90
Daniel Vetter5d536e22013-07-06 12:52:06 +020091static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040092 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +020093 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +020094 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -040095 .m = { .min = 96, .max = 140 },
96 .m1 = { .min = 18, .max = 26 },
97 .m2 = { .min = 6, .max = 16 },
98 .p = { .min = 4, .max = 128 },
99 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700100 .p2 = { .dot_limit = 165000,
101 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700102};
103
Daniel Vetter5d536e22013-07-06 12:52:06 +0200104static const intel_limit_t intel_limits_i8xx_dvo = {
105 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200106 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200107 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200108 .m = { .min = 96, .max = 140 },
109 .m1 = { .min = 18, .max = 26 },
110 .m2 = { .min = 6, .max = 16 },
111 .p = { .min = 4, .max = 128 },
112 .p1 = { .min = 2, .max = 33 },
113 .p2 = { .dot_limit = 165000,
114 .p2_slow = 4, .p2_fast = 4 },
115};
116
Keith Packarde4b36692009-06-05 19:22:17 -0700117static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200119 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200120 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 .m = { .min = 96, .max = 140 },
122 .m1 = { .min = 18, .max = 26 },
123 .m2 = { .min = 6, .max = 16 },
124 .p = { .min = 4, .max = 128 },
125 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700126 .p2 = { .dot_limit = 165000,
127 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700128};
Eric Anholt273e27c2011-03-30 13:01:10 -0700129
Keith Packarde4b36692009-06-05 19:22:17 -0700130static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 .dot = { .min = 20000, .max = 400000 },
132 .vco = { .min = 1400000, .max = 2800000 },
133 .n = { .min = 1, .max = 6 },
134 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100135 .m1 = { .min = 8, .max = 18 },
136 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .p = { .min = 5, .max = 80 },
138 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700139 .p2 = { .dot_limit = 200000,
140 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700141};
142
143static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400144 .dot = { .min = 20000, .max = 400000 },
145 .vco = { .min = 1400000, .max = 2800000 },
146 .n = { .min = 1, .max = 6 },
147 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100148 .m1 = { .min = 8, .max = 18 },
149 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .p = { .min = 7, .max = 98 },
151 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700152 .p2 = { .dot_limit = 112000,
153 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700154};
155
Eric Anholt273e27c2011-03-30 13:01:10 -0700156
Keith Packarde4b36692009-06-05 19:22:17 -0700157static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .dot = { .min = 25000, .max = 270000 },
159 .vco = { .min = 1750000, .max = 3500000},
160 .n = { .min = 1, .max = 4 },
161 .m = { .min = 104, .max = 138 },
162 .m1 = { .min = 17, .max = 23 },
163 .m2 = { .min = 5, .max = 11 },
164 .p = { .min = 10, .max = 30 },
165 .p1 = { .min = 1, .max = 3},
166 .p2 = { .dot_limit = 270000,
167 .p2_slow = 10,
168 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800169 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
172static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .dot = { .min = 22000, .max = 400000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 16, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8},
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 20000, .max = 115000 },
187 .vco = { .min = 1750000, .max = 3500000 },
188 .n = { .min = 1, .max = 3 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 28, .max = 112 },
193 .p1 = { .min = 2, .max = 8 },
194 .p2 = { .dot_limit = 0,
195 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800196 },
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
198
199static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 80000, .max = 224000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 14, .max = 42 },
207 .p1 = { .min = 2, .max = 6 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800210 },
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500213static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400214 .dot = { .min = 20000, .max = 400000},
215 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .n = { .min = 3, .max = 6 },
218 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .m1 = { .min = 0, .max = 0 },
221 .m2 = { .min = 0, .max = 254 },
222 .p = { .min = 5, .max = 80 },
223 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700224 .p2 = { .dot_limit = 200000,
225 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500228static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 20000, .max = 400000 },
230 .vco = { .min = 1700000, .max = 3500000 },
231 .n = { .min = 3, .max = 6 },
232 .m = { .min = 2, .max = 256 },
233 .m1 = { .min = 0, .max = 0 },
234 .m2 = { .min = 0, .max = 254 },
235 .p = { .min = 7, .max = 112 },
236 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700237 .p2 = { .dot_limit = 112000,
238 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
Eric Anholt273e27c2011-03-30 13:01:10 -0700241/* Ironlake / Sandybridge
242 *
243 * We calculate clock using (register_value + 2) for N/M1/M2, so here
244 * the range value for them is (actual_value - 2).
245 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800246static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 25000, .max = 350000 },
248 .vco = { .min = 1760000, .max = 3510000 },
249 .n = { .min = 1, .max = 5 },
250 .m = { .min = 79, .max = 127 },
251 .m1 = { .min = 12, .max = 22 },
252 .m2 = { .min = 5, .max = 9 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
255 .p2 = { .dot_limit = 225000,
256 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800259static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .dot = { .min = 25000, .max = 350000 },
261 .vco = { .min = 1760000, .max = 3510000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 79, .max = 118 },
264 .m1 = { .min = 12, .max = 22 },
265 .m2 = { .min = 5, .max = 9 },
266 .p = { .min = 28, .max = 112 },
267 .p1 = { .min = 2, .max = 8 },
268 .p2 = { .dot_limit = 225000,
269 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800270};
271
272static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .dot = { .min = 25000, .max = 350000 },
274 .vco = { .min = 1760000, .max = 3510000 },
275 .n = { .min = 1, .max = 3 },
276 .m = { .min = 79, .max = 127 },
277 .m1 = { .min = 12, .max = 22 },
278 .m2 = { .min = 5, .max = 9 },
279 .p = { .min = 14, .max = 56 },
280 .p1 = { .min = 2, .max = 8 },
281 .p2 = { .dot_limit = 225000,
282 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800286static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .dot = { .min = 25000, .max = 350000 },
288 .vco = { .min = 1760000, .max = 3510000 },
289 .n = { .min = 1, .max = 2 },
290 .m = { .min = 79, .max = 126 },
291 .m1 = { .min = 12, .max = 22 },
292 .m2 = { .min = 5, .max = 9 },
293 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .p2 = { .dot_limit = 225000,
296 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 126 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800310};
311
Ville Syrjälädc730512013-09-24 21:26:30 +0300312static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300313 /*
314 * These are the data rate limits (measured in fast clocks)
315 * since those are the strictest limits we have. The fast
316 * clock and actual rate limits are more relaxed, so checking
317 * them would make no difference.
318 */
319 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200320 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700321 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .m1 = { .min = 2, .max = 3 },
323 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300324 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300325 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700326};
327
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300328static void vlv_clock(int refclk, intel_clock_t *clock)
329{
330 clock->m = clock->m1 * clock->m2;
331 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200332 if (WARN_ON(clock->n == 0 || clock->p == 0))
333 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300334 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
335 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300336}
337
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300338/**
339 * Returns whether any output on the specified pipe is of the specified type
340 */
341static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
342{
343 struct drm_device *dev = crtc->dev;
344 struct intel_encoder *encoder;
345
346 for_each_encoder_on_crtc(dev, crtc, encoder)
347 if (encoder->type == type)
348 return true;
349
350 return false;
351}
352
Chris Wilson1b894b52010-12-14 20:04:54 +0000353static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
354 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800355{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800356 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800357 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800358
359 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100360 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000361 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362 limit = &intel_limits_ironlake_dual_lvds_100m;
363 else
364 limit = &intel_limits_ironlake_dual_lvds;
365 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000366 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800367 limit = &intel_limits_ironlake_single_lvds_100m;
368 else
369 limit = &intel_limits_ironlake_single_lvds;
370 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200371 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800372 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800373
374 return limit;
375}
376
Ma Ling044c7c42009-03-18 20:13:23 +0800377static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
378{
379 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800380 const intel_limit_t *limit;
381
382 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100383 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700384 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800385 else
Keith Packarde4b36692009-06-05 19:22:17 -0700386 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800387 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
388 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700389 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800390 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700391 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800392 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700393 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800394
395 return limit;
396}
397
Chris Wilson1b894b52010-12-14 20:04:54 +0000398static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800399{
400 struct drm_device *dev = crtc->dev;
401 const intel_limit_t *limit;
402
Eric Anholtbad720f2009-10-22 16:11:14 -0700403 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000404 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800405 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800406 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500407 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800408 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500409 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800410 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500411 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700412 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300413 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100414 } else if (!IS_GEN2(dev)) {
415 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
416 limit = &intel_limits_i9xx_lvds;
417 else
418 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800419 } else {
420 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700421 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200422 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700423 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200424 else
425 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800426 }
427 return limit;
428}
429
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500430/* m1 is reserved as 0 in Pineview, n is a ring counter */
431static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800432{
Shaohua Li21778322009-02-23 15:19:16 +0800433 clock->m = clock->m2 + 2;
434 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200435 if (WARN_ON(clock->n == 0 || clock->p == 0))
436 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300437 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
438 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800439}
440
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200441static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
442{
443 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
444}
445
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200446static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800447{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200448 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800449 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200450 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
451 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300452 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
453 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800454}
455
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800456#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800457/**
458 * Returns whether the given set of divisors are valid for a given refclk with
459 * the given connectors.
460 */
461
Chris Wilson1b894b52010-12-14 20:04:54 +0000462static bool intel_PLL_is_valid(struct drm_device *dev,
463 const intel_limit_t *limit,
464 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800465{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300466 if (clock->n < limit->n.min || limit->n.max < clock->n)
467 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800468 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400469 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800470 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400471 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800472 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400473 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300474
475 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
476 if (clock->m1 <= clock->m2)
477 INTELPllInvalid("m1 <= m2\n");
478
479 if (!IS_VALLEYVIEW(dev)) {
480 if (clock->p < limit->p.min || limit->p.max < clock->p)
481 INTELPllInvalid("p out of range\n");
482 if (clock->m < limit->m.min || limit->m.max < clock->m)
483 INTELPllInvalid("m out of range\n");
484 }
485
Jesse Barnes79e53942008-11-07 14:24:08 -0800486 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400487 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800488 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
489 * connector, etc., rather than just a single range.
490 */
491 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400492 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800493
494 return true;
495}
496
Ma Lingd4906092009-03-18 20:13:27 +0800497static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200498i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800499 int target, int refclk, intel_clock_t *match_clock,
500 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800501{
502 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800503 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800504 int err = target;
505
Daniel Vettera210b022012-11-26 17:22:08 +0100506 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100508 * For LVDS just rely on its current settings for dual-channel.
509 * We haven't figured out how to reliably set up different
510 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800511 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100512 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 clock.p2 = limit->p2.p2_fast;
514 else
515 clock.p2 = limit->p2.p2_slow;
516 } else {
517 if (target < limit->p2.dot_limit)
518 clock.p2 = limit->p2.p2_slow;
519 else
520 clock.p2 = limit->p2.p2_fast;
521 }
522
Akshay Joshi0206e352011-08-16 15:34:10 -0400523 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800524
Zhao Yakui42158662009-11-20 11:24:18 +0800525 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
526 clock.m1++) {
527 for (clock.m2 = limit->m2.min;
528 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200529 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800530 break;
531 for (clock.n = limit->n.min;
532 clock.n <= limit->n.max; clock.n++) {
533 for (clock.p1 = limit->p1.min;
534 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800535 int this_err;
536
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200537 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000538 if (!intel_PLL_is_valid(dev, limit,
539 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800540 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800541 if (match_clock &&
542 clock.p != match_clock->p)
543 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800544
545 this_err = abs(clock.dot - target);
546 if (this_err < err) {
547 *best_clock = clock;
548 err = this_err;
549 }
550 }
551 }
552 }
553 }
554
555 return (err != target);
556}
557
Ma Lingd4906092009-03-18 20:13:27 +0800558static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200559pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
560 int target, int refclk, intel_clock_t *match_clock,
561 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200562{
563 struct drm_device *dev = crtc->dev;
564 intel_clock_t clock;
565 int err = target;
566
567 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
568 /*
569 * For LVDS just rely on its current settings for dual-channel.
570 * We haven't figured out how to reliably set up different
571 * single/dual channel state, if we even can.
572 */
573 if (intel_is_dual_link_lvds(dev))
574 clock.p2 = limit->p2.p2_fast;
575 else
576 clock.p2 = limit->p2.p2_slow;
577 } else {
578 if (target < limit->p2.dot_limit)
579 clock.p2 = limit->p2.p2_slow;
580 else
581 clock.p2 = limit->p2.p2_fast;
582 }
583
584 memset(best_clock, 0, sizeof(*best_clock));
585
586 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
587 clock.m1++) {
588 for (clock.m2 = limit->m2.min;
589 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200590 for (clock.n = limit->n.min;
591 clock.n <= limit->n.max; clock.n++) {
592 for (clock.p1 = limit->p1.min;
593 clock.p1 <= limit->p1.max; clock.p1++) {
594 int this_err;
595
596 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 if (!intel_PLL_is_valid(dev, limit,
598 &clock))
599 continue;
600 if (match_clock &&
601 clock.p != match_clock->p)
602 continue;
603
604 this_err = abs(clock.dot - target);
605 if (this_err < err) {
606 *best_clock = clock;
607 err = this_err;
608 }
609 }
610 }
611 }
612 }
613
614 return (err != target);
615}
616
Ma Lingd4906092009-03-18 20:13:27 +0800617static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200618g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
619 int target, int refclk, intel_clock_t *match_clock,
620 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800621{
622 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800623 intel_clock_t clock;
624 int max_n;
625 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400626 /* approximately equals target * 0.00585 */
627 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800628 found = false;
629
630 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100631 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800632 clock.p2 = limit->p2.p2_fast;
633 else
634 clock.p2 = limit->p2.p2_slow;
635 } else {
636 if (target < limit->p2.dot_limit)
637 clock.p2 = limit->p2.p2_slow;
638 else
639 clock.p2 = limit->p2.p2_fast;
640 }
641
642 memset(best_clock, 0, sizeof(*best_clock));
643 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200644 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800645 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200646 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800647 for (clock.m1 = limit->m1.max;
648 clock.m1 >= limit->m1.min; clock.m1--) {
649 for (clock.m2 = limit->m2.max;
650 clock.m2 >= limit->m2.min; clock.m2--) {
651 for (clock.p1 = limit->p1.max;
652 clock.p1 >= limit->p1.min; clock.p1--) {
653 int this_err;
654
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200655 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000656 if (!intel_PLL_is_valid(dev, limit,
657 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800658 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000659
660 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800661 if (this_err < err_most) {
662 *best_clock = clock;
663 err_most = this_err;
664 max_n = clock.n;
665 found = true;
666 }
667 }
668 }
669 }
670 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800671 return found;
672}
Ma Lingd4906092009-03-18 20:13:27 +0800673
Zhenyu Wang2c072452009-06-05 15:38:42 +0800674static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200675vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
676 int target, int refclk, intel_clock_t *match_clock,
677 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700678{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300679 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300680 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300681 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300682 /* min update 19.2 MHz */
683 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300684 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700685
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300686 target *= 5; /* fast clock */
687
688 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700689
690 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300691 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300692 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300693 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300694 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300695 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700696 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300697 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300698 unsigned int ppm, diff;
699
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300700 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
701 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300702
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300703 vlv_clock(refclk, &clock);
704
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300705 if (!intel_PLL_is_valid(dev, limit,
706 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300707 continue;
708
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300709 diff = abs(clock.dot - target);
710 ppm = div_u64(1000000ULL * diff, target);
711
712 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300713 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300714 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300715 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300716 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300717
Ville Syrjäläc6861222013-09-24 21:26:21 +0300718 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300719 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300720 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300721 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700722 }
723 }
724 }
725 }
726 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700727
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300728 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700729}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700730
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300731bool intel_crtc_active(struct drm_crtc *crtc)
732{
733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
734
735 /* Be paranoid as we can arrive here with only partial
736 * state retrieved from the hardware during setup.
737 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100738 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300739 * as Haswell has gained clock readout/fastboot support.
740 *
741 * We can ditch the crtc->fb check as soon as we can
742 * properly reconstruct framebuffers.
743 */
744 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100745 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300746}
747
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200748enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
749 enum pipe pipe)
750{
751 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
753
Daniel Vetter3b117c82013-04-17 20:15:07 +0200754 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200755}
756
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200757static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300758{
759 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200760 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300761
762 frame = I915_READ(frame_reg);
763
764 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
765 DRM_DEBUG_KMS("vblank wait timed out\n");
766}
767
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700768/**
769 * intel_wait_for_vblank - wait for vblank on a given pipe
770 * @dev: drm device
771 * @pipe: pipe to wait for
772 *
773 * Wait for vblank to occur on a given pipe. Needed for various bits of
774 * mode setting code.
775 */
776void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800777{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700778 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800779 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700780
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200781 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
782 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300783 return;
784 }
785
Chris Wilson300387c2010-09-05 20:25:43 +0100786 /* Clear existing vblank status. Note this will clear any other
787 * sticky status fields as well.
788 *
789 * This races with i915_driver_irq_handler() with the result
790 * that either function could miss a vblank event. Here it is not
791 * fatal, as we will either wait upon the next vblank interrupt or
792 * timeout. Generally speaking intel_wait_for_vblank() is only
793 * called during modeset at which time the GPU should be idle and
794 * should *not* be performing page flips and thus not waiting on
795 * vblanks...
796 * Currently, the result of us stealing a vblank from the irq
797 * handler is that a single frame will be skipped during swapbuffers.
798 */
799 I915_WRITE(pipestat_reg,
800 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
801
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700802 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100803 if (wait_for(I915_READ(pipestat_reg) &
804 PIPE_VBLANK_INTERRUPT_STATUS,
805 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700806 DRM_DEBUG_KMS("vblank wait timed out\n");
807}
808
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300809static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
810{
811 struct drm_i915_private *dev_priv = dev->dev_private;
812 u32 reg = PIPEDSL(pipe);
813 u32 line1, line2;
814 u32 line_mask;
815
816 if (IS_GEN2(dev))
817 line_mask = DSL_LINEMASK_GEN2;
818 else
819 line_mask = DSL_LINEMASK_GEN3;
820
821 line1 = I915_READ(reg) & line_mask;
822 mdelay(5);
823 line2 = I915_READ(reg) & line_mask;
824
825 return line1 == line2;
826}
827
Keith Packardab7ad7f2010-10-03 00:33:06 -0700828/*
829 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700830 * @dev: drm device
831 * @pipe: pipe to wait for
832 *
833 * After disabling a pipe, we can't wait for vblank in the usual way,
834 * spinning on the vblank interrupt status bit, since we won't actually
835 * see an interrupt when the pipe is disabled.
836 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700837 * On Gen4 and above:
838 * wait for the pipe register state bit to turn off
839 *
840 * Otherwise:
841 * wait for the display line value to settle (it usually
842 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100843 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700844 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100845void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700846{
847 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200848 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
849 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700850
Keith Packardab7ad7f2010-10-03 00:33:06 -0700851 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200852 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700853
Keith Packardab7ad7f2010-10-03 00:33:06 -0700854 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100855 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
856 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200857 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700858 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700859 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300860 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200861 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700862 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800863}
864
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000865/*
866 * ibx_digital_port_connected - is the specified port connected?
867 * @dev_priv: i915 private structure
868 * @port: the port to test
869 *
870 * Returns true if @port is connected, false otherwise.
871 */
872bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
873 struct intel_digital_port *port)
874{
875 u32 bit;
876
Damien Lespiauc36346e2012-12-13 16:09:03 +0000877 if (HAS_PCH_IBX(dev_priv->dev)) {
878 switch(port->port) {
879 case PORT_B:
880 bit = SDE_PORTB_HOTPLUG;
881 break;
882 case PORT_C:
883 bit = SDE_PORTC_HOTPLUG;
884 break;
885 case PORT_D:
886 bit = SDE_PORTD_HOTPLUG;
887 break;
888 default:
889 return true;
890 }
891 } else {
892 switch(port->port) {
893 case PORT_B:
894 bit = SDE_PORTB_HOTPLUG_CPT;
895 break;
896 case PORT_C:
897 bit = SDE_PORTC_HOTPLUG_CPT;
898 break;
899 case PORT_D:
900 bit = SDE_PORTD_HOTPLUG_CPT;
901 break;
902 default:
903 return true;
904 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000905 }
906
907 return I915_READ(SDEISR) & bit;
908}
909
Jesse Barnesb24e7172011-01-04 15:09:30 -0800910static const char *state_string(bool enabled)
911{
912 return enabled ? "on" : "off";
913}
914
915/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200916void assert_pll(struct drm_i915_private *dev_priv,
917 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800918{
919 int reg;
920 u32 val;
921 bool cur_state;
922
923 reg = DPLL(pipe);
924 val = I915_READ(reg);
925 cur_state = !!(val & DPLL_VCO_ENABLE);
926 WARN(cur_state != state,
927 "PLL state assertion failure (expected %s, current %s)\n",
928 state_string(state), state_string(cur_state));
929}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800930
Jani Nikula23538ef2013-08-27 15:12:22 +0300931/* XXX: the dsi pll is shared between MIPI DSI ports */
932static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
933{
934 u32 val;
935 bool cur_state;
936
937 mutex_lock(&dev_priv->dpio_lock);
938 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
939 mutex_unlock(&dev_priv->dpio_lock);
940
941 cur_state = val & DSI_PLL_VCO_EN;
942 WARN(cur_state != state,
943 "DSI PLL state assertion failure (expected %s, current %s)\n",
944 state_string(state), state_string(cur_state));
945}
946#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
947#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
948
Daniel Vetter55607e82013-06-16 21:42:39 +0200949struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200950intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800951{
Daniel Vettere2b78262013-06-07 23:10:03 +0200952 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
953
Daniel Vettera43f6e02013-06-07 23:10:32 +0200954 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200955 return NULL;
956
Daniel Vettera43f6e02013-06-07 23:10:32 +0200957 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200958}
959
Jesse Barnesb24e7172011-01-04 15:09:30 -0800960/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200961void assert_shared_dpll(struct drm_i915_private *dev_priv,
962 struct intel_shared_dpll *pll,
963 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800964{
Jesse Barnes040484a2011-01-03 12:14:26 -0800965 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200966 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800967
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300968 if (HAS_PCH_LPT(dev_priv->dev)) {
969 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
970 return;
971 }
972
Chris Wilson92b27b02012-05-20 18:10:50 +0100973 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200974 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100975 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100976
Daniel Vetter53589012013-06-05 13:34:16 +0200977 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100978 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200979 "%s assertion failure (expected %s, current %s)\n",
980 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800981}
Jesse Barnes040484a2011-01-03 12:14:26 -0800982
983static void assert_fdi_tx(struct drm_i915_private *dev_priv,
984 enum pipe pipe, bool state)
985{
986 int reg;
987 u32 val;
988 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200989 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
990 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800991
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200992 if (HAS_DDI(dev_priv->dev)) {
993 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200994 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300995 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200996 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300997 } else {
998 reg = FDI_TX_CTL(pipe);
999 val = I915_READ(reg);
1000 cur_state = !!(val & FDI_TX_ENABLE);
1001 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001002 WARN(cur_state != state,
1003 "FDI TX state assertion failure (expected %s, current %s)\n",
1004 state_string(state), state_string(cur_state));
1005}
1006#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1007#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1008
1009static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1010 enum pipe pipe, bool state)
1011{
1012 int reg;
1013 u32 val;
1014 bool cur_state;
1015
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001016 reg = FDI_RX_CTL(pipe);
1017 val = I915_READ(reg);
1018 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001019 WARN(cur_state != state,
1020 "FDI RX state assertion failure (expected %s, current %s)\n",
1021 state_string(state), state_string(cur_state));
1022}
1023#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1024#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1025
1026static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 int reg;
1030 u32 val;
1031
1032 /* ILK FDI PLL is always enabled */
1033 if (dev_priv->info->gen == 5)
1034 return;
1035
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001036 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001037 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001038 return;
1039
Jesse Barnes040484a2011-01-03 12:14:26 -08001040 reg = FDI_TX_CTL(pipe);
1041 val = I915_READ(reg);
1042 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1043}
1044
Daniel Vetter55607e82013-06-16 21:42:39 +02001045void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1046 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001047{
1048 int reg;
1049 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001050 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001051
1052 reg = FDI_RX_CTL(pipe);
1053 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001054 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1055 WARN(cur_state != state,
1056 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1057 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001058}
1059
Jesse Barnesea0760c2011-01-04 15:09:32 -08001060static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1061 enum pipe pipe)
1062{
1063 int pp_reg, lvds_reg;
1064 u32 val;
1065 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001066 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001067
1068 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1069 pp_reg = PCH_PP_CONTROL;
1070 lvds_reg = PCH_LVDS;
1071 } else {
1072 pp_reg = PP_CONTROL;
1073 lvds_reg = LVDS;
1074 }
1075
1076 val = I915_READ(pp_reg);
1077 if (!(val & PANEL_POWER_ON) ||
1078 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1079 locked = false;
1080
1081 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1082 panel_pipe = PIPE_B;
1083
1084 WARN(panel_pipe == pipe && locked,
1085 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001086 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001087}
1088
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001089static void assert_cursor(struct drm_i915_private *dev_priv,
1090 enum pipe pipe, bool state)
1091{
1092 struct drm_device *dev = dev_priv->dev;
1093 bool cur_state;
1094
1095 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1096 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1097 else if (IS_845G(dev) || IS_I865G(dev))
1098 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1099 else
1100 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1101
1102 WARN(cur_state != state,
1103 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1104 pipe_name(pipe), state_string(state), state_string(cur_state));
1105}
1106#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1107#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1108
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001109void assert_pipe(struct drm_i915_private *dev_priv,
1110 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001111{
1112 int reg;
1113 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001114 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001115 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1116 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001117
Daniel Vetter8e636782012-01-22 01:36:48 +01001118 /* if we need the pipe A quirk it must be always on */
1119 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1120 state = true;
1121
Paulo Zanonib97186f2013-05-03 12:15:36 -03001122 if (!intel_display_power_enabled(dev_priv->dev,
1123 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001124 cur_state = false;
1125 } else {
1126 reg = PIPECONF(cpu_transcoder);
1127 val = I915_READ(reg);
1128 cur_state = !!(val & PIPECONF_ENABLE);
1129 }
1130
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001131 WARN(cur_state != state,
1132 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001133 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134}
1135
Chris Wilson931872f2012-01-16 23:01:13 +00001136static void assert_plane(struct drm_i915_private *dev_priv,
1137 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138{
1139 int reg;
1140 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001141 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142
1143 reg = DSPCNTR(plane);
1144 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001145 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1146 WARN(cur_state != state,
1147 "plane %c assertion failure (expected %s, current %s)\n",
1148 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149}
1150
Chris Wilson931872f2012-01-16 23:01:13 +00001151#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1152#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1153
Jesse Barnesb24e7172011-01-04 15:09:30 -08001154static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1155 enum pipe pipe)
1156{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001157 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001158 int reg, i;
1159 u32 val;
1160 int cur_pipe;
1161
Ville Syrjälä653e1022013-06-04 13:49:05 +03001162 /* Primary planes are fixed to pipes on gen4+ */
1163 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001164 reg = DSPCNTR(pipe);
1165 val = I915_READ(reg);
1166 WARN((val & DISPLAY_PLANE_ENABLE),
1167 "plane %c assertion failure, should be disabled but not\n",
1168 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001169 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001170 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001171
Jesse Barnesb24e7172011-01-04 15:09:30 -08001172 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001173 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001174 reg = DSPCNTR(i);
1175 val = I915_READ(reg);
1176 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1177 DISPPLANE_SEL_PIPE_SHIFT;
1178 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001179 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1180 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001181 }
1182}
1183
Jesse Barnes19332d72013-03-28 09:55:38 -07001184static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1185 enum pipe pipe)
1186{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001187 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001188 int reg, i;
1189 u32 val;
1190
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001191 if (IS_VALLEYVIEW(dev)) {
1192 for (i = 0; i < dev_priv->num_plane; i++) {
1193 reg = SPCNTR(pipe, i);
1194 val = I915_READ(reg);
1195 WARN((val & SP_ENABLE),
1196 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1197 sprite_name(pipe, i), pipe_name(pipe));
1198 }
1199 } else if (INTEL_INFO(dev)->gen >= 7) {
1200 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001201 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001202 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001203 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001204 plane_name(pipe), pipe_name(pipe));
1205 } else if (INTEL_INFO(dev)->gen >= 5) {
1206 reg = DVSCNTR(pipe);
1207 val = I915_READ(reg);
1208 WARN((val & DVS_ENABLE),
1209 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1210 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001211 }
1212}
1213
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
Ville Syrjälä8212d562013-12-10 14:06:45 +02001367 /* Enable the CRI clock source so we can get at the display */
1368 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
1369 DPLL_INTEGRATED_CRI_CLK_VLV);
1370
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001371 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001372}
1373
1374static void intel_reset_dpio(struct drm_device *dev)
1375{
1376 struct drm_i915_private *dev_priv = dev->dev_private;
1377
1378 if (!IS_VALLEYVIEW(dev))
1379 return;
1380
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001381 /*
1382 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1383 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1384 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1385 * b. The other bits such as sfr settings / modesel may all be set
1386 * to 0.
1387 *
1388 * This should only be done on init and resume from S3 with both
1389 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1390 */
1391 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1392}
1393
Daniel Vetter426115c2013-07-11 22:13:42 +02001394static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001395{
Daniel Vetter426115c2013-07-11 22:13:42 +02001396 struct drm_device *dev = crtc->base.dev;
1397 struct drm_i915_private *dev_priv = dev->dev_private;
1398 int reg = DPLL(crtc->pipe);
1399 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001400
Daniel Vetter426115c2013-07-11 22:13:42 +02001401 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001402
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001403 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001404 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1405
1406 /* PLL is protected by panel, make sure we can write it */
1407 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001408 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001409
Daniel Vetter426115c2013-07-11 22:13:42 +02001410 I915_WRITE(reg, dpll);
1411 POSTING_READ(reg);
1412 udelay(150);
1413
1414 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1415 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1416
1417 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1418 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001419
1420 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001421 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001422 POSTING_READ(reg);
1423 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001424 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001425 POSTING_READ(reg);
1426 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001427 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001428 POSTING_READ(reg);
1429 udelay(150); /* wait for warmup */
1430}
1431
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001432static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001433{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001434 struct drm_device *dev = crtc->base.dev;
1435 struct drm_i915_private *dev_priv = dev->dev_private;
1436 int reg = DPLL(crtc->pipe);
1437 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001438
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001439 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001440
1441 /* No really, not for ILK+ */
1442 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001443
1444 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001445 if (IS_MOBILE(dev) && !IS_I830(dev))
1446 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001447
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001448 I915_WRITE(reg, dpll);
1449
1450 /* Wait for the clocks to stabilize. */
1451 POSTING_READ(reg);
1452 udelay(150);
1453
1454 if (INTEL_INFO(dev)->gen >= 4) {
1455 I915_WRITE(DPLL_MD(crtc->pipe),
1456 crtc->config.dpll_hw_state.dpll_md);
1457 } else {
1458 /* The pixel multiplier can only be updated once the
1459 * DPLL is enabled and the clocks are stable.
1460 *
1461 * So write it again.
1462 */
1463 I915_WRITE(reg, dpll);
1464 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001465
1466 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001467 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001468 POSTING_READ(reg);
1469 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001470 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001471 POSTING_READ(reg);
1472 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001473 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001474 POSTING_READ(reg);
1475 udelay(150); /* wait for warmup */
1476}
1477
1478/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001479 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001480 * @dev_priv: i915 private structure
1481 * @pipe: pipe PLL to disable
1482 *
1483 * Disable the PLL for @pipe, making sure the pipe is off first.
1484 *
1485 * Note! This is for pre-ILK only.
1486 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001487static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001488{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001489 /* Don't disable pipe A or pipe A PLLs if needed */
1490 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1491 return;
1492
1493 /* Make sure the pipe isn't still relying on us */
1494 assert_pipe_disabled(dev_priv, pipe);
1495
Daniel Vetter50b44a42013-06-05 13:34:33 +02001496 I915_WRITE(DPLL(pipe), 0);
1497 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001498}
1499
Jesse Barnesf6071162013-10-01 10:41:38 -07001500static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1501{
1502 u32 val = 0;
1503
1504 /* Make sure the pipe isn't still relying on us */
1505 assert_pipe_disabled(dev_priv, pipe);
1506
1507 /* Leave integrated clock source enabled */
1508 if (pipe == PIPE_B)
1509 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1510 I915_WRITE(DPLL(pipe), val);
1511 POSTING_READ(DPLL(pipe));
1512}
1513
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001514void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1515 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001516{
1517 u32 port_mask;
1518
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001519 switch (dport->port) {
1520 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001521 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001522 break;
1523 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001524 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001525 break;
1526 default:
1527 BUG();
1528 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001529
1530 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1531 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Ville Syrjäläbe46ffd2013-11-29 13:21:49 +02001532 port_name(dport->port), I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001533}
1534
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001535/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001536 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001537 * @dev_priv: i915 private structure
1538 * @pipe: pipe PLL to enable
1539 *
1540 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1541 * drives the transcoder clock.
1542 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001543static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001544{
Daniel Vettere2b78262013-06-07 23:10:03 +02001545 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1546 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001547
Chris Wilson48da64a2012-05-13 20:16:12 +01001548 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001549 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001550 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001551 return;
1552
1553 if (WARN_ON(pll->refcount == 0))
1554 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001555
Daniel Vetter46edb022013-06-05 13:34:12 +02001556 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1557 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001558 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001559
Daniel Vettercdbd2312013-06-05 13:34:03 +02001560 if (pll->active++) {
1561 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001562 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001563 return;
1564 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001565 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001566
Daniel Vetter46edb022013-06-05 13:34:12 +02001567 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001568 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001569 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001570}
1571
Daniel Vettere2b78262013-06-07 23:10:03 +02001572static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001573{
Daniel Vettere2b78262013-06-07 23:10:03 +02001574 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1575 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001576
Jesse Barnes92f25842011-01-04 15:09:34 -08001577 /* PCH only available on ILK+ */
1578 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001579 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001580 return;
1581
Chris Wilson48da64a2012-05-13 20:16:12 +01001582 if (WARN_ON(pll->refcount == 0))
1583 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001584
Daniel Vetter46edb022013-06-05 13:34:12 +02001585 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1586 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001587 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001588
Chris Wilson48da64a2012-05-13 20:16:12 +01001589 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001590 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001591 return;
1592 }
1593
Daniel Vettere9d69442013-06-05 13:34:15 +02001594 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001595 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001596 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001597 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001598
Daniel Vetter46edb022013-06-05 13:34:12 +02001599 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001600 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001601 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001602}
1603
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001604static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1605 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001606{
Daniel Vetter23670b322012-11-01 09:15:30 +01001607 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001608 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001610 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001611
1612 /* PCH only available on ILK+ */
1613 BUG_ON(dev_priv->info->gen < 5);
1614
1615 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001616 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001617 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001618
1619 /* FDI must be feeding us bits for PCH ports */
1620 assert_fdi_tx_enabled(dev_priv, pipe);
1621 assert_fdi_rx_enabled(dev_priv, pipe);
1622
Daniel Vetter23670b322012-11-01 09:15:30 +01001623 if (HAS_PCH_CPT(dev)) {
1624 /* Workaround: Set the timing override bit before enabling the
1625 * pch transcoder. */
1626 reg = TRANS_CHICKEN2(pipe);
1627 val = I915_READ(reg);
1628 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1629 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001630 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001631
Daniel Vetterab9412b2013-05-03 11:49:46 +02001632 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001633 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001634 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001635
1636 if (HAS_PCH_IBX(dev_priv->dev)) {
1637 /*
1638 * make the BPC in transcoder be consistent with
1639 * that in pipeconf reg.
1640 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001641 val &= ~PIPECONF_BPC_MASK;
1642 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001643 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001644
1645 val &= ~TRANS_INTERLACE_MASK;
1646 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001647 if (HAS_PCH_IBX(dev_priv->dev) &&
1648 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1649 val |= TRANS_LEGACY_INTERLACED_ILK;
1650 else
1651 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001652 else
1653 val |= TRANS_PROGRESSIVE;
1654
Jesse Barnes040484a2011-01-03 12:14:26 -08001655 I915_WRITE(reg, val | TRANS_ENABLE);
1656 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001657 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001658}
1659
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001660static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001661 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001662{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001663 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001664
1665 /* PCH only available on ILK+ */
1666 BUG_ON(dev_priv->info->gen < 5);
1667
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001668 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001669 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001670 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001671
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001672 /* Workaround: set timing override bit. */
1673 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001674 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001675 I915_WRITE(_TRANSA_CHICKEN2, val);
1676
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001677 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001678 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001679
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001680 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1681 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001682 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001683 else
1684 val |= TRANS_PROGRESSIVE;
1685
Daniel Vetterab9412b2013-05-03 11:49:46 +02001686 I915_WRITE(LPT_TRANSCONF, val);
1687 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001688 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001689}
1690
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001691static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1692 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001693{
Daniel Vetter23670b322012-11-01 09:15:30 +01001694 struct drm_device *dev = dev_priv->dev;
1695 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001696
1697 /* FDI relies on the transcoder */
1698 assert_fdi_tx_disabled(dev_priv, pipe);
1699 assert_fdi_rx_disabled(dev_priv, pipe);
1700
Jesse Barnes291906f2011-02-02 12:28:03 -08001701 /* Ports must be off as well */
1702 assert_pch_ports_disabled(dev_priv, pipe);
1703
Daniel Vetterab9412b2013-05-03 11:49:46 +02001704 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001705 val = I915_READ(reg);
1706 val &= ~TRANS_ENABLE;
1707 I915_WRITE(reg, val);
1708 /* wait for PCH transcoder off, transcoder state */
1709 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001710 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001711
1712 if (!HAS_PCH_IBX(dev)) {
1713 /* Workaround: Clear the timing override chicken bit again. */
1714 reg = TRANS_CHICKEN2(pipe);
1715 val = I915_READ(reg);
1716 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1717 I915_WRITE(reg, val);
1718 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001719}
1720
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001721static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001722{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001723 u32 val;
1724
Daniel Vetterab9412b2013-05-03 11:49:46 +02001725 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001726 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001727 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001728 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001729 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001730 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001731
1732 /* Workaround: clear timing override bit. */
1733 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001734 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001735 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001736}
1737
1738/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001739 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001740 * @dev_priv: i915 private structure
1741 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001742 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001743 *
1744 * Enable @pipe, making sure that various hardware specific requirements
1745 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1746 *
1747 * @pipe should be %PIPE_A or %PIPE_B.
1748 *
1749 * Will wait until the pipe is actually running (i.e. first vblank) before
1750 * returning.
1751 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001752static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001753 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001754{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001755 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1756 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001757 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001758 int reg;
1759 u32 val;
1760
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001761 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001762 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001763 assert_sprites_disabled(dev_priv, pipe);
1764
Paulo Zanoni681e5812012-12-06 11:12:38 -02001765 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001766 pch_transcoder = TRANSCODER_A;
1767 else
1768 pch_transcoder = pipe;
1769
Jesse Barnesb24e7172011-01-04 15:09:30 -08001770 /*
1771 * A pipe without a PLL won't actually be able to drive bits from
1772 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1773 * need the check.
1774 */
1775 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001776 if (dsi)
1777 assert_dsi_pll_enabled(dev_priv);
1778 else
1779 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001780 else {
1781 if (pch_port) {
1782 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001783 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001784 assert_fdi_tx_pll_enabled(dev_priv,
1785 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001786 }
1787 /* FIXME: assert CPU port conditions for SNB+ */
1788 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001789
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001790 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001791 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001792 if (val & PIPECONF_ENABLE)
1793 return;
1794
1795 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001796 intel_wait_for_vblank(dev_priv->dev, pipe);
1797}
1798
1799/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001800 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001801 * @dev_priv: i915 private structure
1802 * @pipe: pipe to disable
1803 *
1804 * Disable @pipe, making sure that various hardware specific requirements
1805 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1806 *
1807 * @pipe should be %PIPE_A or %PIPE_B.
1808 *
1809 * Will wait until the pipe has shut down before returning.
1810 */
1811static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1812 enum pipe pipe)
1813{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001814 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1815 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001816 int reg;
1817 u32 val;
1818
1819 /*
1820 * Make sure planes won't keep trying to pump pixels to us,
1821 * or we might hang the display.
1822 */
1823 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001824 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001825 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001826
1827 /* Don't disable pipe A or pipe A PLLs if needed */
1828 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1829 return;
1830
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001831 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001832 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001833 if ((val & PIPECONF_ENABLE) == 0)
1834 return;
1835
1836 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001837 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1838}
1839
Keith Packardd74362c2011-07-28 14:47:14 -07001840/*
1841 * Plane regs are double buffered, going from enabled->disabled needs a
1842 * trigger in order to latch. The display address reg provides this.
1843 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001844void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1845 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001846{
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001847 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1848
1849 I915_WRITE(reg, I915_READ(reg));
1850 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001851}
1852
Jesse Barnesb24e7172011-01-04 15:09:30 -08001853/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001854 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001855 * @dev_priv: i915 private structure
1856 * @plane: plane to enable
1857 * @pipe: pipe being fed
1858 *
1859 * Enable @plane on @pipe, making sure that @pipe is running first.
1860 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001861static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1862 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001863{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001864 struct intel_crtc *intel_crtc =
1865 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001866 int reg;
1867 u32 val;
1868
1869 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1870 assert_pipe_enabled(dev_priv, pipe);
1871
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001872 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001873
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001874 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001875
Jesse Barnesb24e7172011-01-04 15:09:30 -08001876 reg = DSPCNTR(plane);
1877 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001878 if (val & DISPLAY_PLANE_ENABLE)
1879 return;
1880
1881 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001882 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001883 intel_wait_for_vblank(dev_priv->dev, pipe);
1884}
1885
Jesse Barnesb24e7172011-01-04 15:09:30 -08001886/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001887 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001888 * @dev_priv: i915 private structure
1889 * @plane: plane to disable
1890 * @pipe: pipe consuming the data
1891 *
1892 * Disable @plane; should be an independent operation.
1893 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001894static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1895 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001896{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001897 struct intel_crtc *intel_crtc =
1898 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001899 int reg;
1900 u32 val;
1901
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001902 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001903
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001904 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001905
Jesse Barnesb24e7172011-01-04 15:09:30 -08001906 reg = DSPCNTR(plane);
1907 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001908 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1909 return;
1910
1911 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001912 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001913 intel_wait_for_vblank(dev_priv->dev, pipe);
1914}
1915
Chris Wilson693db182013-03-05 14:52:39 +00001916static bool need_vtd_wa(struct drm_device *dev)
1917{
1918#ifdef CONFIG_INTEL_IOMMU
1919 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1920 return true;
1921#endif
1922 return false;
1923}
1924
Chris Wilson127bd2a2010-07-23 23:32:05 +01001925int
Chris Wilson48b956c2010-09-14 12:50:34 +01001926intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001927 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001928 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001929{
Chris Wilsonce453d82011-02-21 14:43:56 +00001930 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001931 u32 alignment;
1932 int ret;
1933
Chris Wilson05394f32010-11-08 19:18:58 +00001934 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001935 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001936 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1937 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001938 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001939 alignment = 4 * 1024;
1940 else
1941 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001942 break;
1943 case I915_TILING_X:
1944 /* pin() will align the object as required by fence */
1945 alignment = 0;
1946 break;
1947 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001948 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001949 return -EINVAL;
1950 default:
1951 BUG();
1952 }
1953
Chris Wilson693db182013-03-05 14:52:39 +00001954 /* Note that the w/a also requires 64 PTE of padding following the
1955 * bo. We currently fill all unused PTE with the shadow page and so
1956 * we should always have valid PTE following the scanout preventing
1957 * the VT-d warning.
1958 */
1959 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1960 alignment = 256 * 1024;
1961
Chris Wilsonce453d82011-02-21 14:43:56 +00001962 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001963 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001964 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001965 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001966
1967 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1968 * fence, whereas 965+ only requires a fence if using
1969 * framebuffer compression. For simplicity, we always install
1970 * a fence as the cost is not that onerous.
1971 */
Chris Wilson06d98132012-04-17 15:31:24 +01001972 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001973 if (ret)
1974 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001975
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001976 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001977
Chris Wilsonce453d82011-02-21 14:43:56 +00001978 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001979 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001980
1981err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001982 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001983err_interruptible:
1984 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001985 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001986}
1987
Chris Wilson1690e1e2011-12-14 13:57:08 +01001988void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1989{
1990 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001991 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001992}
1993
Daniel Vetterc2c75132012-07-05 12:17:30 +02001994/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1995 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001996unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1997 unsigned int tiling_mode,
1998 unsigned int cpp,
1999 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002000{
Chris Wilsonbc752862013-02-21 20:04:31 +00002001 if (tiling_mode != I915_TILING_NONE) {
2002 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002003
Chris Wilsonbc752862013-02-21 20:04:31 +00002004 tile_rows = *y / 8;
2005 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002006
Chris Wilsonbc752862013-02-21 20:04:31 +00002007 tiles = *x / (512/cpp);
2008 *x %= 512/cpp;
2009
2010 return tile_rows * pitch * 8 + tiles * 4096;
2011 } else {
2012 unsigned int offset;
2013
2014 offset = *y * pitch + *x * cpp;
2015 *y = 0;
2016 *x = (offset & 4095) / cpp;
2017 return offset & -4096;
2018 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002019}
2020
Jesse Barnes17638cd2011-06-24 12:19:23 -07002021static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2022 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002023{
2024 struct drm_device *dev = crtc->dev;
2025 struct drm_i915_private *dev_priv = dev->dev_private;
2026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2027 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002028 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002029 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002030 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002031 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002032 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002033
2034 switch (plane) {
2035 case 0:
2036 case 1:
2037 break;
2038 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002039 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002040 return -EINVAL;
2041 }
2042
2043 intel_fb = to_intel_framebuffer(fb);
2044 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002045
Chris Wilson5eddb702010-09-11 13:48:45 +01002046 reg = DSPCNTR(plane);
2047 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002048 /* Mask out pixel format bits in case we change it */
2049 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002050 switch (fb->pixel_format) {
2051 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002052 dspcntr |= DISPPLANE_8BPP;
2053 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002054 case DRM_FORMAT_XRGB1555:
2055 case DRM_FORMAT_ARGB1555:
2056 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002057 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002058 case DRM_FORMAT_RGB565:
2059 dspcntr |= DISPPLANE_BGRX565;
2060 break;
2061 case DRM_FORMAT_XRGB8888:
2062 case DRM_FORMAT_ARGB8888:
2063 dspcntr |= DISPPLANE_BGRX888;
2064 break;
2065 case DRM_FORMAT_XBGR8888:
2066 case DRM_FORMAT_ABGR8888:
2067 dspcntr |= DISPPLANE_RGBX888;
2068 break;
2069 case DRM_FORMAT_XRGB2101010:
2070 case DRM_FORMAT_ARGB2101010:
2071 dspcntr |= DISPPLANE_BGRX101010;
2072 break;
2073 case DRM_FORMAT_XBGR2101010:
2074 case DRM_FORMAT_ABGR2101010:
2075 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002076 break;
2077 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002078 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002079 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002080
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002081 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002082 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002083 dspcntr |= DISPPLANE_TILED;
2084 else
2085 dspcntr &= ~DISPPLANE_TILED;
2086 }
2087
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002088 if (IS_G4X(dev))
2089 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2090
Chris Wilson5eddb702010-09-11 13:48:45 +01002091 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002092
Daniel Vettere506a0c2012-07-05 12:17:29 +02002093 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002094
Daniel Vetterc2c75132012-07-05 12:17:30 +02002095 if (INTEL_INFO(dev)->gen >= 4) {
2096 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002097 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2098 fb->bits_per_pixel / 8,
2099 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002100 linear_offset -= intel_crtc->dspaddr_offset;
2101 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002102 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002103 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002104
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002105 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2106 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2107 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002108 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002109 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002110 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002111 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002112 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002113 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002114 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002115 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002116 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002117
Jesse Barnes17638cd2011-06-24 12:19:23 -07002118 return 0;
2119}
2120
2121static int ironlake_update_plane(struct drm_crtc *crtc,
2122 struct drm_framebuffer *fb, int x, int y)
2123{
2124 struct drm_device *dev = crtc->dev;
2125 struct drm_i915_private *dev_priv = dev->dev_private;
2126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2127 struct intel_framebuffer *intel_fb;
2128 struct drm_i915_gem_object *obj;
2129 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002130 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002131 u32 dspcntr;
2132 u32 reg;
2133
2134 switch (plane) {
2135 case 0:
2136 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002137 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002138 break;
2139 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002140 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 return -EINVAL;
2142 }
2143
2144 intel_fb = to_intel_framebuffer(fb);
2145 obj = intel_fb->obj;
2146
2147 reg = DSPCNTR(plane);
2148 dspcntr = I915_READ(reg);
2149 /* Mask out pixel format bits in case we change it */
2150 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002151 switch (fb->pixel_format) {
2152 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002153 dspcntr |= DISPPLANE_8BPP;
2154 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002155 case DRM_FORMAT_RGB565:
2156 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002157 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002158 case DRM_FORMAT_XRGB8888:
2159 case DRM_FORMAT_ARGB8888:
2160 dspcntr |= DISPPLANE_BGRX888;
2161 break;
2162 case DRM_FORMAT_XBGR8888:
2163 case DRM_FORMAT_ABGR8888:
2164 dspcntr |= DISPPLANE_RGBX888;
2165 break;
2166 case DRM_FORMAT_XRGB2101010:
2167 case DRM_FORMAT_ARGB2101010:
2168 dspcntr |= DISPPLANE_BGRX101010;
2169 break;
2170 case DRM_FORMAT_XBGR2101010:
2171 case DRM_FORMAT_ABGR2101010:
2172 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002173 break;
2174 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002175 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002176 }
2177
2178 if (obj->tiling_mode != I915_TILING_NONE)
2179 dspcntr |= DISPPLANE_TILED;
2180 else
2181 dspcntr &= ~DISPPLANE_TILED;
2182
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002183 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002184 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2185 else
2186 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002187
2188 I915_WRITE(reg, dspcntr);
2189
Daniel Vettere506a0c2012-07-05 12:17:29 +02002190 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002191 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002192 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2193 fb->bits_per_pixel / 8,
2194 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002195 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002196
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002197 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2198 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2199 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002200 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002201 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002202 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002203 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002204 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2205 } else {
2206 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2207 I915_WRITE(DSPLINOFF(plane), linear_offset);
2208 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002209 POSTING_READ(reg);
2210
2211 return 0;
2212}
2213
2214/* Assume fb object is pinned & idle & fenced and just update base pointers */
2215static int
2216intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2217 int x, int y, enum mode_set_atomic state)
2218{
2219 struct drm_device *dev = crtc->dev;
2220 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002221
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002222 if (dev_priv->display.disable_fbc)
2223 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002224 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002225
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002226 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002227}
2228
Ville Syrjälä96a02912013-02-18 19:08:49 +02002229void intel_display_handle_reset(struct drm_device *dev)
2230{
2231 struct drm_i915_private *dev_priv = dev->dev_private;
2232 struct drm_crtc *crtc;
2233
2234 /*
2235 * Flips in the rings have been nuked by the reset,
2236 * so complete all pending flips so that user space
2237 * will get its events and not get stuck.
2238 *
2239 * Also update the base address of all primary
2240 * planes to the the last fb to make sure we're
2241 * showing the correct fb after a reset.
2242 *
2243 * Need to make two loops over the crtcs so that we
2244 * don't try to grab a crtc mutex before the
2245 * pending_flip_queue really got woken up.
2246 */
2247
2248 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2250 enum plane plane = intel_crtc->plane;
2251
2252 intel_prepare_page_flip(dev, plane);
2253 intel_finish_page_flip_plane(dev, plane);
2254 }
2255
2256 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2258
2259 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002260 /*
2261 * FIXME: Once we have proper support for primary planes (and
2262 * disabling them without disabling the entire crtc) allow again
2263 * a NULL crtc->fb.
2264 */
2265 if (intel_crtc->active && crtc->fb)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002266 dev_priv->display.update_plane(crtc, crtc->fb,
2267 crtc->x, crtc->y);
2268 mutex_unlock(&crtc->mutex);
2269 }
2270}
2271
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002272static int
Chris Wilson14667a42012-04-03 17:58:35 +01002273intel_finish_fb(struct drm_framebuffer *old_fb)
2274{
2275 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2276 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2277 bool was_interruptible = dev_priv->mm.interruptible;
2278 int ret;
2279
Chris Wilson14667a42012-04-03 17:58:35 +01002280 /* Big Hammer, we also need to ensure that any pending
2281 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2282 * current scanout is retired before unpinning the old
2283 * framebuffer.
2284 *
2285 * This should only fail upon a hung GPU, in which case we
2286 * can safely continue.
2287 */
2288 dev_priv->mm.interruptible = false;
2289 ret = i915_gem_object_finish_gpu(obj);
2290 dev_priv->mm.interruptible = was_interruptible;
2291
2292 return ret;
2293}
2294
Ville Syrjälä198598d2012-10-31 17:50:24 +02002295static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2296{
2297 struct drm_device *dev = crtc->dev;
2298 struct drm_i915_master_private *master_priv;
2299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2300
2301 if (!dev->primary->master)
2302 return;
2303
2304 master_priv = dev->primary->master->driver_priv;
2305 if (!master_priv->sarea_priv)
2306 return;
2307
2308 switch (intel_crtc->pipe) {
2309 case 0:
2310 master_priv->sarea_priv->pipeA_x = x;
2311 master_priv->sarea_priv->pipeA_y = y;
2312 break;
2313 case 1:
2314 master_priv->sarea_priv->pipeB_x = x;
2315 master_priv->sarea_priv->pipeB_y = y;
2316 break;
2317 default:
2318 break;
2319 }
2320}
2321
Chris Wilson14667a42012-04-03 17:58:35 +01002322static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002323intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002324 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002325{
2326 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002327 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002329 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002330 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002331
2332 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002333 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002334 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002335 return 0;
2336 }
2337
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002338 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002339 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2340 plane_name(intel_crtc->plane),
2341 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002342 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002343 }
2344
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002345 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002346 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002347 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002348 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002349 if (ret != 0) {
2350 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002351 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002352 return ret;
2353 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002354
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002355 /*
2356 * Update pipe size and adjust fitter if needed: the reason for this is
2357 * that in compute_mode_changes we check the native mode (not the pfit
2358 * mode) to see if we can flip rather than do a full mode set. In the
2359 * fastboot case, we'll flip, but if we don't update the pipesrc and
2360 * pfit state, we'll end up with a big fb scanned out into the wrong
2361 * sized surface.
2362 *
2363 * To fix this properly, we need to hoist the checks up into
2364 * compute_mode_changes (or above), check the actual pfit state and
2365 * whether the platform allows pfit disable with pipe active, and only
2366 * then update the pipesrc and pfit state, even on the flip path.
2367 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002368 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002369 const struct drm_display_mode *adjusted_mode =
2370 &intel_crtc->config.adjusted_mode;
2371
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002372 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002373 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2374 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002375 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002376 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2377 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2378 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2379 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2380 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2381 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002382 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2383 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002384 }
2385
Daniel Vetter94352cf2012-07-05 22:51:56 +02002386 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002387 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002388 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002389 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002390 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002391 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002392 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002393
Daniel Vetter94352cf2012-07-05 22:51:56 +02002394 old_fb = crtc->fb;
2395 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002396 crtc->x = x;
2397 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002398
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002399 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002400 if (intel_crtc->active && old_fb != fb)
2401 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002402 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002403 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002404
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002405 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002406 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002407 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002408
Ville Syrjälä198598d2012-10-31 17:50:24 +02002409 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002410
2411 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002412}
2413
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002414static void intel_fdi_normal_train(struct drm_crtc *crtc)
2415{
2416 struct drm_device *dev = crtc->dev;
2417 struct drm_i915_private *dev_priv = dev->dev_private;
2418 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2419 int pipe = intel_crtc->pipe;
2420 u32 reg, temp;
2421
2422 /* enable normal train */
2423 reg = FDI_TX_CTL(pipe);
2424 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002425 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002426 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2427 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002428 } else {
2429 temp &= ~FDI_LINK_TRAIN_NONE;
2430 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002431 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002432 I915_WRITE(reg, temp);
2433
2434 reg = FDI_RX_CTL(pipe);
2435 temp = I915_READ(reg);
2436 if (HAS_PCH_CPT(dev)) {
2437 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2438 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2439 } else {
2440 temp &= ~FDI_LINK_TRAIN_NONE;
2441 temp |= FDI_LINK_TRAIN_NONE;
2442 }
2443 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2444
2445 /* wait one idle pattern time */
2446 POSTING_READ(reg);
2447 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002448
2449 /* IVB wants error correction enabled */
2450 if (IS_IVYBRIDGE(dev))
2451 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2452 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002453}
2454
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002455static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002456{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002457 return crtc->base.enabled && crtc->active &&
2458 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002459}
2460
Daniel Vetter01a415f2012-10-27 15:58:40 +02002461static void ivb_modeset_global_resources(struct drm_device *dev)
2462{
2463 struct drm_i915_private *dev_priv = dev->dev_private;
2464 struct intel_crtc *pipe_B_crtc =
2465 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2466 struct intel_crtc *pipe_C_crtc =
2467 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2468 uint32_t temp;
2469
Daniel Vetter1e833f42013-02-19 22:31:57 +01002470 /*
2471 * When everything is off disable fdi C so that we could enable fdi B
2472 * with all lanes. Note that we don't care about enabled pipes without
2473 * an enabled pch encoder.
2474 */
2475 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2476 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002477 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2478 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2479
2480 temp = I915_READ(SOUTH_CHICKEN1);
2481 temp &= ~FDI_BC_BIFURCATION_SELECT;
2482 DRM_DEBUG_KMS("disabling fdi C rx\n");
2483 I915_WRITE(SOUTH_CHICKEN1, temp);
2484 }
2485}
2486
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002487/* The FDI link training functions for ILK/Ibexpeak. */
2488static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2489{
2490 struct drm_device *dev = crtc->dev;
2491 struct drm_i915_private *dev_priv = dev->dev_private;
2492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2493 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002494 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002495 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002496
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002497 /* FDI needs bits from pipe & plane first */
2498 assert_pipe_enabled(dev_priv, pipe);
2499 assert_plane_enabled(dev_priv, plane);
2500
Adam Jacksone1a44742010-06-25 15:32:14 -04002501 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2502 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002503 reg = FDI_RX_IMR(pipe);
2504 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002505 temp &= ~FDI_RX_SYMBOL_LOCK;
2506 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002507 I915_WRITE(reg, temp);
2508 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002509 udelay(150);
2510
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002511 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 reg = FDI_TX_CTL(pipe);
2513 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002514 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2515 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516 temp &= ~FDI_LINK_TRAIN_NONE;
2517 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002518 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519
Chris Wilson5eddb702010-09-11 13:48:45 +01002520 reg = FDI_RX_CTL(pipe);
2521 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002522 temp &= ~FDI_LINK_TRAIN_NONE;
2523 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002524 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2525
2526 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002527 udelay(150);
2528
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002529 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002530 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2531 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2532 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002533
Chris Wilson5eddb702010-09-11 13:48:45 +01002534 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002535 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002536 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002537 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2538
2539 if ((temp & FDI_RX_BIT_LOCK)) {
2540 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002542 break;
2543 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002545 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002546 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547
2548 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002549 reg = FDI_TX_CTL(pipe);
2550 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002551 temp &= ~FDI_LINK_TRAIN_NONE;
2552 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002553 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002554
Chris Wilson5eddb702010-09-11 13:48:45 +01002555 reg = FDI_RX_CTL(pipe);
2556 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557 temp &= ~FDI_LINK_TRAIN_NONE;
2558 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002559 I915_WRITE(reg, temp);
2560
2561 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562 udelay(150);
2563
Chris Wilson5eddb702010-09-11 13:48:45 +01002564 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002565 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002567 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2568
2569 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002570 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571 DRM_DEBUG_KMS("FDI train 2 done.\n");
2572 break;
2573 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002575 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002576 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002577
2578 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002579
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002580}
2581
Akshay Joshi0206e352011-08-16 15:34:10 -04002582static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002583 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2584 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2585 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2586 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2587};
2588
2589/* The FDI link training functions for SNB/Cougarpoint. */
2590static void gen6_fdi_link_train(struct drm_crtc *crtc)
2591{
2592 struct drm_device *dev = crtc->dev;
2593 struct drm_i915_private *dev_priv = dev->dev_private;
2594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2595 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002596 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597
Adam Jacksone1a44742010-06-25 15:32:14 -04002598 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2599 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002600 reg = FDI_RX_IMR(pipe);
2601 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002602 temp &= ~FDI_RX_SYMBOL_LOCK;
2603 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002604 I915_WRITE(reg, temp);
2605
2606 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002607 udelay(150);
2608
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002609 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002610 reg = FDI_TX_CTL(pipe);
2611 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002612 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2613 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002614 temp &= ~FDI_LINK_TRAIN_NONE;
2615 temp |= FDI_LINK_TRAIN_PATTERN_1;
2616 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2617 /* SNB-B */
2618 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002619 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002620
Daniel Vetterd74cf322012-10-26 10:58:13 +02002621 I915_WRITE(FDI_RX_MISC(pipe),
2622 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2623
Chris Wilson5eddb702010-09-11 13:48:45 +01002624 reg = FDI_RX_CTL(pipe);
2625 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002626 if (HAS_PCH_CPT(dev)) {
2627 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2628 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2629 } else {
2630 temp &= ~FDI_LINK_TRAIN_NONE;
2631 temp |= FDI_LINK_TRAIN_PATTERN_1;
2632 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002633 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2634
2635 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002636 udelay(150);
2637
Akshay Joshi0206e352011-08-16 15:34:10 -04002638 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002639 reg = FDI_TX_CTL(pipe);
2640 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002641 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2642 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002643 I915_WRITE(reg, temp);
2644
2645 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002646 udelay(500);
2647
Sean Paulfa37d392012-03-02 12:53:39 -05002648 for (retry = 0; retry < 5; retry++) {
2649 reg = FDI_RX_IIR(pipe);
2650 temp = I915_READ(reg);
2651 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2652 if (temp & FDI_RX_BIT_LOCK) {
2653 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2654 DRM_DEBUG_KMS("FDI train 1 done.\n");
2655 break;
2656 }
2657 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002658 }
Sean Paulfa37d392012-03-02 12:53:39 -05002659 if (retry < 5)
2660 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002661 }
2662 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002663 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002664
2665 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002666 reg = FDI_TX_CTL(pipe);
2667 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002668 temp &= ~FDI_LINK_TRAIN_NONE;
2669 temp |= FDI_LINK_TRAIN_PATTERN_2;
2670 if (IS_GEN6(dev)) {
2671 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2672 /* SNB-B */
2673 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2674 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002675 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002676
Chris Wilson5eddb702010-09-11 13:48:45 +01002677 reg = FDI_RX_CTL(pipe);
2678 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002679 if (HAS_PCH_CPT(dev)) {
2680 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2681 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2682 } else {
2683 temp &= ~FDI_LINK_TRAIN_NONE;
2684 temp |= FDI_LINK_TRAIN_PATTERN_2;
2685 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002686 I915_WRITE(reg, temp);
2687
2688 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002689 udelay(150);
2690
Akshay Joshi0206e352011-08-16 15:34:10 -04002691 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002692 reg = FDI_TX_CTL(pipe);
2693 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002694 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2695 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002696 I915_WRITE(reg, temp);
2697
2698 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002699 udelay(500);
2700
Sean Paulfa37d392012-03-02 12:53:39 -05002701 for (retry = 0; retry < 5; retry++) {
2702 reg = FDI_RX_IIR(pipe);
2703 temp = I915_READ(reg);
2704 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2705 if (temp & FDI_RX_SYMBOL_LOCK) {
2706 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2707 DRM_DEBUG_KMS("FDI train 2 done.\n");
2708 break;
2709 }
2710 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002711 }
Sean Paulfa37d392012-03-02 12:53:39 -05002712 if (retry < 5)
2713 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002714 }
2715 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002716 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002717
2718 DRM_DEBUG_KMS("FDI train done.\n");
2719}
2720
Jesse Barnes357555c2011-04-28 15:09:55 -07002721/* Manual link training for Ivy Bridge A0 parts */
2722static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2723{
2724 struct drm_device *dev = crtc->dev;
2725 struct drm_i915_private *dev_priv = dev->dev_private;
2726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2727 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002728 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002729
2730 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2731 for train result */
2732 reg = FDI_RX_IMR(pipe);
2733 temp = I915_READ(reg);
2734 temp &= ~FDI_RX_SYMBOL_LOCK;
2735 temp &= ~FDI_RX_BIT_LOCK;
2736 I915_WRITE(reg, temp);
2737
2738 POSTING_READ(reg);
2739 udelay(150);
2740
Daniel Vetter01a415f2012-10-27 15:58:40 +02002741 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2742 I915_READ(FDI_RX_IIR(pipe)));
2743
Jesse Barnes139ccd32013-08-19 11:04:55 -07002744 /* Try each vswing and preemphasis setting twice before moving on */
2745 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2746 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002747 reg = FDI_TX_CTL(pipe);
2748 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002749 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2750 temp &= ~FDI_TX_ENABLE;
2751 I915_WRITE(reg, temp);
2752
2753 reg = FDI_RX_CTL(pipe);
2754 temp = I915_READ(reg);
2755 temp &= ~FDI_LINK_TRAIN_AUTO;
2756 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2757 temp &= ~FDI_RX_ENABLE;
2758 I915_WRITE(reg, temp);
2759
2760 /* enable CPU FDI TX and PCH FDI RX */
2761 reg = FDI_TX_CTL(pipe);
2762 temp = I915_READ(reg);
2763 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2764 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2765 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002766 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002767 temp |= snb_b_fdi_train_param[j/2];
2768 temp |= FDI_COMPOSITE_SYNC;
2769 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2770
2771 I915_WRITE(FDI_RX_MISC(pipe),
2772 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2773
2774 reg = FDI_RX_CTL(pipe);
2775 temp = I915_READ(reg);
2776 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2777 temp |= FDI_COMPOSITE_SYNC;
2778 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2779
2780 POSTING_READ(reg);
2781 udelay(1); /* should be 0.5us */
2782
2783 for (i = 0; i < 4; i++) {
2784 reg = FDI_RX_IIR(pipe);
2785 temp = I915_READ(reg);
2786 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2787
2788 if (temp & FDI_RX_BIT_LOCK ||
2789 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2790 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2791 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2792 i);
2793 break;
2794 }
2795 udelay(1); /* should be 0.5us */
2796 }
2797 if (i == 4) {
2798 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2799 continue;
2800 }
2801
2802 /* Train 2 */
2803 reg = FDI_TX_CTL(pipe);
2804 temp = I915_READ(reg);
2805 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2806 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2807 I915_WRITE(reg, temp);
2808
2809 reg = FDI_RX_CTL(pipe);
2810 temp = I915_READ(reg);
2811 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2812 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002813 I915_WRITE(reg, temp);
2814
2815 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002816 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002817
Jesse Barnes139ccd32013-08-19 11:04:55 -07002818 for (i = 0; i < 4; i++) {
2819 reg = FDI_RX_IIR(pipe);
2820 temp = I915_READ(reg);
2821 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002822
Jesse Barnes139ccd32013-08-19 11:04:55 -07002823 if (temp & FDI_RX_SYMBOL_LOCK ||
2824 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2825 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2826 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2827 i);
2828 goto train_done;
2829 }
2830 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002831 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002832 if (i == 4)
2833 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002834 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002835
Jesse Barnes139ccd32013-08-19 11:04:55 -07002836train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002837 DRM_DEBUG_KMS("FDI train done.\n");
2838}
2839
Daniel Vetter88cefb62012-08-12 19:27:14 +02002840static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002841{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002842 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002843 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002844 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002845 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002846
Jesse Barnesc64e3112010-09-10 11:27:03 -07002847
Jesse Barnes0e23b992010-09-10 11:10:00 -07002848 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002849 reg = FDI_RX_CTL(pipe);
2850 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002851 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2852 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002853 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002854 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2855
2856 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002857 udelay(200);
2858
2859 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002860 temp = I915_READ(reg);
2861 I915_WRITE(reg, temp | FDI_PCDCLK);
2862
2863 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002864 udelay(200);
2865
Paulo Zanoni20749732012-11-23 15:30:38 -02002866 /* Enable CPU FDI TX PLL, always on for Ironlake */
2867 reg = FDI_TX_CTL(pipe);
2868 temp = I915_READ(reg);
2869 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2870 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002871
Paulo Zanoni20749732012-11-23 15:30:38 -02002872 POSTING_READ(reg);
2873 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002874 }
2875}
2876
Daniel Vetter88cefb62012-08-12 19:27:14 +02002877static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2878{
2879 struct drm_device *dev = intel_crtc->base.dev;
2880 struct drm_i915_private *dev_priv = dev->dev_private;
2881 int pipe = intel_crtc->pipe;
2882 u32 reg, temp;
2883
2884 /* Switch from PCDclk to Rawclk */
2885 reg = FDI_RX_CTL(pipe);
2886 temp = I915_READ(reg);
2887 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2888
2889 /* Disable CPU FDI TX PLL */
2890 reg = FDI_TX_CTL(pipe);
2891 temp = I915_READ(reg);
2892 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2893
2894 POSTING_READ(reg);
2895 udelay(100);
2896
2897 reg = FDI_RX_CTL(pipe);
2898 temp = I915_READ(reg);
2899 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2900
2901 /* Wait for the clocks to turn off. */
2902 POSTING_READ(reg);
2903 udelay(100);
2904}
2905
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002906static void ironlake_fdi_disable(struct drm_crtc *crtc)
2907{
2908 struct drm_device *dev = crtc->dev;
2909 struct drm_i915_private *dev_priv = dev->dev_private;
2910 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2911 int pipe = intel_crtc->pipe;
2912 u32 reg, temp;
2913
2914 /* disable CPU FDI tx and PCH FDI rx */
2915 reg = FDI_TX_CTL(pipe);
2916 temp = I915_READ(reg);
2917 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2918 POSTING_READ(reg);
2919
2920 reg = FDI_RX_CTL(pipe);
2921 temp = I915_READ(reg);
2922 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002923 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002924 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2925
2926 POSTING_READ(reg);
2927 udelay(100);
2928
2929 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002930 if (HAS_PCH_IBX(dev)) {
2931 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002932 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002933
2934 /* still set train pattern 1 */
2935 reg = FDI_TX_CTL(pipe);
2936 temp = I915_READ(reg);
2937 temp &= ~FDI_LINK_TRAIN_NONE;
2938 temp |= FDI_LINK_TRAIN_PATTERN_1;
2939 I915_WRITE(reg, temp);
2940
2941 reg = FDI_RX_CTL(pipe);
2942 temp = I915_READ(reg);
2943 if (HAS_PCH_CPT(dev)) {
2944 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2945 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2946 } else {
2947 temp &= ~FDI_LINK_TRAIN_NONE;
2948 temp |= FDI_LINK_TRAIN_PATTERN_1;
2949 }
2950 /* BPC in FDI rx is consistent with that in PIPECONF */
2951 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002952 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002953 I915_WRITE(reg, temp);
2954
2955 POSTING_READ(reg);
2956 udelay(100);
2957}
2958
Chris Wilson5bb61642012-09-27 21:25:58 +01002959static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2960{
2961 struct drm_device *dev = crtc->dev;
2962 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002964 unsigned long flags;
2965 bool pending;
2966
Ville Syrjälä10d83732013-01-29 18:13:34 +02002967 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2968 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002969 return false;
2970
2971 spin_lock_irqsave(&dev->event_lock, flags);
2972 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2973 spin_unlock_irqrestore(&dev->event_lock, flags);
2974
2975 return pending;
2976}
2977
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002978static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2979{
Chris Wilson0f911282012-04-17 10:05:38 +01002980 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002981 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002982
2983 if (crtc->fb == NULL)
2984 return;
2985
Daniel Vetter2c10d572012-12-20 21:24:07 +01002986 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2987
Chris Wilson5bb61642012-09-27 21:25:58 +01002988 wait_event(dev_priv->pending_flip_queue,
2989 !intel_crtc_has_pending_flip(crtc));
2990
Chris Wilson0f911282012-04-17 10:05:38 +01002991 mutex_lock(&dev->struct_mutex);
2992 intel_finish_fb(crtc->fb);
2993 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002994}
2995
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002996/* Program iCLKIP clock to the desired frequency */
2997static void lpt_program_iclkip(struct drm_crtc *crtc)
2998{
2999 struct drm_device *dev = crtc->dev;
3000 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003001 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003002 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3003 u32 temp;
3004
Daniel Vetter09153002012-12-12 14:06:44 +01003005 mutex_lock(&dev_priv->dpio_lock);
3006
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003007 /* It is necessary to ungate the pixclk gate prior to programming
3008 * the divisors, and gate it back when it is done.
3009 */
3010 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3011
3012 /* Disable SSCCTL */
3013 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003014 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3015 SBI_SSCCTL_DISABLE,
3016 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003017
3018 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003019 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003020 auxdiv = 1;
3021 divsel = 0x41;
3022 phaseinc = 0x20;
3023 } else {
3024 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003025 * but the adjusted_mode->crtc_clock in in KHz. To get the
3026 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003027 * convert the virtual clock precision to KHz here for higher
3028 * precision.
3029 */
3030 u32 iclk_virtual_root_freq = 172800 * 1000;
3031 u32 iclk_pi_range = 64;
3032 u32 desired_divisor, msb_divisor_value, pi_value;
3033
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003034 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003035 msb_divisor_value = desired_divisor / iclk_pi_range;
3036 pi_value = desired_divisor % iclk_pi_range;
3037
3038 auxdiv = 0;
3039 divsel = msb_divisor_value - 2;
3040 phaseinc = pi_value;
3041 }
3042
3043 /* This should not happen with any sane values */
3044 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3045 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3046 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3047 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3048
3049 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 +03003050 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003051 auxdiv,
3052 divsel,
3053 phasedir,
3054 phaseinc);
3055
3056 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003057 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003058 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3059 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3060 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3061 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3062 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3063 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003064 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003065
3066 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003067 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003068 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3069 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003070 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003071
3072 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003073 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003074 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003075 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003076
3077 /* Wait for initialization time */
3078 udelay(24);
3079
3080 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003081
3082 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003083}
3084
Daniel Vetter275f01b22013-05-03 11:49:47 +02003085static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3086 enum pipe pch_transcoder)
3087{
3088 struct drm_device *dev = crtc->base.dev;
3089 struct drm_i915_private *dev_priv = dev->dev_private;
3090 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3091
3092 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3093 I915_READ(HTOTAL(cpu_transcoder)));
3094 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3095 I915_READ(HBLANK(cpu_transcoder)));
3096 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3097 I915_READ(HSYNC(cpu_transcoder)));
3098
3099 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3100 I915_READ(VTOTAL(cpu_transcoder)));
3101 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3102 I915_READ(VBLANK(cpu_transcoder)));
3103 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3104 I915_READ(VSYNC(cpu_transcoder)));
3105 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3106 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3107}
3108
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003109static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3110{
3111 struct drm_i915_private *dev_priv = dev->dev_private;
3112 uint32_t temp;
3113
3114 temp = I915_READ(SOUTH_CHICKEN1);
3115 if (temp & FDI_BC_BIFURCATION_SELECT)
3116 return;
3117
3118 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3119 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3120
3121 temp |= FDI_BC_BIFURCATION_SELECT;
3122 DRM_DEBUG_KMS("enabling fdi C rx\n");
3123 I915_WRITE(SOUTH_CHICKEN1, temp);
3124 POSTING_READ(SOUTH_CHICKEN1);
3125}
3126
3127static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3128{
3129 struct drm_device *dev = intel_crtc->base.dev;
3130 struct drm_i915_private *dev_priv = dev->dev_private;
3131
3132 switch (intel_crtc->pipe) {
3133 case PIPE_A:
3134 break;
3135 case PIPE_B:
3136 if (intel_crtc->config.fdi_lanes > 2)
3137 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3138 else
3139 cpt_enable_fdi_bc_bifurcation(dev);
3140
3141 break;
3142 case PIPE_C:
3143 cpt_enable_fdi_bc_bifurcation(dev);
3144
3145 break;
3146 default:
3147 BUG();
3148 }
3149}
3150
Jesse Barnesf67a5592011-01-05 10:31:48 -08003151/*
3152 * Enable PCH resources required for PCH ports:
3153 * - PCH PLLs
3154 * - FDI training & RX/TX
3155 * - update transcoder timings
3156 * - DP transcoding bits
3157 * - transcoder
3158 */
3159static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003160{
3161 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003162 struct drm_i915_private *dev_priv = dev->dev_private;
3163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3164 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003165 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003166
Daniel Vetterab9412b2013-05-03 11:49:46 +02003167 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003168
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003169 if (IS_IVYBRIDGE(dev))
3170 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3171
Daniel Vettercd986ab2012-10-26 10:58:12 +02003172 /* Write the TU size bits before fdi link training, so that error
3173 * detection works. */
3174 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3175 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3176
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003177 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003178 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003179
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003180 /* We need to program the right clock selection before writing the pixel
3181 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003182 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003183 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003184
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003185 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003186 temp |= TRANS_DPLL_ENABLE(pipe);
3187 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003188 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003189 temp |= sel;
3190 else
3191 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003192 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003193 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003194
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003195 /* XXX: pch pll's can be enabled any time before we enable the PCH
3196 * transcoder, and we actually should do this to not upset any PCH
3197 * transcoder that already use the clock when we share it.
3198 *
3199 * Note that enable_shared_dpll tries to do the right thing, but
3200 * get_shared_dpll unconditionally resets the pll - we need that to have
3201 * the right LVDS enable sequence. */
3202 ironlake_enable_shared_dpll(intel_crtc);
3203
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003204 /* set transcoder timing, panel must allow it */
3205 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003206 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003207
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003208 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003209
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003210 /* For PCH DP, enable TRANS_DP_CTL */
3211 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003212 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3213 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003214 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003215 reg = TRANS_DP_CTL(pipe);
3216 temp = I915_READ(reg);
3217 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003218 TRANS_DP_SYNC_MASK |
3219 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003220 temp |= (TRANS_DP_OUTPUT_ENABLE |
3221 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003222 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003223
3224 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003225 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003226 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003227 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003228
3229 switch (intel_trans_dp_port_sel(crtc)) {
3230 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003231 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003232 break;
3233 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003234 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003235 break;
3236 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003237 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003238 break;
3239 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003240 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003241 }
3242
Chris Wilson5eddb702010-09-11 13:48:45 +01003243 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003244 }
3245
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003246 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003247}
3248
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003249static void lpt_pch_enable(struct drm_crtc *crtc)
3250{
3251 struct drm_device *dev = crtc->dev;
3252 struct drm_i915_private *dev_priv = dev->dev_private;
3253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003254 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003255
Daniel Vetterab9412b2013-05-03 11:49:46 +02003256 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003257
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003258 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003259
Paulo Zanoni0540e482012-10-31 18:12:40 -02003260 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003261 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003262
Paulo Zanoni937bb612012-10-31 18:12:47 -02003263 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003264}
3265
Daniel Vettere2b78262013-06-07 23:10:03 +02003266static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003267{
Daniel Vettere2b78262013-06-07 23:10:03 +02003268 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003269
3270 if (pll == NULL)
3271 return;
3272
3273 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003274 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003275 return;
3276 }
3277
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003278 if (--pll->refcount == 0) {
3279 WARN_ON(pll->on);
3280 WARN_ON(pll->active);
3281 }
3282
Daniel Vettera43f6e02013-06-07 23:10:32 +02003283 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003284}
3285
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003286static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003287{
Daniel Vettere2b78262013-06-07 23:10:03 +02003288 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3289 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3290 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003291
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003292 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003293 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3294 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003295 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003296 }
3297
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003298 if (HAS_PCH_IBX(dev_priv->dev)) {
3299 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003300 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003301 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003302
Daniel Vetter46edb022013-06-05 13:34:12 +02003303 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3304 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003305
3306 goto found;
3307 }
3308
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003309 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3310 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003311
3312 /* Only want to check enabled timings first */
3313 if (pll->refcount == 0)
3314 continue;
3315
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003316 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3317 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003318 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003319 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003320 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003321
3322 goto found;
3323 }
3324 }
3325
3326 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003327 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3328 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003329 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003330 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3331 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003332 goto found;
3333 }
3334 }
3335
3336 return NULL;
3337
3338found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003339 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003340 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3341 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003342
Daniel Vettercdbd2312013-06-05 13:34:03 +02003343 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003344 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3345 sizeof(pll->hw_state));
3346
Daniel Vetter46edb022013-06-05 13:34:12 +02003347 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003348 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003349 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003350
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003351 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003352 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003353 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003354
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003355 return pll;
3356}
3357
Daniel Vettera1520312013-05-03 11:49:50 +02003358static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003359{
3360 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003361 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003362 u32 temp;
3363
3364 temp = I915_READ(dslreg);
3365 udelay(500);
3366 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003367 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003368 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003369 }
3370}
3371
Jesse Barnesb074cec2013-04-25 12:55:02 -07003372static void ironlake_pfit_enable(struct intel_crtc *crtc)
3373{
3374 struct drm_device *dev = crtc->base.dev;
3375 struct drm_i915_private *dev_priv = dev->dev_private;
3376 int pipe = crtc->pipe;
3377
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003378 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003379 /* Force use of hard-coded filter coefficients
3380 * as some pre-programmed values are broken,
3381 * e.g. x201.
3382 */
3383 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3384 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3385 PF_PIPE_SEL_IVB(pipe));
3386 else
3387 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3388 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3389 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003390 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003391}
3392
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003393static void intel_enable_planes(struct drm_crtc *crtc)
3394{
3395 struct drm_device *dev = crtc->dev;
3396 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3397 struct intel_plane *intel_plane;
3398
3399 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3400 if (intel_plane->pipe == pipe)
3401 intel_plane_restore(&intel_plane->base);
3402}
3403
3404static void intel_disable_planes(struct drm_crtc *crtc)
3405{
3406 struct drm_device *dev = crtc->dev;
3407 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3408 struct intel_plane *intel_plane;
3409
3410 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3411 if (intel_plane->pipe == pipe)
3412 intel_plane_disable(&intel_plane->base);
3413}
3414
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003415void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003416{
3417 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3418
3419 if (!crtc->config.ips_enabled)
3420 return;
3421
3422 /* We can only enable IPS after we enable a plane and wait for a vblank.
3423 * We guarantee that the plane is enabled by calling intel_enable_ips
3424 * only after intel_enable_plane. And intel_enable_plane already waits
3425 * for a vblank, so all we need to do here is to enable the IPS bit. */
3426 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003427 if (IS_BROADWELL(crtc->base.dev)) {
3428 mutex_lock(&dev_priv->rps.hw_lock);
3429 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3430 mutex_unlock(&dev_priv->rps.hw_lock);
3431 /* Quoting Art Runyan: "its not safe to expect any particular
3432 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003433 * mailbox." Moreover, the mailbox may return a bogus state,
3434 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003435 */
3436 } else {
3437 I915_WRITE(IPS_CTL, IPS_ENABLE);
3438 /* The bit only becomes 1 in the next vblank, so this wait here
3439 * is essentially intel_wait_for_vblank. If we don't have this
3440 * and don't wait for vblanks until the end of crtc_enable, then
3441 * the HW state readout code will complain that the expected
3442 * IPS_CTL value is not the one we read. */
3443 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3444 DRM_ERROR("Timed out waiting for IPS enable\n");
3445 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003446}
3447
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003448void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003449{
3450 struct drm_device *dev = crtc->base.dev;
3451 struct drm_i915_private *dev_priv = dev->dev_private;
3452
3453 if (!crtc->config.ips_enabled)
3454 return;
3455
3456 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003457 if (IS_BROADWELL(crtc->base.dev)) {
3458 mutex_lock(&dev_priv->rps.hw_lock);
3459 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3460 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnese59150d2014-01-07 13:30:45 -08003461 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003462 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003463 POSTING_READ(IPS_CTL);
3464 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003465
3466 /* We need to wait for a vblank before we can disable the plane. */
3467 intel_wait_for_vblank(dev, crtc->pipe);
3468}
3469
3470/** Loads the palette/gamma unit for the CRTC with the prepared values */
3471static void intel_crtc_load_lut(struct drm_crtc *crtc)
3472{
3473 struct drm_device *dev = crtc->dev;
3474 struct drm_i915_private *dev_priv = dev->dev_private;
3475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3476 enum pipe pipe = intel_crtc->pipe;
3477 int palreg = PALETTE(pipe);
3478 int i;
3479 bool reenable_ips = false;
3480
3481 /* The clocks have to be on to load the palette. */
3482 if (!crtc->enabled || !intel_crtc->active)
3483 return;
3484
3485 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3486 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3487 assert_dsi_pll_enabled(dev_priv);
3488 else
3489 assert_pll_enabled(dev_priv, pipe);
3490 }
3491
3492 /* use legacy palette for Ironlake */
3493 if (HAS_PCH_SPLIT(dev))
3494 palreg = LGC_PALETTE(pipe);
3495
3496 /* Workaround : Do not read or write the pipe palette/gamma data while
3497 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3498 */
3499 if (intel_crtc->config.ips_enabled &&
3500 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3501 GAMMA_MODE_MODE_SPLIT)) {
3502 hsw_disable_ips(intel_crtc);
3503 reenable_ips = true;
3504 }
3505
3506 for (i = 0; i < 256; i++) {
3507 I915_WRITE(palreg + 4 * i,
3508 (intel_crtc->lut_r[i] << 16) |
3509 (intel_crtc->lut_g[i] << 8) |
3510 intel_crtc->lut_b[i]);
3511 }
3512
3513 if (reenable_ips)
3514 hsw_enable_ips(intel_crtc);
3515}
3516
Jesse Barnesf67a5592011-01-05 10:31:48 -08003517static void ironlake_crtc_enable(struct drm_crtc *crtc)
3518{
3519 struct drm_device *dev = crtc->dev;
3520 struct drm_i915_private *dev_priv = dev->dev_private;
3521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003522 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003523 int pipe = intel_crtc->pipe;
3524 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003525
Daniel Vetter08a48462012-07-02 11:43:47 +02003526 WARN_ON(!crtc->enabled);
3527
Jesse Barnesf67a5592011-01-05 10:31:48 -08003528 if (intel_crtc->active)
3529 return;
3530
3531 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003532
3533 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3534 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3535
Daniel Vetterf6736a12013-06-05 13:34:30 +02003536 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003537 if (encoder->pre_enable)
3538 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003539
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003540 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003541 /* Note: FDI PLL enabling _must_ be done before we enable the
3542 * cpu pipes, hence this is separate from all the other fdi/pch
3543 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003544 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003545 } else {
3546 assert_fdi_tx_disabled(dev_priv, pipe);
3547 assert_fdi_rx_disabled(dev_priv, pipe);
3548 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003549
Jesse Barnesb074cec2013-04-25 12:55:02 -07003550 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003551
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003552 /*
3553 * On ILK+ LUT must be loaded before the pipe is running but with
3554 * clocks enabled
3555 */
3556 intel_crtc_load_lut(crtc);
3557
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003558 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003559 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003560 intel_crtc->config.has_pch_encoder, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003561 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003562 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003563 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003564
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003565 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003566 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003567
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003568 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003569 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003570 mutex_unlock(&dev->struct_mutex);
3571
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003572 for_each_encoder_on_crtc(dev, crtc, encoder)
3573 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003574
3575 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003576 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003577
3578 /*
3579 * There seems to be a race in PCH platform hw (at least on some
3580 * outputs) where an enabled pipe still completes any pageflip right
3581 * away (as if the pipe is off) instead of waiting for vblank. As soon
3582 * as the first vblank happend, everything works as expected. Hence just
3583 * wait for one vblank before returning to avoid strange things
3584 * happening.
3585 */
3586 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003587}
3588
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003589/* IPS only exists on ULT machines and is tied to pipe A. */
3590static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3591{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003592 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003593}
3594
Ville Syrjälädda9a662013-09-19 17:00:37 -03003595static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3596{
3597 struct drm_device *dev = crtc->dev;
3598 struct drm_i915_private *dev_priv = dev->dev_private;
3599 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3600 int pipe = intel_crtc->pipe;
3601 int plane = intel_crtc->plane;
3602
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003603 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003604 intel_enable_planes(crtc);
3605 intel_crtc_update_cursor(crtc, true);
3606
3607 hsw_enable_ips(intel_crtc);
3608
3609 mutex_lock(&dev->struct_mutex);
3610 intel_update_fbc(dev);
3611 mutex_unlock(&dev->struct_mutex);
3612}
3613
3614static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3615{
3616 struct drm_device *dev = crtc->dev;
3617 struct drm_i915_private *dev_priv = dev->dev_private;
3618 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3619 int pipe = intel_crtc->pipe;
3620 int plane = intel_crtc->plane;
3621
3622 intel_crtc_wait_for_pending_flips(crtc);
3623 drm_vblank_off(dev, pipe);
3624
3625 /* FBC must be disabled before disabling the plane on HSW. */
3626 if (dev_priv->fbc.plane == plane)
3627 intel_disable_fbc(dev);
3628
3629 hsw_disable_ips(intel_crtc);
3630
3631 intel_crtc_update_cursor(crtc, false);
3632 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003633 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003634}
3635
Paulo Zanonie4916942013-09-20 16:21:19 -03003636/*
3637 * This implements the workaround described in the "notes" section of the mode
3638 * set sequence documentation. When going from no pipes or single pipe to
3639 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3640 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3641 */
3642static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3643{
3644 struct drm_device *dev = crtc->base.dev;
3645 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3646
3647 /* We want to get the other_active_crtc only if there's only 1 other
3648 * active crtc. */
3649 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3650 if (!crtc_it->active || crtc_it == crtc)
3651 continue;
3652
3653 if (other_active_crtc)
3654 return;
3655
3656 other_active_crtc = crtc_it;
3657 }
3658 if (!other_active_crtc)
3659 return;
3660
3661 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3662 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3663}
3664
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003665static void haswell_crtc_enable(struct drm_crtc *crtc)
3666{
3667 struct drm_device *dev = crtc->dev;
3668 struct drm_i915_private *dev_priv = dev->dev_private;
3669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3670 struct intel_encoder *encoder;
3671 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003672
3673 WARN_ON(!crtc->enabled);
3674
3675 if (intel_crtc->active)
3676 return;
3677
3678 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003679
3680 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3681 if (intel_crtc->config.has_pch_encoder)
3682 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3683
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003684 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003685 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003686
3687 for_each_encoder_on_crtc(dev, crtc, encoder)
3688 if (encoder->pre_enable)
3689 encoder->pre_enable(encoder);
3690
Paulo Zanoni1f544382012-10-24 11:32:00 -02003691 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003692
Jesse Barnesb074cec2013-04-25 12:55:02 -07003693 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003694
3695 /*
3696 * On ILK+ LUT must be loaded before the pipe is running but with
3697 * clocks enabled
3698 */
3699 intel_crtc_load_lut(crtc);
3700
Paulo Zanoni1f544382012-10-24 11:32:00 -02003701 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003702 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003703
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003704 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003705 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003706 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003707
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003708 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003709 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003710
Jani Nikula8807e552013-08-30 19:40:32 +03003711 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003712 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003713 intel_opregion_notify_encoder(encoder, true);
3714 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003715
Paulo Zanonie4916942013-09-20 16:21:19 -03003716 /* If we change the relative order between pipe/planes enabling, we need
3717 * to change the workaround. */
3718 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003719 haswell_crtc_enable_planes(crtc);
3720
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003721 /*
3722 * There seems to be a race in PCH platform hw (at least on some
3723 * outputs) where an enabled pipe still completes any pageflip right
3724 * away (as if the pipe is off) instead of waiting for vblank. As soon
3725 * as the first vblank happend, everything works as expected. Hence just
3726 * wait for one vblank before returning to avoid strange things
3727 * happening.
3728 */
3729 intel_wait_for_vblank(dev, intel_crtc->pipe);
3730}
3731
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003732static void ironlake_pfit_disable(struct intel_crtc *crtc)
3733{
3734 struct drm_device *dev = crtc->base.dev;
3735 struct drm_i915_private *dev_priv = dev->dev_private;
3736 int pipe = crtc->pipe;
3737
3738 /* To avoid upsetting the power well on haswell only disable the pfit if
3739 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003740 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003741 I915_WRITE(PF_CTL(pipe), 0);
3742 I915_WRITE(PF_WIN_POS(pipe), 0);
3743 I915_WRITE(PF_WIN_SZ(pipe), 0);
3744 }
3745}
3746
Jesse Barnes6be4a602010-09-10 10:26:01 -07003747static void ironlake_crtc_disable(struct drm_crtc *crtc)
3748{
3749 struct drm_device *dev = crtc->dev;
3750 struct drm_i915_private *dev_priv = dev->dev_private;
3751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003752 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003753 int pipe = intel_crtc->pipe;
3754 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003755 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003756
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003757
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003758 if (!intel_crtc->active)
3759 return;
3760
Daniel Vetterea9d7582012-07-10 10:42:52 +02003761 for_each_encoder_on_crtc(dev, crtc, encoder)
3762 encoder->disable(encoder);
3763
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003764 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003765 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003766
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003767 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003768 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003769
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003770 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003771 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003772 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003773
Daniel Vetterd925c592013-06-05 13:34:04 +02003774 if (intel_crtc->config.has_pch_encoder)
3775 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3776
Jesse Barnesb24e7172011-01-04 15:09:30 -08003777 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003778
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003779 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003780
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003781 for_each_encoder_on_crtc(dev, crtc, encoder)
3782 if (encoder->post_disable)
3783 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003784
Daniel Vetterd925c592013-06-05 13:34:04 +02003785 if (intel_crtc->config.has_pch_encoder) {
3786 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003787
Daniel Vetterd925c592013-06-05 13:34:04 +02003788 ironlake_disable_pch_transcoder(dev_priv, pipe);
3789 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003790
Daniel Vetterd925c592013-06-05 13:34:04 +02003791 if (HAS_PCH_CPT(dev)) {
3792 /* disable TRANS_DP_CTL */
3793 reg = TRANS_DP_CTL(pipe);
3794 temp = I915_READ(reg);
3795 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3796 TRANS_DP_PORT_SEL_MASK);
3797 temp |= TRANS_DP_PORT_SEL_NONE;
3798 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003799
Daniel Vetterd925c592013-06-05 13:34:04 +02003800 /* disable DPLL_SEL */
3801 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003802 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003803 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003804 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003805
3806 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003807 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003808
3809 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003810 }
3811
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003812 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003813 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003814
3815 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003816 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003817 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003818}
3819
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003820static void haswell_crtc_disable(struct drm_crtc *crtc)
3821{
3822 struct drm_device *dev = crtc->dev;
3823 struct drm_i915_private *dev_priv = dev->dev_private;
3824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3825 struct intel_encoder *encoder;
3826 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003827 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003828
3829 if (!intel_crtc->active)
3830 return;
3831
Ville Syrjälädda9a662013-09-19 17:00:37 -03003832 haswell_crtc_disable_planes(crtc);
3833
Jani Nikula8807e552013-08-30 19:40:32 +03003834 for_each_encoder_on_crtc(dev, crtc, encoder) {
3835 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003836 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003837 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003838
Paulo Zanoni86642812013-04-12 17:57:57 -03003839 if (intel_crtc->config.has_pch_encoder)
3840 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003841 intel_disable_pipe(dev_priv, pipe);
3842
Paulo Zanoniad80a812012-10-24 16:06:19 -02003843 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003844
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003845 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003846
Paulo Zanoni1f544382012-10-24 11:32:00 -02003847 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003848
3849 for_each_encoder_on_crtc(dev, crtc, encoder)
3850 if (encoder->post_disable)
3851 encoder->post_disable(encoder);
3852
Daniel Vetter88adfff2013-03-28 10:42:01 +01003853 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003854 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003855 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003856 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003857 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003858
3859 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003860 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003861
3862 mutex_lock(&dev->struct_mutex);
3863 intel_update_fbc(dev);
3864 mutex_unlock(&dev->struct_mutex);
3865}
3866
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003867static void ironlake_crtc_off(struct drm_crtc *crtc)
3868{
3869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003870 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003871}
3872
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003873static void haswell_crtc_off(struct drm_crtc *crtc)
3874{
3875 intel_ddi_put_crtc_pll(crtc);
3876}
3877
Daniel Vetter02e792f2009-09-15 22:57:34 +02003878static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3879{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003880 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003881 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003882 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003883
Chris Wilson23f09ce2010-08-12 13:53:37 +01003884 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003885 dev_priv->mm.interruptible = false;
3886 (void) intel_overlay_switch_off(intel_crtc->overlay);
3887 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003888 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003889 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003890
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003891 /* Let userspace switch the overlay on again. In most cases userspace
3892 * has to recompute where to put it anyway.
3893 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003894}
3895
Egbert Eich61bc95c2013-03-04 09:24:38 -05003896/**
3897 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3898 * cursor plane briefly if not already running after enabling the display
3899 * plane.
3900 * This workaround avoids occasional blank screens when self refresh is
3901 * enabled.
3902 */
3903static void
3904g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3905{
3906 u32 cntl = I915_READ(CURCNTR(pipe));
3907
3908 if ((cntl & CURSOR_MODE) == 0) {
3909 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3910
3911 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3912 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3913 intel_wait_for_vblank(dev_priv->dev, pipe);
3914 I915_WRITE(CURCNTR(pipe), cntl);
3915 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3916 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3917 }
3918}
3919
Jesse Barnes2dd24552013-04-25 12:55:01 -07003920static void i9xx_pfit_enable(struct intel_crtc *crtc)
3921{
3922 struct drm_device *dev = crtc->base.dev;
3923 struct drm_i915_private *dev_priv = dev->dev_private;
3924 struct intel_crtc_config *pipe_config = &crtc->config;
3925
Daniel Vetter328d8e82013-05-08 10:36:31 +02003926 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003927 return;
3928
Daniel Vetterc0b03412013-05-28 12:05:54 +02003929 /*
3930 * The panel fitter should only be adjusted whilst the pipe is disabled,
3931 * according to register description and PRM.
3932 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003933 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3934 assert_pipe_disabled(dev_priv, crtc->pipe);
3935
Jesse Barnesb074cec2013-04-25 12:55:02 -07003936 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3937 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003938
3939 /* Border color in case we don't scale up to the full screen. Black by
3940 * default, change to something else for debugging. */
3941 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003942}
3943
Jesse Barnes586f49d2013-11-04 16:06:59 -08003944int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003945{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003946 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003947
Jesse Barnes586f49d2013-11-04 16:06:59 -08003948 /* Obtain SKU information */
3949 mutex_lock(&dev_priv->dpio_lock);
3950 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3951 CCK_FUSE_HPLL_FREQ_MASK;
3952 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003953
Jesse Barnes586f49d2013-11-04 16:06:59 -08003954 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003955}
3956
3957/* Adjust CDclk dividers to allow high res or save power if possible */
3958static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3959{
3960 struct drm_i915_private *dev_priv = dev->dev_private;
3961 u32 val, cmd;
3962
3963 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3964 cmd = 2;
3965 else if (cdclk == 266)
3966 cmd = 1;
3967 else
3968 cmd = 0;
3969
3970 mutex_lock(&dev_priv->rps.hw_lock);
3971 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3972 val &= ~DSPFREQGUAR_MASK;
3973 val |= (cmd << DSPFREQGUAR_SHIFT);
3974 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3975 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3976 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3977 50)) {
3978 DRM_ERROR("timed out waiting for CDclk change\n");
3979 }
3980 mutex_unlock(&dev_priv->rps.hw_lock);
3981
3982 if (cdclk == 400) {
3983 u32 divider, vco;
3984
3985 vco = valleyview_get_vco(dev_priv);
3986 divider = ((vco << 1) / cdclk) - 1;
3987
3988 mutex_lock(&dev_priv->dpio_lock);
3989 /* adjust cdclk divider */
3990 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3991 val &= ~0xf;
3992 val |= divider;
3993 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3994 mutex_unlock(&dev_priv->dpio_lock);
3995 }
3996
3997 mutex_lock(&dev_priv->dpio_lock);
3998 /* adjust self-refresh exit latency value */
3999 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4000 val &= ~0x7f;
4001
4002 /*
4003 * For high bandwidth configs, we set a higher latency in the bunit
4004 * so that the core display fetch happens in time to avoid underruns.
4005 */
4006 if (cdclk == 400)
4007 val |= 4500 / 250; /* 4.5 usec */
4008 else
4009 val |= 3000 / 250; /* 3.0 usec */
4010 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4011 mutex_unlock(&dev_priv->dpio_lock);
4012
4013 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4014 intel_i2c_reset(dev);
4015}
4016
4017static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4018{
4019 int cur_cdclk, vco;
4020 int divider;
4021
4022 vco = valleyview_get_vco(dev_priv);
4023
4024 mutex_lock(&dev_priv->dpio_lock);
4025 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4026 mutex_unlock(&dev_priv->dpio_lock);
4027
4028 divider &= 0xf;
4029
4030 cur_cdclk = (vco << 1) / (divider + 1);
4031
4032 return cur_cdclk;
4033}
4034
4035static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4036 int max_pixclk)
4037{
4038 int cur_cdclk;
4039
4040 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4041
4042 /*
4043 * Really only a few cases to deal with, as only 4 CDclks are supported:
4044 * 200MHz
4045 * 267MHz
4046 * 320MHz
4047 * 400MHz
4048 * So we check to see whether we're above 90% of the lower bin and
4049 * adjust if needed.
4050 */
4051 if (max_pixclk > 288000) {
4052 return 400;
4053 } else if (max_pixclk > 240000) {
4054 return 320;
4055 } else
4056 return 266;
4057 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4058}
4059
4060static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4061 unsigned modeset_pipes,
4062 struct intel_crtc_config *pipe_config)
4063{
4064 struct drm_device *dev = dev_priv->dev;
4065 struct intel_crtc *intel_crtc;
4066 int max_pixclk = 0;
4067
4068 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4069 base.head) {
4070 if (modeset_pipes & (1 << intel_crtc->pipe))
4071 max_pixclk = max(max_pixclk,
4072 pipe_config->adjusted_mode.crtc_clock);
4073 else if (intel_crtc->base.enabled)
4074 max_pixclk = max(max_pixclk,
4075 intel_crtc->config.adjusted_mode.crtc_clock);
4076 }
4077
4078 return max_pixclk;
4079}
4080
4081static void valleyview_modeset_global_pipes(struct drm_device *dev,
4082 unsigned *prepare_pipes,
4083 unsigned modeset_pipes,
4084 struct intel_crtc_config *pipe_config)
4085{
4086 struct drm_i915_private *dev_priv = dev->dev_private;
4087 struct intel_crtc *intel_crtc;
4088 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4089 pipe_config);
4090 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4091
4092 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4093 return;
4094
4095 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4096 base.head)
4097 if (intel_crtc->base.enabled)
4098 *prepare_pipes |= (1 << intel_crtc->pipe);
4099}
4100
4101static void valleyview_modeset_global_resources(struct drm_device *dev)
4102{
4103 struct drm_i915_private *dev_priv = dev->dev_private;
4104 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4105 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4106 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4107
4108 if (req_cdclk != cur_cdclk)
4109 valleyview_set_cdclk(dev, req_cdclk);
4110}
4111
Jesse Barnes89b667f2013-04-18 14:51:36 -07004112static void valleyview_crtc_enable(struct drm_crtc *crtc)
4113{
4114 struct drm_device *dev = crtc->dev;
4115 struct drm_i915_private *dev_priv = dev->dev_private;
4116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4117 struct intel_encoder *encoder;
4118 int pipe = intel_crtc->pipe;
4119 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004120 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004121
4122 WARN_ON(!crtc->enabled);
4123
4124 if (intel_crtc->active)
4125 return;
4126
4127 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004128
Jesse Barnes89b667f2013-04-18 14:51:36 -07004129 for_each_encoder_on_crtc(dev, crtc, encoder)
4130 if (encoder->pre_pll_enable)
4131 encoder->pre_pll_enable(encoder);
4132
Jani Nikula23538ef2013-08-27 15:12:22 +03004133 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4134
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004135 if (!is_dsi)
4136 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004137
4138 for_each_encoder_on_crtc(dev, crtc, encoder)
4139 if (encoder->pre_enable)
4140 encoder->pre_enable(encoder);
4141
Jesse Barnes2dd24552013-04-25 12:55:01 -07004142 i9xx_pfit_enable(intel_crtc);
4143
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004144 intel_crtc_load_lut(crtc);
4145
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004146 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004147 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004148 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004149 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004150 intel_crtc_update_cursor(crtc, true);
4151
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004152 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004153
4154 for_each_encoder_on_crtc(dev, crtc, encoder)
4155 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004156}
4157
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004158static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004159{
4160 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004161 struct drm_i915_private *dev_priv = dev->dev_private;
4162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004163 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004164 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004165 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004166
Daniel Vetter08a48462012-07-02 11:43:47 +02004167 WARN_ON(!crtc->enabled);
4168
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004169 if (intel_crtc->active)
4170 return;
4171
4172 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004173
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004174 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004175 if (encoder->pre_enable)
4176 encoder->pre_enable(encoder);
4177
Daniel Vetterf6736a12013-06-05 13:34:30 +02004178 i9xx_enable_pll(intel_crtc);
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);
Jani Nikula23538ef2013-08-27 15:12:22 +03004185 intel_enable_pipe(dev_priv, pipe, false, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004186 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004187 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004188 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004189 if (IS_G4X(dev))
4190 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004191 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004192
4193 /* Give the overlay scaler a chance to enable if it's on this pipe */
4194 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004195
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004196 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004197
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004198 for_each_encoder_on_crtc(dev, crtc, encoder)
4199 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004200}
4201
Daniel Vetter87476d62013-04-11 16:29:06 +02004202static void i9xx_pfit_disable(struct intel_crtc *crtc)
4203{
4204 struct drm_device *dev = crtc->base.dev;
4205 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004206
4207 if (!crtc->config.gmch_pfit.control)
4208 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004209
4210 assert_pipe_disabled(dev_priv, crtc->pipe);
4211
Daniel Vetter328d8e82013-05-08 10:36:31 +02004212 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4213 I915_READ(PFIT_CONTROL));
4214 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004215}
4216
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004217static void i9xx_crtc_disable(struct drm_crtc *crtc)
4218{
4219 struct drm_device *dev = crtc->dev;
4220 struct drm_i915_private *dev_priv = dev->dev_private;
4221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004222 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004223 int pipe = intel_crtc->pipe;
4224 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004225
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004226 if (!intel_crtc->active)
4227 return;
4228
Daniel Vetterea9d7582012-07-10 10:42:52 +02004229 for_each_encoder_on_crtc(dev, crtc, encoder)
4230 encoder->disable(encoder);
4231
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004232 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004233 intel_crtc_wait_for_pending_flips(crtc);
4234 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004235
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004236 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004237 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004238
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004239 intel_crtc_dpms_overlay(intel_crtc, false);
4240 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004241 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004242 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004243
Jesse Barnesb24e7172011-01-04 15:09:30 -08004244 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004245
Daniel Vetter87476d62013-04-11 16:29:06 +02004246 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004247
Jesse Barnes89b667f2013-04-18 14:51:36 -07004248 for_each_encoder_on_crtc(dev, crtc, encoder)
4249 if (encoder->post_disable)
4250 encoder->post_disable(encoder);
4251
Jesse Barnesf6071162013-10-01 10:41:38 -07004252 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4253 vlv_disable_pll(dev_priv, pipe);
4254 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004255 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004256
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004257 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004258 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004259
Chris Wilson6b383a72010-09-13 13:54:26 +01004260 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004261}
4262
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004263static void i9xx_crtc_off(struct drm_crtc *crtc)
4264{
4265}
4266
Daniel Vetter976f8a22012-07-08 22:34:21 +02004267static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4268 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004269{
4270 struct drm_device *dev = crtc->dev;
4271 struct drm_i915_master_private *master_priv;
4272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4273 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004274
4275 if (!dev->primary->master)
4276 return;
4277
4278 master_priv = dev->primary->master->driver_priv;
4279 if (!master_priv->sarea_priv)
4280 return;
4281
Jesse Barnes79e53942008-11-07 14:24:08 -08004282 switch (pipe) {
4283 case 0:
4284 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4285 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4286 break;
4287 case 1:
4288 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4289 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4290 break;
4291 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004292 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004293 break;
4294 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004295}
4296
Daniel Vetter976f8a22012-07-08 22:34:21 +02004297/**
4298 * Sets the power management mode of the pipe and plane.
4299 */
4300void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004301{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004302 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004303 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004304 struct intel_encoder *intel_encoder;
4305 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004306
Daniel Vetter976f8a22012-07-08 22:34:21 +02004307 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4308 enable |= intel_encoder->connectors_active;
4309
4310 if (enable)
4311 dev_priv->display.crtc_enable(crtc);
4312 else
4313 dev_priv->display.crtc_disable(crtc);
4314
4315 intel_crtc_update_sarea(crtc, enable);
4316}
4317
Daniel Vetter976f8a22012-07-08 22:34:21 +02004318static void intel_crtc_disable(struct drm_crtc *crtc)
4319{
4320 struct drm_device *dev = crtc->dev;
4321 struct drm_connector *connector;
4322 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004324
4325 /* crtc should still be enabled when we disable it. */
4326 WARN_ON(!crtc->enabled);
4327
4328 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004329 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004330 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004331 dev_priv->display.off(crtc);
4332
Chris Wilson931872f2012-01-16 23:01:13 +00004333 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004334 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004335 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004336
4337 if (crtc->fb) {
4338 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004339 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004340 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004341 crtc->fb = NULL;
4342 }
4343
4344 /* Update computed state. */
4345 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4346 if (!connector->encoder || !connector->encoder->crtc)
4347 continue;
4348
4349 if (connector->encoder->crtc != crtc)
4350 continue;
4351
4352 connector->dpms = DRM_MODE_DPMS_OFF;
4353 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004354 }
4355}
4356
Chris Wilsonea5b2132010-08-04 13:50:23 +01004357void intel_encoder_destroy(struct drm_encoder *encoder)
4358{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004359 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004360
Chris Wilsonea5b2132010-08-04 13:50:23 +01004361 drm_encoder_cleanup(encoder);
4362 kfree(intel_encoder);
4363}
4364
Damien Lespiau92373292013-08-08 22:28:57 +01004365/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004366 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4367 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004368static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004369{
4370 if (mode == DRM_MODE_DPMS_ON) {
4371 encoder->connectors_active = true;
4372
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004373 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004374 } else {
4375 encoder->connectors_active = false;
4376
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004377 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004378 }
4379}
4380
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004381/* Cross check the actual hw state with our own modeset state tracking (and it's
4382 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004383static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004384{
4385 if (connector->get_hw_state(connector)) {
4386 struct intel_encoder *encoder = connector->encoder;
4387 struct drm_crtc *crtc;
4388 bool encoder_enabled;
4389 enum pipe pipe;
4390
4391 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4392 connector->base.base.id,
4393 drm_get_connector_name(&connector->base));
4394
4395 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4396 "wrong connector dpms state\n");
4397 WARN(connector->base.encoder != &encoder->base,
4398 "active connector not linked to encoder\n");
4399 WARN(!encoder->connectors_active,
4400 "encoder->connectors_active not set\n");
4401
4402 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4403 WARN(!encoder_enabled, "encoder not enabled\n");
4404 if (WARN_ON(!encoder->base.crtc))
4405 return;
4406
4407 crtc = encoder->base.crtc;
4408
4409 WARN(!crtc->enabled, "crtc not enabled\n");
4410 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4411 WARN(pipe != to_intel_crtc(crtc)->pipe,
4412 "encoder active on the wrong pipe\n");
4413 }
4414}
4415
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004416/* Even simpler default implementation, if there's really no special case to
4417 * consider. */
4418void intel_connector_dpms(struct drm_connector *connector, int mode)
4419{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004420 /* All the simple cases only support two dpms states. */
4421 if (mode != DRM_MODE_DPMS_ON)
4422 mode = DRM_MODE_DPMS_OFF;
4423
4424 if (mode == connector->dpms)
4425 return;
4426
4427 connector->dpms = mode;
4428
4429 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004430 if (connector->encoder)
4431 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004432
Daniel Vetterb9805142012-08-31 17:37:33 +02004433 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004434}
4435
Daniel Vetterf0947c32012-07-02 13:10:34 +02004436/* Simple connector->get_hw_state implementation for encoders that support only
4437 * one connector and no cloning and hence the encoder state determines the state
4438 * of the connector. */
4439bool intel_connector_get_hw_state(struct intel_connector *connector)
4440{
Daniel Vetter24929352012-07-02 20:28:59 +02004441 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004442 struct intel_encoder *encoder = connector->encoder;
4443
4444 return encoder->get_hw_state(encoder, &pipe);
4445}
4446
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004447static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4448 struct intel_crtc_config *pipe_config)
4449{
4450 struct drm_i915_private *dev_priv = dev->dev_private;
4451 struct intel_crtc *pipe_B_crtc =
4452 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4453
4454 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4455 pipe_name(pipe), pipe_config->fdi_lanes);
4456 if (pipe_config->fdi_lanes > 4) {
4457 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4458 pipe_name(pipe), pipe_config->fdi_lanes);
4459 return false;
4460 }
4461
Paulo Zanonibafb6552013-11-02 21:07:44 -07004462 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004463 if (pipe_config->fdi_lanes > 2) {
4464 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4465 pipe_config->fdi_lanes);
4466 return false;
4467 } else {
4468 return true;
4469 }
4470 }
4471
4472 if (INTEL_INFO(dev)->num_pipes == 2)
4473 return true;
4474
4475 /* Ivybridge 3 pipe is really complicated */
4476 switch (pipe) {
4477 case PIPE_A:
4478 return true;
4479 case PIPE_B:
4480 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4481 pipe_config->fdi_lanes > 2) {
4482 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4483 pipe_name(pipe), pipe_config->fdi_lanes);
4484 return false;
4485 }
4486 return true;
4487 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004488 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004489 pipe_B_crtc->config.fdi_lanes <= 2) {
4490 if (pipe_config->fdi_lanes > 2) {
4491 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4492 pipe_name(pipe), pipe_config->fdi_lanes);
4493 return false;
4494 }
4495 } else {
4496 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4497 return false;
4498 }
4499 return true;
4500 default:
4501 BUG();
4502 }
4503}
4504
Daniel Vettere29c22c2013-02-21 00:00:16 +01004505#define RETRY 1
4506static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4507 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004508{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004509 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004510 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004511 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004512 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004513
Daniel Vettere29c22c2013-02-21 00:00:16 +01004514retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004515 /* FDI is a binary signal running at ~2.7GHz, encoding
4516 * each output octet as 10 bits. The actual frequency
4517 * is stored as a divider into a 100MHz clock, and the
4518 * mode pixel clock is stored in units of 1KHz.
4519 * Hence the bw of each lane in terms of the mode signal
4520 * is:
4521 */
4522 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4523
Damien Lespiau241bfc32013-09-25 16:45:37 +01004524 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004525
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004526 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004527 pipe_config->pipe_bpp);
4528
4529 pipe_config->fdi_lanes = lane;
4530
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004531 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004532 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004533
Daniel Vettere29c22c2013-02-21 00:00:16 +01004534 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4535 intel_crtc->pipe, pipe_config);
4536 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4537 pipe_config->pipe_bpp -= 2*3;
4538 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4539 pipe_config->pipe_bpp);
4540 needs_recompute = true;
4541 pipe_config->bw_constrained = true;
4542
4543 goto retry;
4544 }
4545
4546 if (needs_recompute)
4547 return RETRY;
4548
4549 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004550}
4551
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004552static void hsw_compute_ips_config(struct intel_crtc *crtc,
4553 struct intel_crtc_config *pipe_config)
4554{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004555 pipe_config->ips_enabled = i915_enable_ips &&
4556 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004557 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004558}
4559
Daniel Vettera43f6e02013-06-07 23:10:32 +02004560static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004561 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004562{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004563 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004564 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004565
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004566 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004567 if (INTEL_INFO(dev)->gen < 4) {
4568 struct drm_i915_private *dev_priv = dev->dev_private;
4569 int clock_limit =
4570 dev_priv->display.get_display_clock_speed(dev);
4571
4572 /*
4573 * Enable pixel doubling when the dot clock
4574 * is > 90% of the (display) core speed.
4575 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004576 * GDG double wide on either pipe,
4577 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004578 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004579 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004580 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004581 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004582 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004583 }
4584
Damien Lespiau241bfc32013-09-25 16:45:37 +01004585 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004586 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004587 }
Chris Wilson89749352010-09-12 18:25:19 +01004588
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004589 /*
4590 * Pipe horizontal size must be even in:
4591 * - DVO ganged mode
4592 * - LVDS dual channel mode
4593 * - Double wide pipe
4594 */
4595 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4596 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4597 pipe_config->pipe_src_w &= ~1;
4598
Damien Lespiau8693a822013-05-03 18:48:11 +01004599 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4600 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004601 */
4602 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4603 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004604 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004605
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004606 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004607 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004608 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004609 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4610 * for lvds. */
4611 pipe_config->pipe_bpp = 8*3;
4612 }
4613
Damien Lespiauf5adf942013-06-24 18:29:34 +01004614 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004615 hsw_compute_ips_config(crtc, pipe_config);
4616
4617 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4618 * clock survives for now. */
4619 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4620 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004621
Daniel Vetter877d48d2013-04-19 11:24:43 +02004622 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004623 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004624
Daniel Vettere29c22c2013-02-21 00:00:16 +01004625 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004626}
4627
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004628static int valleyview_get_display_clock_speed(struct drm_device *dev)
4629{
4630 return 400000; /* FIXME */
4631}
4632
Jesse Barnese70236a2009-09-21 10:42:27 -07004633static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004634{
Jesse Barnese70236a2009-09-21 10:42:27 -07004635 return 400000;
4636}
Jesse Barnes79e53942008-11-07 14:24:08 -08004637
Jesse Barnese70236a2009-09-21 10:42:27 -07004638static int i915_get_display_clock_speed(struct drm_device *dev)
4639{
4640 return 333000;
4641}
Jesse Barnes79e53942008-11-07 14:24:08 -08004642
Jesse Barnese70236a2009-09-21 10:42:27 -07004643static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4644{
4645 return 200000;
4646}
Jesse Barnes79e53942008-11-07 14:24:08 -08004647
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004648static int pnv_get_display_clock_speed(struct drm_device *dev)
4649{
4650 u16 gcfgc = 0;
4651
4652 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4653
4654 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4655 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4656 return 267000;
4657 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4658 return 333000;
4659 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4660 return 444000;
4661 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4662 return 200000;
4663 default:
4664 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4665 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4666 return 133000;
4667 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4668 return 167000;
4669 }
4670}
4671
Jesse Barnese70236a2009-09-21 10:42:27 -07004672static int i915gm_get_display_clock_speed(struct drm_device *dev)
4673{
4674 u16 gcfgc = 0;
4675
4676 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4677
4678 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004679 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004680 else {
4681 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4682 case GC_DISPLAY_CLOCK_333_MHZ:
4683 return 333000;
4684 default:
4685 case GC_DISPLAY_CLOCK_190_200_MHZ:
4686 return 190000;
4687 }
4688 }
4689}
Jesse Barnes79e53942008-11-07 14:24:08 -08004690
Jesse Barnese70236a2009-09-21 10:42:27 -07004691static int i865_get_display_clock_speed(struct drm_device *dev)
4692{
4693 return 266000;
4694}
4695
4696static int i855_get_display_clock_speed(struct drm_device *dev)
4697{
4698 u16 hpllcc = 0;
4699 /* Assume that the hardware is in the high speed state. This
4700 * should be the default.
4701 */
4702 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4703 case GC_CLOCK_133_200:
4704 case GC_CLOCK_100_200:
4705 return 200000;
4706 case GC_CLOCK_166_250:
4707 return 250000;
4708 case GC_CLOCK_100_133:
4709 return 133000;
4710 }
4711
4712 /* Shouldn't happen */
4713 return 0;
4714}
4715
4716static int i830_get_display_clock_speed(struct drm_device *dev)
4717{
4718 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004719}
4720
Zhenyu Wang2c072452009-06-05 15:38:42 +08004721static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004722intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004723{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004724 while (*num > DATA_LINK_M_N_MASK ||
4725 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004726 *num >>= 1;
4727 *den >>= 1;
4728 }
4729}
4730
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004731static void compute_m_n(unsigned int m, unsigned int n,
4732 uint32_t *ret_m, uint32_t *ret_n)
4733{
4734 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4735 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4736 intel_reduce_m_n_ratio(ret_m, ret_n);
4737}
4738
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004739void
4740intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4741 int pixel_clock, int link_clock,
4742 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004743{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004744 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004745
4746 compute_m_n(bits_per_pixel * pixel_clock,
4747 link_clock * nlanes * 8,
4748 &m_n->gmch_m, &m_n->gmch_n);
4749
4750 compute_m_n(pixel_clock, link_clock,
4751 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004752}
4753
Chris Wilsona7615032011-01-12 17:04:08 +00004754static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4755{
Keith Packard72bbe582011-09-26 16:09:45 -07004756 if (i915_panel_use_ssc >= 0)
4757 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004758 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004759 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004760}
4761
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004762static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4763{
4764 struct drm_device *dev = crtc->dev;
4765 struct drm_i915_private *dev_priv = dev->dev_private;
4766 int refclk;
4767
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004768 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004769 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004770 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004771 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02004772 refclk = dev_priv->vbt.lvds_ssc_freq;
4773 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004774 } else if (!IS_GEN2(dev)) {
4775 refclk = 96000;
4776 } else {
4777 refclk = 48000;
4778 }
4779
4780 return refclk;
4781}
4782
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004783static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004784{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004785 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004786}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004787
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004788static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4789{
4790 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004791}
4792
Daniel Vetterf47709a2013-03-28 10:42:02 +01004793static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004794 intel_clock_t *reduced_clock)
4795{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004796 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004797 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004798 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004799 u32 fp, fp2 = 0;
4800
4801 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004802 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004803 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004804 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004805 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004806 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004807 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004808 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004809 }
4810
4811 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004812 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004813
Daniel Vetterf47709a2013-03-28 10:42:02 +01004814 crtc->lowfreq_avail = false;
4815 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004816 reduced_clock && i915_powersave) {
4817 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004818 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004819 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004820 } else {
4821 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004822 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004823 }
4824}
4825
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004826static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4827 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004828{
4829 u32 reg_val;
4830
4831 /*
4832 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4833 * and set it to a reasonable value instead.
4834 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004835 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004836 reg_val &= 0xffffff00;
4837 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004838 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004839
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004840 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004841 reg_val &= 0x8cffffff;
4842 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004843 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004844
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004845 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004846 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004847 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004848
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004849 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004850 reg_val &= 0x00ffffff;
4851 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004852 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004853}
4854
Daniel Vetterb5518422013-05-03 11:49:48 +02004855static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4856 struct intel_link_m_n *m_n)
4857{
4858 struct drm_device *dev = crtc->base.dev;
4859 struct drm_i915_private *dev_priv = dev->dev_private;
4860 int pipe = crtc->pipe;
4861
Daniel Vettere3b95f12013-05-03 11:49:49 +02004862 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4863 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4864 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4865 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004866}
4867
4868static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4869 struct intel_link_m_n *m_n)
4870{
4871 struct drm_device *dev = crtc->base.dev;
4872 struct drm_i915_private *dev_priv = dev->dev_private;
4873 int pipe = crtc->pipe;
4874 enum transcoder transcoder = crtc->config.cpu_transcoder;
4875
4876 if (INTEL_INFO(dev)->gen >= 5) {
4877 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4878 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4879 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4880 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4881 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004882 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4883 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4884 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4885 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004886 }
4887}
4888
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004889static void intel_dp_set_m_n(struct intel_crtc *crtc)
4890{
4891 if (crtc->config.has_pch_encoder)
4892 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4893 else
4894 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4895}
4896
Daniel Vetterf47709a2013-03-28 10:42:02 +01004897static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004898{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004899 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004900 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004901 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004902 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004903 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004904 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004905
Daniel Vetter09153002012-12-12 14:06:44 +01004906 mutex_lock(&dev_priv->dpio_lock);
4907
Daniel Vetterf47709a2013-03-28 10:42:02 +01004908 bestn = crtc->config.dpll.n;
4909 bestm1 = crtc->config.dpll.m1;
4910 bestm2 = crtc->config.dpll.m2;
4911 bestp1 = crtc->config.dpll.p1;
4912 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004913
Jesse Barnes89b667f2013-04-18 14:51:36 -07004914 /* See eDP HDMI DPIO driver vbios notes doc */
4915
4916 /* PLL B needs special handling */
4917 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004918 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004919
4920 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004921 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004922
4923 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004924 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004925 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004926 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004927
4928 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004929 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004930
4931 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004932 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4933 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4934 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004935 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004936
4937 /*
4938 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4939 * but we don't support that).
4940 * Note: don't use the DAC post divider as it seems unstable.
4941 */
4942 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004944
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004945 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004946 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004947
Jesse Barnes89b667f2013-04-18 14:51:36 -07004948 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004949 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004950 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004951 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004953 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004954 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004956 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004957
Jesse Barnes89b667f2013-04-18 14:51:36 -07004958 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4959 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4960 /* Use SSC source */
4961 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004963 0x0df40000);
4964 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004965 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004966 0x0df70000);
4967 } else { /* HDMI or VGA */
4968 /* Use bend source */
4969 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004970 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004971 0x0df70000);
4972 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004974 0x0df40000);
4975 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004976
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004977 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004978 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4979 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4980 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4981 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004982 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004983
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004984 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004985
Jesse Barnes89b667f2013-04-18 14:51:36 -07004986 /* Enable DPIO clock input */
4987 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4988 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004989 /* We should never disable this, set it here for state tracking */
4990 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004991 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004992 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004993 crtc->config.dpll_hw_state.dpll = dpll;
4994
Daniel Vetteref1b4602013-06-01 17:17:04 +02004995 dpll_md = (crtc->config.pixel_multiplier - 1)
4996 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004997 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4998
Daniel Vetterf47709a2013-03-28 10:42:02 +01004999 if (crtc->config.has_dp_encoder)
5000 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305001
Daniel Vetter09153002012-12-12 14:06:44 +01005002 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005003}
5004
Daniel Vetterf47709a2013-03-28 10:42:02 +01005005static void i9xx_update_pll(struct intel_crtc *crtc,
5006 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005007 int num_connectors)
5008{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005009 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005010 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005011 u32 dpll;
5012 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005013 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005014
Daniel Vetterf47709a2013-03-28 10:42:02 +01005015 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305016
Daniel Vetterf47709a2013-03-28 10:42:02 +01005017 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5018 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005019
5020 dpll = DPLL_VGA_MODE_DIS;
5021
Daniel Vetterf47709a2013-03-28 10:42:02 +01005022 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005023 dpll |= DPLLB_MODE_LVDS;
5024 else
5025 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005026
Daniel Vetteref1b4602013-06-01 17:17:04 +02005027 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005028 dpll |= (crtc->config.pixel_multiplier - 1)
5029 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005030 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005031
5032 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005033 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005034
Daniel Vetterf47709a2013-03-28 10:42:02 +01005035 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005036 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005037
5038 /* compute bitmask from p1 value */
5039 if (IS_PINEVIEW(dev))
5040 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5041 else {
5042 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5043 if (IS_G4X(dev) && reduced_clock)
5044 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5045 }
5046 switch (clock->p2) {
5047 case 5:
5048 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5049 break;
5050 case 7:
5051 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5052 break;
5053 case 10:
5054 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5055 break;
5056 case 14:
5057 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5058 break;
5059 }
5060 if (INTEL_INFO(dev)->gen >= 4)
5061 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5062
Daniel Vetter09ede542013-04-30 14:01:45 +02005063 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005064 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005065 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005066 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5067 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5068 else
5069 dpll |= PLL_REF_INPUT_DREFCLK;
5070
5071 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005072 crtc->config.dpll_hw_state.dpll = dpll;
5073
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005074 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005075 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5076 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005077 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005078 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005079
5080 if (crtc->config.has_dp_encoder)
5081 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005082}
5083
Daniel Vetterf47709a2013-03-28 10:42:02 +01005084static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005085 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005086 int num_connectors)
5087{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005088 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005089 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005090 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005091 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005092
Daniel Vetterf47709a2013-03-28 10:42:02 +01005093 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305094
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005095 dpll = DPLL_VGA_MODE_DIS;
5096
Daniel Vetterf47709a2013-03-28 10:42:02 +01005097 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005098 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5099 } else {
5100 if (clock->p1 == 2)
5101 dpll |= PLL_P1_DIVIDE_BY_TWO;
5102 else
5103 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5104 if (clock->p2 == 4)
5105 dpll |= PLL_P2_DIVIDE_BY_4;
5106 }
5107
Daniel Vetter4a33e482013-07-06 12:52:05 +02005108 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5109 dpll |= DPLL_DVO_2X_MODE;
5110
Daniel Vetterf47709a2013-03-28 10:42:02 +01005111 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005112 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5113 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5114 else
5115 dpll |= PLL_REF_INPUT_DREFCLK;
5116
5117 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005118 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005119}
5120
Daniel Vetter8a654f32013-06-01 17:16:22 +02005121static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005122{
5123 struct drm_device *dev = intel_crtc->base.dev;
5124 struct drm_i915_private *dev_priv = dev->dev_private;
5125 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005126 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005127 struct drm_display_mode *adjusted_mode =
5128 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005129 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5130
5131 /* We need to be careful not to changed the adjusted mode, for otherwise
5132 * the hw state checker will get angry at the mismatch. */
5133 crtc_vtotal = adjusted_mode->crtc_vtotal;
5134 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005135
5136 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5137 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005138 crtc_vtotal -= 1;
5139 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005140 vsyncshift = adjusted_mode->crtc_hsync_start
5141 - adjusted_mode->crtc_htotal / 2;
5142 } else {
5143 vsyncshift = 0;
5144 }
5145
5146 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005147 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005148
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005149 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005150 (adjusted_mode->crtc_hdisplay - 1) |
5151 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005152 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005153 (adjusted_mode->crtc_hblank_start - 1) |
5154 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005155 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005156 (adjusted_mode->crtc_hsync_start - 1) |
5157 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5158
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005159 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005160 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005161 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005162 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005163 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005164 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005165 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005166 (adjusted_mode->crtc_vsync_start - 1) |
5167 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5168
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005169 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5170 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5171 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5172 * bits. */
5173 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5174 (pipe == PIPE_B || pipe == PIPE_C))
5175 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5176
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005177 /* pipesrc controls the size that is scaled from, which should
5178 * always be the user's requested size.
5179 */
5180 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005181 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5182 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005183}
5184
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005185static void intel_get_pipe_timings(struct intel_crtc *crtc,
5186 struct intel_crtc_config *pipe_config)
5187{
5188 struct drm_device *dev = crtc->base.dev;
5189 struct drm_i915_private *dev_priv = dev->dev_private;
5190 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5191 uint32_t tmp;
5192
5193 tmp = I915_READ(HTOTAL(cpu_transcoder));
5194 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5195 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5196 tmp = I915_READ(HBLANK(cpu_transcoder));
5197 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5198 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5199 tmp = I915_READ(HSYNC(cpu_transcoder));
5200 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5201 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5202
5203 tmp = I915_READ(VTOTAL(cpu_transcoder));
5204 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5205 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5206 tmp = I915_READ(VBLANK(cpu_transcoder));
5207 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5208 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5209 tmp = I915_READ(VSYNC(cpu_transcoder));
5210 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5211 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5212
5213 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5214 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5215 pipe_config->adjusted_mode.crtc_vtotal += 1;
5216 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5217 }
5218
5219 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005220 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5221 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5222
5223 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5224 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005225}
5226
Jesse Barnesbabea612013-06-26 18:57:38 +03005227static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5228 struct intel_crtc_config *pipe_config)
5229{
5230 struct drm_crtc *crtc = &intel_crtc->base;
5231
5232 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5233 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5234 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5235 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5236
5237 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5238 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5239 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5240 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5241
5242 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5243
Damien Lespiau241bfc32013-09-25 16:45:37 +01005244 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005245 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5246}
5247
Daniel Vetter84b046f2013-02-19 18:48:54 +01005248static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5249{
5250 struct drm_device *dev = intel_crtc->base.dev;
5251 struct drm_i915_private *dev_priv = dev->dev_private;
5252 uint32_t pipeconf;
5253
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005254 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005255
Daniel Vetter67c72a12013-09-24 11:46:14 +02005256 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5257 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5258 pipeconf |= PIPECONF_ENABLE;
5259
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005260 if (intel_crtc->config.double_wide)
5261 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005262
Daniel Vetterff9ce462013-04-24 14:57:17 +02005263 /* only g4x and later have fancy bpc/dither controls */
5264 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005265 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5266 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5267 pipeconf |= PIPECONF_DITHER_EN |
5268 PIPECONF_DITHER_TYPE_SP;
5269
5270 switch (intel_crtc->config.pipe_bpp) {
5271 case 18:
5272 pipeconf |= PIPECONF_6BPC;
5273 break;
5274 case 24:
5275 pipeconf |= PIPECONF_8BPC;
5276 break;
5277 case 30:
5278 pipeconf |= PIPECONF_10BPC;
5279 break;
5280 default:
5281 /* Case prevented by intel_choose_pipe_bpp_dither. */
5282 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005283 }
5284 }
5285
5286 if (HAS_PIPE_CXSR(dev)) {
5287 if (intel_crtc->lowfreq_avail) {
5288 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5289 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5290 } else {
5291 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005292 }
5293 }
5294
Daniel Vetter84b046f2013-02-19 18:48:54 +01005295 if (!IS_GEN2(dev) &&
5296 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5297 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5298 else
5299 pipeconf |= PIPECONF_PROGRESSIVE;
5300
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005301 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5302 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005303
Daniel Vetter84b046f2013-02-19 18:48:54 +01005304 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5305 POSTING_READ(PIPECONF(intel_crtc->pipe));
5306}
5307
Eric Anholtf564048e2011-03-30 13:01:02 -07005308static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005309 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005310 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005311{
5312 struct drm_device *dev = crtc->dev;
5313 struct drm_i915_private *dev_priv = dev->dev_private;
5314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5315 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005316 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005317 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005318 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005319 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005320 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005321 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005322 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005323 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005324 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005325
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005326 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005327 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005328 case INTEL_OUTPUT_LVDS:
5329 is_lvds = true;
5330 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005331 case INTEL_OUTPUT_DSI:
5332 is_dsi = true;
5333 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005334 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005335
Eric Anholtc751ce42010-03-25 11:48:48 -07005336 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005337 }
5338
Jani Nikulaf2335332013-09-13 11:03:09 +03005339 if (is_dsi)
5340 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005341
Jani Nikulaf2335332013-09-13 11:03:09 +03005342 if (!intel_crtc->config.clock_set) {
5343 refclk = i9xx_get_refclk(crtc, num_connectors);
5344
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005345 /*
5346 * Returns a set of divisors for the desired target clock with
5347 * the given refclk, or FALSE. The returned values represent
5348 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5349 * 2) / p1 / p2.
5350 */
5351 limit = intel_limit(crtc, refclk);
5352 ok = dev_priv->display.find_dpll(limit, crtc,
5353 intel_crtc->config.port_clock,
5354 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005355 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005356 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5357 return -EINVAL;
5358 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005359
Jani Nikulaf2335332013-09-13 11:03:09 +03005360 if (is_lvds && dev_priv->lvds_downclock_avail) {
5361 /*
5362 * Ensure we match the reduced clock's P to the target
5363 * clock. If the clocks don't match, we can't switch
5364 * the display clock by using the FP0/FP1. In such case
5365 * we will disable the LVDS downclock feature.
5366 */
5367 has_reduced_clock =
5368 dev_priv->display.find_dpll(limit, crtc,
5369 dev_priv->lvds_downclock,
5370 refclk, &clock,
5371 &reduced_clock);
5372 }
5373 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005374 intel_crtc->config.dpll.n = clock.n;
5375 intel_crtc->config.dpll.m1 = clock.m1;
5376 intel_crtc->config.dpll.m2 = clock.m2;
5377 intel_crtc->config.dpll.p1 = clock.p1;
5378 intel_crtc->config.dpll.p2 = clock.p2;
5379 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005380
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005381 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005382 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305383 has_reduced_clock ? &reduced_clock : NULL,
5384 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005385 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005386 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005387 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005388 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005389 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005390 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005391 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005392
Jani Nikulaf2335332013-09-13 11:03:09 +03005393skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005394 /* Set up the display plane register */
5395 dspcntr = DISPPLANE_GAMMA_ENABLE;
5396
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005397 if (!IS_VALLEYVIEW(dev)) {
5398 if (pipe == 0)
5399 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5400 else
5401 dspcntr |= DISPPLANE_SEL_PIPE_B;
5402 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005403
Daniel Vetter8a654f32013-06-01 17:16:22 +02005404 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005405
5406 /* pipesrc and dspsize control the size that is scaled from,
5407 * which should always be the user's requested size.
5408 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005409 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005410 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5411 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005412 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005413
Daniel Vetter84b046f2013-02-19 18:48:54 +01005414 i9xx_set_pipeconf(intel_crtc);
5415
Eric Anholtf564048e2011-03-30 13:01:02 -07005416 I915_WRITE(DSPCNTR(plane), dspcntr);
5417 POSTING_READ(DSPCNTR(plane));
5418
Daniel Vetter94352cf2012-07-05 22:51:56 +02005419 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005420
Eric Anholtf564048e2011-03-30 13:01:02 -07005421 return ret;
5422}
5423
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005424static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5425 struct intel_crtc_config *pipe_config)
5426{
5427 struct drm_device *dev = crtc->base.dev;
5428 struct drm_i915_private *dev_priv = dev->dev_private;
5429 uint32_t tmp;
5430
5431 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005432 if (!(tmp & PFIT_ENABLE))
5433 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005434
Daniel Vetter06922822013-07-11 13:35:40 +02005435 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005436 if (INTEL_INFO(dev)->gen < 4) {
5437 if (crtc->pipe != PIPE_B)
5438 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005439 } else {
5440 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5441 return;
5442 }
5443
Daniel Vetter06922822013-07-11 13:35:40 +02005444 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005445 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5446 if (INTEL_INFO(dev)->gen < 5)
5447 pipe_config->gmch_pfit.lvds_border_bits =
5448 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5449}
5450
Jesse Barnesacbec812013-09-20 11:29:32 -07005451static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5452 struct intel_crtc_config *pipe_config)
5453{
5454 struct drm_device *dev = crtc->base.dev;
5455 struct drm_i915_private *dev_priv = dev->dev_private;
5456 int pipe = pipe_config->cpu_transcoder;
5457 intel_clock_t clock;
5458 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005459 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005460
5461 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005462 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005463 mutex_unlock(&dev_priv->dpio_lock);
5464
5465 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5466 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5467 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5468 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5469 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5470
Ville Syrjäläf6466282013-10-14 14:50:31 +03005471 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005472
Ville Syrjäläf6466282013-10-14 14:50:31 +03005473 /* clock.dot is the fast clock */
5474 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005475}
5476
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005477static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5478 struct intel_crtc_config *pipe_config)
5479{
5480 struct drm_device *dev = crtc->base.dev;
5481 struct drm_i915_private *dev_priv = dev->dev_private;
5482 uint32_t tmp;
5483
Daniel Vettere143a212013-07-04 12:01:15 +02005484 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005485 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005486
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005487 tmp = I915_READ(PIPECONF(crtc->pipe));
5488 if (!(tmp & PIPECONF_ENABLE))
5489 return false;
5490
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005491 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5492 switch (tmp & PIPECONF_BPC_MASK) {
5493 case PIPECONF_6BPC:
5494 pipe_config->pipe_bpp = 18;
5495 break;
5496 case PIPECONF_8BPC:
5497 pipe_config->pipe_bpp = 24;
5498 break;
5499 case PIPECONF_10BPC:
5500 pipe_config->pipe_bpp = 30;
5501 break;
5502 default:
5503 break;
5504 }
5505 }
5506
Ville Syrjälä282740f2013-09-04 18:30:03 +03005507 if (INTEL_INFO(dev)->gen < 4)
5508 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5509
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005510 intel_get_pipe_timings(crtc, pipe_config);
5511
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005512 i9xx_get_pfit_config(crtc, pipe_config);
5513
Daniel Vetter6c49f242013-06-06 12:45:25 +02005514 if (INTEL_INFO(dev)->gen >= 4) {
5515 tmp = I915_READ(DPLL_MD(crtc->pipe));
5516 pipe_config->pixel_multiplier =
5517 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5518 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005519 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005520 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5521 tmp = I915_READ(DPLL(crtc->pipe));
5522 pipe_config->pixel_multiplier =
5523 ((tmp & SDVO_MULTIPLIER_MASK)
5524 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5525 } else {
5526 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5527 * port and will be fixed up in the encoder->get_config
5528 * function. */
5529 pipe_config->pixel_multiplier = 1;
5530 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005531 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5532 if (!IS_VALLEYVIEW(dev)) {
5533 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5534 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005535 } else {
5536 /* Mask out read-only status bits. */
5537 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5538 DPLL_PORTC_READY_MASK |
5539 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005540 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005541
Jesse Barnesacbec812013-09-20 11:29:32 -07005542 if (IS_VALLEYVIEW(dev))
5543 vlv_crtc_clock_get(crtc, pipe_config);
5544 else
5545 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005546
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005547 return true;
5548}
5549
Paulo Zanonidde86e22012-12-01 12:04:25 -02005550static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005551{
5552 struct drm_i915_private *dev_priv = dev->dev_private;
5553 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005554 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005555 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005556 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005557 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005558 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005559 bool has_ck505 = false;
5560 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005561
5562 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005563 list_for_each_entry(encoder, &mode_config->encoder_list,
5564 base.head) {
5565 switch (encoder->type) {
5566 case INTEL_OUTPUT_LVDS:
5567 has_panel = true;
5568 has_lvds = true;
5569 break;
5570 case INTEL_OUTPUT_EDP:
5571 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005572 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005573 has_cpu_edp = true;
5574 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005575 }
5576 }
5577
Keith Packard99eb6a02011-09-26 14:29:12 -07005578 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005579 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005580 can_ssc = has_ck505;
5581 } else {
5582 has_ck505 = false;
5583 can_ssc = true;
5584 }
5585
Imre Deak2de69052013-05-08 13:14:04 +03005586 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5587 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005588
5589 /* Ironlake: try to setup display ref clock before DPLL
5590 * enabling. This is only under driver's control after
5591 * PCH B stepping, previous chipset stepping should be
5592 * ignoring this setting.
5593 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005594 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005595
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005596 /* As we must carefully and slowly disable/enable each source in turn,
5597 * compute the final state we want first and check if we need to
5598 * make any changes at all.
5599 */
5600 final = val;
5601 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005602 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005603 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005604 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005605 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5606
5607 final &= ~DREF_SSC_SOURCE_MASK;
5608 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5609 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005610
Keith Packard199e5d72011-09-22 12:01:57 -07005611 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005612 final |= DREF_SSC_SOURCE_ENABLE;
5613
5614 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5615 final |= DREF_SSC1_ENABLE;
5616
5617 if (has_cpu_edp) {
5618 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5619 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5620 else
5621 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5622 } else
5623 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5624 } else {
5625 final |= DREF_SSC_SOURCE_DISABLE;
5626 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5627 }
5628
5629 if (final == val)
5630 return;
5631
5632 /* Always enable nonspread source */
5633 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5634
5635 if (has_ck505)
5636 val |= DREF_NONSPREAD_CK505_ENABLE;
5637 else
5638 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5639
5640 if (has_panel) {
5641 val &= ~DREF_SSC_SOURCE_MASK;
5642 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005643
Keith Packard199e5d72011-09-22 12:01:57 -07005644 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005645 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005646 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005647 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005648 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005649 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005650
5651 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005652 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005653 POSTING_READ(PCH_DREF_CONTROL);
5654 udelay(200);
5655
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005656 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005657
5658 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005659 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005660 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005661 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005662 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005663 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005664 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005665 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005666 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005667 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005668
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005669 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005670 POSTING_READ(PCH_DREF_CONTROL);
5671 udelay(200);
5672 } else {
5673 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5674
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005675 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005676
5677 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005678 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005679
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005680 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005681 POSTING_READ(PCH_DREF_CONTROL);
5682 udelay(200);
5683
5684 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005685 val &= ~DREF_SSC_SOURCE_MASK;
5686 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005687
5688 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005689 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005690
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005691 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005692 POSTING_READ(PCH_DREF_CONTROL);
5693 udelay(200);
5694 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005695
5696 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005697}
5698
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005699static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005700{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005701 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005702
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005703 tmp = I915_READ(SOUTH_CHICKEN2);
5704 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5705 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005706
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005707 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5708 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5709 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005710
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005711 tmp = I915_READ(SOUTH_CHICKEN2);
5712 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5713 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005714
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005715 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5716 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5717 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005718}
5719
5720/* WaMPhyProgramming:hsw */
5721static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5722{
5723 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005724
5725 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5726 tmp &= ~(0xFF << 24);
5727 tmp |= (0x12 << 24);
5728 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5729
Paulo Zanonidde86e22012-12-01 12:04:25 -02005730 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5731 tmp |= (1 << 11);
5732 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5733
5734 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5735 tmp |= (1 << 11);
5736 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5737
Paulo Zanonidde86e22012-12-01 12:04:25 -02005738 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5739 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5740 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5741
5742 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5743 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5744 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5745
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005746 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5747 tmp &= ~(7 << 13);
5748 tmp |= (5 << 13);
5749 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005750
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005751 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5752 tmp &= ~(7 << 13);
5753 tmp |= (5 << 13);
5754 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005755
5756 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5757 tmp &= ~0xFF;
5758 tmp |= 0x1C;
5759 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5760
5761 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5762 tmp &= ~0xFF;
5763 tmp |= 0x1C;
5764 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5765
5766 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5767 tmp &= ~(0xFF << 16);
5768 tmp |= (0x1C << 16);
5769 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5770
5771 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5772 tmp &= ~(0xFF << 16);
5773 tmp |= (0x1C << 16);
5774 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5775
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005776 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5777 tmp |= (1 << 27);
5778 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005779
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005780 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5781 tmp |= (1 << 27);
5782 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005783
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005784 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5785 tmp &= ~(0xF << 28);
5786 tmp |= (4 << 28);
5787 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005788
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005789 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5790 tmp &= ~(0xF << 28);
5791 tmp |= (4 << 28);
5792 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005793}
5794
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005795/* Implements 3 different sequences from BSpec chapter "Display iCLK
5796 * Programming" based on the parameters passed:
5797 * - Sequence to enable CLKOUT_DP
5798 * - Sequence to enable CLKOUT_DP without spread
5799 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5800 */
5801static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5802 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005803{
5804 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005805 uint32_t reg, tmp;
5806
5807 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5808 with_spread = true;
5809 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5810 with_fdi, "LP PCH doesn't have FDI\n"))
5811 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005812
5813 mutex_lock(&dev_priv->dpio_lock);
5814
5815 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5816 tmp &= ~SBI_SSCCTL_DISABLE;
5817 tmp |= SBI_SSCCTL_PATHALT;
5818 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5819
5820 udelay(24);
5821
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005822 if (with_spread) {
5823 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5824 tmp &= ~SBI_SSCCTL_PATHALT;
5825 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005826
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005827 if (with_fdi) {
5828 lpt_reset_fdi_mphy(dev_priv);
5829 lpt_program_fdi_mphy(dev_priv);
5830 }
5831 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005832
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005833 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5834 SBI_GEN0 : SBI_DBUFF0;
5835 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5836 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5837 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005838
5839 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005840}
5841
Paulo Zanoni47701c32013-07-23 11:19:25 -03005842/* Sequence to disable CLKOUT_DP */
5843static void lpt_disable_clkout_dp(struct drm_device *dev)
5844{
5845 struct drm_i915_private *dev_priv = dev->dev_private;
5846 uint32_t reg, tmp;
5847
5848 mutex_lock(&dev_priv->dpio_lock);
5849
5850 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5851 SBI_GEN0 : SBI_DBUFF0;
5852 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5853 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5854 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5855
5856 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5857 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5858 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5859 tmp |= SBI_SSCCTL_PATHALT;
5860 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5861 udelay(32);
5862 }
5863 tmp |= SBI_SSCCTL_DISABLE;
5864 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5865 }
5866
5867 mutex_unlock(&dev_priv->dpio_lock);
5868}
5869
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005870static void lpt_init_pch_refclk(struct drm_device *dev)
5871{
5872 struct drm_mode_config *mode_config = &dev->mode_config;
5873 struct intel_encoder *encoder;
5874 bool has_vga = false;
5875
5876 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5877 switch (encoder->type) {
5878 case INTEL_OUTPUT_ANALOG:
5879 has_vga = true;
5880 break;
5881 }
5882 }
5883
Paulo Zanoni47701c32013-07-23 11:19:25 -03005884 if (has_vga)
5885 lpt_enable_clkout_dp(dev, true, true);
5886 else
5887 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005888}
5889
Paulo Zanonidde86e22012-12-01 12:04:25 -02005890/*
5891 * Initialize reference clocks when the driver loads
5892 */
5893void intel_init_pch_refclk(struct drm_device *dev)
5894{
5895 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5896 ironlake_init_pch_refclk(dev);
5897 else if (HAS_PCH_LPT(dev))
5898 lpt_init_pch_refclk(dev);
5899}
5900
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005901static int ironlake_get_refclk(struct drm_crtc *crtc)
5902{
5903 struct drm_device *dev = crtc->dev;
5904 struct drm_i915_private *dev_priv = dev->dev_private;
5905 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005906 int num_connectors = 0;
5907 bool is_lvds = false;
5908
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005909 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005910 switch (encoder->type) {
5911 case INTEL_OUTPUT_LVDS:
5912 is_lvds = true;
5913 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005914 }
5915 num_connectors++;
5916 }
5917
5918 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005919 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005920 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005921 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005922 }
5923
5924 return 120000;
5925}
5926
Daniel Vetter6ff93602013-04-19 11:24:36 +02005927static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005928{
5929 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5931 int pipe = intel_crtc->pipe;
5932 uint32_t val;
5933
Daniel Vetter78114072013-06-13 00:54:57 +02005934 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005935
Daniel Vetter965e0c42013-03-27 00:44:57 +01005936 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005937 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005938 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005939 break;
5940 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005941 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005942 break;
5943 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005944 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005945 break;
5946 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005947 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005948 break;
5949 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005950 /* Case prevented by intel_choose_pipe_bpp_dither. */
5951 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005952 }
5953
Daniel Vetterd8b32242013-04-25 17:54:44 +02005954 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005955 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5956
Daniel Vetter6ff93602013-04-19 11:24:36 +02005957 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005958 val |= PIPECONF_INTERLACED_ILK;
5959 else
5960 val |= PIPECONF_PROGRESSIVE;
5961
Daniel Vetter50f3b012013-03-27 00:44:56 +01005962 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005963 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005964
Paulo Zanonic8203562012-09-12 10:06:29 -03005965 I915_WRITE(PIPECONF(pipe), val);
5966 POSTING_READ(PIPECONF(pipe));
5967}
5968
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005969/*
5970 * Set up the pipe CSC unit.
5971 *
5972 * Currently only full range RGB to limited range RGB conversion
5973 * is supported, but eventually this should handle various
5974 * RGB<->YCbCr scenarios as well.
5975 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005976static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005977{
5978 struct drm_device *dev = crtc->dev;
5979 struct drm_i915_private *dev_priv = dev->dev_private;
5980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5981 int pipe = intel_crtc->pipe;
5982 uint16_t coeff = 0x7800; /* 1.0 */
5983
5984 /*
5985 * TODO: Check what kind of values actually come out of the pipe
5986 * with these coeff/postoff values and adjust to get the best
5987 * accuracy. Perhaps we even need to take the bpc value into
5988 * consideration.
5989 */
5990
Daniel Vetter50f3b012013-03-27 00:44:56 +01005991 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005992 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5993
5994 /*
5995 * GY/GU and RY/RU should be the other way around according
5996 * to BSpec, but reality doesn't agree. Just set them up in
5997 * a way that results in the correct picture.
5998 */
5999 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6000 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6001
6002 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6003 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6004
6005 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6006 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6007
6008 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6009 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6010 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6011
6012 if (INTEL_INFO(dev)->gen > 6) {
6013 uint16_t postoff = 0;
6014
Daniel Vetter50f3b012013-03-27 00:44:56 +01006015 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006016 postoff = (16 * (1 << 13) / 255) & 0x1fff;
6017
6018 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6019 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6020 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6021
6022 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6023 } else {
6024 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6025
Daniel Vetter50f3b012013-03-27 00:44:56 +01006026 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006027 mode |= CSC_BLACK_SCREEN_OFFSET;
6028
6029 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6030 }
6031}
6032
Daniel Vetter6ff93602013-04-19 11:24:36 +02006033static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006034{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006035 struct drm_device *dev = crtc->dev;
6036 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006038 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006039 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006040 uint32_t val;
6041
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006042 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006043
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006044 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006045 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6046
Daniel Vetter6ff93602013-04-19 11:24:36 +02006047 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006048 val |= PIPECONF_INTERLACED_ILK;
6049 else
6050 val |= PIPECONF_PROGRESSIVE;
6051
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006052 I915_WRITE(PIPECONF(cpu_transcoder), val);
6053 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006054
6055 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6056 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006057
6058 if (IS_BROADWELL(dev)) {
6059 val = 0;
6060
6061 switch (intel_crtc->config.pipe_bpp) {
6062 case 18:
6063 val |= PIPEMISC_DITHER_6_BPC;
6064 break;
6065 case 24:
6066 val |= PIPEMISC_DITHER_8_BPC;
6067 break;
6068 case 30:
6069 val |= PIPEMISC_DITHER_10_BPC;
6070 break;
6071 case 36:
6072 val |= PIPEMISC_DITHER_12_BPC;
6073 break;
6074 default:
6075 /* Case prevented by pipe_config_set_bpp. */
6076 BUG();
6077 }
6078
6079 if (intel_crtc->config.dither)
6080 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6081
6082 I915_WRITE(PIPEMISC(pipe), val);
6083 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006084}
6085
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006086static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006087 intel_clock_t *clock,
6088 bool *has_reduced_clock,
6089 intel_clock_t *reduced_clock)
6090{
6091 struct drm_device *dev = crtc->dev;
6092 struct drm_i915_private *dev_priv = dev->dev_private;
6093 struct intel_encoder *intel_encoder;
6094 int refclk;
6095 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006096 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006097
6098 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6099 switch (intel_encoder->type) {
6100 case INTEL_OUTPUT_LVDS:
6101 is_lvds = true;
6102 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006103 }
6104 }
6105
6106 refclk = ironlake_get_refclk(crtc);
6107
6108 /*
6109 * Returns a set of divisors for the desired target clock with the given
6110 * refclk, or FALSE. The returned values represent the clock equation:
6111 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6112 */
6113 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006114 ret = dev_priv->display.find_dpll(limit, crtc,
6115 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006116 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006117 if (!ret)
6118 return false;
6119
6120 if (is_lvds && dev_priv->lvds_downclock_avail) {
6121 /*
6122 * Ensure we match the reduced clock's P to the target clock.
6123 * If the clocks don't match, we can't switch the display clock
6124 * by using the FP0/FP1. In such case we will disable the LVDS
6125 * downclock feature.
6126 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006127 *has_reduced_clock =
6128 dev_priv->display.find_dpll(limit, crtc,
6129 dev_priv->lvds_downclock,
6130 refclk, clock,
6131 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006132 }
6133
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006134 return true;
6135}
6136
Paulo Zanonid4b19312012-11-29 11:29:32 -02006137int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6138{
6139 /*
6140 * Account for spread spectrum to avoid
6141 * oversubscribing the link. Max center spread
6142 * is 2.5%; use 5% for safety's sake.
6143 */
6144 u32 bps = target_clock * bpp * 21 / 20;
6145 return bps / (link_bw * 8) + 1;
6146}
6147
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006148static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006149{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006150 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006151}
6152
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006153static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006154 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006155 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006156{
6157 struct drm_crtc *crtc = &intel_crtc->base;
6158 struct drm_device *dev = crtc->dev;
6159 struct drm_i915_private *dev_priv = dev->dev_private;
6160 struct intel_encoder *intel_encoder;
6161 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006162 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006163 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006164
6165 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6166 switch (intel_encoder->type) {
6167 case INTEL_OUTPUT_LVDS:
6168 is_lvds = true;
6169 break;
6170 case INTEL_OUTPUT_SDVO:
6171 case INTEL_OUTPUT_HDMI:
6172 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006173 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006174 }
6175
6176 num_connectors++;
6177 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006178
Chris Wilsonc1858122010-12-03 21:35:48 +00006179 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006180 factor = 21;
6181 if (is_lvds) {
6182 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006183 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006184 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006185 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006186 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006187 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006188
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006189 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006190 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006191
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006192 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6193 *fp2 |= FP_CB_TUNE;
6194
Chris Wilson5eddb702010-09-11 13:48:45 +01006195 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006196
Eric Anholta07d6782011-03-30 13:01:08 -07006197 if (is_lvds)
6198 dpll |= DPLLB_MODE_LVDS;
6199 else
6200 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006201
Daniel Vetteref1b4602013-06-01 17:17:04 +02006202 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6203 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006204
6205 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006206 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006207 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006208 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006209
Eric Anholta07d6782011-03-30 13:01:08 -07006210 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006211 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006212 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006213 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006214
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006215 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006216 case 5:
6217 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6218 break;
6219 case 7:
6220 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6221 break;
6222 case 10:
6223 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6224 break;
6225 case 14:
6226 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6227 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006228 }
6229
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006230 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006231 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006232 else
6233 dpll |= PLL_REF_INPUT_DREFCLK;
6234
Daniel Vetter959e16d2013-06-05 13:34:21 +02006235 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006236}
6237
Jesse Barnes79e53942008-11-07 14:24:08 -08006238static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006239 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006240 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006241{
6242 struct drm_device *dev = crtc->dev;
6243 struct drm_i915_private *dev_priv = dev->dev_private;
6244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6245 int pipe = intel_crtc->pipe;
6246 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006247 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006248 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006249 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006250 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006251 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006252 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006253 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006254 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006255
6256 for_each_encoder_on_crtc(dev, crtc, encoder) {
6257 switch (encoder->type) {
6258 case INTEL_OUTPUT_LVDS:
6259 is_lvds = true;
6260 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006261 }
6262
6263 num_connectors++;
6264 }
6265
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006266 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6267 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6268
Daniel Vetterff9a6752013-06-01 17:16:21 +02006269 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006270 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006271 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006272 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6273 return -EINVAL;
6274 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006275 /* Compat-code for transition, will disappear. */
6276 if (!intel_crtc->config.clock_set) {
6277 intel_crtc->config.dpll.n = clock.n;
6278 intel_crtc->config.dpll.m1 = clock.m1;
6279 intel_crtc->config.dpll.m2 = clock.m2;
6280 intel_crtc->config.dpll.p1 = clock.p1;
6281 intel_crtc->config.dpll.p2 = clock.p2;
6282 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006283
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006284 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006285 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006286 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006287 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006288 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006289
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006290 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006291 &fp, &reduced_clock,
6292 has_reduced_clock ? &fp2 : NULL);
6293
Daniel Vetter959e16d2013-06-05 13:34:21 +02006294 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006295 intel_crtc->config.dpll_hw_state.fp0 = fp;
6296 if (has_reduced_clock)
6297 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6298 else
6299 intel_crtc->config.dpll_hw_state.fp1 = fp;
6300
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006301 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006302 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006303 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6304 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006305 return -EINVAL;
6306 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006307 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006308 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006309
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006310 if (intel_crtc->config.has_dp_encoder)
6311 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006312
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006313 if (is_lvds && has_reduced_clock && i915_powersave)
6314 intel_crtc->lowfreq_avail = true;
6315 else
6316 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006317
Daniel Vetter8a654f32013-06-01 17:16:22 +02006318 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006319
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006320 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006321 intel_cpu_transcoder_set_m_n(intel_crtc,
6322 &intel_crtc->config.fdi_m_n);
6323 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006324
Daniel Vetter6ff93602013-04-19 11:24:36 +02006325 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006326
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006327 /* Set up the display plane register */
6328 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006329 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006330
Daniel Vetter94352cf2012-07-05 22:51:56 +02006331 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006332
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006333 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006334}
6335
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006336static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6337 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006338{
6339 struct drm_device *dev = crtc->base.dev;
6340 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006341 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006342
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006343 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6344 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6345 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6346 & ~TU_SIZE_MASK;
6347 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6348 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6349 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6350}
6351
6352static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6353 enum transcoder transcoder,
6354 struct intel_link_m_n *m_n)
6355{
6356 struct drm_device *dev = crtc->base.dev;
6357 struct drm_i915_private *dev_priv = dev->dev_private;
6358 enum pipe pipe = crtc->pipe;
6359
6360 if (INTEL_INFO(dev)->gen >= 5) {
6361 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6362 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6363 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6364 & ~TU_SIZE_MASK;
6365 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6366 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6367 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6368 } else {
6369 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6370 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6371 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6372 & ~TU_SIZE_MASK;
6373 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6374 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6375 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6376 }
6377}
6378
6379void intel_dp_get_m_n(struct intel_crtc *crtc,
6380 struct intel_crtc_config *pipe_config)
6381{
6382 if (crtc->config.has_pch_encoder)
6383 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6384 else
6385 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6386 &pipe_config->dp_m_n);
6387}
6388
Daniel Vetter72419202013-04-04 13:28:53 +02006389static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6390 struct intel_crtc_config *pipe_config)
6391{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006392 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6393 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006394}
6395
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006396static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6397 struct intel_crtc_config *pipe_config)
6398{
6399 struct drm_device *dev = crtc->base.dev;
6400 struct drm_i915_private *dev_priv = dev->dev_private;
6401 uint32_t tmp;
6402
6403 tmp = I915_READ(PF_CTL(crtc->pipe));
6404
6405 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006406 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006407 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6408 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006409
6410 /* We currently do not free assignements of panel fitters on
6411 * ivb/hsw (since we don't use the higher upscaling modes which
6412 * differentiates them) so just WARN about this case for now. */
6413 if (IS_GEN7(dev)) {
6414 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6415 PF_PIPE_SEL_IVB(crtc->pipe));
6416 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006417 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006418}
6419
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006420static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6421 struct intel_crtc_config *pipe_config)
6422{
6423 struct drm_device *dev = crtc->base.dev;
6424 struct drm_i915_private *dev_priv = dev->dev_private;
6425 uint32_t tmp;
6426
Daniel Vettere143a212013-07-04 12:01:15 +02006427 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006428 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006429
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006430 tmp = I915_READ(PIPECONF(crtc->pipe));
6431 if (!(tmp & PIPECONF_ENABLE))
6432 return false;
6433
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006434 switch (tmp & PIPECONF_BPC_MASK) {
6435 case PIPECONF_6BPC:
6436 pipe_config->pipe_bpp = 18;
6437 break;
6438 case PIPECONF_8BPC:
6439 pipe_config->pipe_bpp = 24;
6440 break;
6441 case PIPECONF_10BPC:
6442 pipe_config->pipe_bpp = 30;
6443 break;
6444 case PIPECONF_12BPC:
6445 pipe_config->pipe_bpp = 36;
6446 break;
6447 default:
6448 break;
6449 }
6450
Daniel Vetterab9412b2013-05-03 11:49:46 +02006451 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006452 struct intel_shared_dpll *pll;
6453
Daniel Vetter88adfff2013-03-28 10:42:01 +01006454 pipe_config->has_pch_encoder = true;
6455
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006456 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6457 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6458 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006459
6460 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006461
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006462 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006463 pipe_config->shared_dpll =
6464 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006465 } else {
6466 tmp = I915_READ(PCH_DPLL_SEL);
6467 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6468 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6469 else
6470 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6471 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006472
6473 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6474
6475 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6476 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006477
6478 tmp = pipe_config->dpll_hw_state.dpll;
6479 pipe_config->pixel_multiplier =
6480 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6481 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006482
6483 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006484 } else {
6485 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006486 }
6487
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006488 intel_get_pipe_timings(crtc, pipe_config);
6489
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006490 ironlake_get_pfit_config(crtc, pipe_config);
6491
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006492 return true;
6493}
6494
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006495static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6496{
6497 struct drm_device *dev = dev_priv->dev;
6498 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6499 struct intel_crtc *crtc;
6500 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006501 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006502
6503 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
Paulo Zanoni798183c2013-12-06 20:29:01 -02006504 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006505 pipe_name(crtc->pipe));
6506
6507 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6508 WARN(plls->spll_refcount, "SPLL enabled\n");
6509 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6510 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6511 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6512 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6513 "CPU PWM1 enabled\n");
6514 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6515 "CPU PWM2 enabled\n");
6516 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6517 "PCH PWM1 enabled\n");
6518 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6519 "Utility pin enabled\n");
6520 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6521
6522 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6523 val = I915_READ(DEIMR);
Paulo Zanoni6806e632013-11-21 13:47:24 -02006524 WARN((val | DE_PCH_EVENT_IVB) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006525 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6526 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006527 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006528 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6529 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6530}
6531
6532/*
6533 * This function implements pieces of two sequences from BSpec:
6534 * - Sequence for display software to disable LCPLL
6535 * - Sequence for display software to allow package C8+
6536 * The steps implemented here are just the steps that actually touch the LCPLL
6537 * register. Callers should take care of disabling all the display engine
6538 * functions, doing the mode unset, fixing interrupts, etc.
6539 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006540static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6541 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006542{
6543 uint32_t val;
6544
6545 assert_can_disable_lcpll(dev_priv);
6546
6547 val = I915_READ(LCPLL_CTL);
6548
6549 if (switch_to_fclk) {
6550 val |= LCPLL_CD_SOURCE_FCLK;
6551 I915_WRITE(LCPLL_CTL, val);
6552
6553 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6554 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6555 DRM_ERROR("Switching to FCLK failed\n");
6556
6557 val = I915_READ(LCPLL_CTL);
6558 }
6559
6560 val |= LCPLL_PLL_DISABLE;
6561 I915_WRITE(LCPLL_CTL, val);
6562 POSTING_READ(LCPLL_CTL);
6563
6564 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6565 DRM_ERROR("LCPLL still locked\n");
6566
6567 val = I915_READ(D_COMP);
6568 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006569 mutex_lock(&dev_priv->rps.hw_lock);
6570 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6571 DRM_ERROR("Failed to disable D_COMP\n");
6572 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006573 POSTING_READ(D_COMP);
6574 ndelay(100);
6575
6576 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6577 DRM_ERROR("D_COMP RCOMP still in progress\n");
6578
6579 if (allow_power_down) {
6580 val = I915_READ(LCPLL_CTL);
6581 val |= LCPLL_POWER_DOWN_ALLOW;
6582 I915_WRITE(LCPLL_CTL, val);
6583 POSTING_READ(LCPLL_CTL);
6584 }
6585}
6586
6587/*
6588 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6589 * source.
6590 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006591static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006592{
6593 uint32_t val;
6594
6595 val = I915_READ(LCPLL_CTL);
6596
6597 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6598 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6599 return;
6600
Paulo Zanoni215733f2013-08-19 13:18:07 -03006601 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6602 * we'll hang the machine! */
Deepak Sc8d9a592013-11-23 14:55:42 +05306603 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03006604
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006605 if (val & LCPLL_POWER_DOWN_ALLOW) {
6606 val &= ~LCPLL_POWER_DOWN_ALLOW;
6607 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006608 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006609 }
6610
6611 val = I915_READ(D_COMP);
6612 val |= D_COMP_COMP_FORCE;
6613 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006614 mutex_lock(&dev_priv->rps.hw_lock);
6615 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6616 DRM_ERROR("Failed to enable D_COMP\n");
6617 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006618 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006619
6620 val = I915_READ(LCPLL_CTL);
6621 val &= ~LCPLL_PLL_DISABLE;
6622 I915_WRITE(LCPLL_CTL, val);
6623
6624 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6625 DRM_ERROR("LCPLL not locked yet\n");
6626
6627 if (val & LCPLL_CD_SOURCE_FCLK) {
6628 val = I915_READ(LCPLL_CTL);
6629 val &= ~LCPLL_CD_SOURCE_FCLK;
6630 I915_WRITE(LCPLL_CTL, val);
6631
6632 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6633 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6634 DRM_ERROR("Switching back to LCPLL failed\n");
6635 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006636
Deepak Sc8d9a592013-11-23 14:55:42 +05306637 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006638}
6639
Paulo Zanonic67a4702013-08-19 13:18:09 -03006640void hsw_enable_pc8_work(struct work_struct *__work)
6641{
6642 struct drm_i915_private *dev_priv =
6643 container_of(to_delayed_work(__work), struct drm_i915_private,
6644 pc8.enable_work);
6645 struct drm_device *dev = dev_priv->dev;
6646 uint32_t val;
6647
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006648 WARN_ON(!HAS_PC8(dev));
6649
Paulo Zanonic67a4702013-08-19 13:18:09 -03006650 if (dev_priv->pc8.enabled)
6651 return;
6652
6653 DRM_DEBUG_KMS("Enabling package C8+\n");
6654
6655 dev_priv->pc8.enabled = true;
6656
6657 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6658 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6659 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6660 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6661 }
6662
6663 lpt_disable_clkout_dp(dev);
6664 hsw_pc8_disable_interrupts(dev);
6665 hsw_disable_lcpll(dev_priv, true, true);
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006666
6667 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006668}
6669
6670static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6671{
6672 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6673 WARN(dev_priv->pc8.disable_count < 1,
6674 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6675
6676 dev_priv->pc8.disable_count--;
6677 if (dev_priv->pc8.disable_count != 0)
6678 return;
6679
6680 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006681 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006682}
6683
6684static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6685{
6686 struct drm_device *dev = dev_priv->dev;
6687 uint32_t val;
6688
6689 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6690 WARN(dev_priv->pc8.disable_count < 0,
6691 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6692
6693 dev_priv->pc8.disable_count++;
6694 if (dev_priv->pc8.disable_count != 1)
6695 return;
6696
Paulo Zanoni7125ecb82013-11-21 13:47:15 -02006697 WARN_ON(!HAS_PC8(dev));
6698
Paulo Zanonic67a4702013-08-19 13:18:09 -03006699 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6700 if (!dev_priv->pc8.enabled)
6701 return;
6702
6703 DRM_DEBUG_KMS("Disabling package C8+\n");
6704
Paulo Zanoni8771a7f2013-11-21 13:47:28 -02006705 intel_runtime_pm_get(dev_priv);
6706
Paulo Zanonic67a4702013-08-19 13:18:09 -03006707 hsw_restore_lcpll(dev_priv);
6708 hsw_pc8_restore_interrupts(dev);
6709 lpt_init_pch_refclk(dev);
6710
6711 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6712 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6713 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6714 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6715 }
6716
6717 intel_prepare_ddi(dev);
6718 i915_gem_init_swizzling(dev);
6719 mutex_lock(&dev_priv->rps.hw_lock);
6720 gen6_update_ring_freq(dev);
6721 mutex_unlock(&dev_priv->rps.hw_lock);
6722 dev_priv->pc8.enabled = false;
6723}
6724
6725void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6726{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006727 if (!HAS_PC8(dev_priv->dev))
6728 return;
6729
Paulo Zanonic67a4702013-08-19 13:18:09 -03006730 mutex_lock(&dev_priv->pc8.lock);
6731 __hsw_enable_package_c8(dev_priv);
6732 mutex_unlock(&dev_priv->pc8.lock);
6733}
6734
6735void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6736{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006737 if (!HAS_PC8(dev_priv->dev))
6738 return;
6739
Paulo Zanonic67a4702013-08-19 13:18:09 -03006740 mutex_lock(&dev_priv->pc8.lock);
6741 __hsw_disable_package_c8(dev_priv);
6742 mutex_unlock(&dev_priv->pc8.lock);
6743}
6744
6745static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6746{
6747 struct drm_device *dev = dev_priv->dev;
6748 struct intel_crtc *crtc;
6749 uint32_t val;
6750
6751 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6752 if (crtc->base.enabled)
6753 return false;
6754
6755 /* This case is still possible since we have the i915.disable_power_well
6756 * parameter and also the KVMr or something else might be requesting the
6757 * power well. */
6758 val = I915_READ(HSW_PWR_WELL_DRIVER);
6759 if (val != 0) {
6760 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6761 return false;
6762 }
6763
6764 return true;
6765}
6766
6767/* Since we're called from modeset_global_resources there's no way to
6768 * symmetrically increase and decrease the refcount, so we use
6769 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6770 * or not.
6771 */
6772static void hsw_update_package_c8(struct drm_device *dev)
6773{
6774 struct drm_i915_private *dev_priv = dev->dev_private;
6775 bool allow;
6776
Chris Wilson7c6c2652013-11-18 18:32:37 -08006777 if (!HAS_PC8(dev_priv->dev))
6778 return;
6779
Paulo Zanonic67a4702013-08-19 13:18:09 -03006780 if (!i915_enable_pc8)
6781 return;
6782
6783 mutex_lock(&dev_priv->pc8.lock);
6784
6785 allow = hsw_can_enable_package_c8(dev_priv);
6786
6787 if (allow == dev_priv->pc8.requirements_met)
6788 goto done;
6789
6790 dev_priv->pc8.requirements_met = allow;
6791
6792 if (allow)
6793 __hsw_enable_package_c8(dev_priv);
6794 else
6795 __hsw_disable_package_c8(dev_priv);
6796
6797done:
6798 mutex_unlock(&dev_priv->pc8.lock);
6799}
6800
6801static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6802{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006803 if (!HAS_PC8(dev_priv->dev))
6804 return;
6805
Chris Wilson34581222013-11-18 18:32:36 -08006806 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006807 if (!dev_priv->pc8.gpu_idle) {
6808 dev_priv->pc8.gpu_idle = true;
Chris Wilson34581222013-11-18 18:32:36 -08006809 __hsw_enable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006810 }
Chris Wilson34581222013-11-18 18:32:36 -08006811 mutex_unlock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006812}
6813
6814static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6815{
Chris Wilson7c6c2652013-11-18 18:32:37 -08006816 if (!HAS_PC8(dev_priv->dev))
6817 return;
6818
Chris Wilson34581222013-11-18 18:32:36 -08006819 mutex_lock(&dev_priv->pc8.lock);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006820 if (dev_priv->pc8.gpu_idle) {
6821 dev_priv->pc8.gpu_idle = false;
Chris Wilson34581222013-11-18 18:32:36 -08006822 __hsw_disable_package_c8(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006823 }
Chris Wilson34581222013-11-18 18:32:36 -08006824 mutex_unlock(&dev_priv->pc8.lock);
Daniel Vetter94352cf2012-07-05 22:51:56 +02006825}
Eric Anholtf564048e2011-03-30 13:01:02 -07006826
Imre Deak6efdf352013-10-16 17:25:52 +03006827#define for_each_power_domain(domain, mask) \
6828 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6829 if ((1 << (domain)) & (mask))
6830
6831static unsigned long get_pipe_power_domains(struct drm_device *dev,
6832 enum pipe pipe, bool pfit_enabled)
6833{
6834 unsigned long mask;
6835 enum transcoder transcoder;
6836
6837 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6838
6839 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6840 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6841 if (pfit_enabled)
6842 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6843
6844 return mask;
6845}
6846
Imre Deakbaa70702013-10-25 17:36:48 +03006847void intel_display_set_init_power(struct drm_device *dev, bool enable)
6848{
6849 struct drm_i915_private *dev_priv = dev->dev_private;
6850
6851 if (dev_priv->power_domains.init_power_on == enable)
6852 return;
6853
6854 if (enable)
6855 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6856 else
6857 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6858
6859 dev_priv->power_domains.init_power_on = enable;
6860}
6861
Imre Deak4f074122013-10-16 17:25:51 +03006862static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006863{
Imre Deak6efdf352013-10-16 17:25:52 +03006864 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006865 struct intel_crtc *crtc;
6866
Imre Deak6efdf352013-10-16 17:25:52 +03006867 /*
6868 * First get all needed power domains, then put all unneeded, to avoid
6869 * any unnecessary toggling of the power wells.
6870 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006871 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006872 enum intel_display_power_domain domain;
6873
Jesse Barnes79e53942008-11-07 14:24:08 -08006874 if (!crtc->base.enabled)
6875 continue;
6876
Imre Deak6efdf352013-10-16 17:25:52 +03006877 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6878 crtc->pipe,
6879 crtc->config.pch_pfit.enabled);
6880
6881 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6882 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006883 }
6884
Imre Deak6efdf352013-10-16 17:25:52 +03006885 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6886 enum intel_display_power_domain domain;
6887
6888 for_each_power_domain(domain, crtc->enabled_power_domains)
6889 intel_display_power_put(dev, domain);
6890
6891 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6892 }
Imre Deakbaa70702013-10-25 17:36:48 +03006893
6894 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006895}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006896
Imre Deak4f074122013-10-16 17:25:51 +03006897static void haswell_modeset_global_resources(struct drm_device *dev)
6898{
6899 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006900 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006901}
6902
6903static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6904 int x, int y,
6905 struct drm_framebuffer *fb)
6906{
6907 struct drm_device *dev = crtc->dev;
6908 struct drm_i915_private *dev_priv = dev->dev_private;
6909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6910 int plane = intel_crtc->plane;
6911 int ret;
6912
Paulo Zanoni566b7342013-11-25 15:27:08 -02006913 if (!intel_ddi_pll_select(intel_crtc))
Chris Wilson560b85b2010-08-07 11:01:38 +01006914 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02006915 intel_ddi_pll_enable(intel_crtc);
Chris Wilson560b85b2010-08-07 11:01:38 +01006916
Chris Wilson560b85b2010-08-07 11:01:38 +01006917 if (intel_crtc->config.has_dp_encoder)
6918 intel_dp_set_m_n(intel_crtc);
6919
6920 intel_crtc->lowfreq_avail = false;
6921
6922 intel_set_pipe_timings(intel_crtc);
6923
6924 if (intel_crtc->config.has_pch_encoder) {
6925 intel_cpu_transcoder_set_m_n(intel_crtc,
6926 &intel_crtc->config.fdi_m_n);
6927 }
6928
6929 haswell_set_pipeconf(crtc);
6930
6931 intel_set_pipe_csc(crtc);
6932
6933 /* Set up the display plane register */
6934 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6935 POSTING_READ(DSPCNTR(plane));
6936
6937 ret = intel_pipe_set_base(crtc, x, y, fb);
6938
Chris Wilson560b85b2010-08-07 11:01:38 +01006939 return ret;
6940}
6941
6942static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6943 struct intel_crtc_config *pipe_config)
6944{
6945 struct drm_device *dev = crtc->base.dev;
6946 struct drm_i915_private *dev_priv = dev->dev_private;
6947 enum intel_display_power_domain pfit_domain;
6948 uint32_t tmp;
6949
6950 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6951 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6952
6953 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6954 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6955 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006956 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01006957 default:
6958 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006959 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6960 case TRANS_DDI_EDP_INPUT_A_ON:
6961 trans_edp_pipe = PIPE_A;
6962 break;
6963 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6964 trans_edp_pipe = PIPE_B;
6965 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01006966 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006967 trans_edp_pipe = PIPE_C;
6968 break;
6969 }
6970
Chris Wilson6b383a72010-09-13 13:54:26 +01006971 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006972 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6973 }
6974
6975 if (!intel_display_power_enabled(dev,
6976 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6977 return false;
6978
6979 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6980 if (!(tmp & PIPECONF_ENABLE))
6981 return false;
6982
6983 /*
6984 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6985 * DDI E. So just check whether this pipe is wired to DDI E and whether
6986 * the PCH transcoder is on.
6987 */
6988 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6989 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6990 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6991 pipe_config->has_pch_encoder = true;
6992
6993 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6994 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6995 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6996
6997 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6998 }
6999
Chris Wilson560b85b2010-08-07 11:01:38 +01007000 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007001
7002 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
7003 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01007004 ironlake_get_pfit_config(crtc, pipe_config);
7005
Jesse Barnese59150d2014-01-07 13:30:45 -08007006 if (IS_HASWELL(dev))
7007 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7008 (I915_READ(IPS_CTL) & IPS_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08007009
7010 pipe_config->pixel_multiplier = 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07007011
7012 return true;
7013}
7014
7015static int intel_crtc_mode_set(struct drm_crtc *crtc,
7016 int x, int y,
7017 struct drm_framebuffer *fb)
7018{
Eric Anholt0b701d22011-03-30 13:01:03 -07007019 struct drm_device *dev = crtc->dev;
7020 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01007021 struct intel_encoder *encoder;
Eric Anholtf564048e2011-03-30 13:01:02 -07007022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007023 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholtf564048e2011-03-30 13:01:02 -07007024 int pipe = intel_crtc->pipe;
7025 int ret;
7026
Eric Anholt0b701d22011-03-30 13:01:03 -07007027 drm_vblank_pre_modeset(dev, pipe);
7028
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007029 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7030
Jesse Barnes79e53942008-11-07 14:24:08 -08007031 drm_vblank_post_modeset(dev, pipe);
7032
Daniel Vetter9256aa12012-10-31 19:26:13 +01007033 if (ret != 0)
7034 return ret;
7035
7036 for_each_encoder_on_crtc(dev, crtc, encoder) {
7037 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7038 encoder->base.base.id,
7039 drm_get_encoder_name(&encoder->base),
7040 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02007041 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007042 }
7043
7044 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007045}
7046
Jani Nikula1a915102013-10-16 12:34:48 +03007047static struct {
7048 int clock;
7049 u32 config;
7050} hdmi_audio_clock[] = {
7051 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7052 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7053 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7054 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7055 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7056 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7057 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7058 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7059 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7060 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7061};
7062
7063/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7064static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7065{
7066 int i;
7067
7068 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7069 if (mode->clock == hdmi_audio_clock[i].clock)
7070 break;
7071 }
7072
7073 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7074 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7075 i = 1;
7076 }
7077
7078 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7079 hdmi_audio_clock[i].clock,
7080 hdmi_audio_clock[i].config);
7081
7082 return hdmi_audio_clock[i].config;
7083}
7084
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007085static bool intel_eld_uptodate(struct drm_connector *connector,
7086 int reg_eldv, uint32_t bits_eldv,
7087 int reg_elda, uint32_t bits_elda,
7088 int reg_edid)
7089{
7090 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7091 uint8_t *eld = connector->eld;
7092 uint32_t i;
7093
7094 i = I915_READ(reg_eldv);
7095 i &= bits_eldv;
7096
7097 if (!eld[0])
7098 return !i;
7099
7100 if (!i)
7101 return false;
7102
7103 i = I915_READ(reg_elda);
7104 i &= ~bits_elda;
7105 I915_WRITE(reg_elda, i);
7106
7107 for (i = 0; i < eld[2]; i++)
7108 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7109 return false;
7110
7111 return true;
7112}
7113
Wu Fengguange0dac652011-09-05 14:25:34 +08007114static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007115 struct drm_crtc *crtc,
7116 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007117{
7118 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7119 uint8_t *eld = connector->eld;
7120 uint32_t eldv;
7121 uint32_t len;
7122 uint32_t i;
7123
7124 i = I915_READ(G4X_AUD_VID_DID);
7125
7126 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7127 eldv = G4X_ELDV_DEVCL_DEVBLC;
7128 else
7129 eldv = G4X_ELDV_DEVCTG;
7130
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007131 if (intel_eld_uptodate(connector,
7132 G4X_AUD_CNTL_ST, eldv,
7133 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7134 G4X_HDMIW_HDMIEDID))
7135 return;
7136
Wu Fengguange0dac652011-09-05 14:25:34 +08007137 i = I915_READ(G4X_AUD_CNTL_ST);
7138 i &= ~(eldv | G4X_ELD_ADDR);
7139 len = (i >> 9) & 0x1f; /* ELD buffer size */
7140 I915_WRITE(G4X_AUD_CNTL_ST, i);
7141
7142 if (!eld[0])
7143 return;
7144
7145 len = min_t(uint8_t, eld[2], len);
7146 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7147 for (i = 0; i < len; i++)
7148 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7149
7150 i = I915_READ(G4X_AUD_CNTL_ST);
7151 i |= eldv;
7152 I915_WRITE(G4X_AUD_CNTL_ST, i);
7153}
7154
Wang Xingchao83358c852012-08-16 22:43:37 +08007155static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007156 struct drm_crtc *crtc,
7157 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007158{
7159 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7160 uint8_t *eld = connector->eld;
7161 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007163 uint32_t eldv;
7164 uint32_t i;
7165 int len;
7166 int pipe = to_intel_crtc(crtc)->pipe;
7167 int tmp;
7168
7169 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7170 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7171 int aud_config = HSW_AUD_CFG(pipe);
7172 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7173
7174
7175 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7176
7177 /* Audio output enable */
7178 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7179 tmp = I915_READ(aud_cntrl_st2);
7180 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7181 I915_WRITE(aud_cntrl_st2, tmp);
7182
7183 /* Wait for 1 vertical blank */
7184 intel_wait_for_vblank(dev, pipe);
7185
7186 /* Set ELD valid state */
7187 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007188 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007189 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7190 I915_WRITE(aud_cntrl_st2, tmp);
7191 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007192 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007193
7194 /* Enable HDMI mode */
7195 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007196 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007197 /* clear N_programing_enable and N_value_index */
7198 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7199 I915_WRITE(aud_config, tmp);
7200
7201 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7202
7203 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007204 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007205
7206 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7207 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7208 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7209 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007210 } else {
7211 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7212 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007213
7214 if (intel_eld_uptodate(connector,
7215 aud_cntrl_st2, eldv,
7216 aud_cntl_st, IBX_ELD_ADDRESS,
7217 hdmiw_hdmiedid))
7218 return;
7219
7220 i = I915_READ(aud_cntrl_st2);
7221 i &= ~eldv;
7222 I915_WRITE(aud_cntrl_st2, i);
7223
7224 if (!eld[0])
7225 return;
7226
7227 i = I915_READ(aud_cntl_st);
7228 i &= ~IBX_ELD_ADDRESS;
7229 I915_WRITE(aud_cntl_st, i);
7230 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7231 DRM_DEBUG_DRIVER("port num:%d\n", i);
7232
7233 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7234 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7235 for (i = 0; i < len; i++)
7236 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7237
7238 i = I915_READ(aud_cntrl_st2);
7239 i |= eldv;
7240 I915_WRITE(aud_cntrl_st2, i);
7241
7242}
7243
Wu Fengguange0dac652011-09-05 14:25:34 +08007244static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007245 struct drm_crtc *crtc,
7246 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007247{
7248 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7249 uint8_t *eld = connector->eld;
7250 uint32_t eldv;
7251 uint32_t i;
7252 int len;
7253 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007254 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007255 int aud_cntl_st;
7256 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007257 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007258
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007259 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007260 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7261 aud_config = IBX_AUD_CFG(pipe);
7262 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007263 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007264 } else if (IS_VALLEYVIEW(connector->dev)) {
7265 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7266 aud_config = VLV_AUD_CFG(pipe);
7267 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7268 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007269 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007270 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7271 aud_config = CPT_AUD_CFG(pipe);
7272 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007273 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007274 }
7275
Wang Xingchao9b138a82012-08-09 16:52:18 +08007276 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007277
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007278 if (IS_VALLEYVIEW(connector->dev)) {
7279 struct intel_encoder *intel_encoder;
7280 struct intel_digital_port *intel_dig_port;
7281
7282 intel_encoder = intel_attached_encoder(connector);
7283 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7284 i = intel_dig_port->port;
7285 } else {
7286 i = I915_READ(aud_cntl_st);
7287 i = (i >> 29) & DIP_PORT_SEL_MASK;
7288 /* DIP_Port_Select, 0x1 = PortB */
7289 }
7290
Wu Fengguange0dac652011-09-05 14:25:34 +08007291 if (!i) {
7292 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7293 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007294 eldv = IBX_ELD_VALIDB;
7295 eldv |= IBX_ELD_VALIDB << 4;
7296 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007297 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007298 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007299 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007300 }
7301
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007302 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7303 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7304 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007305 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007306 } else {
7307 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7308 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007309
7310 if (intel_eld_uptodate(connector,
7311 aud_cntrl_st2, eldv,
7312 aud_cntl_st, IBX_ELD_ADDRESS,
7313 hdmiw_hdmiedid))
7314 return;
7315
Wu Fengguange0dac652011-09-05 14:25:34 +08007316 i = I915_READ(aud_cntrl_st2);
7317 i &= ~eldv;
7318 I915_WRITE(aud_cntrl_st2, i);
7319
7320 if (!eld[0])
7321 return;
7322
Wu Fengguange0dac652011-09-05 14:25:34 +08007323 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007324 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007325 I915_WRITE(aud_cntl_st, i);
7326
7327 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7328 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7329 for (i = 0; i < len; i++)
7330 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7331
7332 i = I915_READ(aud_cntrl_st2);
7333 i |= eldv;
7334 I915_WRITE(aud_cntrl_st2, i);
7335}
7336
7337void intel_write_eld(struct drm_encoder *encoder,
7338 struct drm_display_mode *mode)
7339{
7340 struct drm_crtc *crtc = encoder->crtc;
7341 struct drm_connector *connector;
7342 struct drm_device *dev = encoder->dev;
7343 struct drm_i915_private *dev_priv = dev->dev_private;
7344
7345 connector = drm_select_eld(encoder, mode);
7346 if (!connector)
7347 return;
7348
7349 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7350 connector->base.id,
7351 drm_get_connector_name(connector),
7352 connector->encoder->base.id,
7353 drm_get_encoder_name(connector->encoder));
7354
7355 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7356
7357 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007358 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007359}
7360
Jesse Barnes79e53942008-11-07 14:24:08 -08007361static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7362{
7363 struct drm_device *dev = crtc->dev;
7364 struct drm_i915_private *dev_priv = dev->dev_private;
7365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7366 bool visible = base != 0;
7367 u32 cntl;
7368
7369 if (intel_crtc->cursor_visible == visible)
7370 return;
7371
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007372 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007373 if (visible) {
7374 /* On these chipsets we can only modify the base whilst
7375 * the cursor is disabled.
7376 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007377 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007378
7379 cntl &= ~(CURSOR_FORMAT_MASK);
7380 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7381 cntl |= CURSOR_ENABLE |
7382 CURSOR_GAMMA_ENABLE |
7383 CURSOR_FORMAT_ARGB;
7384 } else
7385 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007386 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007387
7388 intel_crtc->cursor_visible = visible;
7389}
7390
7391static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7392{
7393 struct drm_device *dev = crtc->dev;
7394 struct drm_i915_private *dev_priv = dev->dev_private;
7395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7396 int pipe = intel_crtc->pipe;
7397 bool visible = base != 0;
7398
7399 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007400 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007401 if (base) {
7402 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7403 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7404 cntl |= pipe << 28; /* Connect to correct pipe */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007405 } else {
Eric Anholtbad720f2009-10-22 16:11:14 -07007406 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007407 cntl |= CURSOR_MODE_DISABLE;
7408 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007409 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007410
7411 intel_crtc->cursor_visible = visible;
7412 }
7413 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007414 POSTING_READ(CURCNTR(pipe));
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007415 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007416 POSTING_READ(CURBASE(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007417}
7418
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007419static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7420{
7421 struct drm_device *dev = crtc->dev;
7422 struct drm_i915_private *dev_priv = dev->dev_private;
7423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7424 int pipe = intel_crtc->pipe;
7425 bool visible = base != 0;
7426
7427 if (intel_crtc->cursor_visible != visible) {
7428 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7429 if (base) {
7430 cntl &= ~CURSOR_MODE;
7431 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7432 } else {
7433 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7434 cntl |= CURSOR_MODE_DISABLE;
7435 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007436 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007437 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007438 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7439 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007440 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7441
7442 intel_crtc->cursor_visible = visible;
7443 }
7444 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007445 POSTING_READ(CURCNTR_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007446 I915_WRITE(CURBASE_IVB(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007447 POSTING_READ(CURBASE_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007448}
7449
Jesse Barnes79e53942008-11-07 14:24:08 -08007450/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007451static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7452 bool on)
7453{
7454 struct drm_device *dev = crtc->dev;
7455 struct drm_i915_private *dev_priv = dev->dev_private;
7456 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7457 int pipe = intel_crtc->pipe;
7458 int x = intel_crtc->cursor_x;
7459 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007460 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007461 bool visible;
7462
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007463 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007464 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007465
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007466 if (x >= intel_crtc->config.pipe_src_w)
7467 base = 0;
7468
7469 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007470 base = 0;
7471
7472 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007473 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007474 base = 0;
7475
7476 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7477 x = -x;
7478 }
7479 pos |= x << CURSOR_X_SHIFT;
7480
7481 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007482 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007483 base = 0;
7484
7485 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7486 y = -y;
7487 }
7488 pos |= y << CURSOR_Y_SHIFT;
7489
7490 visible = base != 0;
7491 if (!visible && !intel_crtc->cursor_visible)
7492 return;
7493
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007494 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007495 I915_WRITE(CURPOS_IVB(pipe), pos);
7496 ivb_update_cursor(crtc, base);
7497 } else {
7498 I915_WRITE(CURPOS(pipe), pos);
7499 if (IS_845G(dev) || IS_I865G(dev))
7500 i845_update_cursor(crtc, base);
7501 else
7502 i9xx_update_cursor(crtc, base);
7503 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007504}
7505
Jesse Barnes79e53942008-11-07 14:24:08 -08007506static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00007507 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08007508 uint32_t handle,
7509 uint32_t width, uint32_t height)
7510{
7511 struct drm_device *dev = crtc->dev;
7512 struct drm_i915_private *dev_priv = dev->dev_private;
7513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007514 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007515 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007516 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007517
Jesse Barnes79e53942008-11-07 14:24:08 -08007518 /* if we want to turn off the cursor ignore width and height */
7519 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007520 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007521 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007522 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007523 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007524 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007525 }
7526
7527 /* Currently we only support 64x64 cursors */
7528 if (width != 64 || height != 64) {
7529 DRM_ERROR("we currently only support 64x64 cursors\n");
7530 return -EINVAL;
7531 }
7532
Chris Wilson05394f32010-11-08 19:18:58 +00007533 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007534 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007535 return -ENOENT;
7536
Chris Wilson05394f32010-11-08 19:18:58 +00007537 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007538 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007539 ret = -ENOMEM;
7540 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007541 }
7542
Dave Airlie71acb5e2008-12-30 20:31:46 +10007543 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007544 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007545 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007546 unsigned alignment;
7547
Chris Wilsond9e86c02010-11-10 16:40:20 +00007548 if (obj->tiling_mode) {
7549 DRM_ERROR("cursor cannot be tiled\n");
7550 ret = -EINVAL;
7551 goto fail_locked;
7552 }
7553
Chris Wilson693db182013-03-05 14:52:39 +00007554 /* Note that the w/a also requires 2 PTE of padding following
7555 * the bo. We currently fill all unused PTE with the shadow
7556 * page and so we should always have valid PTE following the
7557 * cursor preventing the VT-d warning.
7558 */
7559 alignment = 0;
7560 if (need_vtd_wa(dev))
7561 alignment = 64*1024;
7562
7563 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007564 if (ret) {
7565 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007566 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007567 }
7568
Chris Wilsond9e86c02010-11-10 16:40:20 +00007569 ret = i915_gem_object_put_fence(obj);
7570 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007571 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007572 goto fail_unpin;
7573 }
7574
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007575 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007576 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007577 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007578 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007579 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7580 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007581 if (ret) {
7582 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007583 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007584 }
Chris Wilson05394f32010-11-08 19:18:58 +00007585 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007586 }
7587
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007588 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007589 I915_WRITE(CURSIZE, (height << 12) | width);
7590
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007591 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007592 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007593 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007594 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007595 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7596 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007597 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007598 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007599 }
Jesse Barnes80824002009-09-10 15:28:06 -07007600
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007601 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007602
7603 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007604 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007605 intel_crtc->cursor_width = width;
7606 intel_crtc->cursor_height = height;
7607
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007608 if (intel_crtc->active)
7609 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007610
Jesse Barnes79e53942008-11-07 14:24:08 -08007611 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007612fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007613 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007614fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007615 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007616fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007617 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007618 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007619}
7620
7621static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7622{
Jesse Barnes79e53942008-11-07 14:24:08 -08007623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007624
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007625 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7626 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007627
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007628 if (intel_crtc->active)
7629 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007630
7631 return 0;
7632}
7633
Jesse Barnes79e53942008-11-07 14:24:08 -08007634static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007635 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007636{
James Simmons72034252010-08-03 01:33:19 +01007637 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007639
James Simmons72034252010-08-03 01:33:19 +01007640 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007641 intel_crtc->lut_r[i] = red[i] >> 8;
7642 intel_crtc->lut_g[i] = green[i] >> 8;
7643 intel_crtc->lut_b[i] = blue[i] >> 8;
7644 }
7645
7646 intel_crtc_load_lut(crtc);
7647}
7648
Jesse Barnes79e53942008-11-07 14:24:08 -08007649/* VESA 640x480x72Hz mode to set on the pipe */
7650static struct drm_display_mode load_detect_mode = {
7651 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7652 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7653};
7654
Chris Wilsond2dff872011-04-19 08:36:26 +01007655static struct drm_framebuffer *
7656intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007657 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007658 struct drm_i915_gem_object *obj)
7659{
7660 struct intel_framebuffer *intel_fb;
7661 int ret;
7662
7663 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7664 if (!intel_fb) {
7665 drm_gem_object_unreference_unlocked(&obj->base);
7666 return ERR_PTR(-ENOMEM);
7667 }
7668
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007669 ret = i915_mutex_lock_interruptible(dev);
7670 if (ret)
7671 goto err;
7672
Chris Wilsond2dff872011-04-19 08:36:26 +01007673 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007674 mutex_unlock(&dev->struct_mutex);
7675 if (ret)
7676 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007677
7678 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007679err:
7680 drm_gem_object_unreference_unlocked(&obj->base);
7681 kfree(intel_fb);
7682
7683 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007684}
7685
7686static u32
7687intel_framebuffer_pitch_for_width(int width, int bpp)
7688{
7689 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7690 return ALIGN(pitch, 64);
7691}
7692
7693static u32
7694intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7695{
7696 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7697 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7698}
7699
7700static struct drm_framebuffer *
7701intel_framebuffer_create_for_mode(struct drm_device *dev,
7702 struct drm_display_mode *mode,
7703 int depth, int bpp)
7704{
7705 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007706 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007707
7708 obj = i915_gem_alloc_object(dev,
7709 intel_framebuffer_size_for_mode(mode, bpp));
7710 if (obj == NULL)
7711 return ERR_PTR(-ENOMEM);
7712
7713 mode_cmd.width = mode->hdisplay;
7714 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007715 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7716 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007717 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007718
7719 return intel_framebuffer_create(dev, &mode_cmd, obj);
7720}
7721
7722static struct drm_framebuffer *
7723mode_fits_in_fbdev(struct drm_device *dev,
7724 struct drm_display_mode *mode)
7725{
Daniel Vetter4520f532013-10-09 09:18:51 +02007726#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007727 struct drm_i915_private *dev_priv = dev->dev_private;
7728 struct drm_i915_gem_object *obj;
7729 struct drm_framebuffer *fb;
7730
7731 if (dev_priv->fbdev == NULL)
7732 return NULL;
7733
7734 obj = dev_priv->fbdev->ifb.obj;
7735 if (obj == NULL)
7736 return NULL;
7737
7738 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007739 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7740 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007741 return NULL;
7742
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007743 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007744 return NULL;
7745
7746 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007747#else
7748 return NULL;
7749#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007750}
7751
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007752bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007753 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007754 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007755{
7756 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007757 struct intel_encoder *intel_encoder =
7758 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007759 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007760 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007761 struct drm_crtc *crtc = NULL;
7762 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007763 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007764 int i = -1;
7765
Chris Wilsond2dff872011-04-19 08:36:26 +01007766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7767 connector->base.id, drm_get_connector_name(connector),
7768 encoder->base.id, drm_get_encoder_name(encoder));
7769
Jesse Barnes79e53942008-11-07 14:24:08 -08007770 /*
7771 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007772 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007773 * - if the connector already has an assigned crtc, use it (but make
7774 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007775 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007776 * - try to find the first unused crtc that can drive this connector,
7777 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007778 */
7779
7780 /* See if we already have a CRTC for this connector */
7781 if (encoder->crtc) {
7782 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007783
Daniel Vetter7b240562012-12-12 00:35:33 +01007784 mutex_lock(&crtc->mutex);
7785
Daniel Vetter24218aa2012-08-12 19:27:11 +02007786 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007787 old->load_detect_temp = false;
7788
7789 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007790 if (connector->dpms != DRM_MODE_DPMS_ON)
7791 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007792
Chris Wilson71731882011-04-19 23:10:58 +01007793 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007794 }
7795
7796 /* Find an unused one (if possible) */
7797 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7798 i++;
7799 if (!(encoder->possible_crtcs & (1 << i)))
7800 continue;
7801 if (!possible_crtc->enabled) {
7802 crtc = possible_crtc;
7803 break;
7804 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007805 }
7806
7807 /*
7808 * If we didn't find an unused CRTC, don't use any.
7809 */
7810 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007811 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7812 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007813 }
7814
Daniel Vetter7b240562012-12-12 00:35:33 +01007815 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007816 intel_encoder->new_crtc = to_intel_crtc(crtc);
7817 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007818
7819 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007820 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007821 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007822 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007823
Chris Wilson64927112011-04-20 07:25:26 +01007824 if (!mode)
7825 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007826
Chris Wilsond2dff872011-04-19 08:36:26 +01007827 /* We need a framebuffer large enough to accommodate all accesses
7828 * that the plane may generate whilst we perform load detection.
7829 * We can not rely on the fbcon either being present (we get called
7830 * during its initialisation to detect all boot displays, or it may
7831 * not even exist) or that it is large enough to satisfy the
7832 * requested mode.
7833 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007834 fb = mode_fits_in_fbdev(dev, mode);
7835 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007836 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007837 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7838 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007839 } else
7840 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007841 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007842 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007843 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007844 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007845 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007846
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007847 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007848 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007849 if (old->release_fb)
7850 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007851 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007852 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007853 }
Chris Wilson71731882011-04-19 23:10:58 +01007854
Jesse Barnes79e53942008-11-07 14:24:08 -08007855 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007856 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007857 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007858}
7859
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007860void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007861 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007862{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007863 struct intel_encoder *intel_encoder =
7864 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007865 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007866 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007867
Chris Wilsond2dff872011-04-19 08:36:26 +01007868 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7869 connector->base.id, drm_get_connector_name(connector),
7870 encoder->base.id, drm_get_encoder_name(encoder));
7871
Chris Wilson8261b192011-04-19 23:18:09 +01007872 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007873 to_intel_connector(connector)->new_encoder = NULL;
7874 intel_encoder->new_crtc = NULL;
7875 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007876
Daniel Vetter36206362012-12-10 20:42:17 +01007877 if (old->release_fb) {
7878 drm_framebuffer_unregister_private(old->release_fb);
7879 drm_framebuffer_unreference(old->release_fb);
7880 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007881
Daniel Vetter67c96402013-01-23 16:25:09 +00007882 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007883 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007884 }
7885
Eric Anholtc751ce42010-03-25 11:48:48 -07007886 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007887 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7888 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007889
7890 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007891}
7892
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007893static int i9xx_pll_refclk(struct drm_device *dev,
7894 const struct intel_crtc_config *pipe_config)
7895{
7896 struct drm_i915_private *dev_priv = dev->dev_private;
7897 u32 dpll = pipe_config->dpll_hw_state.dpll;
7898
7899 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007900 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007901 else if (HAS_PCH_SPLIT(dev))
7902 return 120000;
7903 else if (!IS_GEN2(dev))
7904 return 96000;
7905 else
7906 return 48000;
7907}
7908
Jesse Barnes79e53942008-11-07 14:24:08 -08007909/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007910static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7911 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007912{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007913 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007914 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007915 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007916 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007917 u32 fp;
7918 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007919 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007920
7921 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007922 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007923 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007924 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007925
7926 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007927 if (IS_PINEVIEW(dev)) {
7928 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7929 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007930 } else {
7931 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7932 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7933 }
7934
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007935 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007936 if (IS_PINEVIEW(dev))
7937 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7938 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007939 else
7940 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007941 DPLL_FPA01_P1_POST_DIV_SHIFT);
7942
7943 switch (dpll & DPLL_MODE_MASK) {
7944 case DPLLB_MODE_DAC_SERIAL:
7945 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7946 5 : 10;
7947 break;
7948 case DPLLB_MODE_LVDS:
7949 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7950 7 : 14;
7951 break;
7952 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007953 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007954 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007955 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007956 }
7957
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007958 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007959 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007960 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007961 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007962 } else {
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02007963 u32 lvds = I915_READ(LVDS);
7964 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08007965
7966 if (is_lvds) {
7967 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7968 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02007969
7970 if (lvds & LVDS_CLKB_POWER_UP)
7971 clock.p2 = 7;
7972 else
7973 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007974 } else {
7975 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7976 clock.p1 = 2;
7977 else {
7978 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7979 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7980 }
7981 if (dpll & PLL_P2_DIVIDE_BY_4)
7982 clock.p2 = 4;
7983 else
7984 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007985 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007986
7987 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007988 }
7989
Ville Syrjälä18442d02013-09-13 16:00:08 +03007990 /*
7991 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007992 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007993 * encoder's get_config() function.
7994 */
7995 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007996}
7997
Ville Syrjälä6878da02013-09-13 15:59:11 +03007998int intel_dotclock_calculate(int link_freq,
7999 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008000{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008001 /*
8002 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008003 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008004 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008005 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008006 *
8007 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008008 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008009 */
8010
Ville Syrjälä6878da02013-09-13 15:59:11 +03008011 if (!m_n->link_n)
8012 return 0;
8013
8014 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8015}
8016
Ville Syrjälä18442d02013-09-13 16:00:08 +03008017static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8018 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008019{
8020 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008021
8022 /* read out port_clock from the DPLL */
8023 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008024
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008025 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008026 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008027 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008028 * agree once we know their relationship in the encoder's
8029 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008030 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008031 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008032 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8033 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008034}
8035
8036/** Returns the currently programmed mode of the given pipe. */
8037struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8038 struct drm_crtc *crtc)
8039{
Jesse Barnes548f2452011-02-17 10:40:53 -08008040 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008042 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008043 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008044 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008045 int htot = I915_READ(HTOTAL(cpu_transcoder));
8046 int hsync = I915_READ(HSYNC(cpu_transcoder));
8047 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8048 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008049 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008050
8051 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8052 if (!mode)
8053 return NULL;
8054
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008055 /*
8056 * Construct a pipe_config sufficient for getting the clock info
8057 * back out of crtc_clock_get.
8058 *
8059 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8060 * to use a real value here instead.
8061 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008062 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008063 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008064 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8065 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8066 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008067 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8068
Ville Syrjälä773ae032013-09-23 17:48:20 +03008069 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008070 mode->hdisplay = (htot & 0xffff) + 1;
8071 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8072 mode->hsync_start = (hsync & 0xffff) + 1;
8073 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8074 mode->vdisplay = (vtot & 0xffff) + 1;
8075 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8076 mode->vsync_start = (vsync & 0xffff) + 1;
8077 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8078
8079 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008080
8081 return mode;
8082}
8083
Daniel Vetter3dec0092010-08-20 21:40:52 +02008084static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008085{
8086 struct drm_device *dev = crtc->dev;
8087 drm_i915_private_t *dev_priv = dev->dev_private;
8088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8089 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008090 int dpll_reg = DPLL(pipe);
8091 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008092
Eric Anholtbad720f2009-10-22 16:11:14 -07008093 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008094 return;
8095
8096 if (!dev_priv->lvds_downclock_avail)
8097 return;
8098
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008099 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008100 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008101 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008102
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008103 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008104
8105 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8106 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008107 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008108
Jesse Barnes652c3932009-08-17 13:31:43 -07008109 dpll = I915_READ(dpll_reg);
8110 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008111 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008112 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008113}
8114
8115static void intel_decrease_pllclock(struct drm_crtc *crtc)
8116{
8117 struct drm_device *dev = crtc->dev;
8118 drm_i915_private_t *dev_priv = dev->dev_private;
8119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008120
Eric Anholtbad720f2009-10-22 16:11:14 -07008121 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008122 return;
8123
8124 if (!dev_priv->lvds_downclock_avail)
8125 return;
8126
8127 /*
8128 * Since this is called by a timer, we should never get here in
8129 * the manual case.
8130 */
8131 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008132 int pipe = intel_crtc->pipe;
8133 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008134 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008135
Zhao Yakui44d98a62009-10-09 11:39:40 +08008136 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008137
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008138 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008139
Chris Wilson074b5e12012-05-02 12:07:06 +01008140 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008141 dpll |= DISPLAY_RATE_SELECT_FPA1;
8142 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008143 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008144 dpll = I915_READ(dpll_reg);
8145 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008146 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008147 }
8148
8149}
8150
Chris Wilsonf047e392012-07-21 12:31:41 +01008151void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008152{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008153 struct drm_i915_private *dev_priv = dev->dev_private;
8154
8155 hsw_package_c8_gpu_busy(dev_priv);
8156 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008157}
8158
8159void intel_mark_idle(struct drm_device *dev)
8160{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008161 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008162 struct drm_crtc *crtc;
8163
Paulo Zanonic67a4702013-08-19 13:18:09 -03008164 hsw_package_c8_gpu_idle(dev_priv);
8165
Chris Wilson725a5b52013-01-08 11:02:57 +00008166 if (!i915_powersave)
8167 return;
8168
8169 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8170 if (!crtc->fb)
8171 continue;
8172
8173 intel_decrease_pllclock(crtc);
8174 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008175
8176 if (dev_priv->info->gen >= 6)
8177 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008178}
8179
Chris Wilsonc65355b2013-06-06 16:53:41 -03008180void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8181 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008182{
8183 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008184 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008185
8186 if (!i915_powersave)
8187 return;
8188
Jesse Barnes652c3932009-08-17 13:31:43 -07008189 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008190 if (!crtc->fb)
8191 continue;
8192
Chris Wilsonc65355b2013-06-06 16:53:41 -03008193 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8194 continue;
8195
8196 intel_increase_pllclock(crtc);
8197 if (ring && intel_fbc_enabled(dev))
8198 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008199 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008200}
8201
Jesse Barnes79e53942008-11-07 14:24:08 -08008202static void intel_crtc_destroy(struct drm_crtc *crtc)
8203{
8204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008205 struct drm_device *dev = crtc->dev;
8206 struct intel_unpin_work *work;
8207 unsigned long flags;
8208
8209 spin_lock_irqsave(&dev->event_lock, flags);
8210 work = intel_crtc->unpin_work;
8211 intel_crtc->unpin_work = NULL;
8212 spin_unlock_irqrestore(&dev->event_lock, flags);
8213
8214 if (work) {
8215 cancel_work_sync(&work->work);
8216 kfree(work);
8217 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008218
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008219 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8220
Jesse Barnes79e53942008-11-07 14:24:08 -08008221 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008222
Jesse Barnes79e53942008-11-07 14:24:08 -08008223 kfree(intel_crtc);
8224}
8225
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008226static void intel_unpin_work_fn(struct work_struct *__work)
8227{
8228 struct intel_unpin_work *work =
8229 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008230 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008231
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008232 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008233 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008234 drm_gem_object_unreference(&work->pending_flip_obj->base);
8235 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008236
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008237 intel_update_fbc(dev);
8238 mutex_unlock(&dev->struct_mutex);
8239
8240 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8241 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8242
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008243 kfree(work);
8244}
8245
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008246static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008247 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008248{
8249 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8251 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008252 unsigned long flags;
8253
8254 /* Ignore early vblank irqs */
8255 if (intel_crtc == NULL)
8256 return;
8257
8258 spin_lock_irqsave(&dev->event_lock, flags);
8259 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008260
8261 /* Ensure we don't miss a work->pending update ... */
8262 smp_rmb();
8263
8264 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008265 spin_unlock_irqrestore(&dev->event_lock, flags);
8266 return;
8267 }
8268
Chris Wilsone7d841c2012-12-03 11:36:30 +00008269 /* and that the unpin work is consistent wrt ->pending. */
8270 smp_rmb();
8271
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008272 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008273
Rob Clark45a066e2012-10-08 14:50:40 -05008274 if (work->event)
8275 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008276
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008277 drm_vblank_put(dev, intel_crtc->pipe);
8278
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008279 spin_unlock_irqrestore(&dev->event_lock, flags);
8280
Daniel Vetter2c10d572012-12-20 21:24:07 +01008281 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008282
8283 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008284
8285 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008286}
8287
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008288void intel_finish_page_flip(struct drm_device *dev, int pipe)
8289{
8290 drm_i915_private_t *dev_priv = dev->dev_private;
8291 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8292
Mario Kleiner49b14a52010-12-09 07:00:07 +01008293 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008294}
8295
8296void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8297{
8298 drm_i915_private_t *dev_priv = dev->dev_private;
8299 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8300
Mario Kleiner49b14a52010-12-09 07:00:07 +01008301 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008302}
8303
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008304void intel_prepare_page_flip(struct drm_device *dev, int plane)
8305{
8306 drm_i915_private_t *dev_priv = dev->dev_private;
8307 struct intel_crtc *intel_crtc =
8308 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8309 unsigned long flags;
8310
Chris Wilsone7d841c2012-12-03 11:36:30 +00008311 /* NB: An MMIO update of the plane base pointer will also
8312 * generate a page-flip completion irq, i.e. every modeset
8313 * is also accompanied by a spurious intel_prepare_page_flip().
8314 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008315 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008316 if (intel_crtc->unpin_work)
8317 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008318 spin_unlock_irqrestore(&dev->event_lock, flags);
8319}
8320
Chris Wilsone7d841c2012-12-03 11:36:30 +00008321inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8322{
8323 /* Ensure that the work item is consistent when activating it ... */
8324 smp_wmb();
8325 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8326 /* and that it is marked active as soon as the irq could fire. */
8327 smp_wmb();
8328}
8329
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008330static int intel_gen2_queue_flip(struct drm_device *dev,
8331 struct drm_crtc *crtc,
8332 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008333 struct drm_i915_gem_object *obj,
8334 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008335{
8336 struct drm_i915_private *dev_priv = dev->dev_private;
8337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008338 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008339 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008340 int ret;
8341
Daniel Vetter6d90c952012-04-26 23:28:05 +02008342 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008343 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008344 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008345
Daniel Vetter6d90c952012-04-26 23:28:05 +02008346 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008347 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008348 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008349
8350 /* Can't queue multiple flips, so wait for the previous
8351 * one to finish before executing the next.
8352 */
8353 if (intel_crtc->plane)
8354 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8355 else
8356 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008357 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8358 intel_ring_emit(ring, MI_NOOP);
8359 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8360 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8361 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008362 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008363 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008364
8365 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008366 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008367 return 0;
8368
8369err_unpin:
8370 intel_unpin_fb_obj(obj);
8371err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008372 return ret;
8373}
8374
8375static int intel_gen3_queue_flip(struct drm_device *dev,
8376 struct drm_crtc *crtc,
8377 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008378 struct drm_i915_gem_object *obj,
8379 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008380{
8381 struct drm_i915_private *dev_priv = dev->dev_private;
8382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008383 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008384 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008385 int ret;
8386
Daniel Vetter6d90c952012-04-26 23:28:05 +02008387 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008388 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008389 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008390
Daniel Vetter6d90c952012-04-26 23:28:05 +02008391 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008392 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008393 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008394
8395 if (intel_crtc->plane)
8396 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8397 else
8398 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008399 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8400 intel_ring_emit(ring, MI_NOOP);
8401 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8402 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8403 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008404 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008405 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008406
Chris Wilsone7d841c2012-12-03 11:36:30 +00008407 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008408 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008409 return 0;
8410
8411err_unpin:
8412 intel_unpin_fb_obj(obj);
8413err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008414 return ret;
8415}
8416
8417static int intel_gen4_queue_flip(struct drm_device *dev,
8418 struct drm_crtc *crtc,
8419 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008420 struct drm_i915_gem_object *obj,
8421 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008422{
8423 struct drm_i915_private *dev_priv = dev->dev_private;
8424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8425 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008426 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008427 int ret;
8428
Daniel Vetter6d90c952012-04-26 23:28:05 +02008429 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008430 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008431 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008432
Daniel Vetter6d90c952012-04-26 23:28:05 +02008433 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008434 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008435 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008436
8437 /* i965+ uses the linear or tiled offsets from the
8438 * Display Registers (which do not change across a page-flip)
8439 * so we need only reprogram the base address.
8440 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008441 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8442 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8443 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008444 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008445 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008446 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008447
8448 /* XXX Enabling the panel-fitter across page-flip is so far
8449 * untested on non-native modes, so ignore it for now.
8450 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8451 */
8452 pf = 0;
8453 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008454 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008455
8456 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008457 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008458 return 0;
8459
8460err_unpin:
8461 intel_unpin_fb_obj(obj);
8462err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008463 return ret;
8464}
8465
8466static int intel_gen6_queue_flip(struct drm_device *dev,
8467 struct drm_crtc *crtc,
8468 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008469 struct drm_i915_gem_object *obj,
8470 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008471{
8472 struct drm_i915_private *dev_priv = dev->dev_private;
8473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008474 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008475 uint32_t pf, pipesrc;
8476 int ret;
8477
Daniel Vetter6d90c952012-04-26 23:28:05 +02008478 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008479 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008480 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008481
Daniel Vetter6d90c952012-04-26 23:28:05 +02008482 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008483 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008484 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008485
Daniel Vetter6d90c952012-04-26 23:28:05 +02008486 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8487 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8488 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008489 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008490
Chris Wilson99d9acd2012-04-17 20:37:00 +01008491 /* Contrary to the suggestions in the documentation,
8492 * "Enable Panel Fitter" does not seem to be required when page
8493 * flipping with a non-native mode, and worse causes a normal
8494 * modeset to fail.
8495 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8496 */
8497 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008498 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008499 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008500
8501 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008502 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008503 return 0;
8504
8505err_unpin:
8506 intel_unpin_fb_obj(obj);
8507err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008508 return ret;
8509}
8510
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008511static int intel_gen7_queue_flip(struct drm_device *dev,
8512 struct drm_crtc *crtc,
8513 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008514 struct drm_i915_gem_object *obj,
8515 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008516{
8517 struct drm_i915_private *dev_priv = dev->dev_private;
8518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008519 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008520 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008521 int len, ret;
8522
8523 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008524 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008525 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008526
8527 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8528 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008529 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008530
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008531 switch(intel_crtc->plane) {
8532 case PLANE_A:
8533 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8534 break;
8535 case PLANE_B:
8536 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8537 break;
8538 case PLANE_C:
8539 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8540 break;
8541 default:
8542 WARN_ONCE(1, "unknown plane in flip command\n");
8543 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008544 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008545 }
8546
Chris Wilsonffe74d72013-08-26 20:58:12 +01008547 len = 4;
8548 if (ring->id == RCS)
8549 len += 6;
8550
8551 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008552 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008553 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008554
Chris Wilsonffe74d72013-08-26 20:58:12 +01008555 /* Unmask the flip-done completion message. Note that the bspec says that
8556 * we should do this for both the BCS and RCS, and that we must not unmask
8557 * more than one flip event at any time (or ensure that one flip message
8558 * can be sent by waiting for flip-done prior to queueing new flips).
8559 * Experimentation says that BCS works despite DERRMR masking all
8560 * flip-done completion events and that unmasking all planes at once
8561 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8562 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8563 */
8564 if (ring->id == RCS) {
8565 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8566 intel_ring_emit(ring, DERRMR);
8567 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8568 DERRMR_PIPEB_PRI_FLIP_DONE |
8569 DERRMR_PIPEC_PRI_FLIP_DONE));
8570 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8571 intel_ring_emit(ring, DERRMR);
8572 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8573 }
8574
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008575 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008576 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008577 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008578 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008579
8580 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008581 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008582 return 0;
8583
8584err_unpin:
8585 intel_unpin_fb_obj(obj);
8586err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008587 return ret;
8588}
8589
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008590static int intel_default_queue_flip(struct drm_device *dev,
8591 struct drm_crtc *crtc,
8592 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008593 struct drm_i915_gem_object *obj,
8594 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008595{
8596 return -ENODEV;
8597}
8598
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008599static int intel_crtc_page_flip(struct drm_crtc *crtc,
8600 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008601 struct drm_pending_vblank_event *event,
8602 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008603{
8604 struct drm_device *dev = crtc->dev;
8605 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008606 struct drm_framebuffer *old_fb = crtc->fb;
8607 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8609 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008610 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008611 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008612
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008613 /* Can't change pixel format via MI display flips. */
8614 if (fb->pixel_format != crtc->fb->pixel_format)
8615 return -EINVAL;
8616
8617 /*
8618 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8619 * Note that pitch changes could also affect these register.
8620 */
8621 if (INTEL_INFO(dev)->gen > 3 &&
8622 (fb->offsets[0] != crtc->fb->offsets[0] ||
8623 fb->pitches[0] != crtc->fb->pitches[0]))
8624 return -EINVAL;
8625
Daniel Vetterb14c5672013-09-19 12:18:32 +02008626 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008627 if (work == NULL)
8628 return -ENOMEM;
8629
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008630 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008631 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008632 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008633 INIT_WORK(&work->work, intel_unpin_work_fn);
8634
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008635 ret = drm_vblank_get(dev, intel_crtc->pipe);
8636 if (ret)
8637 goto free_work;
8638
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008639 /* We borrow the event spin lock for protecting unpin_work */
8640 spin_lock_irqsave(&dev->event_lock, flags);
8641 if (intel_crtc->unpin_work) {
8642 spin_unlock_irqrestore(&dev->event_lock, flags);
8643 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008644 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008645
8646 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008647 return -EBUSY;
8648 }
8649 intel_crtc->unpin_work = work;
8650 spin_unlock_irqrestore(&dev->event_lock, flags);
8651
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008652 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8653 flush_workqueue(dev_priv->wq);
8654
Chris Wilson79158102012-05-23 11:13:58 +01008655 ret = i915_mutex_lock_interruptible(dev);
8656 if (ret)
8657 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008658
Jesse Barnes75dfca82010-02-10 15:09:44 -08008659 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008660 drm_gem_object_reference(&work->old_fb_obj->base);
8661 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008662
8663 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008664
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008665 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008666
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008667 work->enable_stall_check = true;
8668
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008669 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008670 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008671
Keith Packarded8d1972013-07-22 18:49:58 -07008672 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008673 if (ret)
8674 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008675
Chris Wilson7782de32011-07-08 12:22:41 +01008676 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008677 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008678 mutex_unlock(&dev->struct_mutex);
8679
Jesse Barnese5510fa2010-07-01 16:48:37 -07008680 trace_i915_flip_request(intel_crtc->plane, obj);
8681
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008682 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008683
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008684cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008685 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008686 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008687 drm_gem_object_unreference(&work->old_fb_obj->base);
8688 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008689 mutex_unlock(&dev->struct_mutex);
8690
Chris Wilson79158102012-05-23 11:13:58 +01008691cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008692 spin_lock_irqsave(&dev->event_lock, flags);
8693 intel_crtc->unpin_work = NULL;
8694 spin_unlock_irqrestore(&dev->event_lock, flags);
8695
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008696 drm_vblank_put(dev, intel_crtc->pipe);
8697free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008698 kfree(work);
8699
8700 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008701}
8702
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008703static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008704 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8705 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008706};
8707
Daniel Vetter50f56112012-07-02 09:35:43 +02008708static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8709 struct drm_crtc *crtc)
8710{
8711 struct drm_device *dev;
8712 struct drm_crtc *tmp;
8713 int crtc_mask = 1;
8714
8715 WARN(!crtc, "checking null crtc?\n");
8716
8717 dev = crtc->dev;
8718
8719 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8720 if (tmp == crtc)
8721 break;
8722 crtc_mask <<= 1;
8723 }
8724
8725 if (encoder->possible_crtcs & crtc_mask)
8726 return true;
8727 return false;
8728}
8729
Daniel Vetter9a935852012-07-05 22:34:27 +02008730/**
8731 * intel_modeset_update_staged_output_state
8732 *
8733 * Updates the staged output configuration state, e.g. after we've read out the
8734 * current hw state.
8735 */
8736static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8737{
8738 struct intel_encoder *encoder;
8739 struct intel_connector *connector;
8740
8741 list_for_each_entry(connector, &dev->mode_config.connector_list,
8742 base.head) {
8743 connector->new_encoder =
8744 to_intel_encoder(connector->base.encoder);
8745 }
8746
8747 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8748 base.head) {
8749 encoder->new_crtc =
8750 to_intel_crtc(encoder->base.crtc);
8751 }
8752}
8753
8754/**
8755 * intel_modeset_commit_output_state
8756 *
8757 * This function copies the stage display pipe configuration to the real one.
8758 */
8759static void intel_modeset_commit_output_state(struct drm_device *dev)
8760{
8761 struct intel_encoder *encoder;
8762 struct intel_connector *connector;
8763
8764 list_for_each_entry(connector, &dev->mode_config.connector_list,
8765 base.head) {
8766 connector->base.encoder = &connector->new_encoder->base;
8767 }
8768
8769 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8770 base.head) {
8771 encoder->base.crtc = &encoder->new_crtc->base;
8772 }
8773}
8774
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008775static void
8776connected_sink_compute_bpp(struct intel_connector * connector,
8777 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008778{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008779 int bpp = pipe_config->pipe_bpp;
8780
8781 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8782 connector->base.base.id,
8783 drm_get_connector_name(&connector->base));
8784
8785 /* Don't use an invalid EDID bpc value */
8786 if (connector->base.display_info.bpc &&
8787 connector->base.display_info.bpc * 3 < bpp) {
8788 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8789 bpp, connector->base.display_info.bpc*3);
8790 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8791 }
8792
8793 /* Clamp bpp to 8 on screens without EDID 1.4 */
8794 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8795 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8796 bpp);
8797 pipe_config->pipe_bpp = 24;
8798 }
8799}
8800
8801static int
8802compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8803 struct drm_framebuffer *fb,
8804 struct intel_crtc_config *pipe_config)
8805{
8806 struct drm_device *dev = crtc->base.dev;
8807 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008808 int bpp;
8809
Daniel Vetterd42264b2013-03-28 16:38:08 +01008810 switch (fb->pixel_format) {
8811 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008812 bpp = 8*3; /* since we go through a colormap */
8813 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008814 case DRM_FORMAT_XRGB1555:
8815 case DRM_FORMAT_ARGB1555:
8816 /* checked in intel_framebuffer_init already */
8817 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8818 return -EINVAL;
8819 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008820 bpp = 6*3; /* min is 18bpp */
8821 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008822 case DRM_FORMAT_XBGR8888:
8823 case DRM_FORMAT_ABGR8888:
8824 /* checked in intel_framebuffer_init already */
8825 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8826 return -EINVAL;
8827 case DRM_FORMAT_XRGB8888:
8828 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008829 bpp = 8*3;
8830 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008831 case DRM_FORMAT_XRGB2101010:
8832 case DRM_FORMAT_ARGB2101010:
8833 case DRM_FORMAT_XBGR2101010:
8834 case DRM_FORMAT_ABGR2101010:
8835 /* checked in intel_framebuffer_init already */
8836 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008837 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008838 bpp = 10*3;
8839 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008840 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008841 default:
8842 DRM_DEBUG_KMS("unsupported depth\n");
8843 return -EINVAL;
8844 }
8845
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008846 pipe_config->pipe_bpp = bpp;
8847
8848 /* Clamp display bpp to EDID value */
8849 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008850 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008851 if (!connector->new_encoder ||
8852 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008853 continue;
8854
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008855 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008856 }
8857
8858 return bpp;
8859}
8860
Daniel Vetter644db712013-09-19 14:53:58 +02008861static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8862{
8863 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8864 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008865 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008866 mode->crtc_hdisplay, mode->crtc_hsync_start,
8867 mode->crtc_hsync_end, mode->crtc_htotal,
8868 mode->crtc_vdisplay, mode->crtc_vsync_start,
8869 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8870}
8871
Daniel Vetterc0b03412013-05-28 12:05:54 +02008872static void intel_dump_pipe_config(struct intel_crtc *crtc,
8873 struct intel_crtc_config *pipe_config,
8874 const char *context)
8875{
8876 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8877 context, pipe_name(crtc->pipe));
8878
8879 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8880 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8881 pipe_config->pipe_bpp, pipe_config->dither);
8882 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8883 pipe_config->has_pch_encoder,
8884 pipe_config->fdi_lanes,
8885 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8886 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8887 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008888 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8889 pipe_config->has_dp_encoder,
8890 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8891 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8892 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008893 DRM_DEBUG_KMS("requested mode:\n");
8894 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8895 DRM_DEBUG_KMS("adjusted mode:\n");
8896 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008897 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008898 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008899 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8900 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008901 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8902 pipe_config->gmch_pfit.control,
8903 pipe_config->gmch_pfit.pgm_ratios,
8904 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008905 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008906 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008907 pipe_config->pch_pfit.size,
8908 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008909 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008910 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008911}
8912
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008913static bool check_encoder_cloning(struct drm_crtc *crtc)
8914{
8915 int num_encoders = 0;
8916 bool uncloneable_encoders = false;
8917 struct intel_encoder *encoder;
8918
8919 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8920 base.head) {
8921 if (&encoder->new_crtc->base != crtc)
8922 continue;
8923
8924 num_encoders++;
8925 if (!encoder->cloneable)
8926 uncloneable_encoders = true;
8927 }
8928
8929 return !(num_encoders > 1 && uncloneable_encoders);
8930}
8931
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008932static struct intel_crtc_config *
8933intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008934 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008935 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008936{
8937 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008938 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008939 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008940 int plane_bpp, ret = -EINVAL;
8941 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008942
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008943 if (!check_encoder_cloning(crtc)) {
8944 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8945 return ERR_PTR(-EINVAL);
8946 }
8947
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008948 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8949 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008950 return ERR_PTR(-ENOMEM);
8951
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008952 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8953 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008954
Daniel Vettere143a212013-07-04 12:01:15 +02008955 pipe_config->cpu_transcoder =
8956 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008957 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008958
Imre Deak2960bc92013-07-30 13:36:32 +03008959 /*
8960 * Sanitize sync polarity flags based on requested ones. If neither
8961 * positive or negative polarity is requested, treat this as meaning
8962 * negative polarity.
8963 */
8964 if (!(pipe_config->adjusted_mode.flags &
8965 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8966 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8967
8968 if (!(pipe_config->adjusted_mode.flags &
8969 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8970 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8971
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008972 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8973 * plane pixel format and any sink constraints into account. Returns the
8974 * source plane bpp so that dithering can be selected on mismatches
8975 * after encoders and crtc also have had their say. */
8976 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8977 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008978 if (plane_bpp < 0)
8979 goto fail;
8980
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008981 /*
8982 * Determine the real pipe dimensions. Note that stereo modes can
8983 * increase the actual pipe size due to the frame doubling and
8984 * insertion of additional space for blanks between the frame. This
8985 * is stored in the crtc timings. We use the requested mode to do this
8986 * computation to clearly distinguish it from the adjusted mode, which
8987 * can be changed by the connectors in the below retry loop.
8988 */
8989 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8990 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8991 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8992
Daniel Vettere29c22c2013-02-21 00:00:16 +01008993encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008994 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008995 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008996 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008997
Daniel Vetter135c81b2013-07-21 21:37:09 +02008998 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008999 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009000
Daniel Vetter7758a112012-07-08 19:40:39 +02009001 /* Pass our mode to the connectors and the CRTC to give them a chance to
9002 * adjust it according to limitations or connector properties, and also
9003 * a chance to reject the mode entirely.
9004 */
9005 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9006 base.head) {
9007
9008 if (&encoder->new_crtc->base != crtc)
9009 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009010
Daniel Vetterefea6e82013-07-21 21:36:59 +02009011 if (!(encoder->compute_config(encoder, pipe_config))) {
9012 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009013 goto fail;
9014 }
9015 }
9016
Daniel Vetterff9a6752013-06-01 17:16:21 +02009017 /* Set default port clock if not overwritten by the encoder. Needs to be
9018 * done afterwards in case the encoder adjusts the mode. */
9019 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009020 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9021 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009022
Daniel Vettera43f6e02013-06-07 23:10:32 +02009023 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009024 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009025 DRM_DEBUG_KMS("CRTC fixup failed\n");
9026 goto fail;
9027 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009028
9029 if (ret == RETRY) {
9030 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9031 ret = -EINVAL;
9032 goto fail;
9033 }
9034
9035 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9036 retry = false;
9037 goto encoder_retry;
9038 }
9039
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009040 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9041 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9042 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9043
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009044 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009045fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009046 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009047 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009048}
9049
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009050/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9051 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9052static void
9053intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9054 unsigned *prepare_pipes, unsigned *disable_pipes)
9055{
9056 struct intel_crtc *intel_crtc;
9057 struct drm_device *dev = crtc->dev;
9058 struct intel_encoder *encoder;
9059 struct intel_connector *connector;
9060 struct drm_crtc *tmp_crtc;
9061
9062 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9063
9064 /* Check which crtcs have changed outputs connected to them, these need
9065 * to be part of the prepare_pipes mask. We don't (yet) support global
9066 * modeset across multiple crtcs, so modeset_pipes will only have one
9067 * bit set at most. */
9068 list_for_each_entry(connector, &dev->mode_config.connector_list,
9069 base.head) {
9070 if (connector->base.encoder == &connector->new_encoder->base)
9071 continue;
9072
9073 if (connector->base.encoder) {
9074 tmp_crtc = connector->base.encoder->crtc;
9075
9076 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9077 }
9078
9079 if (connector->new_encoder)
9080 *prepare_pipes |=
9081 1 << connector->new_encoder->new_crtc->pipe;
9082 }
9083
9084 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9085 base.head) {
9086 if (encoder->base.crtc == &encoder->new_crtc->base)
9087 continue;
9088
9089 if (encoder->base.crtc) {
9090 tmp_crtc = encoder->base.crtc;
9091
9092 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9093 }
9094
9095 if (encoder->new_crtc)
9096 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9097 }
9098
9099 /* Check for any pipes that will be fully disabled ... */
9100 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9101 base.head) {
9102 bool used = false;
9103
9104 /* Don't try to disable disabled crtcs. */
9105 if (!intel_crtc->base.enabled)
9106 continue;
9107
9108 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9109 base.head) {
9110 if (encoder->new_crtc == intel_crtc)
9111 used = true;
9112 }
9113
9114 if (!used)
9115 *disable_pipes |= 1 << intel_crtc->pipe;
9116 }
9117
9118
9119 /* set_mode is also used to update properties on life display pipes. */
9120 intel_crtc = to_intel_crtc(crtc);
9121 if (crtc->enabled)
9122 *prepare_pipes |= 1 << intel_crtc->pipe;
9123
Daniel Vetterb6c51642013-04-12 18:48:43 +02009124 /*
9125 * For simplicity do a full modeset on any pipe where the output routing
9126 * changed. We could be more clever, but that would require us to be
9127 * more careful with calling the relevant encoder->mode_set functions.
9128 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009129 if (*prepare_pipes)
9130 *modeset_pipes = *prepare_pipes;
9131
9132 /* ... and mask these out. */
9133 *modeset_pipes &= ~(*disable_pipes);
9134 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009135
9136 /*
9137 * HACK: We don't (yet) fully support global modesets. intel_set_config
9138 * obies this rule, but the modeset restore mode of
9139 * intel_modeset_setup_hw_state does not.
9140 */
9141 *modeset_pipes &= 1 << intel_crtc->pipe;
9142 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009143
9144 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9145 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009146}
9147
Daniel Vetterea9d7582012-07-10 10:42:52 +02009148static bool intel_crtc_in_use(struct drm_crtc *crtc)
9149{
9150 struct drm_encoder *encoder;
9151 struct drm_device *dev = crtc->dev;
9152
9153 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9154 if (encoder->crtc == crtc)
9155 return true;
9156
9157 return false;
9158}
9159
9160static void
9161intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9162{
9163 struct intel_encoder *intel_encoder;
9164 struct intel_crtc *intel_crtc;
9165 struct drm_connector *connector;
9166
9167 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9168 base.head) {
9169 if (!intel_encoder->base.crtc)
9170 continue;
9171
9172 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9173
9174 if (prepare_pipes & (1 << intel_crtc->pipe))
9175 intel_encoder->connectors_active = false;
9176 }
9177
9178 intel_modeset_commit_output_state(dev);
9179
9180 /* Update computed state. */
9181 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9182 base.head) {
9183 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9184 }
9185
9186 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9187 if (!connector->encoder || !connector->encoder->crtc)
9188 continue;
9189
9190 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9191
9192 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009193 struct drm_property *dpms_property =
9194 dev->mode_config.dpms_property;
9195
Daniel Vetterea9d7582012-07-10 10:42:52 +02009196 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009197 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009198 dpms_property,
9199 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009200
9201 intel_encoder = to_intel_encoder(connector->encoder);
9202 intel_encoder->connectors_active = true;
9203 }
9204 }
9205
9206}
9207
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009208static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009209{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009210 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009211
9212 if (clock1 == clock2)
9213 return true;
9214
9215 if (!clock1 || !clock2)
9216 return false;
9217
9218 diff = abs(clock1 - clock2);
9219
9220 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9221 return true;
9222
9223 return false;
9224}
9225
Daniel Vetter25c5b262012-07-08 22:08:04 +02009226#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9227 list_for_each_entry((intel_crtc), \
9228 &(dev)->mode_config.crtc_list, \
9229 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009230 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009231
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009232static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009233intel_pipe_config_compare(struct drm_device *dev,
9234 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009235 struct intel_crtc_config *pipe_config)
9236{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009237#define PIPE_CONF_CHECK_X(name) \
9238 if (current_config->name != pipe_config->name) { \
9239 DRM_ERROR("mismatch in " #name " " \
9240 "(expected 0x%08x, found 0x%08x)\n", \
9241 current_config->name, \
9242 pipe_config->name); \
9243 return false; \
9244 }
9245
Daniel Vetter08a24032013-04-19 11:25:34 +02009246#define PIPE_CONF_CHECK_I(name) \
9247 if (current_config->name != pipe_config->name) { \
9248 DRM_ERROR("mismatch in " #name " " \
9249 "(expected %i, found %i)\n", \
9250 current_config->name, \
9251 pipe_config->name); \
9252 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009253 }
9254
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009255#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9256 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009257 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009258 "(expected %i, found %i)\n", \
9259 current_config->name & (mask), \
9260 pipe_config->name & (mask)); \
9261 return false; \
9262 }
9263
Ville Syrjälä5e550652013-09-06 23:29:07 +03009264#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9265 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9266 DRM_ERROR("mismatch in " #name " " \
9267 "(expected %i, found %i)\n", \
9268 current_config->name, \
9269 pipe_config->name); \
9270 return false; \
9271 }
9272
Daniel Vetterbb760062013-06-06 14:55:52 +02009273#define PIPE_CONF_QUIRK(quirk) \
9274 ((current_config->quirks | pipe_config->quirks) & (quirk))
9275
Daniel Vettereccb1402013-05-22 00:50:22 +02009276 PIPE_CONF_CHECK_I(cpu_transcoder);
9277
Daniel Vetter08a24032013-04-19 11:25:34 +02009278 PIPE_CONF_CHECK_I(has_pch_encoder);
9279 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009280 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9281 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9282 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9283 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9284 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009285
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009286 PIPE_CONF_CHECK_I(has_dp_encoder);
9287 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9288 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9289 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9290 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9291 PIPE_CONF_CHECK_I(dp_m_n.tu);
9292
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009293 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9294 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9295 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9296 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9297 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9298 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9299
9300 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9301 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9302 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9303 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9304 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9305 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9306
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009307 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009308
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009309 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9310 DRM_MODE_FLAG_INTERLACE);
9311
Daniel Vetterbb760062013-06-06 14:55:52 +02009312 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9313 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9314 DRM_MODE_FLAG_PHSYNC);
9315 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9316 DRM_MODE_FLAG_NHSYNC);
9317 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9318 DRM_MODE_FLAG_PVSYNC);
9319 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9320 DRM_MODE_FLAG_NVSYNC);
9321 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009322
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009323 PIPE_CONF_CHECK_I(pipe_src_w);
9324 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009325
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009326 PIPE_CONF_CHECK_I(gmch_pfit.control);
9327 /* pfit ratios are autocomputed by the hw on gen4+ */
9328 if (INTEL_INFO(dev)->gen < 4)
9329 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9330 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009331 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9332 if (current_config->pch_pfit.enabled) {
9333 PIPE_CONF_CHECK_I(pch_pfit.pos);
9334 PIPE_CONF_CHECK_I(pch_pfit.size);
9335 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009336
Jesse Barnese59150d2014-01-07 13:30:45 -08009337 /* BDW+ don't expose a synchronous way to read the state */
9338 if (IS_HASWELL(dev))
9339 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009340
Ville Syrjälä282740f2013-09-04 18:30:03 +03009341 PIPE_CONF_CHECK_I(double_wide);
9342
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009343 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009344 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009345 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009346 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9347 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009348
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009349 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9350 PIPE_CONF_CHECK_I(pipe_bpp);
9351
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009352 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01009353 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009354 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9355 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03009356
Daniel Vetter66e985c2013-06-05 13:34:20 +02009357#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009358#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009359#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009360#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009361#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009362
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009363 return true;
9364}
9365
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009366static void
9367check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009368{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009369 struct intel_connector *connector;
9370
9371 list_for_each_entry(connector, &dev->mode_config.connector_list,
9372 base.head) {
9373 /* This also checks the encoder/connector hw state with the
9374 * ->get_hw_state callbacks. */
9375 intel_connector_check_state(connector);
9376
9377 WARN(&connector->new_encoder->base != connector->base.encoder,
9378 "connector's staged encoder doesn't match current encoder\n");
9379 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009380}
9381
9382static void
9383check_encoder_state(struct drm_device *dev)
9384{
9385 struct intel_encoder *encoder;
9386 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009387
9388 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9389 base.head) {
9390 bool enabled = false;
9391 bool active = false;
9392 enum pipe pipe, tracked_pipe;
9393
9394 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9395 encoder->base.base.id,
9396 drm_get_encoder_name(&encoder->base));
9397
9398 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9399 "encoder's stage crtc doesn't match current crtc\n");
9400 WARN(encoder->connectors_active && !encoder->base.crtc,
9401 "encoder's active_connectors set, but no crtc\n");
9402
9403 list_for_each_entry(connector, &dev->mode_config.connector_list,
9404 base.head) {
9405 if (connector->base.encoder != &encoder->base)
9406 continue;
9407 enabled = true;
9408 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9409 active = true;
9410 }
9411 WARN(!!encoder->base.crtc != enabled,
9412 "encoder's enabled state mismatch "
9413 "(expected %i, found %i)\n",
9414 !!encoder->base.crtc, enabled);
9415 WARN(active && !encoder->base.crtc,
9416 "active encoder with no crtc\n");
9417
9418 WARN(encoder->connectors_active != active,
9419 "encoder's computed active state doesn't match tracked active state "
9420 "(expected %i, found %i)\n", active, encoder->connectors_active);
9421
9422 active = encoder->get_hw_state(encoder, &pipe);
9423 WARN(active != encoder->connectors_active,
9424 "encoder's hw state doesn't match sw tracking "
9425 "(expected %i, found %i)\n",
9426 encoder->connectors_active, active);
9427
9428 if (!encoder->base.crtc)
9429 continue;
9430
9431 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9432 WARN(active && pipe != tracked_pipe,
9433 "active encoder's pipe doesn't match"
9434 "(expected %i, found %i)\n",
9435 tracked_pipe, pipe);
9436
9437 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009438}
9439
9440static void
9441check_crtc_state(struct drm_device *dev)
9442{
9443 drm_i915_private_t *dev_priv = dev->dev_private;
9444 struct intel_crtc *crtc;
9445 struct intel_encoder *encoder;
9446 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009447
9448 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9449 base.head) {
9450 bool enabled = false;
9451 bool active = false;
9452
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009453 memset(&pipe_config, 0, sizeof(pipe_config));
9454
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009455 DRM_DEBUG_KMS("[CRTC:%d]\n",
9456 crtc->base.base.id);
9457
9458 WARN(crtc->active && !crtc->base.enabled,
9459 "active crtc, but not enabled in sw tracking\n");
9460
9461 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9462 base.head) {
9463 if (encoder->base.crtc != &crtc->base)
9464 continue;
9465 enabled = true;
9466 if (encoder->connectors_active)
9467 active = true;
9468 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009469
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009470 WARN(active != crtc->active,
9471 "crtc's computed active state doesn't match tracked active state "
9472 "(expected %i, found %i)\n", active, crtc->active);
9473 WARN(enabled != crtc->base.enabled,
9474 "crtc's computed enabled state doesn't match tracked enabled state "
9475 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9476
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009477 active = dev_priv->display.get_pipe_config(crtc,
9478 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009479
9480 /* hw state is inconsistent with the pipe A quirk */
9481 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9482 active = crtc->active;
9483
Daniel Vetter6c49f242013-06-06 12:45:25 +02009484 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9485 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009486 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009487 if (encoder->base.crtc != &crtc->base)
9488 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +01009489 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009490 encoder->get_config(encoder, &pipe_config);
9491 }
9492
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009493 WARN(crtc->active != active,
9494 "crtc active state doesn't match with hw state "
9495 "(expected %i, found %i)\n", crtc->active, active);
9496
Daniel Vetterc0b03412013-05-28 12:05:54 +02009497 if (active &&
9498 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9499 WARN(1, "pipe state doesn't match!\n");
9500 intel_dump_pipe_config(crtc, &pipe_config,
9501 "[hw state]");
9502 intel_dump_pipe_config(crtc, &crtc->config,
9503 "[sw state]");
9504 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009505 }
9506}
9507
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009508static void
9509check_shared_dpll_state(struct drm_device *dev)
9510{
9511 drm_i915_private_t *dev_priv = dev->dev_private;
9512 struct intel_crtc *crtc;
9513 struct intel_dpll_hw_state dpll_hw_state;
9514 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009515
9516 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9517 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9518 int enabled_crtcs = 0, active_crtcs = 0;
9519 bool active;
9520
9521 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9522
9523 DRM_DEBUG_KMS("%s\n", pll->name);
9524
9525 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9526
9527 WARN(pll->active > pll->refcount,
9528 "more active pll users than references: %i vs %i\n",
9529 pll->active, pll->refcount);
9530 WARN(pll->active && !pll->on,
9531 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009532 WARN(pll->on && !pll->active,
9533 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009534 WARN(pll->on != active,
9535 "pll on state mismatch (expected %i, found %i)\n",
9536 pll->on, active);
9537
9538 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9539 base.head) {
9540 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9541 enabled_crtcs++;
9542 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9543 active_crtcs++;
9544 }
9545 WARN(pll->active != active_crtcs,
9546 "pll active crtcs mismatch (expected %i, found %i)\n",
9547 pll->active, active_crtcs);
9548 WARN(pll->refcount != enabled_crtcs,
9549 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9550 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009551
9552 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9553 sizeof(dpll_hw_state)),
9554 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009555 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009556}
9557
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009558void
9559intel_modeset_check_state(struct drm_device *dev)
9560{
9561 check_connector_state(dev);
9562 check_encoder_state(dev);
9563 check_crtc_state(dev);
9564 check_shared_dpll_state(dev);
9565}
9566
Ville Syrjälä18442d02013-09-13 16:00:08 +03009567void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9568 int dotclock)
9569{
9570 /*
9571 * FDI already provided one idea for the dotclock.
9572 * Yell if the encoder disagrees.
9573 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009574 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009575 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009576 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009577}
9578
Daniel Vetterf30da182013-04-11 20:22:50 +02009579static int __intel_set_mode(struct drm_crtc *crtc,
9580 struct drm_display_mode *mode,
9581 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009582{
9583 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009584 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009585 struct drm_display_mode *saved_mode, *saved_hwmode;
9586 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009587 struct intel_crtc *intel_crtc;
9588 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009589 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009590
Daniel Vettera1e22652013-09-21 00:35:38 +02009591 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009592 if (!saved_mode)
9593 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009594 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009595
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009596 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009597 &prepare_pipes, &disable_pipes);
9598
Tim Gardner3ac18232012-12-07 07:54:26 -07009599 *saved_hwmode = crtc->hwmode;
9600 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009601
Daniel Vetter25c5b262012-07-08 22:08:04 +02009602 /* Hack: Because we don't (yet) support global modeset on multiple
9603 * crtcs, we don't keep track of the new mode for more than one crtc.
9604 * Hence simply check whether any bit is set in modeset_pipes in all the
9605 * pieces of code that are not yet converted to deal with mutliple crtcs
9606 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009607 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009608 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009609 if (IS_ERR(pipe_config)) {
9610 ret = PTR_ERR(pipe_config);
9611 pipe_config = NULL;
9612
Tim Gardner3ac18232012-12-07 07:54:26 -07009613 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009614 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009615 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9616 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009617 }
9618
Jesse Barnes30a970c2013-11-04 13:48:12 -08009619 /*
9620 * See if the config requires any additional preparation, e.g.
9621 * to adjust global state with pipes off. We need to do this
9622 * here so we can get the modeset_pipe updated config for the new
9623 * mode set on this crtc. For other crtcs we need to use the
9624 * adjusted_mode bits in the crtc directly.
9625 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009626 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08009627 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9628 modeset_pipes, pipe_config);
9629
Ville Syrjäläc164f832013-11-05 22:34:12 +02009630 /* may have added more to prepare_pipes than we should */
9631 prepare_pipes &= ~disable_pipes;
9632 }
9633
Daniel Vetter460da9162013-03-27 00:44:51 +01009634 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9635 intel_crtc_disable(&intel_crtc->base);
9636
Daniel Vetterea9d7582012-07-10 10:42:52 +02009637 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9638 if (intel_crtc->base.enabled)
9639 dev_priv->display.crtc_disable(&intel_crtc->base);
9640 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009641
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009642 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9643 * to set it here already despite that we pass it down the callchain.
9644 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009645 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009646 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009647 /* mode_set/enable/disable functions rely on a correct pipe
9648 * config. */
9649 to_intel_crtc(crtc)->config = *pipe_config;
9650 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009651
Daniel Vetterea9d7582012-07-10 10:42:52 +02009652 /* Only after disabling all output pipelines that will be changed can we
9653 * update the the output configuration. */
9654 intel_modeset_update_state(dev, prepare_pipes);
9655
Daniel Vetter47fab732012-10-26 10:58:18 +02009656 if (dev_priv->display.modeset_global_resources)
9657 dev_priv->display.modeset_global_resources(dev);
9658
Daniel Vettera6778b32012-07-02 09:56:42 +02009659 /* Set up the DPLL and any encoders state that needs to adjust or depend
9660 * on the DPLL.
9661 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009662 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009663 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009664 x, y, fb);
9665 if (ret)
9666 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009667 }
9668
9669 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009670 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9671 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009672
Daniel Vetter25c5b262012-07-08 22:08:04 +02009673 if (modeset_pipes) {
9674 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009675 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009676
Daniel Vetter25c5b262012-07-08 22:08:04 +02009677 /* Calculate and store various constants which
9678 * are later needed by vblank and swap-completion
9679 * timestamping. They are derived from true hwmode.
9680 */
9681 drm_calc_timestamping_constants(crtc);
9682 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009683
9684 /* FIXME: add subpixel order */
9685done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009686 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009687 crtc->hwmode = *saved_hwmode;
9688 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009689 }
9690
Tim Gardner3ac18232012-12-07 07:54:26 -07009691out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009692 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009693 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009694 return ret;
9695}
9696
Damien Lespiaue7457a92013-08-08 22:28:59 +01009697static int intel_set_mode(struct drm_crtc *crtc,
9698 struct drm_display_mode *mode,
9699 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009700{
9701 int ret;
9702
9703 ret = __intel_set_mode(crtc, mode, x, y, fb);
9704
9705 if (ret == 0)
9706 intel_modeset_check_state(crtc->dev);
9707
9708 return ret;
9709}
9710
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009711void intel_crtc_restore_mode(struct drm_crtc *crtc)
9712{
9713 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9714}
9715
Daniel Vetter25c5b262012-07-08 22:08:04 +02009716#undef for_each_intel_crtc_masked
9717
Daniel Vetterd9e55602012-07-04 22:16:09 +02009718static void intel_set_config_free(struct intel_set_config *config)
9719{
9720 if (!config)
9721 return;
9722
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009723 kfree(config->save_connector_encoders);
9724 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009725 kfree(config);
9726}
9727
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009728static int intel_set_config_save_state(struct drm_device *dev,
9729 struct intel_set_config *config)
9730{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009731 struct drm_encoder *encoder;
9732 struct drm_connector *connector;
9733 int count;
9734
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009735 config->save_encoder_crtcs =
9736 kcalloc(dev->mode_config.num_encoder,
9737 sizeof(struct drm_crtc *), GFP_KERNEL);
9738 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009739 return -ENOMEM;
9740
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009741 config->save_connector_encoders =
9742 kcalloc(dev->mode_config.num_connector,
9743 sizeof(struct drm_encoder *), GFP_KERNEL);
9744 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009745 return -ENOMEM;
9746
9747 /* Copy data. Note that driver private data is not affected.
9748 * Should anything bad happen only the expected state is
9749 * restored, not the drivers personal bookkeeping.
9750 */
9751 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009752 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009753 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009754 }
9755
9756 count = 0;
9757 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009758 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009759 }
9760
9761 return 0;
9762}
9763
9764static void intel_set_config_restore_state(struct drm_device *dev,
9765 struct intel_set_config *config)
9766{
Daniel Vetter9a935852012-07-05 22:34:27 +02009767 struct intel_encoder *encoder;
9768 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009769 int count;
9770
9771 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009772 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9773 encoder->new_crtc =
9774 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009775 }
9776
9777 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009778 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9779 connector->new_encoder =
9780 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009781 }
9782}
9783
Imre Deake3de42b2013-05-03 19:44:07 +02009784static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009785is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009786{
9787 int i;
9788
Chris Wilson2e57f472013-07-17 12:14:40 +01009789 if (set->num_connectors == 0)
9790 return false;
9791
9792 if (WARN_ON(set->connectors == NULL))
9793 return false;
9794
9795 for (i = 0; i < set->num_connectors; i++)
9796 if (set->connectors[i]->encoder &&
9797 set->connectors[i]->encoder->crtc == set->crtc &&
9798 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009799 return true;
9800
9801 return false;
9802}
9803
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009804static void
9805intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9806 struct intel_set_config *config)
9807{
9808
9809 /* We should be able to check here if the fb has the same properties
9810 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009811 if (is_crtc_connector_off(set)) {
9812 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009813 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009814 /* If we have no fb then treat it as a full mode set */
9815 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009816 struct intel_crtc *intel_crtc =
9817 to_intel_crtc(set->crtc);
9818
9819 if (intel_crtc->active && i915_fastboot) {
9820 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9821 config->fb_changed = true;
9822 } else {
9823 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9824 config->mode_changed = true;
9825 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009826 } else if (set->fb == NULL) {
9827 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009828 } else if (set->fb->pixel_format !=
9829 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009830 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009831 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009832 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009833 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009834 }
9835
Daniel Vetter835c5872012-07-10 18:11:08 +02009836 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009837 config->fb_changed = true;
9838
9839 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9840 DRM_DEBUG_KMS("modes are different, full mode set\n");
9841 drm_mode_debug_printmodeline(&set->crtc->mode);
9842 drm_mode_debug_printmodeline(set->mode);
9843 config->mode_changed = true;
9844 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009845
9846 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9847 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009848}
9849
Daniel Vetter2e431052012-07-04 22:42:15 +02009850static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009851intel_modeset_stage_output_state(struct drm_device *dev,
9852 struct drm_mode_set *set,
9853 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009854{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009855 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009856 struct intel_connector *connector;
9857 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009858 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009859
Damien Lespiau9abdda72013-02-13 13:29:23 +00009860 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009861 * of connectors. For paranoia, double-check this. */
9862 WARN_ON(!set->fb && (set->num_connectors != 0));
9863 WARN_ON(set->fb && (set->num_connectors == 0));
9864
Daniel Vetter9a935852012-07-05 22:34:27 +02009865 list_for_each_entry(connector, &dev->mode_config.connector_list,
9866 base.head) {
9867 /* Otherwise traverse passed in connector list and get encoders
9868 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009869 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009870 if (set->connectors[ro] == &connector->base) {
9871 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009872 break;
9873 }
9874 }
9875
Daniel Vetter9a935852012-07-05 22:34:27 +02009876 /* If we disable the crtc, disable all its connectors. Also, if
9877 * the connector is on the changing crtc but not on the new
9878 * connector list, disable it. */
9879 if ((!set->fb || ro == set->num_connectors) &&
9880 connector->base.encoder &&
9881 connector->base.encoder->crtc == set->crtc) {
9882 connector->new_encoder = NULL;
9883
9884 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9885 connector->base.base.id,
9886 drm_get_connector_name(&connector->base));
9887 }
9888
9889
9890 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009891 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009892 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009893 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009894 }
9895 /* connector->new_encoder is now updated for all connectors. */
9896
9897 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009898 list_for_each_entry(connector, &dev->mode_config.connector_list,
9899 base.head) {
9900 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009901 continue;
9902
Daniel Vetter9a935852012-07-05 22:34:27 +02009903 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009904
9905 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009906 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009907 new_crtc = set->crtc;
9908 }
9909
9910 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009911 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9912 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009913 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009914 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009915 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9916
9917 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9918 connector->base.base.id,
9919 drm_get_connector_name(&connector->base),
9920 new_crtc->base.id);
9921 }
9922
9923 /* Check for any encoders that needs to be disabled. */
9924 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9925 base.head) {
9926 list_for_each_entry(connector,
9927 &dev->mode_config.connector_list,
9928 base.head) {
9929 if (connector->new_encoder == encoder) {
9930 WARN_ON(!connector->new_encoder->new_crtc);
9931
9932 goto next_encoder;
9933 }
9934 }
9935 encoder->new_crtc = NULL;
9936next_encoder:
9937 /* Only now check for crtc changes so we don't miss encoders
9938 * that will be disabled. */
9939 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009940 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009941 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009942 }
9943 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009944 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009945
Daniel Vetter2e431052012-07-04 22:42:15 +02009946 return 0;
9947}
9948
9949static int intel_crtc_set_config(struct drm_mode_set *set)
9950{
9951 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009952 struct drm_mode_set save_set;
9953 struct intel_set_config *config;
9954 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009955
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009956 BUG_ON(!set);
9957 BUG_ON(!set->crtc);
9958 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009959
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009960 /* Enforce sane interface api - has been abused by the fb helper. */
9961 BUG_ON(!set->mode && set->fb);
9962 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009963
Daniel Vetter2e431052012-07-04 22:42:15 +02009964 if (set->fb) {
9965 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9966 set->crtc->base.id, set->fb->base.id,
9967 (int)set->num_connectors, set->x, set->y);
9968 } else {
9969 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009970 }
9971
9972 dev = set->crtc->dev;
9973
9974 ret = -ENOMEM;
9975 config = kzalloc(sizeof(*config), GFP_KERNEL);
9976 if (!config)
9977 goto out_config;
9978
9979 ret = intel_set_config_save_state(dev, config);
9980 if (ret)
9981 goto out_config;
9982
9983 save_set.crtc = set->crtc;
9984 save_set.mode = &set->crtc->mode;
9985 save_set.x = set->crtc->x;
9986 save_set.y = set->crtc->y;
9987 save_set.fb = set->crtc->fb;
9988
9989 /* Compute whether we need a full modeset, only an fb base update or no
9990 * change at all. In the future we might also check whether only the
9991 * mode changed, e.g. for LVDS where we only change the panel fitter in
9992 * such cases. */
9993 intel_set_config_compute_mode_changes(set, config);
9994
Daniel Vetter9a935852012-07-05 22:34:27 +02009995 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009996 if (ret)
9997 goto fail;
9998
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009999 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010000 ret = intel_set_mode(set->crtc, set->mode,
10001 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010002 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010003 intel_crtc_wait_for_pending_flips(set->crtc);
10004
Daniel Vetter4f660f42012-07-02 09:47:37 +020010005 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010006 set->x, set->y, set->fb);
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010007 /*
10008 * In the fastboot case this may be our only check of the
10009 * state after boot. It would be better to only do it on
10010 * the first update, but we don't have a nice way of doing that
10011 * (and really, set_config isn't used much for high freq page
10012 * flipping, so increasing its cost here shouldn't be a big
10013 * deal).
10014 */
10015 if (i915_fastboot && ret == 0)
10016 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010017 }
10018
Chris Wilson2d05eae2013-05-03 17:36:25 +010010019 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010020 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10021 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010022fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010023 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010024
Chris Wilson2d05eae2013-05-03 17:36:25 +010010025 /* Try to restore the config */
10026 if (config->mode_changed &&
10027 intel_set_mode(save_set.crtc, save_set.mode,
10028 save_set.x, save_set.y, save_set.fb))
10029 DRM_ERROR("failed to restore config after modeset failure\n");
10030 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010031
Daniel Vetterd9e55602012-07-04 22:16:09 +020010032out_config:
10033 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010034 return ret;
10035}
10036
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010037static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010038 .cursor_set = intel_crtc_cursor_set,
10039 .cursor_move = intel_crtc_cursor_move,
10040 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010041 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010042 .destroy = intel_crtc_destroy,
10043 .page_flip = intel_crtc_page_flip,
10044};
10045
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010046static void intel_cpu_pll_init(struct drm_device *dev)
10047{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010048 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010049 intel_ddi_pll_init(dev);
10050}
10051
Daniel Vetter53589012013-06-05 13:34:16 +020010052static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10053 struct intel_shared_dpll *pll,
10054 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010055{
Daniel Vetter53589012013-06-05 13:34:16 +020010056 uint32_t val;
10057
10058 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010059 hw_state->dpll = val;
10060 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10061 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010062
10063 return val & DPLL_VCO_ENABLE;
10064}
10065
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010066static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10067 struct intel_shared_dpll *pll)
10068{
10069 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10070 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10071}
10072
Daniel Vettere7b903d2013-06-05 13:34:14 +020010073static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10074 struct intel_shared_dpll *pll)
10075{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010076 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020010077 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020010078
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010079 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10080
10081 /* Wait for the clocks to stabilize. */
10082 POSTING_READ(PCH_DPLL(pll->id));
10083 udelay(150);
10084
10085 /* The pixel multiplier can only be updated once the
10086 * DPLL is enabled and the clocks are stable.
10087 *
10088 * So write it again.
10089 */
10090 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10091 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010092 udelay(200);
10093}
10094
10095static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10096 struct intel_shared_dpll *pll)
10097{
10098 struct drm_device *dev = dev_priv->dev;
10099 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010100
10101 /* Make sure no transcoder isn't still depending on us. */
10102 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10103 if (intel_crtc_to_shared_dpll(crtc) == pll)
10104 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10105 }
10106
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010107 I915_WRITE(PCH_DPLL(pll->id), 0);
10108 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010109 udelay(200);
10110}
10111
Daniel Vetter46edb022013-06-05 13:34:12 +020010112static char *ibx_pch_dpll_names[] = {
10113 "PCH DPLL A",
10114 "PCH DPLL B",
10115};
10116
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010117static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010118{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010120 int i;
10121
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010122 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010123
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010124 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010125 dev_priv->shared_dplls[i].id = i;
10126 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010127 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010128 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10129 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010130 dev_priv->shared_dplls[i].get_hw_state =
10131 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010132 }
10133}
10134
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010135static void intel_shared_dpll_init(struct drm_device *dev)
10136{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010137 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010138
10139 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10140 ibx_pch_dpll_init(dev);
10141 else
10142 dev_priv->num_shared_dpll = 0;
10143
10144 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010145}
10146
Hannes Ederb358d0a2008-12-18 21:18:47 +010010147static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010148{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010149 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010150 struct intel_crtc *intel_crtc;
10151 int i;
10152
Daniel Vetter955382f2013-09-19 14:05:45 +020010153 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010154 if (intel_crtc == NULL)
10155 return;
10156
10157 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10158
10159 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010160 for (i = 0; i < 256; i++) {
10161 intel_crtc->lut_r[i] = i;
10162 intel_crtc->lut_g[i] = i;
10163 intel_crtc->lut_b[i] = i;
10164 }
10165
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010166 /*
10167 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10168 * is hooked to plane B. Hence we want plane A feeding pipe B.
10169 */
Jesse Barnes80824002009-09-10 15:28:06 -070010170 intel_crtc->pipe = pipe;
10171 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010010172 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010173 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010174 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010175 }
10176
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010177 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10178 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10179 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10180 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10181
Jesse Barnes79e53942008-11-07 14:24:08 -080010182 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010183}
10184
Jesse Barnes752aa882013-10-31 18:55:49 +020010185enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10186{
10187 struct drm_encoder *encoder = connector->base.encoder;
10188
10189 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10190
10191 if (!encoder)
10192 return INVALID_PIPE;
10193
10194 return to_intel_crtc(encoder->crtc)->pipe;
10195}
10196
Carl Worth08d7b3d2009-04-29 14:43:54 -070010197int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010198 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010199{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010200 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010201 struct drm_mode_object *drmmode_obj;
10202 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010203
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010204 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10205 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010206
Daniel Vetterc05422d2009-08-11 16:05:30 +020010207 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10208 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010209
Daniel Vetterc05422d2009-08-11 16:05:30 +020010210 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010211 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010212 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010213 }
10214
Daniel Vetterc05422d2009-08-11 16:05:30 +020010215 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10216 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010217
Daniel Vetterc05422d2009-08-11 16:05:30 +020010218 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010219}
10220
Daniel Vetter66a92782012-07-12 20:08:18 +020010221static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010222{
Daniel Vetter66a92782012-07-12 20:08:18 +020010223 struct drm_device *dev = encoder->base.dev;
10224 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010225 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010226 int entry = 0;
10227
Daniel Vetter66a92782012-07-12 20:08:18 +020010228 list_for_each_entry(source_encoder,
10229 &dev->mode_config.encoder_list, base.head) {
10230
10231 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010232 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010233
10234 /* Intel hw has only one MUX where enocoders could be cloned. */
10235 if (encoder->cloneable && source_encoder->cloneable)
10236 index_mask |= (1 << entry);
10237
Jesse Barnes79e53942008-11-07 14:24:08 -080010238 entry++;
10239 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010240
Jesse Barnes79e53942008-11-07 14:24:08 -080010241 return index_mask;
10242}
10243
Chris Wilson4d302442010-12-14 19:21:29 +000010244static bool has_edp_a(struct drm_device *dev)
10245{
10246 struct drm_i915_private *dev_priv = dev->dev_private;
10247
10248 if (!IS_MOBILE(dev))
10249 return false;
10250
10251 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10252 return false;
10253
10254 if (IS_GEN5(dev) &&
10255 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10256 return false;
10257
10258 return true;
10259}
10260
Jesse Barnes79e53942008-11-07 14:24:08 -080010261static void intel_setup_outputs(struct drm_device *dev)
10262{
Eric Anholt725e30a2009-01-22 13:01:02 -080010263 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010264 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010265 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010266
Daniel Vetterc9093352013-06-06 22:22:47 +020010267 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010268
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010269 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010270 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010271
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010272 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010273 int found;
10274
10275 /* Haswell uses DDI functions to detect digital outputs */
10276 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10277 /* DDI A only supports eDP */
10278 if (found)
10279 intel_ddi_init(dev, PORT_A);
10280
10281 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10282 * register */
10283 found = I915_READ(SFUSE_STRAP);
10284
10285 if (found & SFUSE_STRAP_DDIB_DETECTED)
10286 intel_ddi_init(dev, PORT_B);
10287 if (found & SFUSE_STRAP_DDIC_DETECTED)
10288 intel_ddi_init(dev, PORT_C);
10289 if (found & SFUSE_STRAP_DDID_DETECTED)
10290 intel_ddi_init(dev, PORT_D);
10291 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010292 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +020010293 dpd_is_edp = intel_dpd_is_edp(dev);
10294
10295 if (has_edp_a(dev))
10296 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010297
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010298 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010299 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010300 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010301 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010302 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010303 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010304 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010305 }
10306
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010307 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010308 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010309
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010310 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010311 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010312
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010313 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010314 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010315
Daniel Vetter270b3042012-10-27 15:52:05 +020010316 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010317 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010318 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010319 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10320 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10321 PORT_B);
10322 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10323 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10324 }
10325
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010326 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10327 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10328 PORT_C);
10329 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10330 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10331 PORT_C);
10332 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010333
Jani Nikula3cfca972013-08-27 15:12:26 +030010334 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010335 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010336 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010337
Paulo Zanonie2debe92013-02-18 19:00:27 -030010338 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010339 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010340 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010341 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10342 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010343 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010344 }
Ma Ling27185ae2009-08-24 13:50:23 +080010345
Imre Deake7281ea2013-05-08 13:14:08 +030010346 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010347 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010348 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010349
10350 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010351
Paulo Zanonie2debe92013-02-18 19:00:27 -030010352 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010353 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010354 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010355 }
Ma Ling27185ae2009-08-24 13:50:23 +080010356
Paulo Zanonie2debe92013-02-18 19:00:27 -030010357 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010358
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010359 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10360 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010361 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010362 }
Imre Deake7281ea2013-05-08 13:14:08 +030010363 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010364 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010365 }
Ma Ling27185ae2009-08-24 13:50:23 +080010366
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010367 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010368 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010369 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010370 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010371 intel_dvo_init(dev);
10372
Zhenyu Wang103a1962009-11-27 11:44:36 +080010373 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010374 intel_tv_init(dev);
10375
Chris Wilson4ef69c72010-09-09 15:14:28 +010010376 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10377 encoder->base.possible_crtcs = encoder->crtc_mask;
10378 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010379 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010380 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010381
Paulo Zanonidde86e22012-12-01 12:04:25 -020010382 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010383
10384 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010385}
10386
Chris Wilsonddfe1562013-08-06 17:43:07 +010010387void intel_framebuffer_fini(struct intel_framebuffer *fb)
10388{
10389 drm_framebuffer_cleanup(&fb->base);
Daniel Vetter80075d42013-10-09 21:23:52 +020010390 WARN_ON(!fb->obj->framebuffer_references--);
Chris Wilsonddfe1562013-08-06 17:43:07 +010010391 drm_gem_object_unreference_unlocked(&fb->obj->base);
10392}
10393
Jesse Barnes79e53942008-11-07 14:24:08 -080010394static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10395{
10396 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010397
Chris Wilsonddfe1562013-08-06 17:43:07 +010010398 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010399 kfree(intel_fb);
10400}
10401
10402static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010403 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 unsigned int *handle)
10405{
10406 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010407 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010408
Chris Wilson05394f32010-11-08 19:18:58 +000010409 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010410}
10411
10412static const struct drm_framebuffer_funcs intel_fb_funcs = {
10413 .destroy = intel_user_framebuffer_destroy,
10414 .create_handle = intel_user_framebuffer_create_handle,
10415};
10416
Dave Airlie38651672010-03-30 05:34:13 +000010417int intel_framebuffer_init(struct drm_device *dev,
10418 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010419 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010420 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010421{
Daniel Vetter53155c02013-10-09 21:55:33 +020010422 int aligned_height, tile_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010423 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010424 int ret;
10425
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010426 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10427
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010428 if (obj->tiling_mode == I915_TILING_Y) {
10429 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010430 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010431 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010432
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010433 if (mode_cmd->pitches[0] & 63) {
10434 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10435 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010436 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010437 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010438
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010439 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10440 pitch_limit = 32*1024;
10441 } else if (INTEL_INFO(dev)->gen >= 4) {
10442 if (obj->tiling_mode)
10443 pitch_limit = 16*1024;
10444 else
10445 pitch_limit = 32*1024;
10446 } else if (INTEL_INFO(dev)->gen >= 3) {
10447 if (obj->tiling_mode)
10448 pitch_limit = 8*1024;
10449 else
10450 pitch_limit = 16*1024;
10451 } else
10452 /* XXX DSPC is limited to 4k tiled */
10453 pitch_limit = 8*1024;
10454
10455 if (mode_cmd->pitches[0] > pitch_limit) {
10456 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10457 obj->tiling_mode ? "tiled" : "linear",
10458 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010459 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010460 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010461
10462 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010463 mode_cmd->pitches[0] != obj->stride) {
10464 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10465 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010466 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010467 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010468
Ville Syrjälä57779d02012-10-31 17:50:14 +020010469 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010470 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010471 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010472 case DRM_FORMAT_RGB565:
10473 case DRM_FORMAT_XRGB8888:
10474 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010475 break;
10476 case DRM_FORMAT_XRGB1555:
10477 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010478 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010479 DRM_DEBUG("unsupported pixel format: %s\n",
10480 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010481 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010482 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010483 break;
10484 case DRM_FORMAT_XBGR8888:
10485 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010486 case DRM_FORMAT_XRGB2101010:
10487 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010488 case DRM_FORMAT_XBGR2101010:
10489 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010490 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010491 DRM_DEBUG("unsupported pixel format: %s\n",
10492 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010493 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010494 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010495 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010496 case DRM_FORMAT_YUYV:
10497 case DRM_FORMAT_UYVY:
10498 case DRM_FORMAT_YVYU:
10499 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010500 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010501 DRM_DEBUG("unsupported pixel format: %s\n",
10502 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010503 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010504 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010505 break;
10506 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010507 DRM_DEBUG("unsupported pixel format: %s\n",
10508 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010509 return -EINVAL;
10510 }
10511
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010512 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10513 if (mode_cmd->offsets[0] != 0)
10514 return -EINVAL;
10515
Daniel Vetter53155c02013-10-09 21:55:33 +020010516 tile_height = IS_GEN2(dev) ? 16 : 8;
10517 aligned_height = ALIGN(mode_cmd->height,
10518 obj->tiling_mode ? tile_height : 1);
10519 /* FIXME drm helper for size checks (especially planar formats)? */
10520 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10521 return -EINVAL;
10522
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010523 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10524 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010525 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010526
Jesse Barnes79e53942008-11-07 14:24:08 -080010527 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10528 if (ret) {
10529 DRM_ERROR("framebuffer init failed %d\n", ret);
10530 return ret;
10531 }
10532
Jesse Barnes79e53942008-11-07 14:24:08 -080010533 return 0;
10534}
10535
Jesse Barnes79e53942008-11-07 14:24:08 -080010536static struct drm_framebuffer *
10537intel_user_framebuffer_create(struct drm_device *dev,
10538 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010539 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010540{
Chris Wilson05394f32010-11-08 19:18:58 +000010541 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010542
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010543 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10544 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010545 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010546 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010547
Chris Wilsond2dff872011-04-19 08:36:26 +010010548 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010549}
10550
Daniel Vetter4520f532013-10-09 09:18:51 +020010551#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010552static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010553{
10554}
10555#endif
10556
Jesse Barnes79e53942008-11-07 14:24:08 -080010557static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010558 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010559 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010560};
10561
Jesse Barnese70236a2009-09-21 10:42:27 -070010562/* Set up chip specific display functions */
10563static void intel_init_display(struct drm_device *dev)
10564{
10565 struct drm_i915_private *dev_priv = dev->dev_private;
10566
Daniel Vetteree9300b2013-06-03 22:40:22 +020010567 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10568 dev_priv->display.find_dpll = g4x_find_best_dpll;
10569 else if (IS_VALLEYVIEW(dev))
10570 dev_priv->display.find_dpll = vlv_find_best_dpll;
10571 else if (IS_PINEVIEW(dev))
10572 dev_priv->display.find_dpll = pnv_find_best_dpll;
10573 else
10574 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10575
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010576 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010577 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010578 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010579 dev_priv->display.crtc_enable = haswell_crtc_enable;
10580 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010581 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010582 dev_priv->display.update_plane = ironlake_update_plane;
10583 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010584 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010585 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010586 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10587 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010588 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010589 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010590 } else if (IS_VALLEYVIEW(dev)) {
10591 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10592 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10593 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10594 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10595 dev_priv->display.off = i9xx_crtc_off;
10596 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010597 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010598 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010599 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010600 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10601 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010602 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010603 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010604 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010605
Jesse Barnese70236a2009-09-21 10:42:27 -070010606 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010607 if (IS_VALLEYVIEW(dev))
10608 dev_priv->display.get_display_clock_speed =
10609 valleyview_get_display_clock_speed;
10610 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010611 dev_priv->display.get_display_clock_speed =
10612 i945_get_display_clock_speed;
10613 else if (IS_I915G(dev))
10614 dev_priv->display.get_display_clock_speed =
10615 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010616 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010617 dev_priv->display.get_display_clock_speed =
10618 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010619 else if (IS_PINEVIEW(dev))
10620 dev_priv->display.get_display_clock_speed =
10621 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010622 else if (IS_I915GM(dev))
10623 dev_priv->display.get_display_clock_speed =
10624 i915gm_get_display_clock_speed;
10625 else if (IS_I865G(dev))
10626 dev_priv->display.get_display_clock_speed =
10627 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010628 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010629 dev_priv->display.get_display_clock_speed =
10630 i855_get_display_clock_speed;
10631 else /* 852, 830 */
10632 dev_priv->display.get_display_clock_speed =
10633 i830_get_display_clock_speed;
10634
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010635 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010636 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010637 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010638 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010639 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010640 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010641 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010642 } else if (IS_IVYBRIDGE(dev)) {
10643 /* FIXME: detect B0+ stepping and use auto training */
10644 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010645 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010646 dev_priv->display.modeset_global_resources =
10647 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010648 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010649 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010650 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010651 dev_priv->display.modeset_global_resources =
10652 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010653 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010654 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010655 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010656 } else if (IS_VALLEYVIEW(dev)) {
10657 dev_priv->display.modeset_global_resources =
10658 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040010659 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070010660 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010661
10662 /* Default just returns -ENODEV to indicate unsupported */
10663 dev_priv->display.queue_flip = intel_default_queue_flip;
10664
10665 switch (INTEL_INFO(dev)->gen) {
10666 case 2:
10667 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10668 break;
10669
10670 case 3:
10671 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10672 break;
10673
10674 case 4:
10675 case 5:
10676 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10677 break;
10678
10679 case 6:
10680 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10681 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010682 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070010683 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010684 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10685 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010686 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020010687
10688 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010689}
10690
Jesse Barnesb690e962010-07-19 13:53:12 -070010691/*
10692 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10693 * resume, or other times. This quirk makes sure that's the case for
10694 * affected systems.
10695 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010696static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010697{
10698 struct drm_i915_private *dev_priv = dev->dev_private;
10699
10700 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010701 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010702}
10703
Keith Packard435793d2011-07-12 14:56:22 -070010704/*
10705 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10706 */
10707static void quirk_ssc_force_disable(struct drm_device *dev)
10708{
10709 struct drm_i915_private *dev_priv = dev->dev_private;
10710 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010711 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010712}
10713
Carsten Emde4dca20e2012-03-15 15:56:26 +010010714/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010715 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10716 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010717 */
10718static void quirk_invert_brightness(struct drm_device *dev)
10719{
10720 struct drm_i915_private *dev_priv = dev->dev_private;
10721 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010722 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010723}
10724
10725struct intel_quirk {
10726 int device;
10727 int subsystem_vendor;
10728 int subsystem_device;
10729 void (*hook)(struct drm_device *dev);
10730};
10731
Egbert Eich5f85f1762012-10-14 15:46:38 +020010732/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10733struct intel_dmi_quirk {
10734 void (*hook)(struct drm_device *dev);
10735 const struct dmi_system_id (*dmi_id_list)[];
10736};
10737
10738static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10739{
10740 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10741 return 1;
10742}
10743
10744static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10745 {
10746 .dmi_id_list = &(const struct dmi_system_id[]) {
10747 {
10748 .callback = intel_dmi_reverse_brightness,
10749 .ident = "NCR Corporation",
10750 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10751 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10752 },
10753 },
10754 { } /* terminating entry */
10755 },
10756 .hook = quirk_invert_brightness,
10757 },
10758};
10759
Ben Widawskyc43b5632012-04-16 14:07:40 -070010760static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010761 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010762 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010763
Jesse Barnesb690e962010-07-19 13:53:12 -070010764 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10765 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10766
Jesse Barnesb690e962010-07-19 13:53:12 -070010767 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10768 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10769
Chris Wilsona4945f92013-10-08 11:16:59 +010010770 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010771 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010772
10773 /* Lenovo U160 cannot use SSC on LVDS */
10774 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010775
10776 /* Sony Vaio Y cannot use SSC on LVDS */
10777 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010778
Jani Nikulaee1452d2013-09-20 15:05:30 +030010779 /*
10780 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10781 * seem to use inverted backlight PWM.
10782 */
10783 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070010784};
10785
10786static void intel_init_quirks(struct drm_device *dev)
10787{
10788 struct pci_dev *d = dev->pdev;
10789 int i;
10790
10791 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10792 struct intel_quirk *q = &intel_quirks[i];
10793
10794 if (d->device == q->device &&
10795 (d->subsystem_vendor == q->subsystem_vendor ||
10796 q->subsystem_vendor == PCI_ANY_ID) &&
10797 (d->subsystem_device == q->subsystem_device ||
10798 q->subsystem_device == PCI_ANY_ID))
10799 q->hook(dev);
10800 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010801 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10802 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10803 intel_dmi_quirks[i].hook(dev);
10804 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010805}
10806
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010807/* Disable the VGA plane that we never use */
10808static void i915_disable_vga(struct drm_device *dev)
10809{
10810 struct drm_i915_private *dev_priv = dev->dev_private;
10811 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010812 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010813
10814 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010815 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010816 sr1 = inb(VGA_SR_DATA);
10817 outb(sr1 | 1<<5, VGA_SR_DATA);
10818 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10819 udelay(300);
10820
10821 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10822 POSTING_READ(vga_reg);
10823}
10824
Daniel Vetterf8175862012-04-10 15:50:11 +020010825void intel_modeset_init_hw(struct drm_device *dev)
10826{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010827 intel_prepare_ddi(dev);
10828
Daniel Vetterf8175862012-04-10 15:50:11 +020010829 intel_init_clock_gating(dev);
10830
Jesse Barnes5382f5f352013-12-16 16:34:24 -080010831 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010832
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010833 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010834 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010835 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010836}
10837
Imre Deak7d708ee2013-04-17 14:04:50 +030010838void intel_modeset_suspend_hw(struct drm_device *dev)
10839{
10840 intel_suspend_hw(dev);
10841}
10842
Jesse Barnes79e53942008-11-07 14:24:08 -080010843void intel_modeset_init(struct drm_device *dev)
10844{
Jesse Barnes652c3932009-08-17 13:31:43 -070010845 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010846 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010847
10848 drm_mode_config_init(dev);
10849
10850 dev->mode_config.min_width = 0;
10851 dev->mode_config.min_height = 0;
10852
Dave Airlie019d96c2011-09-29 16:20:42 +010010853 dev->mode_config.preferred_depth = 24;
10854 dev->mode_config.prefer_shadow = 1;
10855
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010856 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010857
Jesse Barnesb690e962010-07-19 13:53:12 -070010858 intel_init_quirks(dev);
10859
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010860 intel_init_pm(dev);
10861
Ben Widawskye3c74752013-04-05 13:12:39 -070010862 if (INTEL_INFO(dev)->num_pipes == 0)
10863 return;
10864
Jesse Barnese70236a2009-09-21 10:42:27 -070010865 intel_init_display(dev);
10866
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010867 if (IS_GEN2(dev)) {
10868 dev->mode_config.max_width = 2048;
10869 dev->mode_config.max_height = 2048;
10870 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010871 dev->mode_config.max_width = 4096;
10872 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010873 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010874 dev->mode_config.max_width = 8192;
10875 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010876 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010877 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010878
Zhao Yakui28c97732009-10-09 11:39:41 +080010879 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010880 INTEL_INFO(dev)->num_pipes,
10881 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010882
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010883 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010884 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010885 for (j = 0; j < dev_priv->num_plane; j++) {
10886 ret = intel_plane_init(dev, i, j);
10887 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010888 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10889 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010890 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010891 }
10892
Jesse Barnesf42bb702013-12-16 16:34:23 -080010893 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080010894 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080010895
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010896 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010897 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010898
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010899 /* Just disable it once at startup */
10900 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010901 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010902
10903 /* Just in case the BIOS is doing something questionable. */
10904 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010905}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010906
Daniel Vetter24929352012-07-02 20:28:59 +020010907static void
10908intel_connector_break_all_links(struct intel_connector *connector)
10909{
10910 connector->base.dpms = DRM_MODE_DPMS_OFF;
10911 connector->base.encoder = NULL;
10912 connector->encoder->connectors_active = false;
10913 connector->encoder->base.crtc = NULL;
10914}
10915
Daniel Vetter7fad7982012-07-04 17:51:47 +020010916static void intel_enable_pipe_a(struct drm_device *dev)
10917{
10918 struct intel_connector *connector;
10919 struct drm_connector *crt = NULL;
10920 struct intel_load_detect_pipe load_detect_temp;
10921
10922 /* We can't just switch on the pipe A, we need to set things up with a
10923 * proper mode and output configuration. As a gross hack, enable pipe A
10924 * by enabling the load detect pipe once. */
10925 list_for_each_entry(connector,
10926 &dev->mode_config.connector_list,
10927 base.head) {
10928 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10929 crt = &connector->base;
10930 break;
10931 }
10932 }
10933
10934 if (!crt)
10935 return;
10936
10937 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10938 intel_release_load_detect_pipe(crt, &load_detect_temp);
10939
10940
10941}
10942
Daniel Vetterfa555832012-10-10 23:14:00 +020010943static bool
10944intel_check_plane_mapping(struct intel_crtc *crtc)
10945{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010946 struct drm_device *dev = crtc->base.dev;
10947 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010948 u32 reg, val;
10949
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010950 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010951 return true;
10952
10953 reg = DSPCNTR(!crtc->plane);
10954 val = I915_READ(reg);
10955
10956 if ((val & DISPLAY_PLANE_ENABLE) &&
10957 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10958 return false;
10959
10960 return true;
10961}
10962
Daniel Vetter24929352012-07-02 20:28:59 +020010963static void intel_sanitize_crtc(struct intel_crtc *crtc)
10964{
10965 struct drm_device *dev = crtc->base.dev;
10966 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010967 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010968
Daniel Vetter24929352012-07-02 20:28:59 +020010969 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010970 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010971 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10972
10973 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010974 * disable the crtc (and hence change the state) if it is wrong. Note
10975 * that gen4+ has a fixed plane -> pipe mapping. */
10976 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010977 struct intel_connector *connector;
10978 bool plane;
10979
Daniel Vetter24929352012-07-02 20:28:59 +020010980 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10981 crtc->base.base.id);
10982
10983 /* Pipe has the wrong plane attached and the plane is active.
10984 * Temporarily change the plane mapping and disable everything
10985 * ... */
10986 plane = crtc->plane;
10987 crtc->plane = !plane;
10988 dev_priv->display.crtc_disable(&crtc->base);
10989 crtc->plane = plane;
10990
10991 /* ... and break all links. */
10992 list_for_each_entry(connector, &dev->mode_config.connector_list,
10993 base.head) {
10994 if (connector->encoder->base.crtc != &crtc->base)
10995 continue;
10996
10997 intel_connector_break_all_links(connector);
10998 }
10999
11000 WARN_ON(crtc->active);
11001 crtc->base.enabled = false;
11002 }
Daniel Vetter24929352012-07-02 20:28:59 +020011003
Daniel Vetter7fad7982012-07-04 17:51:47 +020011004 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11005 crtc->pipe == PIPE_A && !crtc->active) {
11006 /* BIOS forgot to enable pipe A, this mostly happens after
11007 * resume. Force-enable the pipe to fix this, the update_dpms
11008 * call below we restore the pipe to the right state, but leave
11009 * the required bits on. */
11010 intel_enable_pipe_a(dev);
11011 }
11012
Daniel Vetter24929352012-07-02 20:28:59 +020011013 /* Adjust the state of the output pipe according to whether we
11014 * have active connectors/encoders. */
11015 intel_crtc_update_dpms(&crtc->base);
11016
11017 if (crtc->active != crtc->base.enabled) {
11018 struct intel_encoder *encoder;
11019
11020 /* This can happen either due to bugs in the get_hw_state
11021 * functions or because the pipe is force-enabled due to the
11022 * pipe A quirk. */
11023 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11024 crtc->base.base.id,
11025 crtc->base.enabled ? "enabled" : "disabled",
11026 crtc->active ? "enabled" : "disabled");
11027
11028 crtc->base.enabled = crtc->active;
11029
11030 /* Because we only establish the connector -> encoder ->
11031 * crtc links if something is active, this means the
11032 * crtc is now deactivated. Break the links. connector
11033 * -> encoder links are only establish when things are
11034 * actually up, hence no need to break them. */
11035 WARN_ON(crtc->active);
11036
11037 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11038 WARN_ON(encoder->connectors_active);
11039 encoder->base.crtc = NULL;
11040 }
11041 }
11042}
11043
11044static void intel_sanitize_encoder(struct intel_encoder *encoder)
11045{
11046 struct intel_connector *connector;
11047 struct drm_device *dev = encoder->base.dev;
11048
11049 /* We need to check both for a crtc link (meaning that the
11050 * encoder is active and trying to read from a pipe) and the
11051 * pipe itself being active. */
11052 bool has_active_crtc = encoder->base.crtc &&
11053 to_intel_crtc(encoder->base.crtc)->active;
11054
11055 if (encoder->connectors_active && !has_active_crtc) {
11056 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11057 encoder->base.base.id,
11058 drm_get_encoder_name(&encoder->base));
11059
11060 /* Connector is active, but has no active pipe. This is
11061 * fallout from our resume register restoring. Disable
11062 * the encoder manually again. */
11063 if (encoder->base.crtc) {
11064 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11065 encoder->base.base.id,
11066 drm_get_encoder_name(&encoder->base));
11067 encoder->disable(encoder);
11068 }
11069
11070 /* Inconsistent output/port/pipe state happens presumably due to
11071 * a bug in one of the get_hw_state functions. Or someplace else
11072 * in our code, like the register restore mess on resume. Clamp
11073 * things to off as a safer default. */
11074 list_for_each_entry(connector,
11075 &dev->mode_config.connector_list,
11076 base.head) {
11077 if (connector->encoder != encoder)
11078 continue;
11079
11080 intel_connector_break_all_links(connector);
11081 }
11082 }
11083 /* Enabled encoders without active connectors will be fixed in
11084 * the crtc fixup. */
11085}
11086
Daniel Vetter44cec742013-01-25 17:53:21 +010011087void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011088{
11089 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011090 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011091
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011092 /* This function can be called both from intel_modeset_setup_hw_state or
11093 * at a very early point in our resume sequence, where the power well
11094 * structures are not yet restored. Since this function is at a very
11095 * paranoid "someone might have enabled VGA while we were not looking"
11096 * level, just check if the power well is enabled instead of trying to
11097 * follow the "don't touch the power well if we don't need it" policy
11098 * the rest of the driver uses. */
Jesse Barnesf9e711e2013-11-25 17:15:32 +020011099 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030011100 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011101 return;
11102
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030011103 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011104 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020011105 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011106 }
11107}
11108
Daniel Vetter30e984d2013-06-05 13:34:17 +020011109static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011110{
11111 struct drm_i915_private *dev_priv = dev->dev_private;
11112 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011113 struct intel_crtc *crtc;
11114 struct intel_encoder *encoder;
11115 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011116 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011117
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011118 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11119 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011120 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011121
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011122 crtc->active = dev_priv->display.get_pipe_config(crtc,
11123 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011124
11125 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030011126 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020011127
11128 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11129 crtc->base.base.id,
11130 crtc->active ? "enabled" : "disabled");
11131 }
11132
Daniel Vetter53589012013-06-05 13:34:16 +020011133 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011134 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011135 intel_ddi_setup_hw_pll_state(dev);
11136
Daniel Vetter53589012013-06-05 13:34:16 +020011137 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11138 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11139
11140 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11141 pll->active = 0;
11142 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11143 base.head) {
11144 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11145 pll->active++;
11146 }
11147 pll->refcount = pll->active;
11148
Daniel Vetter35c95372013-07-17 06:55:04 +020011149 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11150 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011151 }
11152
Daniel Vetter24929352012-07-02 20:28:59 +020011153 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11154 base.head) {
11155 pipe = 0;
11156
11157 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011158 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11159 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011160 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011161 } else {
11162 encoder->base.crtc = NULL;
11163 }
11164
11165 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011166 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011167 encoder->base.base.id,
11168 drm_get_encoder_name(&encoder->base),
11169 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011170 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011171 }
11172
11173 list_for_each_entry(connector, &dev->mode_config.connector_list,
11174 base.head) {
11175 if (connector->get_hw_state(connector)) {
11176 connector->base.dpms = DRM_MODE_DPMS_ON;
11177 connector->encoder->connectors_active = true;
11178 connector->base.encoder = &connector->encoder->base;
11179 } else {
11180 connector->base.dpms = DRM_MODE_DPMS_OFF;
11181 connector->base.encoder = NULL;
11182 }
11183 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11184 connector->base.base.id,
11185 drm_get_connector_name(&connector->base),
11186 connector->base.encoder ? "enabled" : "disabled");
11187 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011188}
11189
11190/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11191 * and i915 state tracking structures. */
11192void intel_modeset_setup_hw_state(struct drm_device *dev,
11193 bool force_restore)
11194{
11195 struct drm_i915_private *dev_priv = dev->dev_private;
11196 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011197 struct intel_crtc *crtc;
11198 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011199 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011200
11201 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011202
Jesse Barnesbabea612013-06-26 18:57:38 +030011203 /*
11204 * Now that we have the config, copy it to each CRTC struct
11205 * Note that this could go away if we move to using crtc_config
11206 * checking everywhere.
11207 */
11208 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11209 base.head) {
11210 if (crtc->active && i915_fastboot) {
11211 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11212
11213 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11214 crtc->base.base.id);
11215 drm_mode_debug_printmodeline(&crtc->base.mode);
11216 }
11217 }
11218
Daniel Vetter24929352012-07-02 20:28:59 +020011219 /* HW state is read out, now we need to sanitize this mess. */
11220 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11221 base.head) {
11222 intel_sanitize_encoder(encoder);
11223 }
11224
11225 for_each_pipe(pipe) {
11226 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11227 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011228 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011229 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011230
Daniel Vetter35c95372013-07-17 06:55:04 +020011231 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11232 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11233
11234 if (!pll->on || pll->active)
11235 continue;
11236
11237 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11238
11239 pll->disable(dev_priv, pll);
11240 pll->on = false;
11241 }
11242
Ville Syrjälä96f90c52013-12-05 15:51:38 +020011243 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011244 ilk_wm_get_hw_state(dev);
11245
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011246 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011247 i915_redisable_vga(dev);
11248
Daniel Vetterf30da182013-04-11 20:22:50 +020011249 /*
11250 * We need to use raw interfaces for restoring state to avoid
11251 * checking (bogus) intermediate states.
11252 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011253 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011254 struct drm_crtc *crtc =
11255 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011256
11257 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11258 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011259 }
11260 } else {
11261 intel_modeset_update_staged_output_state(dev);
11262 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011263
11264 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020011265
11266 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011267}
11268
11269void intel_modeset_gem_init(struct drm_device *dev)
11270{
Chris Wilson1833b132012-05-09 11:56:28 +010011271 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011272
11273 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011274
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011275 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080011276}
11277
11278void intel_modeset_cleanup(struct drm_device *dev)
11279{
Jesse Barnes652c3932009-08-17 13:31:43 -070011280 struct drm_i915_private *dev_priv = dev->dev_private;
11281 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011282 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011283
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011284 /*
11285 * Interrupts and polling as the first thing to avoid creating havoc.
11286 * Too much stuff here (turning of rps, connectors, ...) would
11287 * experience fancy races otherwise.
11288 */
11289 drm_irq_uninstall(dev);
11290 cancel_work_sync(&dev_priv->hotplug_work);
11291 /*
11292 * Due to the hpd irq storm handling the hotplug work can re-arm the
11293 * poll handlers. Hence disable polling after hpd handling is shut down.
11294 */
Keith Packardf87ea762010-10-03 19:36:26 -070011295 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011296
Jesse Barnes652c3932009-08-17 13:31:43 -070011297 mutex_lock(&dev->struct_mutex);
11298
Jesse Barnes723bfd72010-10-07 16:01:13 -070011299 intel_unregister_dsm_handler();
11300
Jesse Barnes652c3932009-08-17 13:31:43 -070011301 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11302 /* Skip inactive CRTCs */
11303 if (!crtc->fb)
11304 continue;
11305
Daniel Vetter3dec0092010-08-20 21:40:52 +020011306 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011307 }
11308
Chris Wilson973d04f2011-07-08 12:22:37 +010011309 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011310
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011311 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011312
Daniel Vetter930ebb42012-06-29 23:32:16 +020011313 ironlake_teardown_rc6(dev);
11314
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011315 mutex_unlock(&dev->struct_mutex);
11316
Chris Wilson1630fe72011-07-08 12:22:42 +010011317 /* flush any delayed tasks or pending work */
11318 flush_scheduled_work();
11319
Jani Nikuladb31af12013-11-08 16:48:53 +020011320 /* destroy the backlight and sysfs files before encoders/connectors */
11321 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11322 intel_panel_destroy_backlight(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030011323 drm_sysfs_connector_remove(connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011324 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011325
Jesse Barnes79e53942008-11-07 14:24:08 -080011326 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011327
11328 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011329}
11330
Dave Airlie28d52042009-09-21 14:33:58 +100011331/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011332 * Return which encoder is currently attached for connector.
11333 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011334struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011335{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011336 return &intel_attached_encoder(connector)->base;
11337}
Jesse Barnes79e53942008-11-07 14:24:08 -080011338
Chris Wilsondf0e9242010-09-09 16:20:55 +010011339void intel_connector_attach_encoder(struct intel_connector *connector,
11340 struct intel_encoder *encoder)
11341{
11342 connector->encoder = encoder;
11343 drm_mode_connector_attach_encoder(&connector->base,
11344 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011345}
Dave Airlie28d52042009-09-21 14:33:58 +100011346
11347/*
11348 * set vga decode state - true == enable VGA decode
11349 */
11350int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11351{
11352 struct drm_i915_private *dev_priv = dev->dev_private;
11353 u16 gmch_ctrl;
11354
11355 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11356 if (state)
11357 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11358 else
11359 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11360 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11361 return 0;
11362}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011363
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011364struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011365
11366 u32 power_well_driver;
11367
Chris Wilson63b66e52013-08-08 15:12:06 +020011368 int num_transcoders;
11369
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011370 struct intel_cursor_error_state {
11371 u32 control;
11372 u32 position;
11373 u32 base;
11374 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011375 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011376
11377 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011378 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011379 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011380 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011381
11382 struct intel_plane_error_state {
11383 u32 control;
11384 u32 stride;
11385 u32 size;
11386 u32 pos;
11387 u32 addr;
11388 u32 surface;
11389 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011390 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011391
11392 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020011393 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020011394 enum transcoder cpu_transcoder;
11395
11396 u32 conf;
11397
11398 u32 htotal;
11399 u32 hblank;
11400 u32 hsync;
11401 u32 vtotal;
11402 u32 vblank;
11403 u32 vsync;
11404 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011405};
11406
11407struct intel_display_error_state *
11408intel_display_capture_error_state(struct drm_device *dev)
11409{
Akshay Joshi0206e352011-08-16 15:34:10 -040011410 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011411 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011412 int transcoders[] = {
11413 TRANSCODER_A,
11414 TRANSCODER_B,
11415 TRANSCODER_C,
11416 TRANSCODER_EDP,
11417 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011418 int i;
11419
Chris Wilson63b66e52013-08-08 15:12:06 +020011420 if (INTEL_INFO(dev)->num_pipes == 0)
11421 return NULL;
11422
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011423 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011424 if (error == NULL)
11425 return NULL;
11426
Imre Deak190be112013-11-25 17:15:31 +020011427 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011428 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11429
Damien Lespiau52331302012-08-15 19:23:25 +010011430 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020011431 error->pipe[i].power_domain_on =
11432 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11433 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011434 continue;
11435
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011436 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11437 error->cursor[i].control = I915_READ(CURCNTR(i));
11438 error->cursor[i].position = I915_READ(CURPOS(i));
11439 error->cursor[i].base = I915_READ(CURBASE(i));
11440 } else {
11441 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11442 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11443 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11444 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011445
11446 error->plane[i].control = I915_READ(DSPCNTR(i));
11447 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011448 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011449 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011450 error->plane[i].pos = I915_READ(DSPPOS(i));
11451 }
Paulo Zanonica291362013-03-06 20:03:14 -030011452 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11453 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011454 if (INTEL_INFO(dev)->gen >= 4) {
11455 error->plane[i].surface = I915_READ(DSPSURF(i));
11456 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11457 }
11458
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011459 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011460 }
11461
11462 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11463 if (HAS_DDI(dev_priv->dev))
11464 error->num_transcoders++; /* Account for eDP. */
11465
11466 for (i = 0; i < error->num_transcoders; i++) {
11467 enum transcoder cpu_transcoder = transcoders[i];
11468
Imre Deakddf9c532013-11-27 22:02:02 +020011469 error->transcoder[i].power_domain_on =
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020011470 intel_display_power_enabled_sw(dev,
11471 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011472 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011473 continue;
11474
Chris Wilson63b66e52013-08-08 15:12:06 +020011475 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11476
11477 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11478 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11479 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11480 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11481 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11482 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11483 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011484 }
11485
11486 return error;
11487}
11488
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011489#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11490
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011491void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011492intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011493 struct drm_device *dev,
11494 struct intel_display_error_state *error)
11495{
11496 int i;
11497
Chris Wilson63b66e52013-08-08 15:12:06 +020011498 if (!error)
11499 return;
11500
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011501 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020011502 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011503 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011504 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011505 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011506 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020011507 err_printf(m, " Power: %s\n",
11508 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011509 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011510
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011511 err_printf(m, "Plane [%d]:\n", i);
11512 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11513 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011514 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011515 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11516 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011517 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011518 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011519 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011520 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011521 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11522 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011523 }
11524
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011525 err_printf(m, "Cursor [%d]:\n", i);
11526 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11527 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11528 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011529 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011530
11531 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011532 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011533 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020011534 err_printf(m, " Power: %s\n",
11535 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020011536 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11537 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11538 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11539 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11540 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11541 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11542 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11543 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011544}