blob: 752d83019f366c1e5701030f710f5a0eaa443c7c [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Daniel Vetter3dec0092010-08-20 21:40:52 +020044static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010045static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080046
Jesse Barnesf1f644d2013-06-27 00:39:25 +030047static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030049static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030051
Damien Lespiaue7457a92013-08-08 22:28:59 +010052static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
54
55
Jesse Barnes79e53942008-11-07 14:24:08 -080056typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040057 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_range_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int dot_limit;
62 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080063} intel_p2_t;
64
Ma Lingd4906092009-03-18 20:13:27 +080065typedef struct intel_limit intel_limit_t;
66struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 intel_range_t dot, vco, n, m, m1, m2, p, p1;
68 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080069};
Jesse Barnes79e53942008-11-07 14:24:08 -080070
Daniel Vetterd2acd212012-10-20 20:57:43 +020071int
72intel_pch_rawclk(struct drm_device *dev)
73{
74 struct drm_i915_private *dev_priv = dev->dev_private;
75
76 WARN_ON(!HAS_PCH_SPLIT(dev));
77
78 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
79}
80
Chris Wilson021357a2010-09-07 20:54:59 +010081static inline u32 /* units of 100MHz */
82intel_fdi_link_freq(struct drm_device *dev)
83{
Chris Wilson8b99e682010-10-13 09:59:17 +010084 if (IS_GEN5(dev)) {
85 struct drm_i915_private *dev_priv = dev->dev_private;
86 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 } else
88 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010089}
90
Daniel Vetter5d536e22013-07-06 12:52:06 +020091static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040092 .dot = { .min = 25000, .max = 350000 },
93 .vco = { .min = 930000, .max = 1400000 },
94 .n = { .min = 3, .max = 16 },
95 .m = { .min = 96, .max = 140 },
96 .m1 = { .min = 18, .max = 26 },
97 .m2 = { .min = 6, .max = 16 },
98 .p = { .min = 4, .max = 128 },
99 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700100 .p2 = { .dot_limit = 165000,
101 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700102};
103
Daniel Vetter5d536e22013-07-06 12:52:06 +0200104static const intel_limit_t intel_limits_i8xx_dvo = {
105 .dot = { .min = 25000, .max = 350000 },
106 .vco = { .min = 930000, .max = 1400000 },
107 .n = { .min = 3, .max = 16 },
108 .m = { .min = 96, .max = 140 },
109 .m1 = { .min = 18, .max = 26 },
110 .m2 = { .min = 6, .max = 16 },
111 .p = { .min = 4, .max = 128 },
112 .p1 = { .min = 2, .max = 33 },
113 .p2 = { .dot_limit = 165000,
114 .p2_slow = 4, .p2_fast = 4 },
115};
116
Keith Packarde4b36692009-06-05 19:22:17 -0700117static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 .dot = { .min = 25000, .max = 350000 },
119 .vco = { .min = 930000, .max = 1400000 },
120 .n = { .min = 3, .max = 16 },
121 .m = { .min = 96, .max = 140 },
122 .m1 = { .min = 18, .max = 26 },
123 .m2 = { .min = 6, .max = 16 },
124 .p = { .min = 4, .max = 128 },
125 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700126 .p2 = { .dot_limit = 165000,
127 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700128};
Eric Anholt273e27c2011-03-30 13:01:10 -0700129
Keith Packarde4b36692009-06-05 19:22:17 -0700130static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 .dot = { .min = 20000, .max = 400000 },
132 .vco = { .min = 1400000, .max = 2800000 },
133 .n = { .min = 1, .max = 6 },
134 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100135 .m1 = { .min = 8, .max = 18 },
136 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .p = { .min = 5, .max = 80 },
138 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700139 .p2 = { .dot_limit = 200000,
140 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700141};
142
143static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400144 .dot = { .min = 20000, .max = 400000 },
145 .vco = { .min = 1400000, .max = 2800000 },
146 .n = { .min = 1, .max = 6 },
147 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100148 .m1 = { .min = 8, .max = 18 },
149 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .p = { .min = 7, .max = 98 },
151 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700152 .p2 = { .dot_limit = 112000,
153 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700154};
155
Eric Anholt273e27c2011-03-30 13:01:10 -0700156
Keith Packarde4b36692009-06-05 19:22:17 -0700157static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .dot = { .min = 25000, .max = 270000 },
159 .vco = { .min = 1750000, .max = 3500000},
160 .n = { .min = 1, .max = 4 },
161 .m = { .min = 104, .max = 138 },
162 .m1 = { .min = 17, .max = 23 },
163 .m2 = { .min = 5, .max = 11 },
164 .p = { .min = 10, .max = 30 },
165 .p1 = { .min = 1, .max = 3},
166 .p2 = { .dot_limit = 270000,
167 .p2_slow = 10,
168 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800169 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
172static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .dot = { .min = 22000, .max = 400000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 16, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8},
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 20000, .max = 115000 },
187 .vco = { .min = 1750000, .max = 3500000 },
188 .n = { .min = 1, .max = 3 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 28, .max = 112 },
193 .p1 = { .min = 2, .max = 8 },
194 .p2 = { .dot_limit = 0,
195 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800196 },
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
198
199static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 80000, .max = 224000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 14, .max = 42 },
207 .p1 = { .min = 2, .max = 6 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800210 },
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500213static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400214 .dot = { .min = 20000, .max = 400000},
215 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .n = { .min = 3, .max = 6 },
218 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .m1 = { .min = 0, .max = 0 },
221 .m2 = { .min = 0, .max = 254 },
222 .p = { .min = 5, .max = 80 },
223 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700224 .p2 = { .dot_limit = 200000,
225 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500228static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 20000, .max = 400000 },
230 .vco = { .min = 1700000, .max = 3500000 },
231 .n = { .min = 3, .max = 6 },
232 .m = { .min = 2, .max = 256 },
233 .m1 = { .min = 0, .max = 0 },
234 .m2 = { .min = 0, .max = 254 },
235 .p = { .min = 7, .max = 112 },
236 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700237 .p2 = { .dot_limit = 112000,
238 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
Eric Anholt273e27c2011-03-30 13:01:10 -0700241/* Ironlake / Sandybridge
242 *
243 * We calculate clock using (register_value + 2) for N/M1/M2, so here
244 * the range value for them is (actual_value - 2).
245 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800246static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 25000, .max = 350000 },
248 .vco = { .min = 1760000, .max = 3510000 },
249 .n = { .min = 1, .max = 5 },
250 .m = { .min = 79, .max = 127 },
251 .m1 = { .min = 12, .max = 22 },
252 .m2 = { .min = 5, .max = 9 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
255 .p2 = { .dot_limit = 225000,
256 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800259static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .dot = { .min = 25000, .max = 350000 },
261 .vco = { .min = 1760000, .max = 3510000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 79, .max = 118 },
264 .m1 = { .min = 12, .max = 22 },
265 .m2 = { .min = 5, .max = 9 },
266 .p = { .min = 28, .max = 112 },
267 .p1 = { .min = 2, .max = 8 },
268 .p2 = { .dot_limit = 225000,
269 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800270};
271
272static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .dot = { .min = 25000, .max = 350000 },
274 .vco = { .min = 1760000, .max = 3510000 },
275 .n = { .min = 1, .max = 3 },
276 .m = { .min = 79, .max = 127 },
277 .m1 = { .min = 12, .max = 22 },
278 .m2 = { .min = 5, .max = 9 },
279 .p = { .min = 14, .max = 56 },
280 .p1 = { .min = 2, .max = 8 },
281 .p2 = { .dot_limit = 225000,
282 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800286static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .dot = { .min = 25000, .max = 350000 },
288 .vco = { .min = 1760000, .max = 3510000 },
289 .n = { .min = 1, .max = 2 },
290 .m = { .min = 79, .max = 126 },
291 .m1 = { .min = 12, .max = 22 },
292 .m2 = { .min = 5, .max = 9 },
293 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .p2 = { .dot_limit = 225000,
296 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 126 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800310};
311
Ville Syrjälädc730512013-09-24 21:26:30 +0300312static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300313 /*
314 * These are the data rate limits (measured in fast clocks)
315 * since those are the strictest limits we have. The fast
316 * clock and actual rate limits are more relaxed, so checking
317 * them would make no difference.
318 */
319 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200320 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700321 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .m1 = { .min = 2, .max = 3 },
323 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300324 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300325 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700326};
327
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300328static void vlv_clock(int refclk, intel_clock_t *clock)
329{
330 clock->m = clock->m1 * clock->m2;
331 clock->p = clock->p1 * clock->p2;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300332 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
333 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300334}
335
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300336/**
337 * Returns whether any output on the specified pipe is of the specified type
338 */
339static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
340{
341 struct drm_device *dev = crtc->dev;
342 struct intel_encoder *encoder;
343
344 for_each_encoder_on_crtc(dev, crtc, encoder)
345 if (encoder->type == type)
346 return true;
347
348 return false;
349}
350
Chris Wilson1b894b52010-12-14 20:04:54 +0000351static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
352 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800353{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800355 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800356
357 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100358 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000359 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360 limit = &intel_limits_ironlake_dual_lvds_100m;
361 else
362 limit = &intel_limits_ironlake_dual_lvds;
363 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000364 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800365 limit = &intel_limits_ironlake_single_lvds_100m;
366 else
367 limit = &intel_limits_ironlake_single_lvds;
368 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200369 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800370 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800371
372 return limit;
373}
374
Ma Ling044c7c42009-03-18 20:13:23 +0800375static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
376{
377 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800378 const intel_limit_t *limit;
379
380 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100381 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700382 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800383 else
Keith Packarde4b36692009-06-05 19:22:17 -0700384 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800385 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
386 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700387 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800388 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700389 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800390 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700391 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800392
393 return limit;
394}
395
Chris Wilson1b894b52010-12-14 20:04:54 +0000396static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800397{
398 struct drm_device *dev = crtc->dev;
399 const intel_limit_t *limit;
400
Eric Anholtbad720f2009-10-22 16:11:14 -0700401 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000402 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800403 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800404 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500405 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500407 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800408 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500409 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700410 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300411 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100412 } else if (!IS_GEN2(dev)) {
413 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
414 limit = &intel_limits_i9xx_lvds;
415 else
416 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800417 } else {
418 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700419 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200420 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700421 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200422 else
423 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800424 }
425 return limit;
426}
427
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500428/* m1 is reserved as 0 in Pineview, n is a ring counter */
429static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800430{
Shaohua Li21778322009-02-23 15:19:16 +0800431 clock->m = clock->m2 + 2;
432 clock->p = clock->p1 * clock->p2;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300433 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
434 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800435}
436
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200437static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
438{
439 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
440}
441
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200442static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800443{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200444 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800445 clock->p = clock->p1 * clock->p2;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300446 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
447 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800448}
449
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800450#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800451/**
452 * Returns whether the given set of divisors are valid for a given refclk with
453 * the given connectors.
454 */
455
Chris Wilson1b894b52010-12-14 20:04:54 +0000456static bool intel_PLL_is_valid(struct drm_device *dev,
457 const intel_limit_t *limit,
458 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800459{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300460 if (clock->n < limit->n.min || limit->n.max < clock->n)
461 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800462 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400463 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800464 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800466 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300468
469 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
470 if (clock->m1 <= clock->m2)
471 INTELPllInvalid("m1 <= m2\n");
472
473 if (!IS_VALLEYVIEW(dev)) {
474 if (clock->p < limit->p.min || limit->p.max < clock->p)
475 INTELPllInvalid("p out of range\n");
476 if (clock->m < limit->m.min || limit->m.max < clock->m)
477 INTELPllInvalid("m out of range\n");
478 }
479
Jesse Barnes79e53942008-11-07 14:24:08 -0800480 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400481 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800482 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
483 * connector, etc., rather than just a single range.
484 */
485 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487
488 return true;
489}
490
Ma Lingd4906092009-03-18 20:13:27 +0800491static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200492i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800493 int target, int refclk, intel_clock_t *match_clock,
494 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800495{
496 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800498 int err = target;
499
Daniel Vettera210b022012-11-26 17:22:08 +0100500 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800501 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100502 * For LVDS just rely on its current settings for dual-channel.
503 * We haven't figured out how to reliably set up different
504 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100506 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 clock.p2 = limit->p2.p2_fast;
508 else
509 clock.p2 = limit->p2.p2_slow;
510 } else {
511 if (target < limit->p2.dot_limit)
512 clock.p2 = limit->p2.p2_slow;
513 else
514 clock.p2 = limit->p2.p2_fast;
515 }
516
Akshay Joshi0206e352011-08-16 15:34:10 -0400517 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800518
Zhao Yakui42158662009-11-20 11:24:18 +0800519 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
520 clock.m1++) {
521 for (clock.m2 = limit->m2.min;
522 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200523 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800524 break;
525 for (clock.n = limit->n.min;
526 clock.n <= limit->n.max; clock.n++) {
527 for (clock.p1 = limit->p1.min;
528 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800529 int this_err;
530
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200531 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000532 if (!intel_PLL_is_valid(dev, limit,
533 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800534 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800535 if (match_clock &&
536 clock.p != match_clock->p)
537 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800538
539 this_err = abs(clock.dot - target);
540 if (this_err < err) {
541 *best_clock = clock;
542 err = this_err;
543 }
544 }
545 }
546 }
547 }
548
549 return (err != target);
550}
551
Ma Lingd4906092009-03-18 20:13:27 +0800552static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200553pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
554 int target, int refclk, intel_clock_t *match_clock,
555 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200556{
557 struct drm_device *dev = crtc->dev;
558 intel_clock_t clock;
559 int err = target;
560
561 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
562 /*
563 * For LVDS just rely on its current settings for dual-channel.
564 * We haven't figured out how to reliably set up different
565 * single/dual channel state, if we even can.
566 */
567 if (intel_is_dual_link_lvds(dev))
568 clock.p2 = limit->p2.p2_fast;
569 else
570 clock.p2 = limit->p2.p2_slow;
571 } else {
572 if (target < limit->p2.dot_limit)
573 clock.p2 = limit->p2.p2_slow;
574 else
575 clock.p2 = limit->p2.p2_fast;
576 }
577
578 memset(best_clock, 0, sizeof(*best_clock));
579
580 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
581 clock.m1++) {
582 for (clock.m2 = limit->m2.min;
583 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200584 for (clock.n = limit->n.min;
585 clock.n <= limit->n.max; clock.n++) {
586 for (clock.p1 = limit->p1.min;
587 clock.p1 <= limit->p1.max; clock.p1++) {
588 int this_err;
589
590 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800591 if (!intel_PLL_is_valid(dev, limit,
592 &clock))
593 continue;
594 if (match_clock &&
595 clock.p != match_clock->p)
596 continue;
597
598 this_err = abs(clock.dot - target);
599 if (this_err < err) {
600 *best_clock = clock;
601 err = this_err;
602 }
603 }
604 }
605 }
606 }
607
608 return (err != target);
609}
610
Ma Lingd4906092009-03-18 20:13:27 +0800611static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200612g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
613 int target, int refclk, intel_clock_t *match_clock,
614 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800615{
616 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800617 intel_clock_t clock;
618 int max_n;
619 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400620 /* approximately equals target * 0.00585 */
621 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800622 found = false;
623
624 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100625 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800626 clock.p2 = limit->p2.p2_fast;
627 else
628 clock.p2 = limit->p2.p2_slow;
629 } else {
630 if (target < limit->p2.dot_limit)
631 clock.p2 = limit->p2.p2_slow;
632 else
633 clock.p2 = limit->p2.p2_fast;
634 }
635
636 memset(best_clock, 0, sizeof(*best_clock));
637 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200638 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800639 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200640 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800641 for (clock.m1 = limit->m1.max;
642 clock.m1 >= limit->m1.min; clock.m1--) {
643 for (clock.m2 = limit->m2.max;
644 clock.m2 >= limit->m2.min; clock.m2--) {
645 for (clock.p1 = limit->p1.max;
646 clock.p1 >= limit->p1.min; clock.p1--) {
647 int this_err;
648
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200649 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000650 if (!intel_PLL_is_valid(dev, limit,
651 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800652 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000653
654 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800655 if (this_err < err_most) {
656 *best_clock = clock;
657 err_most = this_err;
658 max_n = clock.n;
659 found = true;
660 }
661 }
662 }
663 }
664 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800665 return found;
666}
Ma Lingd4906092009-03-18 20:13:27 +0800667
Zhenyu Wang2c072452009-06-05 15:38:42 +0800668static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200669vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
670 int target, int refclk, intel_clock_t *match_clock,
671 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700672{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300673 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300674 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300675 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300676 /* min update 19.2 MHz */
677 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300678 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700679
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300680 target *= 5; /* fast clock */
681
682 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700683
684 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300685 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300686 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300687 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300688 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300689 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700690 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300691 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300692 unsigned int ppm, diff;
693
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300694 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
695 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300696
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300697 vlv_clock(refclk, &clock);
698
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300699 if (!intel_PLL_is_valid(dev, limit,
700 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300701 continue;
702
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300703 diff = abs(clock.dot - target);
704 ppm = div_u64(1000000ULL * diff, target);
705
706 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300707 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300708 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300709 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300710 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300711
Ville Syrjäläc6861222013-09-24 21:26:21 +0300712 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300713 bestppm = ppm;
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;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700716 }
717 }
718 }
719 }
720 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700721
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300722 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700723}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700724
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300725bool intel_crtc_active(struct drm_crtc *crtc)
726{
727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
728
729 /* Be paranoid as we can arrive here with only partial
730 * state retrieved from the hardware during setup.
731 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100732 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300733 * as Haswell has gained clock readout/fastboot support.
734 *
735 * We can ditch the crtc->fb check as soon as we can
736 * properly reconstruct framebuffers.
737 */
738 return intel_crtc->active && crtc->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100739 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300740}
741
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200742enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
743 enum pipe pipe)
744{
745 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
747
Daniel Vetter3b117c82013-04-17 20:15:07 +0200748 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200749}
750
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200751static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300752{
753 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200754 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300755
756 frame = I915_READ(frame_reg);
757
758 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
759 DRM_DEBUG_KMS("vblank wait timed out\n");
760}
761
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700762/**
763 * intel_wait_for_vblank - wait for vblank on a given pipe
764 * @dev: drm device
765 * @pipe: pipe to wait for
766 *
767 * Wait for vblank to occur on a given pipe. Needed for various bits of
768 * mode setting code.
769 */
770void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800771{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700772 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800773 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700774
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200775 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
776 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300777 return;
778 }
779
Chris Wilson300387c2010-09-05 20:25:43 +0100780 /* Clear existing vblank status. Note this will clear any other
781 * sticky status fields as well.
782 *
783 * This races with i915_driver_irq_handler() with the result
784 * that either function could miss a vblank event. Here it is not
785 * fatal, as we will either wait upon the next vblank interrupt or
786 * timeout. Generally speaking intel_wait_for_vblank() is only
787 * called during modeset at which time the GPU should be idle and
788 * should *not* be performing page flips and thus not waiting on
789 * vblanks...
790 * Currently, the result of us stealing a vblank from the irq
791 * handler is that a single frame will be skipped during swapbuffers.
792 */
793 I915_WRITE(pipestat_reg,
794 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
795
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700796 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100797 if (wait_for(I915_READ(pipestat_reg) &
798 PIPE_VBLANK_INTERRUPT_STATUS,
799 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700800 DRM_DEBUG_KMS("vblank wait timed out\n");
801}
802
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300803static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
804{
805 struct drm_i915_private *dev_priv = dev->dev_private;
806 u32 reg = PIPEDSL(pipe);
807 u32 line1, line2;
808 u32 line_mask;
809
810 if (IS_GEN2(dev))
811 line_mask = DSL_LINEMASK_GEN2;
812 else
813 line_mask = DSL_LINEMASK_GEN3;
814
815 line1 = I915_READ(reg) & line_mask;
816 mdelay(5);
817 line2 = I915_READ(reg) & line_mask;
818
819 return line1 == line2;
820}
821
Keith Packardab7ad7f2010-10-03 00:33:06 -0700822/*
823 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700824 * @dev: drm device
825 * @pipe: pipe to wait for
826 *
827 * After disabling a pipe, we can't wait for vblank in the usual way,
828 * spinning on the vblank interrupt status bit, since we won't actually
829 * see an interrupt when the pipe is disabled.
830 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700831 * On Gen4 and above:
832 * wait for the pipe register state bit to turn off
833 *
834 * Otherwise:
835 * wait for the display line value to settle (it usually
836 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100837 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700838 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100839void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700840{
841 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200842 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
843 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700844
Keith Packardab7ad7f2010-10-03 00:33:06 -0700845 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200846 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700847
Keith Packardab7ad7f2010-10-03 00:33:06 -0700848 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100849 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
850 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200851 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700852 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700853 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300854 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200855 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700856 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800857}
858
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000859/*
860 * ibx_digital_port_connected - is the specified port connected?
861 * @dev_priv: i915 private structure
862 * @port: the port to test
863 *
864 * Returns true if @port is connected, false otherwise.
865 */
866bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
867 struct intel_digital_port *port)
868{
869 u32 bit;
870
Damien Lespiauc36346e2012-12-13 16:09:03 +0000871 if (HAS_PCH_IBX(dev_priv->dev)) {
872 switch(port->port) {
873 case PORT_B:
874 bit = SDE_PORTB_HOTPLUG;
875 break;
876 case PORT_C:
877 bit = SDE_PORTC_HOTPLUG;
878 break;
879 case PORT_D:
880 bit = SDE_PORTD_HOTPLUG;
881 break;
882 default:
883 return true;
884 }
885 } else {
886 switch(port->port) {
887 case PORT_B:
888 bit = SDE_PORTB_HOTPLUG_CPT;
889 break;
890 case PORT_C:
891 bit = SDE_PORTC_HOTPLUG_CPT;
892 break;
893 case PORT_D:
894 bit = SDE_PORTD_HOTPLUG_CPT;
895 break;
896 default:
897 return true;
898 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000899 }
900
901 return I915_READ(SDEISR) & bit;
902}
903
Jesse Barnesb24e7172011-01-04 15:09:30 -0800904static const char *state_string(bool enabled)
905{
906 return enabled ? "on" : "off";
907}
908
909/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200910void assert_pll(struct drm_i915_private *dev_priv,
911 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800912{
913 int reg;
914 u32 val;
915 bool cur_state;
916
917 reg = DPLL(pipe);
918 val = I915_READ(reg);
919 cur_state = !!(val & DPLL_VCO_ENABLE);
920 WARN(cur_state != state,
921 "PLL state assertion failure (expected %s, current %s)\n",
922 state_string(state), state_string(cur_state));
923}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800924
Jani Nikula23538ef2013-08-27 15:12:22 +0300925/* XXX: the dsi pll is shared between MIPI DSI ports */
926static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
927{
928 u32 val;
929 bool cur_state;
930
931 mutex_lock(&dev_priv->dpio_lock);
932 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
933 mutex_unlock(&dev_priv->dpio_lock);
934
935 cur_state = val & DSI_PLL_VCO_EN;
936 WARN(cur_state != state,
937 "DSI PLL state assertion failure (expected %s, current %s)\n",
938 state_string(state), state_string(cur_state));
939}
940#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
941#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
942
Daniel Vetter55607e82013-06-16 21:42:39 +0200943struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200944intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800945{
Daniel Vettere2b78262013-06-07 23:10:03 +0200946 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
947
Daniel Vettera43f6e02013-06-07 23:10:32 +0200948 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200949 return NULL;
950
Daniel Vettera43f6e02013-06-07 23:10:32 +0200951 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200952}
953
Jesse Barnesb24e7172011-01-04 15:09:30 -0800954/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200955void assert_shared_dpll(struct drm_i915_private *dev_priv,
956 struct intel_shared_dpll *pll,
957 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800958{
Jesse Barnes040484a2011-01-03 12:14:26 -0800959 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200960 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800961
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300962 if (HAS_PCH_LPT(dev_priv->dev)) {
963 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
964 return;
965 }
966
Chris Wilson92b27b02012-05-20 18:10:50 +0100967 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200968 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100969 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100970
Daniel Vetter53589012013-06-05 13:34:16 +0200971 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100972 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200973 "%s assertion failure (expected %s, current %s)\n",
974 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800975}
Jesse Barnes040484a2011-01-03 12:14:26 -0800976
977static void assert_fdi_tx(struct drm_i915_private *dev_priv,
978 enum pipe pipe, bool state)
979{
980 int reg;
981 u32 val;
982 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200983 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
984 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800985
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200986 if (HAS_DDI(dev_priv->dev)) {
987 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200988 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300989 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200990 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300991 } else {
992 reg = FDI_TX_CTL(pipe);
993 val = I915_READ(reg);
994 cur_state = !!(val & FDI_TX_ENABLE);
995 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800996 WARN(cur_state != state,
997 "FDI TX state assertion failure (expected %s, current %s)\n",
998 state_string(state), state_string(cur_state));
999}
1000#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1001#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1002
1003static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1004 enum pipe pipe, bool state)
1005{
1006 int reg;
1007 u32 val;
1008 bool cur_state;
1009
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001010 reg = FDI_RX_CTL(pipe);
1011 val = I915_READ(reg);
1012 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001013 WARN(cur_state != state,
1014 "FDI RX state assertion failure (expected %s, current %s)\n",
1015 state_string(state), state_string(cur_state));
1016}
1017#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1018#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1019
1020static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1021 enum pipe pipe)
1022{
1023 int reg;
1024 u32 val;
1025
1026 /* ILK FDI PLL is always enabled */
1027 if (dev_priv->info->gen == 5)
1028 return;
1029
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001030 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001031 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001032 return;
1033
Jesse Barnes040484a2011-01-03 12:14:26 -08001034 reg = FDI_TX_CTL(pipe);
1035 val = I915_READ(reg);
1036 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1037}
1038
Daniel Vetter55607e82013-06-16 21:42:39 +02001039void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001041{
1042 int reg;
1043 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001044 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001045
1046 reg = FDI_RX_CTL(pipe);
1047 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001048 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1049 WARN(cur_state != state,
1050 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001052}
1053
Jesse Barnesea0760c2011-01-04 15:09:32 -08001054static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1055 enum pipe pipe)
1056{
1057 int pp_reg, lvds_reg;
1058 u32 val;
1059 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001060 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001061
1062 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1063 pp_reg = PCH_PP_CONTROL;
1064 lvds_reg = PCH_LVDS;
1065 } else {
1066 pp_reg = PP_CONTROL;
1067 lvds_reg = LVDS;
1068 }
1069
1070 val = I915_READ(pp_reg);
1071 if (!(val & PANEL_POWER_ON) ||
1072 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1073 locked = false;
1074
1075 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1076 panel_pipe = PIPE_B;
1077
1078 WARN(panel_pipe == pipe && locked,
1079 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001080 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001081}
1082
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001083static void assert_cursor(struct drm_i915_private *dev_priv,
1084 enum pipe pipe, bool state)
1085{
1086 struct drm_device *dev = dev_priv->dev;
1087 bool cur_state;
1088
1089 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1090 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1091 else if (IS_845G(dev) || IS_I865G(dev))
1092 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1093 else
1094 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1095
1096 WARN(cur_state != state,
1097 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1098 pipe_name(pipe), state_string(state), state_string(cur_state));
1099}
1100#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1101#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1102
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001103void assert_pipe(struct drm_i915_private *dev_priv,
1104 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105{
1106 int reg;
1107 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001108 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001109 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1110 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001111
Daniel Vetter8e636782012-01-22 01:36:48 +01001112 /* if we need the pipe A quirk it must be always on */
1113 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1114 state = true;
1115
Paulo Zanonib97186f2013-05-03 12:15:36 -03001116 if (!intel_display_power_enabled(dev_priv->dev,
1117 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001118 cur_state = false;
1119 } else {
1120 reg = PIPECONF(cpu_transcoder);
1121 val = I915_READ(reg);
1122 cur_state = !!(val & PIPECONF_ENABLE);
1123 }
1124
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001125 WARN(cur_state != state,
1126 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001127 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001128}
1129
Chris Wilson931872f2012-01-16 23:01:13 +00001130static void assert_plane(struct drm_i915_private *dev_priv,
1131 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001132{
1133 int reg;
1134 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001135 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136
1137 reg = DSPCNTR(plane);
1138 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001139 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1140 WARN(cur_state != state,
1141 "plane %c assertion failure (expected %s, current %s)\n",
1142 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001143}
1144
Chris Wilson931872f2012-01-16 23:01:13 +00001145#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1146#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1147
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1149 enum pipe pipe)
1150{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001151 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001152 int reg, i;
1153 u32 val;
1154 int cur_pipe;
1155
Ville Syrjälä653e1022013-06-04 13:49:05 +03001156 /* Primary planes are fixed to pipes on gen4+ */
1157 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001158 reg = DSPCNTR(pipe);
1159 val = I915_READ(reg);
1160 WARN((val & DISPLAY_PLANE_ENABLE),
1161 "plane %c assertion failure, should be disabled but not\n",
1162 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001163 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001164 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001165
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001167 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001168 reg = DSPCNTR(i);
1169 val = I915_READ(reg);
1170 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1171 DISPPLANE_SEL_PIPE_SHIFT;
1172 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001173 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1174 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001175 }
1176}
1177
Jesse Barnes19332d72013-03-28 09:55:38 -07001178static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1179 enum pipe pipe)
1180{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001181 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001182 int reg, i;
1183 u32 val;
1184
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001185 if (IS_VALLEYVIEW(dev)) {
1186 for (i = 0; i < dev_priv->num_plane; i++) {
1187 reg = SPCNTR(pipe, i);
1188 val = I915_READ(reg);
1189 WARN((val & SP_ENABLE),
1190 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1191 sprite_name(pipe, i), pipe_name(pipe));
1192 }
1193 } else if (INTEL_INFO(dev)->gen >= 7) {
1194 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001195 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001196 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001197 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001198 plane_name(pipe), pipe_name(pipe));
1199 } else if (INTEL_INFO(dev)->gen >= 5) {
1200 reg = DVSCNTR(pipe);
1201 val = I915_READ(reg);
1202 WARN((val & DVS_ENABLE),
1203 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1204 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001205 }
1206}
1207
Jesse Barnes92f25842011-01-04 15:09:34 -08001208static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1209{
1210 u32 val;
1211 bool enabled;
1212
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001213 if (HAS_PCH_LPT(dev_priv->dev)) {
1214 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1215 return;
1216 }
1217
Jesse Barnes92f25842011-01-04 15:09:34 -08001218 val = I915_READ(PCH_DREF_CONTROL);
1219 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1220 DREF_SUPERSPREAD_SOURCE_MASK));
1221 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1222}
1223
Daniel Vetterab9412b2013-05-03 11:49:46 +02001224static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1225 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001226{
1227 int reg;
1228 u32 val;
1229 bool enabled;
1230
Daniel Vetterab9412b2013-05-03 11:49:46 +02001231 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001232 val = I915_READ(reg);
1233 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001234 WARN(enabled,
1235 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1236 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001237}
1238
Keith Packard4e634382011-08-06 10:39:45 -07001239static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001241{
1242 if ((val & DP_PORT_EN) == 0)
1243 return false;
1244
1245 if (HAS_PCH_CPT(dev_priv->dev)) {
1246 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1247 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1248 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1249 return false;
1250 } else {
1251 if ((val & DP_PIPE_MASK) != (pipe << 30))
1252 return false;
1253 }
1254 return true;
1255}
1256
Keith Packard1519b992011-08-06 10:35:34 -07001257static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1258 enum pipe pipe, u32 val)
1259{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001260 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001261 return false;
1262
1263 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001264 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001265 return false;
1266 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001267 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001268 return false;
1269 }
1270 return true;
1271}
1272
1273static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1274 enum pipe pipe, u32 val)
1275{
1276 if ((val & LVDS_PORT_EN) == 0)
1277 return false;
1278
1279 if (HAS_PCH_CPT(dev_priv->dev)) {
1280 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1281 return false;
1282 } else {
1283 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1284 return false;
1285 }
1286 return true;
1287}
1288
1289static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1290 enum pipe pipe, u32 val)
1291{
1292 if ((val & ADPA_DAC_ENABLE) == 0)
1293 return false;
1294 if (HAS_PCH_CPT(dev_priv->dev)) {
1295 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1296 return false;
1297 } else {
1298 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1299 return false;
1300 }
1301 return true;
1302}
1303
Jesse Barnes291906f2011-02-02 12:28:03 -08001304static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001305 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001306{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001307 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001308 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001309 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001310 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001311
Daniel Vetter75c5da22012-09-10 21:58:29 +02001312 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1313 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001314 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001315}
1316
1317static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe, int reg)
1319{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001320 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001321 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001322 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001323 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001324
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001325 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001326 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001327 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001328}
1329
1330static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1331 enum pipe pipe)
1332{
1333 int reg;
1334 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001335
Keith Packardf0575e92011-07-25 22:12:43 -07001336 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1337 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1338 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001339
1340 reg = PCH_ADPA;
1341 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001342 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001343 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001344 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001345
1346 reg = PCH_LVDS;
1347 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001348 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001349 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001350 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001351
Paulo Zanonie2debe92013-02-18 19:00:27 -03001352 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1353 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1354 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001355}
1356
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001357static void intel_init_dpio(struct drm_device *dev)
1358{
1359 struct drm_i915_private *dev_priv = dev->dev_private;
1360
1361 if (!IS_VALLEYVIEW(dev))
1362 return;
1363
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001364 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001365 /*
1366 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1367 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1368 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1369 * b. The other bits such as sfr settings / modesel may all be set
1370 * to 0.
1371 *
1372 * This should only be done on init and resume from S3 with both
1373 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1374 */
1375 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1376}
1377
Daniel Vetter426115c2013-07-11 22:13:42 +02001378static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001379{
Daniel Vetter426115c2013-07-11 22:13:42 +02001380 struct drm_device *dev = crtc->base.dev;
1381 struct drm_i915_private *dev_priv = dev->dev_private;
1382 int reg = DPLL(crtc->pipe);
1383 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001384
Daniel Vetter426115c2013-07-11 22:13:42 +02001385 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001386
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001387 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001388 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1389
1390 /* PLL is protected by panel, make sure we can write it */
1391 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001392 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001393
Daniel Vetter426115c2013-07-11 22:13:42 +02001394 I915_WRITE(reg, dpll);
1395 POSTING_READ(reg);
1396 udelay(150);
1397
1398 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1399 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1400
1401 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1402 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001403
1404 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001405 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001406 POSTING_READ(reg);
1407 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001408 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001409 POSTING_READ(reg);
1410 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001411 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001412 POSTING_READ(reg);
1413 udelay(150); /* wait for warmup */
1414}
1415
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001416static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001417{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001418 struct drm_device *dev = crtc->base.dev;
1419 struct drm_i915_private *dev_priv = dev->dev_private;
1420 int reg = DPLL(crtc->pipe);
1421 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001422
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001423 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001424
1425 /* No really, not for ILK+ */
1426 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001427
1428 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001429 if (IS_MOBILE(dev) && !IS_I830(dev))
1430 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001431
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001432 I915_WRITE(reg, dpll);
1433
1434 /* Wait for the clocks to stabilize. */
1435 POSTING_READ(reg);
1436 udelay(150);
1437
1438 if (INTEL_INFO(dev)->gen >= 4) {
1439 I915_WRITE(DPLL_MD(crtc->pipe),
1440 crtc->config.dpll_hw_state.dpll_md);
1441 } else {
1442 /* The pixel multiplier can only be updated once the
1443 * DPLL is enabled and the clocks are stable.
1444 *
1445 * So write it again.
1446 */
1447 I915_WRITE(reg, dpll);
1448 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001449
1450 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001451 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001452 POSTING_READ(reg);
1453 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001454 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001455 POSTING_READ(reg);
1456 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001457 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001458 POSTING_READ(reg);
1459 udelay(150); /* wait for warmup */
1460}
1461
1462/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001463 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001464 * @dev_priv: i915 private structure
1465 * @pipe: pipe PLL to disable
1466 *
1467 * Disable the PLL for @pipe, making sure the pipe is off first.
1468 *
1469 * Note! This is for pre-ILK only.
1470 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001471static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001472{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001473 /* Don't disable pipe A or pipe A PLLs if needed */
1474 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1475 return;
1476
1477 /* Make sure the pipe isn't still relying on us */
1478 assert_pipe_disabled(dev_priv, pipe);
1479
Daniel Vetter50b44a42013-06-05 13:34:33 +02001480 I915_WRITE(DPLL(pipe), 0);
1481 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001482}
1483
Jesse Barnesf6071162013-10-01 10:41:38 -07001484static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1485{
1486 u32 val = 0;
1487
1488 /* Make sure the pipe isn't still relying on us */
1489 assert_pipe_disabled(dev_priv, pipe);
1490
1491 /* Leave integrated clock source enabled */
1492 if (pipe == PIPE_B)
1493 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1494 I915_WRITE(DPLL(pipe), val);
1495 POSTING_READ(DPLL(pipe));
1496}
1497
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001498void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1499 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001500{
1501 u32 port_mask;
1502
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001503 switch (dport->port) {
1504 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001505 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001506 break;
1507 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001508 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001509 break;
1510 default:
1511 BUG();
1512 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001513
1514 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1515 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001516 'B' + dport->port, I915_READ(DPLL(0)));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001517}
1518
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001519/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001520 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001521 * @dev_priv: i915 private structure
1522 * @pipe: pipe PLL to enable
1523 *
1524 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1525 * drives the transcoder clock.
1526 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001527static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001528{
Daniel Vettere2b78262013-06-07 23:10:03 +02001529 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1530 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001531
Chris Wilson48da64a2012-05-13 20:16:12 +01001532 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001533 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001534 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001535 return;
1536
1537 if (WARN_ON(pll->refcount == 0))
1538 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001539
Daniel Vetter46edb022013-06-05 13:34:12 +02001540 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1541 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001542 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001543
Daniel Vettercdbd2312013-06-05 13:34:03 +02001544 if (pll->active++) {
1545 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001546 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001547 return;
1548 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001549 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001550
Daniel Vetter46edb022013-06-05 13:34:12 +02001551 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001552 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001553 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001554}
1555
Daniel Vettere2b78262013-06-07 23:10:03 +02001556static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001557{
Daniel Vettere2b78262013-06-07 23:10:03 +02001558 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1559 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001560
Jesse Barnes92f25842011-01-04 15:09:34 -08001561 /* PCH only available on ILK+ */
1562 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001563 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001564 return;
1565
Chris Wilson48da64a2012-05-13 20:16:12 +01001566 if (WARN_ON(pll->refcount == 0))
1567 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001568
Daniel Vetter46edb022013-06-05 13:34:12 +02001569 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1570 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001571 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001572
Chris Wilson48da64a2012-05-13 20:16:12 +01001573 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001574 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001575 return;
1576 }
1577
Daniel Vettere9d69442013-06-05 13:34:15 +02001578 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001579 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001580 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001581 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001582
Daniel Vetter46edb022013-06-05 13:34:12 +02001583 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001584 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001585 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001586}
1587
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001588static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1589 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001590{
Daniel Vetter23670b322012-11-01 09:15:30 +01001591 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001592 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001594 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001595
1596 /* PCH only available on ILK+ */
1597 BUG_ON(dev_priv->info->gen < 5);
1598
1599 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001600 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001601 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001602
1603 /* FDI must be feeding us bits for PCH ports */
1604 assert_fdi_tx_enabled(dev_priv, pipe);
1605 assert_fdi_rx_enabled(dev_priv, pipe);
1606
Daniel Vetter23670b322012-11-01 09:15:30 +01001607 if (HAS_PCH_CPT(dev)) {
1608 /* Workaround: Set the timing override bit before enabling the
1609 * pch transcoder. */
1610 reg = TRANS_CHICKEN2(pipe);
1611 val = I915_READ(reg);
1612 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1613 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001614 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001615
Daniel Vetterab9412b2013-05-03 11:49:46 +02001616 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001617 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001618 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001619
1620 if (HAS_PCH_IBX(dev_priv->dev)) {
1621 /*
1622 * make the BPC in transcoder be consistent with
1623 * that in pipeconf reg.
1624 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001625 val &= ~PIPECONF_BPC_MASK;
1626 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001627 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001628
1629 val &= ~TRANS_INTERLACE_MASK;
1630 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001631 if (HAS_PCH_IBX(dev_priv->dev) &&
1632 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1633 val |= TRANS_LEGACY_INTERLACED_ILK;
1634 else
1635 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001636 else
1637 val |= TRANS_PROGRESSIVE;
1638
Jesse Barnes040484a2011-01-03 12:14:26 -08001639 I915_WRITE(reg, val | TRANS_ENABLE);
1640 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001641 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001642}
1643
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001644static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001645 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001646{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001647 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001648
1649 /* PCH only available on ILK+ */
1650 BUG_ON(dev_priv->info->gen < 5);
1651
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001652 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001653 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001654 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001655
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001656 /* Workaround: set timing override bit. */
1657 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001658 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001659 I915_WRITE(_TRANSA_CHICKEN2, val);
1660
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001661 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001662 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001663
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001664 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1665 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001666 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001667 else
1668 val |= TRANS_PROGRESSIVE;
1669
Daniel Vetterab9412b2013-05-03 11:49:46 +02001670 I915_WRITE(LPT_TRANSCONF, val);
1671 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001672 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001673}
1674
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001675static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1676 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001677{
Daniel Vetter23670b322012-11-01 09:15:30 +01001678 struct drm_device *dev = dev_priv->dev;
1679 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001680
1681 /* FDI relies on the transcoder */
1682 assert_fdi_tx_disabled(dev_priv, pipe);
1683 assert_fdi_rx_disabled(dev_priv, pipe);
1684
Jesse Barnes291906f2011-02-02 12:28:03 -08001685 /* Ports must be off as well */
1686 assert_pch_ports_disabled(dev_priv, pipe);
1687
Daniel Vetterab9412b2013-05-03 11:49:46 +02001688 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001689 val = I915_READ(reg);
1690 val &= ~TRANS_ENABLE;
1691 I915_WRITE(reg, val);
1692 /* wait for PCH transcoder off, transcoder state */
1693 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001694 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001695
1696 if (!HAS_PCH_IBX(dev)) {
1697 /* Workaround: Clear the timing override chicken bit again. */
1698 reg = TRANS_CHICKEN2(pipe);
1699 val = I915_READ(reg);
1700 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1701 I915_WRITE(reg, val);
1702 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001703}
1704
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001705static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001706{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001707 u32 val;
1708
Daniel Vetterab9412b2013-05-03 11:49:46 +02001709 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001710 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001711 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001712 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001713 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001714 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001715
1716 /* Workaround: clear timing override bit. */
1717 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001718 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001719 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001720}
1721
1722/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001723 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001724 * @dev_priv: i915 private structure
1725 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001726 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001727 *
1728 * Enable @pipe, making sure that various hardware specific requirements
1729 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1730 *
1731 * @pipe should be %PIPE_A or %PIPE_B.
1732 *
1733 * Will wait until the pipe is actually running (i.e. first vblank) before
1734 * returning.
1735 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001736static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03001737 bool pch_port, bool dsi)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001738{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001739 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1740 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001741 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001742 int reg;
1743 u32 val;
1744
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001745 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001746 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001747 assert_sprites_disabled(dev_priv, pipe);
1748
Paulo Zanoni681e5812012-12-06 11:12:38 -02001749 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001750 pch_transcoder = TRANSCODER_A;
1751 else
1752 pch_transcoder = pipe;
1753
Jesse Barnesb24e7172011-01-04 15:09:30 -08001754 /*
1755 * A pipe without a PLL won't actually be able to drive bits from
1756 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1757 * need the check.
1758 */
1759 if (!HAS_PCH_SPLIT(dev_priv->dev))
Jani Nikula23538ef2013-08-27 15:12:22 +03001760 if (dsi)
1761 assert_dsi_pll_enabled(dev_priv);
1762 else
1763 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001764 else {
1765 if (pch_port) {
1766 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001767 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001768 assert_fdi_tx_pll_enabled(dev_priv,
1769 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001770 }
1771 /* FIXME: assert CPU port conditions for SNB+ */
1772 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001773
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001774 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001775 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001776 if (val & PIPECONF_ENABLE)
1777 return;
1778
1779 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001780 intel_wait_for_vblank(dev_priv->dev, pipe);
1781}
1782
1783/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001784 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785 * @dev_priv: i915 private structure
1786 * @pipe: pipe to disable
1787 *
1788 * Disable @pipe, making sure that various hardware specific requirements
1789 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1790 *
1791 * @pipe should be %PIPE_A or %PIPE_B.
1792 *
1793 * Will wait until the pipe has shut down before returning.
1794 */
1795static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1796 enum pipe pipe)
1797{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001798 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1799 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001800 int reg;
1801 u32 val;
1802
1803 /*
1804 * Make sure planes won't keep trying to pump pixels to us,
1805 * or we might hang the display.
1806 */
1807 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001808 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001809 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810
1811 /* Don't disable pipe A or pipe A PLLs if needed */
1812 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1813 return;
1814
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001815 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001816 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001817 if ((val & PIPECONF_ENABLE) == 0)
1818 return;
1819
1820 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001821 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1822}
1823
Keith Packardd74362c2011-07-28 14:47:14 -07001824/*
1825 * Plane regs are double buffered, going from enabled->disabled needs a
1826 * trigger in order to latch. The display address reg provides this.
1827 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001828void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1829 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001830{
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001831 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1832
1833 I915_WRITE(reg, I915_READ(reg));
1834 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07001835}
1836
Jesse Barnesb24e7172011-01-04 15:09:30 -08001837/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001838 * intel_enable_primary_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001839 * @dev_priv: i915 private structure
1840 * @plane: plane to enable
1841 * @pipe: pipe being fed
1842 *
1843 * Enable @plane on @pipe, making sure that @pipe is running first.
1844 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001845static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1846 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001847{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001848 struct intel_crtc *intel_crtc =
1849 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001850 int reg;
1851 u32 val;
1852
1853 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1854 assert_pipe_enabled(dev_priv, pipe);
1855
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001856 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001857
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001858 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001859
Jesse Barnesb24e7172011-01-04 15:09:30 -08001860 reg = DSPCNTR(plane);
1861 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001862 if (val & DISPLAY_PLANE_ENABLE)
1863 return;
1864
1865 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001866 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001867 intel_wait_for_vblank(dev_priv->dev, pipe);
1868}
1869
Jesse Barnesb24e7172011-01-04 15:09:30 -08001870/**
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001871 * intel_disable_primary_plane - disable the primary plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08001872 * @dev_priv: i915 private structure
1873 * @plane: plane to disable
1874 * @pipe: pipe consuming the data
1875 *
1876 * Disable @plane; should be an independent operation.
1877 */
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03001878static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1879 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001880{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001881 struct intel_crtc *intel_crtc =
1882 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001883 int reg;
1884 u32 val;
1885
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001886 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
Ville Syrjälä0037f712013-10-01 18:02:20 +03001887
Ville Syrjälä4c445e02013-10-09 17:24:58 +03001888 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03001889
Jesse Barnesb24e7172011-01-04 15:09:30 -08001890 reg = DSPCNTR(plane);
1891 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001892 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1893 return;
1894
1895 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001896 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001897 intel_wait_for_vblank(dev_priv->dev, pipe);
1898}
1899
Chris Wilson693db182013-03-05 14:52:39 +00001900static bool need_vtd_wa(struct drm_device *dev)
1901{
1902#ifdef CONFIG_INTEL_IOMMU
1903 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1904 return true;
1905#endif
1906 return false;
1907}
1908
Chris Wilson127bd2a2010-07-23 23:32:05 +01001909int
Chris Wilson48b956c2010-09-14 12:50:34 +01001910intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001911 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001912 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001913{
Chris Wilsonce453d82011-02-21 14:43:56 +00001914 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001915 u32 alignment;
1916 int ret;
1917
Chris Wilson05394f32010-11-08 19:18:58 +00001918 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001919 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001920 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1921 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001922 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001923 alignment = 4 * 1024;
1924 else
1925 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001926 break;
1927 case I915_TILING_X:
1928 /* pin() will align the object as required by fence */
1929 alignment = 0;
1930 break;
1931 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02001932 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001933 return -EINVAL;
1934 default:
1935 BUG();
1936 }
1937
Chris Wilson693db182013-03-05 14:52:39 +00001938 /* Note that the w/a also requires 64 PTE of padding following the
1939 * bo. We currently fill all unused PTE with the shadow page and so
1940 * we should always have valid PTE following the scanout preventing
1941 * the VT-d warning.
1942 */
1943 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1944 alignment = 256 * 1024;
1945
Chris Wilsonce453d82011-02-21 14:43:56 +00001946 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001947 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001948 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001949 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001950
1951 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1952 * fence, whereas 965+ only requires a fence if using
1953 * framebuffer compression. For simplicity, we always install
1954 * a fence as the cost is not that onerous.
1955 */
Chris Wilson06d98132012-04-17 15:31:24 +01001956 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001957 if (ret)
1958 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001959
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001960 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001961
Chris Wilsonce453d82011-02-21 14:43:56 +00001962 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001963 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001964
1965err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01001966 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001967err_interruptible:
1968 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001969 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001970}
1971
Chris Wilson1690e1e2011-12-14 13:57:08 +01001972void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1973{
1974 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01001975 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001976}
1977
Daniel Vetterc2c75132012-07-05 12:17:30 +02001978/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1979 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001980unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1981 unsigned int tiling_mode,
1982 unsigned int cpp,
1983 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001984{
Chris Wilsonbc752862013-02-21 20:04:31 +00001985 if (tiling_mode != I915_TILING_NONE) {
1986 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001987
Chris Wilsonbc752862013-02-21 20:04:31 +00001988 tile_rows = *y / 8;
1989 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001990
Chris Wilsonbc752862013-02-21 20:04:31 +00001991 tiles = *x / (512/cpp);
1992 *x %= 512/cpp;
1993
1994 return tile_rows * pitch * 8 + tiles * 4096;
1995 } else {
1996 unsigned int offset;
1997
1998 offset = *y * pitch + *x * cpp;
1999 *y = 0;
2000 *x = (offset & 4095) / cpp;
2001 return offset & -4096;
2002 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002003}
2004
Jesse Barnes17638cd2011-06-24 12:19:23 -07002005static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2006 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002007{
2008 struct drm_device *dev = crtc->dev;
2009 struct drm_i915_private *dev_priv = dev->dev_private;
2010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2011 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002012 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002013 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002014 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002015 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002016 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002017
2018 switch (plane) {
2019 case 0:
2020 case 1:
2021 break;
2022 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002023 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002024 return -EINVAL;
2025 }
2026
2027 intel_fb = to_intel_framebuffer(fb);
2028 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002029
Chris Wilson5eddb702010-09-11 13:48:45 +01002030 reg = DSPCNTR(plane);
2031 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002032 /* Mask out pixel format bits in case we change it */
2033 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002034 switch (fb->pixel_format) {
2035 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002036 dspcntr |= DISPPLANE_8BPP;
2037 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002038 case DRM_FORMAT_XRGB1555:
2039 case DRM_FORMAT_ARGB1555:
2040 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002041 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002042 case DRM_FORMAT_RGB565:
2043 dspcntr |= DISPPLANE_BGRX565;
2044 break;
2045 case DRM_FORMAT_XRGB8888:
2046 case DRM_FORMAT_ARGB8888:
2047 dspcntr |= DISPPLANE_BGRX888;
2048 break;
2049 case DRM_FORMAT_XBGR8888:
2050 case DRM_FORMAT_ABGR8888:
2051 dspcntr |= DISPPLANE_RGBX888;
2052 break;
2053 case DRM_FORMAT_XRGB2101010:
2054 case DRM_FORMAT_ARGB2101010:
2055 dspcntr |= DISPPLANE_BGRX101010;
2056 break;
2057 case DRM_FORMAT_XBGR2101010:
2058 case DRM_FORMAT_ABGR2101010:
2059 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002060 break;
2061 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002062 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002063 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002064
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002065 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002066 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002067 dspcntr |= DISPPLANE_TILED;
2068 else
2069 dspcntr &= ~DISPPLANE_TILED;
2070 }
2071
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002072 if (IS_G4X(dev))
2073 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2074
Chris Wilson5eddb702010-09-11 13:48:45 +01002075 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002076
Daniel Vettere506a0c2012-07-05 12:17:29 +02002077 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002078
Daniel Vetterc2c75132012-07-05 12:17:30 +02002079 if (INTEL_INFO(dev)->gen >= 4) {
2080 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002081 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2082 fb->bits_per_pixel / 8,
2083 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002084 linear_offset -= intel_crtc->dspaddr_offset;
2085 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002086 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002087 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002088
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002089 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2090 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2091 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002092 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002093 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002094 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002095 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002096 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002097 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002098 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002099 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002100 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002101
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 return 0;
2103}
2104
2105static int ironlake_update_plane(struct drm_crtc *crtc,
2106 struct drm_framebuffer *fb, int x, int y)
2107{
2108 struct drm_device *dev = crtc->dev;
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2111 struct intel_framebuffer *intel_fb;
2112 struct drm_i915_gem_object *obj;
2113 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002114 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002115 u32 dspcntr;
2116 u32 reg;
2117
2118 switch (plane) {
2119 case 0:
2120 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002121 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002122 break;
2123 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002124 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002125 return -EINVAL;
2126 }
2127
2128 intel_fb = to_intel_framebuffer(fb);
2129 obj = intel_fb->obj;
2130
2131 reg = DSPCNTR(plane);
2132 dspcntr = I915_READ(reg);
2133 /* Mask out pixel format bits in case we change it */
2134 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002135 switch (fb->pixel_format) {
2136 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002137 dspcntr |= DISPPLANE_8BPP;
2138 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002139 case DRM_FORMAT_RGB565:
2140 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002142 case DRM_FORMAT_XRGB8888:
2143 case DRM_FORMAT_ARGB8888:
2144 dspcntr |= DISPPLANE_BGRX888;
2145 break;
2146 case DRM_FORMAT_XBGR8888:
2147 case DRM_FORMAT_ABGR8888:
2148 dspcntr |= DISPPLANE_RGBX888;
2149 break;
2150 case DRM_FORMAT_XRGB2101010:
2151 case DRM_FORMAT_ARGB2101010:
2152 dspcntr |= DISPPLANE_BGRX101010;
2153 break;
2154 case DRM_FORMAT_XBGR2101010:
2155 case DRM_FORMAT_ABGR2101010:
2156 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002157 break;
2158 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002159 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002160 }
2161
2162 if (obj->tiling_mode != I915_TILING_NONE)
2163 dspcntr |= DISPPLANE_TILED;
2164 else
2165 dspcntr &= ~DISPPLANE_TILED;
2166
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002167 if (IS_HASWELL(dev))
2168 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2169 else
2170 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002171
2172 I915_WRITE(reg, dspcntr);
2173
Daniel Vettere506a0c2012-07-05 12:17:29 +02002174 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002175 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002176 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2177 fb->bits_per_pixel / 8,
2178 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002179 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002180
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002181 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2182 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2183 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002184 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002185 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002186 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002187 if (IS_HASWELL(dev)) {
2188 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2189 } else {
2190 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2191 I915_WRITE(DSPLINOFF(plane), linear_offset);
2192 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002193 POSTING_READ(reg);
2194
2195 return 0;
2196}
2197
2198/* Assume fb object is pinned & idle & fenced and just update base pointers */
2199static int
2200intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2201 int x, int y, enum mode_set_atomic state)
2202{
2203 struct drm_device *dev = crtc->dev;
2204 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002205
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002206 if (dev_priv->display.disable_fbc)
2207 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002208 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002209
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002210 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002211}
2212
Ville Syrjälä96a02912013-02-18 19:08:49 +02002213void intel_display_handle_reset(struct drm_device *dev)
2214{
2215 struct drm_i915_private *dev_priv = dev->dev_private;
2216 struct drm_crtc *crtc;
2217
2218 /*
2219 * Flips in the rings have been nuked by the reset,
2220 * so complete all pending flips so that user space
2221 * will get its events and not get stuck.
2222 *
2223 * Also update the base address of all primary
2224 * planes to the the last fb to make sure we're
2225 * showing the correct fb after a reset.
2226 *
2227 * Need to make two loops over the crtcs so that we
2228 * don't try to grab a crtc mutex before the
2229 * pending_flip_queue really got woken up.
2230 */
2231
2232 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2234 enum plane plane = intel_crtc->plane;
2235
2236 intel_prepare_page_flip(dev, plane);
2237 intel_finish_page_flip_plane(dev, plane);
2238 }
2239
2240 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2242
2243 mutex_lock(&crtc->mutex);
2244 if (intel_crtc->active)
2245 dev_priv->display.update_plane(crtc, crtc->fb,
2246 crtc->x, crtc->y);
2247 mutex_unlock(&crtc->mutex);
2248 }
2249}
2250
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002251static int
Chris Wilson14667a42012-04-03 17:58:35 +01002252intel_finish_fb(struct drm_framebuffer *old_fb)
2253{
2254 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2255 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2256 bool was_interruptible = dev_priv->mm.interruptible;
2257 int ret;
2258
Chris Wilson14667a42012-04-03 17:58:35 +01002259 /* Big Hammer, we also need to ensure that any pending
2260 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2261 * current scanout is retired before unpinning the old
2262 * framebuffer.
2263 *
2264 * This should only fail upon a hung GPU, in which case we
2265 * can safely continue.
2266 */
2267 dev_priv->mm.interruptible = false;
2268 ret = i915_gem_object_finish_gpu(obj);
2269 dev_priv->mm.interruptible = was_interruptible;
2270
2271 return ret;
2272}
2273
Ville Syrjälä198598d2012-10-31 17:50:24 +02002274static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2275{
2276 struct drm_device *dev = crtc->dev;
2277 struct drm_i915_master_private *master_priv;
2278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2279
2280 if (!dev->primary->master)
2281 return;
2282
2283 master_priv = dev->primary->master->driver_priv;
2284 if (!master_priv->sarea_priv)
2285 return;
2286
2287 switch (intel_crtc->pipe) {
2288 case 0:
2289 master_priv->sarea_priv->pipeA_x = x;
2290 master_priv->sarea_priv->pipeA_y = y;
2291 break;
2292 case 1:
2293 master_priv->sarea_priv->pipeB_x = x;
2294 master_priv->sarea_priv->pipeB_y = y;
2295 break;
2296 default:
2297 break;
2298 }
2299}
2300
Chris Wilson14667a42012-04-03 17:58:35 +01002301static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002302intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002303 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002304{
2305 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002306 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002308 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002309 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002310
2311 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002312 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002313 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002314 return 0;
2315 }
2316
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002317 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002318 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2319 plane_name(intel_crtc->plane),
2320 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002321 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002322 }
2323
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002324 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002325 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002326 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002327 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002328 if (ret != 0) {
2329 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002330 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002331 return ret;
2332 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002333
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002334 /*
2335 * Update pipe size and adjust fitter if needed: the reason for this is
2336 * that in compute_mode_changes we check the native mode (not the pfit
2337 * mode) to see if we can flip rather than do a full mode set. In the
2338 * fastboot case, we'll flip, but if we don't update the pipesrc and
2339 * pfit state, we'll end up with a big fb scanned out into the wrong
2340 * sized surface.
2341 *
2342 * To fix this properly, we need to hoist the checks up into
2343 * compute_mode_changes (or above), check the actual pfit state and
2344 * whether the platform allows pfit disable with pipe active, and only
2345 * then update the pipesrc and pfit state, even on the flip path.
2346 */
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002347 if (i915_fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002348 const struct drm_display_mode *adjusted_mode =
2349 &intel_crtc->config.adjusted_mode;
2350
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002351 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002352 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2353 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002354 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002355 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2356 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2357 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2358 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2359 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2360 }
2361 }
2362
Daniel Vetter94352cf2012-07-05 22:51:56 +02002363 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002364 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002365 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002366 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002367 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002368 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002369 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002370
Daniel Vetter94352cf2012-07-05 22:51:56 +02002371 old_fb = crtc->fb;
2372 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002373 crtc->x = x;
2374 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002375
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002376 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002377 if (intel_crtc->active && old_fb != fb)
2378 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002379 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002380 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002381
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002382 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002383 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002384 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002385
Ville Syrjälä198598d2012-10-31 17:50:24 +02002386 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002387
2388 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002389}
2390
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002391static void intel_fdi_normal_train(struct drm_crtc *crtc)
2392{
2393 struct drm_device *dev = crtc->dev;
2394 struct drm_i915_private *dev_priv = dev->dev_private;
2395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2396 int pipe = intel_crtc->pipe;
2397 u32 reg, temp;
2398
2399 /* enable normal train */
2400 reg = FDI_TX_CTL(pipe);
2401 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002402 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002403 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2404 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002405 } else {
2406 temp &= ~FDI_LINK_TRAIN_NONE;
2407 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002408 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002409 I915_WRITE(reg, temp);
2410
2411 reg = FDI_RX_CTL(pipe);
2412 temp = I915_READ(reg);
2413 if (HAS_PCH_CPT(dev)) {
2414 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2415 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2416 } else {
2417 temp &= ~FDI_LINK_TRAIN_NONE;
2418 temp |= FDI_LINK_TRAIN_NONE;
2419 }
2420 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2421
2422 /* wait one idle pattern time */
2423 POSTING_READ(reg);
2424 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002425
2426 /* IVB wants error correction enabled */
2427 if (IS_IVYBRIDGE(dev))
2428 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2429 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002430}
2431
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002432static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002433{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002434 return crtc->base.enabled && crtc->active &&
2435 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002436}
2437
Daniel Vetter01a415f2012-10-27 15:58:40 +02002438static void ivb_modeset_global_resources(struct drm_device *dev)
2439{
2440 struct drm_i915_private *dev_priv = dev->dev_private;
2441 struct intel_crtc *pipe_B_crtc =
2442 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2443 struct intel_crtc *pipe_C_crtc =
2444 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2445 uint32_t temp;
2446
Daniel Vetter1e833f42013-02-19 22:31:57 +01002447 /*
2448 * When everything is off disable fdi C so that we could enable fdi B
2449 * with all lanes. Note that we don't care about enabled pipes without
2450 * an enabled pch encoder.
2451 */
2452 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2453 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002454 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2455 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2456
2457 temp = I915_READ(SOUTH_CHICKEN1);
2458 temp &= ~FDI_BC_BIFURCATION_SELECT;
2459 DRM_DEBUG_KMS("disabling fdi C rx\n");
2460 I915_WRITE(SOUTH_CHICKEN1, temp);
2461 }
2462}
2463
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002464/* The FDI link training functions for ILK/Ibexpeak. */
2465static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2466{
2467 struct drm_device *dev = crtc->dev;
2468 struct drm_i915_private *dev_priv = dev->dev_private;
2469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2470 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002471 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002472 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002473
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002474 /* FDI needs bits from pipe & plane first */
2475 assert_pipe_enabled(dev_priv, pipe);
2476 assert_plane_enabled(dev_priv, plane);
2477
Adam Jacksone1a44742010-06-25 15:32:14 -04002478 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2479 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 reg = FDI_RX_IMR(pipe);
2481 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002482 temp &= ~FDI_RX_SYMBOL_LOCK;
2483 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002484 I915_WRITE(reg, temp);
2485 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002486 udelay(150);
2487
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002489 reg = FDI_TX_CTL(pipe);
2490 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002491 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2492 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 temp &= ~FDI_LINK_TRAIN_NONE;
2494 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002495 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002496
Chris Wilson5eddb702010-09-11 13:48:45 +01002497 reg = FDI_RX_CTL(pipe);
2498 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002499 temp &= ~FDI_LINK_TRAIN_NONE;
2500 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002501 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2502
2503 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504 udelay(150);
2505
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002506 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002507 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2508 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2509 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002510
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002512 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002513 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002514 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2515
2516 if ((temp & FDI_RX_BIT_LOCK)) {
2517 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002518 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519 break;
2520 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002521 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002522 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002523 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002524
2525 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002526 reg = FDI_TX_CTL(pipe);
2527 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528 temp &= ~FDI_LINK_TRAIN_NONE;
2529 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002530 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 reg = FDI_RX_CTL(pipe);
2533 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002534 temp &= ~FDI_LINK_TRAIN_NONE;
2535 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002536 I915_WRITE(reg, temp);
2537
2538 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539 udelay(150);
2540
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002542 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002543 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2545
2546 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002547 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548 DRM_DEBUG_KMS("FDI train 2 done.\n");
2549 break;
2550 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002551 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002552 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002553 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002554
2555 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002556
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557}
2558
Akshay Joshi0206e352011-08-16 15:34:10 -04002559static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002560 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2561 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2562 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2563 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2564};
2565
2566/* The FDI link training functions for SNB/Cougarpoint. */
2567static void gen6_fdi_link_train(struct drm_crtc *crtc)
2568{
2569 struct drm_device *dev = crtc->dev;
2570 struct drm_i915_private *dev_priv = dev->dev_private;
2571 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2572 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002573 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574
Adam Jacksone1a44742010-06-25 15:32:14 -04002575 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2576 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002577 reg = FDI_RX_IMR(pipe);
2578 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002579 temp &= ~FDI_RX_SYMBOL_LOCK;
2580 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002581 I915_WRITE(reg, temp);
2582
2583 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002584 udelay(150);
2585
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002586 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002587 reg = FDI_TX_CTL(pipe);
2588 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002589 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2590 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002591 temp &= ~FDI_LINK_TRAIN_NONE;
2592 temp |= FDI_LINK_TRAIN_PATTERN_1;
2593 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2594 /* SNB-B */
2595 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597
Daniel Vetterd74cf322012-10-26 10:58:13 +02002598 I915_WRITE(FDI_RX_MISC(pipe),
2599 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2600
Chris Wilson5eddb702010-09-11 13:48:45 +01002601 reg = FDI_RX_CTL(pipe);
2602 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002603 if (HAS_PCH_CPT(dev)) {
2604 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2605 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2606 } else {
2607 temp &= ~FDI_LINK_TRAIN_NONE;
2608 temp |= FDI_LINK_TRAIN_PATTERN_1;
2609 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002610 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2611
2612 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002613 udelay(150);
2614
Akshay Joshi0206e352011-08-16 15:34:10 -04002615 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 reg = FDI_TX_CTL(pipe);
2617 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002618 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2619 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002620 I915_WRITE(reg, temp);
2621
2622 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002623 udelay(500);
2624
Sean Paulfa37d392012-03-02 12:53:39 -05002625 for (retry = 0; retry < 5; retry++) {
2626 reg = FDI_RX_IIR(pipe);
2627 temp = I915_READ(reg);
2628 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2629 if (temp & FDI_RX_BIT_LOCK) {
2630 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2631 DRM_DEBUG_KMS("FDI train 1 done.\n");
2632 break;
2633 }
2634 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002635 }
Sean Paulfa37d392012-03-02 12:53:39 -05002636 if (retry < 5)
2637 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002638 }
2639 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002640 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002641
2642 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002643 reg = FDI_TX_CTL(pipe);
2644 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002645 temp &= ~FDI_LINK_TRAIN_NONE;
2646 temp |= FDI_LINK_TRAIN_PATTERN_2;
2647 if (IS_GEN6(dev)) {
2648 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2649 /* SNB-B */
2650 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2651 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002652 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002653
Chris Wilson5eddb702010-09-11 13:48:45 +01002654 reg = FDI_RX_CTL(pipe);
2655 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002656 if (HAS_PCH_CPT(dev)) {
2657 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2658 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2659 } else {
2660 temp &= ~FDI_LINK_TRAIN_NONE;
2661 temp |= FDI_LINK_TRAIN_PATTERN_2;
2662 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002663 I915_WRITE(reg, temp);
2664
2665 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002666 udelay(150);
2667
Akshay Joshi0206e352011-08-16 15:34:10 -04002668 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002669 reg = FDI_TX_CTL(pipe);
2670 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002671 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2672 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002673 I915_WRITE(reg, temp);
2674
2675 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002676 udelay(500);
2677
Sean Paulfa37d392012-03-02 12:53:39 -05002678 for (retry = 0; retry < 5; retry++) {
2679 reg = FDI_RX_IIR(pipe);
2680 temp = I915_READ(reg);
2681 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2682 if (temp & FDI_RX_SYMBOL_LOCK) {
2683 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2684 DRM_DEBUG_KMS("FDI train 2 done.\n");
2685 break;
2686 }
2687 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002688 }
Sean Paulfa37d392012-03-02 12:53:39 -05002689 if (retry < 5)
2690 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002691 }
2692 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002693 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002694
2695 DRM_DEBUG_KMS("FDI train done.\n");
2696}
2697
Jesse Barnes357555c2011-04-28 15:09:55 -07002698/* Manual link training for Ivy Bridge A0 parts */
2699static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2700{
2701 struct drm_device *dev = crtc->dev;
2702 struct drm_i915_private *dev_priv = dev->dev_private;
2703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2704 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002705 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002706
2707 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2708 for train result */
2709 reg = FDI_RX_IMR(pipe);
2710 temp = I915_READ(reg);
2711 temp &= ~FDI_RX_SYMBOL_LOCK;
2712 temp &= ~FDI_RX_BIT_LOCK;
2713 I915_WRITE(reg, temp);
2714
2715 POSTING_READ(reg);
2716 udelay(150);
2717
Daniel Vetter01a415f2012-10-27 15:58:40 +02002718 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2719 I915_READ(FDI_RX_IIR(pipe)));
2720
Jesse Barnes139ccd32013-08-19 11:04:55 -07002721 /* Try each vswing and preemphasis setting twice before moving on */
2722 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2723 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002724 reg = FDI_TX_CTL(pipe);
2725 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002726 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2727 temp &= ~FDI_TX_ENABLE;
2728 I915_WRITE(reg, temp);
2729
2730 reg = FDI_RX_CTL(pipe);
2731 temp = I915_READ(reg);
2732 temp &= ~FDI_LINK_TRAIN_AUTO;
2733 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2734 temp &= ~FDI_RX_ENABLE;
2735 I915_WRITE(reg, temp);
2736
2737 /* enable CPU FDI TX and PCH FDI RX */
2738 reg = FDI_TX_CTL(pipe);
2739 temp = I915_READ(reg);
2740 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2741 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2742 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07002743 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002744 temp |= snb_b_fdi_train_param[j/2];
2745 temp |= FDI_COMPOSITE_SYNC;
2746 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2747
2748 I915_WRITE(FDI_RX_MISC(pipe),
2749 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2750
2751 reg = FDI_RX_CTL(pipe);
2752 temp = I915_READ(reg);
2753 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2754 temp |= FDI_COMPOSITE_SYNC;
2755 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2756
2757 POSTING_READ(reg);
2758 udelay(1); /* should be 0.5us */
2759
2760 for (i = 0; i < 4; i++) {
2761 reg = FDI_RX_IIR(pipe);
2762 temp = I915_READ(reg);
2763 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2764
2765 if (temp & FDI_RX_BIT_LOCK ||
2766 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2767 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2768 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2769 i);
2770 break;
2771 }
2772 udelay(1); /* should be 0.5us */
2773 }
2774 if (i == 4) {
2775 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2776 continue;
2777 }
2778
2779 /* Train 2 */
2780 reg = FDI_TX_CTL(pipe);
2781 temp = I915_READ(reg);
2782 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2783 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2784 I915_WRITE(reg, temp);
2785
2786 reg = FDI_RX_CTL(pipe);
2787 temp = I915_READ(reg);
2788 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2789 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07002790 I915_WRITE(reg, temp);
2791
2792 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002793 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002794
Jesse Barnes139ccd32013-08-19 11:04:55 -07002795 for (i = 0; i < 4; i++) {
2796 reg = FDI_RX_IIR(pipe);
2797 temp = I915_READ(reg);
2798 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07002799
Jesse Barnes139ccd32013-08-19 11:04:55 -07002800 if (temp & FDI_RX_SYMBOL_LOCK ||
2801 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2802 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2803 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2804 i);
2805 goto train_done;
2806 }
2807 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07002808 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07002809 if (i == 4)
2810 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07002811 }
Jesse Barnes357555c2011-04-28 15:09:55 -07002812
Jesse Barnes139ccd32013-08-19 11:04:55 -07002813train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07002814 DRM_DEBUG_KMS("FDI train done.\n");
2815}
2816
Daniel Vetter88cefb62012-08-12 19:27:14 +02002817static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002818{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002819 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002820 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002821 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002822 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002823
Jesse Barnesc64e3112010-09-10 11:27:03 -07002824
Jesse Barnes0e23b992010-09-10 11:10:00 -07002825 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002826 reg = FDI_RX_CTL(pipe);
2827 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002828 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2829 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002830 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002831 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2832
2833 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002834 udelay(200);
2835
2836 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002837 temp = I915_READ(reg);
2838 I915_WRITE(reg, temp | FDI_PCDCLK);
2839
2840 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002841 udelay(200);
2842
Paulo Zanoni20749732012-11-23 15:30:38 -02002843 /* Enable CPU FDI TX PLL, always on for Ironlake */
2844 reg = FDI_TX_CTL(pipe);
2845 temp = I915_READ(reg);
2846 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2847 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002848
Paulo Zanoni20749732012-11-23 15:30:38 -02002849 POSTING_READ(reg);
2850 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002851 }
2852}
2853
Daniel Vetter88cefb62012-08-12 19:27:14 +02002854static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2855{
2856 struct drm_device *dev = intel_crtc->base.dev;
2857 struct drm_i915_private *dev_priv = dev->dev_private;
2858 int pipe = intel_crtc->pipe;
2859 u32 reg, temp;
2860
2861 /* Switch from PCDclk to Rawclk */
2862 reg = FDI_RX_CTL(pipe);
2863 temp = I915_READ(reg);
2864 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2865
2866 /* Disable CPU FDI TX PLL */
2867 reg = FDI_TX_CTL(pipe);
2868 temp = I915_READ(reg);
2869 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2870
2871 POSTING_READ(reg);
2872 udelay(100);
2873
2874 reg = FDI_RX_CTL(pipe);
2875 temp = I915_READ(reg);
2876 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2877
2878 /* Wait for the clocks to turn off. */
2879 POSTING_READ(reg);
2880 udelay(100);
2881}
2882
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002883static void ironlake_fdi_disable(struct drm_crtc *crtc)
2884{
2885 struct drm_device *dev = crtc->dev;
2886 struct drm_i915_private *dev_priv = dev->dev_private;
2887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2888 int pipe = intel_crtc->pipe;
2889 u32 reg, temp;
2890
2891 /* disable CPU FDI tx and PCH FDI rx */
2892 reg = FDI_TX_CTL(pipe);
2893 temp = I915_READ(reg);
2894 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2895 POSTING_READ(reg);
2896
2897 reg = FDI_RX_CTL(pipe);
2898 temp = I915_READ(reg);
2899 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002900 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002901 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2902
2903 POSTING_READ(reg);
2904 udelay(100);
2905
2906 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002907 if (HAS_PCH_IBX(dev)) {
2908 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002909 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002910
2911 /* still set train pattern 1 */
2912 reg = FDI_TX_CTL(pipe);
2913 temp = I915_READ(reg);
2914 temp &= ~FDI_LINK_TRAIN_NONE;
2915 temp |= FDI_LINK_TRAIN_PATTERN_1;
2916 I915_WRITE(reg, temp);
2917
2918 reg = FDI_RX_CTL(pipe);
2919 temp = I915_READ(reg);
2920 if (HAS_PCH_CPT(dev)) {
2921 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2922 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2923 } else {
2924 temp &= ~FDI_LINK_TRAIN_NONE;
2925 temp |= FDI_LINK_TRAIN_PATTERN_1;
2926 }
2927 /* BPC in FDI rx is consistent with that in PIPECONF */
2928 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002929 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002930 I915_WRITE(reg, temp);
2931
2932 POSTING_READ(reg);
2933 udelay(100);
2934}
2935
Chris Wilson5bb61642012-09-27 21:25:58 +01002936static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2937{
2938 struct drm_device *dev = crtc->dev;
2939 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002941 unsigned long flags;
2942 bool pending;
2943
Ville Syrjälä10d83732013-01-29 18:13:34 +02002944 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2945 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002946 return false;
2947
2948 spin_lock_irqsave(&dev->event_lock, flags);
2949 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2950 spin_unlock_irqrestore(&dev->event_lock, flags);
2951
2952 return pending;
2953}
2954
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002955static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2956{
Chris Wilson0f911282012-04-17 10:05:38 +01002957 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002958 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002959
2960 if (crtc->fb == NULL)
2961 return;
2962
Daniel Vetter2c10d572012-12-20 21:24:07 +01002963 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2964
Chris Wilson5bb61642012-09-27 21:25:58 +01002965 wait_event(dev_priv->pending_flip_queue,
2966 !intel_crtc_has_pending_flip(crtc));
2967
Chris Wilson0f911282012-04-17 10:05:38 +01002968 mutex_lock(&dev->struct_mutex);
2969 intel_finish_fb(crtc->fb);
2970 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002971}
2972
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002973/* Program iCLKIP clock to the desired frequency */
2974static void lpt_program_iclkip(struct drm_crtc *crtc)
2975{
2976 struct drm_device *dev = crtc->dev;
2977 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002978 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002979 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2980 u32 temp;
2981
Daniel Vetter09153002012-12-12 14:06:44 +01002982 mutex_lock(&dev_priv->dpio_lock);
2983
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002984 /* It is necessary to ungate the pixclk gate prior to programming
2985 * the divisors, and gate it back when it is done.
2986 */
2987 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2988
2989 /* Disable SSCCTL */
2990 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002991 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2992 SBI_SSCCTL_DISABLE,
2993 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002994
2995 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03002996 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002997 auxdiv = 1;
2998 divsel = 0x41;
2999 phaseinc = 0x20;
3000 } else {
3001 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003002 * but the adjusted_mode->crtc_clock in in KHz. To get the
3003 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003004 * convert the virtual clock precision to KHz here for higher
3005 * precision.
3006 */
3007 u32 iclk_virtual_root_freq = 172800 * 1000;
3008 u32 iclk_pi_range = 64;
3009 u32 desired_divisor, msb_divisor_value, pi_value;
3010
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003011 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003012 msb_divisor_value = desired_divisor / iclk_pi_range;
3013 pi_value = desired_divisor % iclk_pi_range;
3014
3015 auxdiv = 0;
3016 divsel = msb_divisor_value - 2;
3017 phaseinc = pi_value;
3018 }
3019
3020 /* This should not happen with any sane values */
3021 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3022 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3023 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3024 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3025
3026 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 +03003027 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003028 auxdiv,
3029 divsel,
3030 phasedir,
3031 phaseinc);
3032
3033 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003034 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003035 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3036 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3037 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3038 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3039 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3040 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003041 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003042
3043 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003044 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003045 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3046 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003047 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003048
3049 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003050 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003051 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003052 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003053
3054 /* Wait for initialization time */
3055 udelay(24);
3056
3057 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003058
3059 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003060}
3061
Daniel Vetter275f01b22013-05-03 11:49:47 +02003062static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3063 enum pipe pch_transcoder)
3064{
3065 struct drm_device *dev = crtc->base.dev;
3066 struct drm_i915_private *dev_priv = dev->dev_private;
3067 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3068
3069 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3070 I915_READ(HTOTAL(cpu_transcoder)));
3071 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3072 I915_READ(HBLANK(cpu_transcoder)));
3073 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3074 I915_READ(HSYNC(cpu_transcoder)));
3075
3076 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3077 I915_READ(VTOTAL(cpu_transcoder)));
3078 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3079 I915_READ(VBLANK(cpu_transcoder)));
3080 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3081 I915_READ(VSYNC(cpu_transcoder)));
3082 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3083 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3084}
3085
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003086static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3087{
3088 struct drm_i915_private *dev_priv = dev->dev_private;
3089 uint32_t temp;
3090
3091 temp = I915_READ(SOUTH_CHICKEN1);
3092 if (temp & FDI_BC_BIFURCATION_SELECT)
3093 return;
3094
3095 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3096 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3097
3098 temp |= FDI_BC_BIFURCATION_SELECT;
3099 DRM_DEBUG_KMS("enabling fdi C rx\n");
3100 I915_WRITE(SOUTH_CHICKEN1, temp);
3101 POSTING_READ(SOUTH_CHICKEN1);
3102}
3103
3104static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3105{
3106 struct drm_device *dev = intel_crtc->base.dev;
3107 struct drm_i915_private *dev_priv = dev->dev_private;
3108
3109 switch (intel_crtc->pipe) {
3110 case PIPE_A:
3111 break;
3112 case PIPE_B:
3113 if (intel_crtc->config.fdi_lanes > 2)
3114 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3115 else
3116 cpt_enable_fdi_bc_bifurcation(dev);
3117
3118 break;
3119 case PIPE_C:
3120 cpt_enable_fdi_bc_bifurcation(dev);
3121
3122 break;
3123 default:
3124 BUG();
3125 }
3126}
3127
Jesse Barnesf67a5592011-01-05 10:31:48 -08003128/*
3129 * Enable PCH resources required for PCH ports:
3130 * - PCH PLLs
3131 * - FDI training & RX/TX
3132 * - update transcoder timings
3133 * - DP transcoding bits
3134 * - transcoder
3135 */
3136static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003137{
3138 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003139 struct drm_i915_private *dev_priv = dev->dev_private;
3140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3141 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003142 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003143
Daniel Vetterab9412b2013-05-03 11:49:46 +02003144 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003145
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003146 if (IS_IVYBRIDGE(dev))
3147 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3148
Daniel Vettercd986ab2012-10-26 10:58:12 +02003149 /* Write the TU size bits before fdi link training, so that error
3150 * detection works. */
3151 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3152 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3153
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003154 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003155 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003156
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003157 /* We need to program the right clock selection before writing the pixel
3158 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003159 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003160 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003161
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003162 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003163 temp |= TRANS_DPLL_ENABLE(pipe);
3164 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003165 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003166 temp |= sel;
3167 else
3168 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003169 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003170 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003171
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003172 /* XXX: pch pll's can be enabled any time before we enable the PCH
3173 * transcoder, and we actually should do this to not upset any PCH
3174 * transcoder that already use the clock when we share it.
3175 *
3176 * Note that enable_shared_dpll tries to do the right thing, but
3177 * get_shared_dpll unconditionally resets the pll - we need that to have
3178 * the right LVDS enable sequence. */
3179 ironlake_enable_shared_dpll(intel_crtc);
3180
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003181 /* set transcoder timing, panel must allow it */
3182 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003183 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003184
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003185 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003186
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003187 /* For PCH DP, enable TRANS_DP_CTL */
3188 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003189 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3190 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003191 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003192 reg = TRANS_DP_CTL(pipe);
3193 temp = I915_READ(reg);
3194 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003195 TRANS_DP_SYNC_MASK |
3196 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003197 temp |= (TRANS_DP_OUTPUT_ENABLE |
3198 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003199 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003200
3201 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003202 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003203 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003204 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003205
3206 switch (intel_trans_dp_port_sel(crtc)) {
3207 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003208 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003209 break;
3210 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003211 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003212 break;
3213 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003214 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003215 break;
3216 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003217 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003218 }
3219
Chris Wilson5eddb702010-09-11 13:48:45 +01003220 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003221 }
3222
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003223 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003224}
3225
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003226static void lpt_pch_enable(struct drm_crtc *crtc)
3227{
3228 struct drm_device *dev = crtc->dev;
3229 struct drm_i915_private *dev_priv = dev->dev_private;
3230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003231 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003232
Daniel Vetterab9412b2013-05-03 11:49:46 +02003233 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003234
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003235 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003236
Paulo Zanoni0540e482012-10-31 18:12:40 -02003237 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003238 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003239
Paulo Zanoni937bb612012-10-31 18:12:47 -02003240 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003241}
3242
Daniel Vettere2b78262013-06-07 23:10:03 +02003243static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003244{
Daniel Vettere2b78262013-06-07 23:10:03 +02003245 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003246
3247 if (pll == NULL)
3248 return;
3249
3250 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003251 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003252 return;
3253 }
3254
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003255 if (--pll->refcount == 0) {
3256 WARN_ON(pll->on);
3257 WARN_ON(pll->active);
3258 }
3259
Daniel Vettera43f6e02013-06-07 23:10:32 +02003260 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003261}
3262
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003263static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003264{
Daniel Vettere2b78262013-06-07 23:10:03 +02003265 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3266 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3267 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003268
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003269 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003270 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3271 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003272 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003273 }
3274
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003275 if (HAS_PCH_IBX(dev_priv->dev)) {
3276 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003277 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003278 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003279
Daniel Vetter46edb022013-06-05 13:34:12 +02003280 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3281 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003282
3283 goto found;
3284 }
3285
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003286 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3287 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003288
3289 /* Only want to check enabled timings first */
3290 if (pll->refcount == 0)
3291 continue;
3292
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003293 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3294 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003295 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003296 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003297 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003298
3299 goto found;
3300 }
3301 }
3302
3303 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003304 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3305 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003306 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003307 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3308 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003309 goto found;
3310 }
3311 }
3312
3313 return NULL;
3314
3315found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003316 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003317 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3318 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003319
Daniel Vettercdbd2312013-06-05 13:34:03 +02003320 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003321 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3322 sizeof(pll->hw_state));
3323
Daniel Vetter46edb022013-06-05 13:34:12 +02003324 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003325 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003326 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003327
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003328 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003329 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003330 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003331
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003332 return pll;
3333}
3334
Daniel Vettera1520312013-05-03 11:49:50 +02003335static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003336{
3337 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003338 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003339 u32 temp;
3340
3341 temp = I915_READ(dslreg);
3342 udelay(500);
3343 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003344 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003345 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003346 }
3347}
3348
Jesse Barnesb074cec2013-04-25 12:55:02 -07003349static void ironlake_pfit_enable(struct intel_crtc *crtc)
3350{
3351 struct drm_device *dev = crtc->base.dev;
3352 struct drm_i915_private *dev_priv = dev->dev_private;
3353 int pipe = crtc->pipe;
3354
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003355 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003356 /* Force use of hard-coded filter coefficients
3357 * as some pre-programmed values are broken,
3358 * e.g. x201.
3359 */
3360 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3361 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3362 PF_PIPE_SEL_IVB(pipe));
3363 else
3364 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3365 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3366 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003367 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003368}
3369
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003370static void intel_enable_planes(struct drm_crtc *crtc)
3371{
3372 struct drm_device *dev = crtc->dev;
3373 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3374 struct intel_plane *intel_plane;
3375
3376 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3377 if (intel_plane->pipe == pipe)
3378 intel_plane_restore(&intel_plane->base);
3379}
3380
3381static void intel_disable_planes(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
3384 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3385 struct intel_plane *intel_plane;
3386
3387 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3388 if (intel_plane->pipe == pipe)
3389 intel_plane_disable(&intel_plane->base);
3390}
3391
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003392void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003393{
3394 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3395
3396 if (!crtc->config.ips_enabled)
3397 return;
3398
3399 /* We can only enable IPS after we enable a plane and wait for a vblank.
3400 * We guarantee that the plane is enabled by calling intel_enable_ips
3401 * only after intel_enable_plane. And intel_enable_plane already waits
3402 * for a vblank, so all we need to do here is to enable the IPS bit. */
3403 assert_plane_enabled(dev_priv, crtc->plane);
3404 I915_WRITE(IPS_CTL, IPS_ENABLE);
Paulo Zanoni5ade2c22013-09-19 17:03:06 -03003405
3406 /* The bit only becomes 1 in the next vblank, so this wait here is
3407 * essentially intel_wait_for_vblank. If we don't have this and don't
3408 * wait for vblanks until the end of crtc_enable, then the HW state
3409 * readout code will complain that the expected IPS_CTL value is not the
3410 * one we read. */
3411 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3412 DRM_ERROR("Timed out waiting for IPS enable\n");
Paulo Zanonid77e4532013-09-24 13:52:55 -03003413}
3414
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003415void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003416{
3417 struct drm_device *dev = crtc->base.dev;
3418 struct drm_i915_private *dev_priv = dev->dev_private;
3419
3420 if (!crtc->config.ips_enabled)
3421 return;
3422
3423 assert_plane_enabled(dev_priv, crtc->plane);
3424 I915_WRITE(IPS_CTL, 0);
3425 POSTING_READ(IPS_CTL);
3426
3427 /* We need to wait for a vblank before we can disable the plane. */
3428 intel_wait_for_vblank(dev, crtc->pipe);
3429}
3430
3431/** Loads the palette/gamma unit for the CRTC with the prepared values */
3432static void intel_crtc_load_lut(struct drm_crtc *crtc)
3433{
3434 struct drm_device *dev = crtc->dev;
3435 struct drm_i915_private *dev_priv = dev->dev_private;
3436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3437 enum pipe pipe = intel_crtc->pipe;
3438 int palreg = PALETTE(pipe);
3439 int i;
3440 bool reenable_ips = false;
3441
3442 /* The clocks have to be on to load the palette. */
3443 if (!crtc->enabled || !intel_crtc->active)
3444 return;
3445
3446 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3447 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3448 assert_dsi_pll_enabled(dev_priv);
3449 else
3450 assert_pll_enabled(dev_priv, pipe);
3451 }
3452
3453 /* use legacy palette for Ironlake */
3454 if (HAS_PCH_SPLIT(dev))
3455 palreg = LGC_PALETTE(pipe);
3456
3457 /* Workaround : Do not read or write the pipe palette/gamma data while
3458 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3459 */
3460 if (intel_crtc->config.ips_enabled &&
3461 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3462 GAMMA_MODE_MODE_SPLIT)) {
3463 hsw_disable_ips(intel_crtc);
3464 reenable_ips = true;
3465 }
3466
3467 for (i = 0; i < 256; i++) {
3468 I915_WRITE(palreg + 4 * i,
3469 (intel_crtc->lut_r[i] << 16) |
3470 (intel_crtc->lut_g[i] << 8) |
3471 intel_crtc->lut_b[i]);
3472 }
3473
3474 if (reenable_ips)
3475 hsw_enable_ips(intel_crtc);
3476}
3477
Jesse Barnesf67a5592011-01-05 10:31:48 -08003478static void ironlake_crtc_enable(struct drm_crtc *crtc)
3479{
3480 struct drm_device *dev = crtc->dev;
3481 struct drm_i915_private *dev_priv = dev->dev_private;
3482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003483 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003484 int pipe = intel_crtc->pipe;
3485 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003486
Daniel Vetter08a48462012-07-02 11:43:47 +02003487 WARN_ON(!crtc->enabled);
3488
Jesse Barnesf67a5592011-01-05 10:31:48 -08003489 if (intel_crtc->active)
3490 return;
3491
3492 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003493
3494 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3495 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3496
Daniel Vetterf6736a12013-06-05 13:34:30 +02003497 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003498 if (encoder->pre_enable)
3499 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003500
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003501 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003502 /* Note: FDI PLL enabling _must_ be done before we enable the
3503 * cpu pipes, hence this is separate from all the other fdi/pch
3504 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003505 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003506 } else {
3507 assert_fdi_tx_disabled(dev_priv, pipe);
3508 assert_fdi_rx_disabled(dev_priv, pipe);
3509 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003510
Jesse Barnesb074cec2013-04-25 12:55:02 -07003511 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003512
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003513 /*
3514 * On ILK+ LUT must be loaded before the pipe is running but with
3515 * clocks enabled
3516 */
3517 intel_crtc_load_lut(crtc);
3518
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003519 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003520 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003521 intel_crtc->config.has_pch_encoder, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003522 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003523 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003524 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003525
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003526 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003527 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003528
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003529 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003530 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003531 mutex_unlock(&dev->struct_mutex);
3532
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003533 for_each_encoder_on_crtc(dev, crtc, encoder)
3534 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003535
3536 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003537 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003538
3539 /*
3540 * There seems to be a race in PCH platform hw (at least on some
3541 * outputs) where an enabled pipe still completes any pageflip right
3542 * away (as if the pipe is off) instead of waiting for vblank. As soon
3543 * as the first vblank happend, everything works as expected. Hence just
3544 * wait for one vblank before returning to avoid strange things
3545 * happening.
3546 */
3547 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003548}
3549
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003550/* IPS only exists on ULT machines and is tied to pipe A. */
3551static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3552{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003553 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003554}
3555
Ville Syrjälädda9a662013-09-19 17:00:37 -03003556static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3557{
3558 struct drm_device *dev = crtc->dev;
3559 struct drm_i915_private *dev_priv = dev->dev_private;
3560 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3561 int pipe = intel_crtc->pipe;
3562 int plane = intel_crtc->plane;
3563
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003564 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003565 intel_enable_planes(crtc);
3566 intel_crtc_update_cursor(crtc, true);
3567
3568 hsw_enable_ips(intel_crtc);
3569
3570 mutex_lock(&dev->struct_mutex);
3571 intel_update_fbc(dev);
3572 mutex_unlock(&dev->struct_mutex);
3573}
3574
3575static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3576{
3577 struct drm_device *dev = crtc->dev;
3578 struct drm_i915_private *dev_priv = dev->dev_private;
3579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3580 int pipe = intel_crtc->pipe;
3581 int plane = intel_crtc->plane;
3582
3583 intel_crtc_wait_for_pending_flips(crtc);
3584 drm_vblank_off(dev, pipe);
3585
3586 /* FBC must be disabled before disabling the plane on HSW. */
3587 if (dev_priv->fbc.plane == plane)
3588 intel_disable_fbc(dev);
3589
3590 hsw_disable_ips(intel_crtc);
3591
3592 intel_crtc_update_cursor(crtc, false);
3593 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003594 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003595}
3596
Paulo Zanonie4916942013-09-20 16:21:19 -03003597/*
3598 * This implements the workaround described in the "notes" section of the mode
3599 * set sequence documentation. When going from no pipes or single pipe to
3600 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3601 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3602 */
3603static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3604{
3605 struct drm_device *dev = crtc->base.dev;
3606 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3607
3608 /* We want to get the other_active_crtc only if there's only 1 other
3609 * active crtc. */
3610 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3611 if (!crtc_it->active || crtc_it == crtc)
3612 continue;
3613
3614 if (other_active_crtc)
3615 return;
3616
3617 other_active_crtc = crtc_it;
3618 }
3619 if (!other_active_crtc)
3620 return;
3621
3622 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3623 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3624}
3625
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003626static void haswell_crtc_enable(struct drm_crtc *crtc)
3627{
3628 struct drm_device *dev = crtc->dev;
3629 struct drm_i915_private *dev_priv = dev->dev_private;
3630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3631 struct intel_encoder *encoder;
3632 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003633
3634 WARN_ON(!crtc->enabled);
3635
3636 if (intel_crtc->active)
3637 return;
3638
3639 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003640
3641 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3642 if (intel_crtc->config.has_pch_encoder)
3643 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3644
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003645 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003646 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003647
3648 for_each_encoder_on_crtc(dev, crtc, encoder)
3649 if (encoder->pre_enable)
3650 encoder->pre_enable(encoder);
3651
Paulo Zanoni1f544382012-10-24 11:32:00 -02003652 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003653
Jesse Barnesb074cec2013-04-25 12:55:02 -07003654 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003655
3656 /*
3657 * On ILK+ LUT must be loaded before the pipe is running but with
3658 * clocks enabled
3659 */
3660 intel_crtc_load_lut(crtc);
3661
Paulo Zanoni1f544382012-10-24 11:32:00 -02003662 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003663 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003664
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003665 intel_update_watermarks(crtc);
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003666 intel_enable_pipe(dev_priv, pipe,
Jani Nikula23538ef2013-08-27 15:12:22 +03003667 intel_crtc->config.has_pch_encoder, false);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003668
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003669 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003670 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003671
Jani Nikula8807e552013-08-30 19:40:32 +03003672 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003673 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003674 intel_opregion_notify_encoder(encoder, true);
3675 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003676
Paulo Zanonie4916942013-09-20 16:21:19 -03003677 /* If we change the relative order between pipe/planes enabling, we need
3678 * to change the workaround. */
3679 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03003680 haswell_crtc_enable_planes(crtc);
3681
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003682 /*
3683 * There seems to be a race in PCH platform hw (at least on some
3684 * outputs) where an enabled pipe still completes any pageflip right
3685 * away (as if the pipe is off) instead of waiting for vblank. As soon
3686 * as the first vblank happend, everything works as expected. Hence just
3687 * wait for one vblank before returning to avoid strange things
3688 * happening.
3689 */
3690 intel_wait_for_vblank(dev, intel_crtc->pipe);
3691}
3692
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003693static void ironlake_pfit_disable(struct intel_crtc *crtc)
3694{
3695 struct drm_device *dev = crtc->base.dev;
3696 struct drm_i915_private *dev_priv = dev->dev_private;
3697 int pipe = crtc->pipe;
3698
3699 /* To avoid upsetting the power well on haswell only disable the pfit if
3700 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003701 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003702 I915_WRITE(PF_CTL(pipe), 0);
3703 I915_WRITE(PF_WIN_POS(pipe), 0);
3704 I915_WRITE(PF_WIN_SZ(pipe), 0);
3705 }
3706}
3707
Jesse Barnes6be4a602010-09-10 10:26:01 -07003708static void ironlake_crtc_disable(struct drm_crtc *crtc)
3709{
3710 struct drm_device *dev = crtc->dev;
3711 struct drm_i915_private *dev_priv = dev->dev_private;
3712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003713 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003714 int pipe = intel_crtc->pipe;
3715 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003716 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003717
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003718
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003719 if (!intel_crtc->active)
3720 return;
3721
Daniel Vetterea9d7582012-07-10 10:42:52 +02003722 for_each_encoder_on_crtc(dev, crtc, encoder)
3723 encoder->disable(encoder);
3724
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003725 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003726 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003727
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003728 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003729 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003730
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003731 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003732 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03003733 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003734
Daniel Vetterd925c592013-06-05 13:34:04 +02003735 if (intel_crtc->config.has_pch_encoder)
3736 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3737
Jesse Barnesb24e7172011-01-04 15:09:30 -08003738 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003739
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003740 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003741
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003742 for_each_encoder_on_crtc(dev, crtc, encoder)
3743 if (encoder->post_disable)
3744 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003745
Daniel Vetterd925c592013-06-05 13:34:04 +02003746 if (intel_crtc->config.has_pch_encoder) {
3747 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003748
Daniel Vetterd925c592013-06-05 13:34:04 +02003749 ironlake_disable_pch_transcoder(dev_priv, pipe);
3750 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003751
Daniel Vetterd925c592013-06-05 13:34:04 +02003752 if (HAS_PCH_CPT(dev)) {
3753 /* disable TRANS_DP_CTL */
3754 reg = TRANS_DP_CTL(pipe);
3755 temp = I915_READ(reg);
3756 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3757 TRANS_DP_PORT_SEL_MASK);
3758 temp |= TRANS_DP_PORT_SEL_NONE;
3759 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003760
Daniel Vetterd925c592013-06-05 13:34:04 +02003761 /* disable DPLL_SEL */
3762 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003763 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003764 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003765 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003766
3767 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003768 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003769
3770 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003771 }
3772
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003773 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003774 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003775
3776 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003777 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003778 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003779}
3780
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003781static void haswell_crtc_disable(struct drm_crtc *crtc)
3782{
3783 struct drm_device *dev = crtc->dev;
3784 struct drm_i915_private *dev_priv = dev->dev_private;
3785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3786 struct intel_encoder *encoder;
3787 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003788 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003789
3790 if (!intel_crtc->active)
3791 return;
3792
Ville Syrjälädda9a662013-09-19 17:00:37 -03003793 haswell_crtc_disable_planes(crtc);
3794
Jani Nikula8807e552013-08-30 19:40:32 +03003795 for_each_encoder_on_crtc(dev, crtc, encoder) {
3796 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003797 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03003798 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003799
Paulo Zanoni86642812013-04-12 17:57:57 -03003800 if (intel_crtc->config.has_pch_encoder)
3801 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003802 intel_disable_pipe(dev_priv, pipe);
3803
Paulo Zanoniad80a812012-10-24 16:06:19 -02003804 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003805
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003806 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003807
Paulo Zanoni1f544382012-10-24 11:32:00 -02003808 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003809
3810 for_each_encoder_on_crtc(dev, crtc, encoder)
3811 if (encoder->post_disable)
3812 encoder->post_disable(encoder);
3813
Daniel Vetter88adfff2013-03-28 10:42:01 +01003814 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003815 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003816 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003817 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003818 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003819
3820 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003821 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003822
3823 mutex_lock(&dev->struct_mutex);
3824 intel_update_fbc(dev);
3825 mutex_unlock(&dev->struct_mutex);
3826}
3827
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003828static void ironlake_crtc_off(struct drm_crtc *crtc)
3829{
3830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003831 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003832}
3833
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003834static void haswell_crtc_off(struct drm_crtc *crtc)
3835{
3836 intel_ddi_put_crtc_pll(crtc);
3837}
3838
Daniel Vetter02e792f2009-09-15 22:57:34 +02003839static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3840{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003841 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003842 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003843 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003844
Chris Wilson23f09ce2010-08-12 13:53:37 +01003845 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003846 dev_priv->mm.interruptible = false;
3847 (void) intel_overlay_switch_off(intel_crtc->overlay);
3848 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003849 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003850 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003851
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003852 /* Let userspace switch the overlay on again. In most cases userspace
3853 * has to recompute where to put it anyway.
3854 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003855}
3856
Egbert Eich61bc95c2013-03-04 09:24:38 -05003857/**
3858 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3859 * cursor plane briefly if not already running after enabling the display
3860 * plane.
3861 * This workaround avoids occasional blank screens when self refresh is
3862 * enabled.
3863 */
3864static void
3865g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3866{
3867 u32 cntl = I915_READ(CURCNTR(pipe));
3868
3869 if ((cntl & CURSOR_MODE) == 0) {
3870 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3871
3872 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3873 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3874 intel_wait_for_vblank(dev_priv->dev, pipe);
3875 I915_WRITE(CURCNTR(pipe), cntl);
3876 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3877 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3878 }
3879}
3880
Jesse Barnes2dd24552013-04-25 12:55:01 -07003881static void i9xx_pfit_enable(struct intel_crtc *crtc)
3882{
3883 struct drm_device *dev = crtc->base.dev;
3884 struct drm_i915_private *dev_priv = dev->dev_private;
3885 struct intel_crtc_config *pipe_config = &crtc->config;
3886
Daniel Vetter328d8e82013-05-08 10:36:31 +02003887 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003888 return;
3889
Daniel Vetterc0b03412013-05-28 12:05:54 +02003890 /*
3891 * The panel fitter should only be adjusted whilst the pipe is disabled,
3892 * according to register description and PRM.
3893 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003894 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3895 assert_pipe_disabled(dev_priv, crtc->pipe);
3896
Jesse Barnesb074cec2013-04-25 12:55:02 -07003897 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3898 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003899
3900 /* Border color in case we don't scale up to the full screen. Black by
3901 * default, change to something else for debugging. */
3902 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003903}
3904
Jesse Barnes586f49d2013-11-04 16:06:59 -08003905int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08003906{
Jesse Barnes586f49d2013-11-04 16:06:59 -08003907 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08003908
Jesse Barnes586f49d2013-11-04 16:06:59 -08003909 /* Obtain SKU information */
3910 mutex_lock(&dev_priv->dpio_lock);
3911 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3912 CCK_FUSE_HPLL_FREQ_MASK;
3913 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08003914
Jesse Barnes586f49d2013-11-04 16:06:59 -08003915 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08003916}
3917
3918/* Adjust CDclk dividers to allow high res or save power if possible */
3919static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3920{
3921 struct drm_i915_private *dev_priv = dev->dev_private;
3922 u32 val, cmd;
3923
3924 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3925 cmd = 2;
3926 else if (cdclk == 266)
3927 cmd = 1;
3928 else
3929 cmd = 0;
3930
3931 mutex_lock(&dev_priv->rps.hw_lock);
3932 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3933 val &= ~DSPFREQGUAR_MASK;
3934 val |= (cmd << DSPFREQGUAR_SHIFT);
3935 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3936 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3937 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3938 50)) {
3939 DRM_ERROR("timed out waiting for CDclk change\n");
3940 }
3941 mutex_unlock(&dev_priv->rps.hw_lock);
3942
3943 if (cdclk == 400) {
3944 u32 divider, vco;
3945
3946 vco = valleyview_get_vco(dev_priv);
3947 divider = ((vco << 1) / cdclk) - 1;
3948
3949 mutex_lock(&dev_priv->dpio_lock);
3950 /* adjust cdclk divider */
3951 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3952 val &= ~0xf;
3953 val |= divider;
3954 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3955 mutex_unlock(&dev_priv->dpio_lock);
3956 }
3957
3958 mutex_lock(&dev_priv->dpio_lock);
3959 /* adjust self-refresh exit latency value */
3960 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
3961 val &= ~0x7f;
3962
3963 /*
3964 * For high bandwidth configs, we set a higher latency in the bunit
3965 * so that the core display fetch happens in time to avoid underruns.
3966 */
3967 if (cdclk == 400)
3968 val |= 4500 / 250; /* 4.5 usec */
3969 else
3970 val |= 3000 / 250; /* 3.0 usec */
3971 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
3972 mutex_unlock(&dev_priv->dpio_lock);
3973
3974 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
3975 intel_i2c_reset(dev);
3976}
3977
3978static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
3979{
3980 int cur_cdclk, vco;
3981 int divider;
3982
3983 vco = valleyview_get_vco(dev_priv);
3984
3985 mutex_lock(&dev_priv->dpio_lock);
3986 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3987 mutex_unlock(&dev_priv->dpio_lock);
3988
3989 divider &= 0xf;
3990
3991 cur_cdclk = (vco << 1) / (divider + 1);
3992
3993 return cur_cdclk;
3994}
3995
3996static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
3997 int max_pixclk)
3998{
3999 int cur_cdclk;
4000
4001 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4002
4003 /*
4004 * Really only a few cases to deal with, as only 4 CDclks are supported:
4005 * 200MHz
4006 * 267MHz
4007 * 320MHz
4008 * 400MHz
4009 * So we check to see whether we're above 90% of the lower bin and
4010 * adjust if needed.
4011 */
4012 if (max_pixclk > 288000) {
4013 return 400;
4014 } else if (max_pixclk > 240000) {
4015 return 320;
4016 } else
4017 return 266;
4018 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4019}
4020
4021static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4022 unsigned modeset_pipes,
4023 struct intel_crtc_config *pipe_config)
4024{
4025 struct drm_device *dev = dev_priv->dev;
4026 struct intel_crtc *intel_crtc;
4027 int max_pixclk = 0;
4028
4029 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4030 base.head) {
4031 if (modeset_pipes & (1 << intel_crtc->pipe))
4032 max_pixclk = max(max_pixclk,
4033 pipe_config->adjusted_mode.crtc_clock);
4034 else if (intel_crtc->base.enabled)
4035 max_pixclk = max(max_pixclk,
4036 intel_crtc->config.adjusted_mode.crtc_clock);
4037 }
4038
4039 return max_pixclk;
4040}
4041
4042static void valleyview_modeset_global_pipes(struct drm_device *dev,
4043 unsigned *prepare_pipes,
4044 unsigned modeset_pipes,
4045 struct intel_crtc_config *pipe_config)
4046{
4047 struct drm_i915_private *dev_priv = dev->dev_private;
4048 struct intel_crtc *intel_crtc;
4049 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4050 pipe_config);
4051 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4052
4053 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4054 return;
4055
4056 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4057 base.head)
4058 if (intel_crtc->base.enabled)
4059 *prepare_pipes |= (1 << intel_crtc->pipe);
4060}
4061
4062static void valleyview_modeset_global_resources(struct drm_device *dev)
4063{
4064 struct drm_i915_private *dev_priv = dev->dev_private;
4065 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4066 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4067 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4068
4069 if (req_cdclk != cur_cdclk)
4070 valleyview_set_cdclk(dev, req_cdclk);
4071}
4072
Jesse Barnes89b667f2013-04-18 14:51:36 -07004073static void valleyview_crtc_enable(struct drm_crtc *crtc)
4074{
4075 struct drm_device *dev = crtc->dev;
4076 struct drm_i915_private *dev_priv = dev->dev_private;
4077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4078 struct intel_encoder *encoder;
4079 int pipe = intel_crtc->pipe;
4080 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004081 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004082
4083 WARN_ON(!crtc->enabled);
4084
4085 if (intel_crtc->active)
4086 return;
4087
4088 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004089
Jesse Barnes89b667f2013-04-18 14:51:36 -07004090 for_each_encoder_on_crtc(dev, crtc, encoder)
4091 if (encoder->pre_pll_enable)
4092 encoder->pre_pll_enable(encoder);
4093
Jani Nikula23538ef2013-08-27 15:12:22 +03004094 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4095
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004096 if (!is_dsi)
4097 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004098
4099 for_each_encoder_on_crtc(dev, crtc, encoder)
4100 if (encoder->pre_enable)
4101 encoder->pre_enable(encoder);
4102
Jesse Barnes2dd24552013-04-25 12:55:01 -07004103 i9xx_pfit_enable(intel_crtc);
4104
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004105 intel_crtc_load_lut(crtc);
4106
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004107 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004108 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004109 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004110 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004111 intel_crtc_update_cursor(crtc, true);
4112
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004113 intel_update_fbc(dev);
Jani Nikula50049452013-07-30 12:20:32 +03004114
4115 for_each_encoder_on_crtc(dev, crtc, encoder)
4116 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004117}
4118
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004119static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004120{
4121 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004122 struct drm_i915_private *dev_priv = dev->dev_private;
4123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004124 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004125 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004126 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004127
Daniel Vetter08a48462012-07-02 11:43:47 +02004128 WARN_ON(!crtc->enabled);
4129
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004130 if (intel_crtc->active)
4131 return;
4132
4133 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004134
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004135 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004136 if (encoder->pre_enable)
4137 encoder->pre_enable(encoder);
4138
Daniel Vetterf6736a12013-06-05 13:34:30 +02004139 i9xx_enable_pll(intel_crtc);
4140
Jesse Barnes2dd24552013-04-25 12:55:01 -07004141 i9xx_pfit_enable(intel_crtc);
4142
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004143 intel_crtc_load_lut(crtc);
4144
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004145 intel_update_watermarks(crtc);
Jani Nikula23538ef2013-08-27 15:12:22 +03004146 intel_enable_pipe(dev_priv, pipe, false, false);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004147 intel_enable_primary_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004148 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004149 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05004150 if (IS_G4X(dev))
4151 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03004152 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004153
4154 /* Give the overlay scaler a chance to enable if it's on this pipe */
4155 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004156
Ville Syrjäläf440eb12013-06-04 13:49:01 +03004157 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004158
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004159 for_each_encoder_on_crtc(dev, crtc, encoder)
4160 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004161}
4162
Daniel Vetter87476d62013-04-11 16:29:06 +02004163static void i9xx_pfit_disable(struct intel_crtc *crtc)
4164{
4165 struct drm_device *dev = crtc->base.dev;
4166 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004167
4168 if (!crtc->config.gmch_pfit.control)
4169 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004170
4171 assert_pipe_disabled(dev_priv, crtc->pipe);
4172
Daniel Vetter328d8e82013-05-08 10:36:31 +02004173 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4174 I915_READ(PFIT_CONTROL));
4175 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004176}
4177
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004178static void i9xx_crtc_disable(struct drm_crtc *crtc)
4179{
4180 struct drm_device *dev = crtc->dev;
4181 struct drm_i915_private *dev_priv = dev->dev_private;
4182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004183 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004184 int pipe = intel_crtc->pipe;
4185 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004186
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004187 if (!intel_crtc->active)
4188 return;
4189
Daniel Vetterea9d7582012-07-10 10:42:52 +02004190 for_each_encoder_on_crtc(dev, crtc, encoder)
4191 encoder->disable(encoder);
4192
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004193 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004194 intel_crtc_wait_for_pending_flips(crtc);
4195 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004196
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07004197 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01004198 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004199
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004200 intel_crtc_dpms_overlay(intel_crtc, false);
4201 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004202 intel_disable_planes(crtc);
Ville Syrjäläd1de00e2013-10-01 18:02:19 +03004203 intel_disable_primary_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004204
Jesse Barnesb24e7172011-01-04 15:09:30 -08004205 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004206
Daniel Vetter87476d62013-04-11 16:29:06 +02004207 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004208
Jesse Barnes89b667f2013-04-18 14:51:36 -07004209 for_each_encoder_on_crtc(dev, crtc, encoder)
4210 if (encoder->post_disable)
4211 encoder->post_disable(encoder);
4212
Jesse Barnesf6071162013-10-01 10:41:38 -07004213 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4214 vlv_disable_pll(dev_priv, pipe);
4215 else if (!IS_VALLEYVIEW(dev))
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004216 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004217
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004218 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004219 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004220
Chris Wilson6b383a72010-09-13 13:54:26 +01004221 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004222}
4223
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004224static void i9xx_crtc_off(struct drm_crtc *crtc)
4225{
4226}
4227
Daniel Vetter976f8a22012-07-08 22:34:21 +02004228static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4229 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004230{
4231 struct drm_device *dev = crtc->dev;
4232 struct drm_i915_master_private *master_priv;
4233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4234 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004235
4236 if (!dev->primary->master)
4237 return;
4238
4239 master_priv = dev->primary->master->driver_priv;
4240 if (!master_priv->sarea_priv)
4241 return;
4242
Jesse Barnes79e53942008-11-07 14:24:08 -08004243 switch (pipe) {
4244 case 0:
4245 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4246 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4247 break;
4248 case 1:
4249 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4250 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4251 break;
4252 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004253 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004254 break;
4255 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004256}
4257
Daniel Vetter976f8a22012-07-08 22:34:21 +02004258/**
4259 * Sets the power management mode of the pipe and plane.
4260 */
4261void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004262{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004263 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004264 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004265 struct intel_encoder *intel_encoder;
4266 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004267
Daniel Vetter976f8a22012-07-08 22:34:21 +02004268 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4269 enable |= intel_encoder->connectors_active;
4270
4271 if (enable)
4272 dev_priv->display.crtc_enable(crtc);
4273 else
4274 dev_priv->display.crtc_disable(crtc);
4275
4276 intel_crtc_update_sarea(crtc, enable);
4277}
4278
Daniel Vetter976f8a22012-07-08 22:34:21 +02004279static void intel_crtc_disable(struct drm_crtc *crtc)
4280{
4281 struct drm_device *dev = crtc->dev;
4282 struct drm_connector *connector;
4283 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004285
4286 /* crtc should still be enabled when we disable it. */
4287 WARN_ON(!crtc->enabled);
4288
4289 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004290 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004291 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004292 dev_priv->display.off(crtc);
4293
Chris Wilson931872f2012-01-16 23:01:13 +00004294 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004295 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004296 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004297
4298 if (crtc->fb) {
4299 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01004300 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004301 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004302 crtc->fb = NULL;
4303 }
4304
4305 /* Update computed state. */
4306 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4307 if (!connector->encoder || !connector->encoder->crtc)
4308 continue;
4309
4310 if (connector->encoder->crtc != crtc)
4311 continue;
4312
4313 connector->dpms = DRM_MODE_DPMS_OFF;
4314 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004315 }
4316}
4317
Chris Wilsonea5b2132010-08-04 13:50:23 +01004318void intel_encoder_destroy(struct drm_encoder *encoder)
4319{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004320 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004321
Chris Wilsonea5b2132010-08-04 13:50:23 +01004322 drm_encoder_cleanup(encoder);
4323 kfree(intel_encoder);
4324}
4325
Damien Lespiau92373292013-08-08 22:28:57 +01004326/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004327 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4328 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004329static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004330{
4331 if (mode == DRM_MODE_DPMS_ON) {
4332 encoder->connectors_active = true;
4333
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004334 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004335 } else {
4336 encoder->connectors_active = false;
4337
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004338 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004339 }
4340}
4341
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004342/* Cross check the actual hw state with our own modeset state tracking (and it's
4343 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004344static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004345{
4346 if (connector->get_hw_state(connector)) {
4347 struct intel_encoder *encoder = connector->encoder;
4348 struct drm_crtc *crtc;
4349 bool encoder_enabled;
4350 enum pipe pipe;
4351
4352 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4353 connector->base.base.id,
4354 drm_get_connector_name(&connector->base));
4355
4356 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4357 "wrong connector dpms state\n");
4358 WARN(connector->base.encoder != &encoder->base,
4359 "active connector not linked to encoder\n");
4360 WARN(!encoder->connectors_active,
4361 "encoder->connectors_active not set\n");
4362
4363 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4364 WARN(!encoder_enabled, "encoder not enabled\n");
4365 if (WARN_ON(!encoder->base.crtc))
4366 return;
4367
4368 crtc = encoder->base.crtc;
4369
4370 WARN(!crtc->enabled, "crtc not enabled\n");
4371 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4372 WARN(pipe != to_intel_crtc(crtc)->pipe,
4373 "encoder active on the wrong pipe\n");
4374 }
4375}
4376
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004377/* Even simpler default implementation, if there's really no special case to
4378 * consider. */
4379void intel_connector_dpms(struct drm_connector *connector, int mode)
4380{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004381 /* All the simple cases only support two dpms states. */
4382 if (mode != DRM_MODE_DPMS_ON)
4383 mode = DRM_MODE_DPMS_OFF;
4384
4385 if (mode == connector->dpms)
4386 return;
4387
4388 connector->dpms = mode;
4389
4390 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004391 if (connector->encoder)
4392 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004393
Daniel Vetterb9805142012-08-31 17:37:33 +02004394 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004395}
4396
Daniel Vetterf0947c32012-07-02 13:10:34 +02004397/* Simple connector->get_hw_state implementation for encoders that support only
4398 * one connector and no cloning and hence the encoder state determines the state
4399 * of the connector. */
4400bool intel_connector_get_hw_state(struct intel_connector *connector)
4401{
Daniel Vetter24929352012-07-02 20:28:59 +02004402 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004403 struct intel_encoder *encoder = connector->encoder;
4404
4405 return encoder->get_hw_state(encoder, &pipe);
4406}
4407
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004408static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4409 struct intel_crtc_config *pipe_config)
4410{
4411 struct drm_i915_private *dev_priv = dev->dev_private;
4412 struct intel_crtc *pipe_B_crtc =
4413 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4414
4415 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4416 pipe_name(pipe), pipe_config->fdi_lanes);
4417 if (pipe_config->fdi_lanes > 4) {
4418 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4419 pipe_name(pipe), pipe_config->fdi_lanes);
4420 return false;
4421 }
4422
4423 if (IS_HASWELL(dev)) {
4424 if (pipe_config->fdi_lanes > 2) {
4425 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4426 pipe_config->fdi_lanes);
4427 return false;
4428 } else {
4429 return true;
4430 }
4431 }
4432
4433 if (INTEL_INFO(dev)->num_pipes == 2)
4434 return true;
4435
4436 /* Ivybridge 3 pipe is really complicated */
4437 switch (pipe) {
4438 case PIPE_A:
4439 return true;
4440 case PIPE_B:
4441 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4442 pipe_config->fdi_lanes > 2) {
4443 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4444 pipe_name(pipe), pipe_config->fdi_lanes);
4445 return false;
4446 }
4447 return true;
4448 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004449 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004450 pipe_B_crtc->config.fdi_lanes <= 2) {
4451 if (pipe_config->fdi_lanes > 2) {
4452 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4453 pipe_name(pipe), pipe_config->fdi_lanes);
4454 return false;
4455 }
4456 } else {
4457 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4458 return false;
4459 }
4460 return true;
4461 default:
4462 BUG();
4463 }
4464}
4465
Daniel Vettere29c22c2013-02-21 00:00:16 +01004466#define RETRY 1
4467static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4468 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004469{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004470 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004471 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004472 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004473 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004474
Daniel Vettere29c22c2013-02-21 00:00:16 +01004475retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004476 /* FDI is a binary signal running at ~2.7GHz, encoding
4477 * each output octet as 10 bits. The actual frequency
4478 * is stored as a divider into a 100MHz clock, and the
4479 * mode pixel clock is stored in units of 1KHz.
4480 * Hence the bw of each lane in terms of the mode signal
4481 * is:
4482 */
4483 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4484
Damien Lespiau241bfc32013-09-25 16:45:37 +01004485 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004486
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004487 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004488 pipe_config->pipe_bpp);
4489
4490 pipe_config->fdi_lanes = lane;
4491
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004492 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004493 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004494
Daniel Vettere29c22c2013-02-21 00:00:16 +01004495 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4496 intel_crtc->pipe, pipe_config);
4497 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4498 pipe_config->pipe_bpp -= 2*3;
4499 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4500 pipe_config->pipe_bpp);
4501 needs_recompute = true;
4502 pipe_config->bw_constrained = true;
4503
4504 goto retry;
4505 }
4506
4507 if (needs_recompute)
4508 return RETRY;
4509
4510 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004511}
4512
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004513static void hsw_compute_ips_config(struct intel_crtc *crtc,
4514 struct intel_crtc_config *pipe_config)
4515{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004516 pipe_config->ips_enabled = i915_enable_ips &&
4517 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004518 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004519}
4520
Daniel Vettera43f6e02013-06-07 23:10:32 +02004521static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004522 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004523{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004524 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004525 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004526
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004527 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004528 if (INTEL_INFO(dev)->gen < 4) {
4529 struct drm_i915_private *dev_priv = dev->dev_private;
4530 int clock_limit =
4531 dev_priv->display.get_display_clock_speed(dev);
4532
4533 /*
4534 * Enable pixel doubling when the dot clock
4535 * is > 90% of the (display) core speed.
4536 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004537 * GDG double wide on either pipe,
4538 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004539 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004540 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004541 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004542 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004543 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004544 }
4545
Damien Lespiau241bfc32013-09-25 16:45:37 +01004546 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004547 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004548 }
Chris Wilson89749352010-09-12 18:25:19 +01004549
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004550 /*
4551 * Pipe horizontal size must be even in:
4552 * - DVO ganged mode
4553 * - LVDS dual channel mode
4554 * - Double wide pipe
4555 */
4556 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4557 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4558 pipe_config->pipe_src_w &= ~1;
4559
Damien Lespiau8693a822013-05-03 18:48:11 +01004560 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4561 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004562 */
4563 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4564 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004565 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004566
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004567 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004568 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004569 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004570 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4571 * for lvds. */
4572 pipe_config->pipe_bpp = 8*3;
4573 }
4574
Damien Lespiauf5adf942013-06-24 18:29:34 +01004575 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004576 hsw_compute_ips_config(crtc, pipe_config);
4577
4578 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4579 * clock survives for now. */
4580 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4581 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004582
Daniel Vetter877d48d2013-04-19 11:24:43 +02004583 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004584 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004585
Daniel Vettere29c22c2013-02-21 00:00:16 +01004586 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004587}
4588
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004589static int valleyview_get_display_clock_speed(struct drm_device *dev)
4590{
4591 return 400000; /* FIXME */
4592}
4593
Jesse Barnese70236a2009-09-21 10:42:27 -07004594static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004595{
Jesse Barnese70236a2009-09-21 10:42:27 -07004596 return 400000;
4597}
Jesse Barnes79e53942008-11-07 14:24:08 -08004598
Jesse Barnese70236a2009-09-21 10:42:27 -07004599static int i915_get_display_clock_speed(struct drm_device *dev)
4600{
4601 return 333000;
4602}
Jesse Barnes79e53942008-11-07 14:24:08 -08004603
Jesse Barnese70236a2009-09-21 10:42:27 -07004604static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4605{
4606 return 200000;
4607}
Jesse Barnes79e53942008-11-07 14:24:08 -08004608
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004609static int pnv_get_display_clock_speed(struct drm_device *dev)
4610{
4611 u16 gcfgc = 0;
4612
4613 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4614
4615 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4616 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4617 return 267000;
4618 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4619 return 333000;
4620 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4621 return 444000;
4622 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4623 return 200000;
4624 default:
4625 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4626 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4627 return 133000;
4628 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4629 return 167000;
4630 }
4631}
4632
Jesse Barnese70236a2009-09-21 10:42:27 -07004633static int i915gm_get_display_clock_speed(struct drm_device *dev)
4634{
4635 u16 gcfgc = 0;
4636
4637 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4638
4639 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004640 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004641 else {
4642 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4643 case GC_DISPLAY_CLOCK_333_MHZ:
4644 return 333000;
4645 default:
4646 case GC_DISPLAY_CLOCK_190_200_MHZ:
4647 return 190000;
4648 }
4649 }
4650}
Jesse Barnes79e53942008-11-07 14:24:08 -08004651
Jesse Barnese70236a2009-09-21 10:42:27 -07004652static int i865_get_display_clock_speed(struct drm_device *dev)
4653{
4654 return 266000;
4655}
4656
4657static int i855_get_display_clock_speed(struct drm_device *dev)
4658{
4659 u16 hpllcc = 0;
4660 /* Assume that the hardware is in the high speed state. This
4661 * should be the default.
4662 */
4663 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4664 case GC_CLOCK_133_200:
4665 case GC_CLOCK_100_200:
4666 return 200000;
4667 case GC_CLOCK_166_250:
4668 return 250000;
4669 case GC_CLOCK_100_133:
4670 return 133000;
4671 }
4672
4673 /* Shouldn't happen */
4674 return 0;
4675}
4676
4677static int i830_get_display_clock_speed(struct drm_device *dev)
4678{
4679 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004680}
4681
Zhenyu Wang2c072452009-06-05 15:38:42 +08004682static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004683intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004684{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004685 while (*num > DATA_LINK_M_N_MASK ||
4686 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004687 *num >>= 1;
4688 *den >>= 1;
4689 }
4690}
4691
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004692static void compute_m_n(unsigned int m, unsigned int n,
4693 uint32_t *ret_m, uint32_t *ret_n)
4694{
4695 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4696 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4697 intel_reduce_m_n_ratio(ret_m, ret_n);
4698}
4699
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004700void
4701intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4702 int pixel_clock, int link_clock,
4703 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004704{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004705 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004706
4707 compute_m_n(bits_per_pixel * pixel_clock,
4708 link_clock * nlanes * 8,
4709 &m_n->gmch_m, &m_n->gmch_n);
4710
4711 compute_m_n(pixel_clock, link_clock,
4712 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004713}
4714
Chris Wilsona7615032011-01-12 17:04:08 +00004715static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4716{
Keith Packard72bbe582011-09-26 16:09:45 -07004717 if (i915_panel_use_ssc >= 0)
4718 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004719 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004720 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004721}
4722
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004723static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4724{
4725 struct drm_device *dev = crtc->dev;
4726 struct drm_i915_private *dev_priv = dev->dev_private;
4727 int refclk;
4728
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004729 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02004730 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004731 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004732 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004733 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004734 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4735 refclk / 1000);
4736 } else if (!IS_GEN2(dev)) {
4737 refclk = 96000;
4738 } else {
4739 refclk = 48000;
4740 }
4741
4742 return refclk;
4743}
4744
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004745static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004746{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004747 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004748}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004749
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004750static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4751{
4752 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004753}
4754
Daniel Vetterf47709a2013-03-28 10:42:02 +01004755static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004756 intel_clock_t *reduced_clock)
4757{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004758 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004759 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004760 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004761 u32 fp, fp2 = 0;
4762
4763 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004764 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004765 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004766 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004767 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004768 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004769 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004770 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004771 }
4772
4773 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004774 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004775
Daniel Vetterf47709a2013-03-28 10:42:02 +01004776 crtc->lowfreq_avail = false;
4777 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004778 reduced_clock && i915_powersave) {
4779 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004780 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004781 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004782 } else {
4783 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004784 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004785 }
4786}
4787
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004788static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4789 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004790{
4791 u32 reg_val;
4792
4793 /*
4794 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4795 * and set it to a reasonable value instead.
4796 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004797 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004798 reg_val &= 0xffffff00;
4799 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004800 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004801
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004802 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004803 reg_val &= 0x8cffffff;
4804 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004805 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004806
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004807 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004808 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004809 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004810
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004811 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004812 reg_val &= 0x00ffffff;
4813 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004814 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004815}
4816
Daniel Vetterb5518422013-05-03 11:49:48 +02004817static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4818 struct intel_link_m_n *m_n)
4819{
4820 struct drm_device *dev = crtc->base.dev;
4821 struct drm_i915_private *dev_priv = dev->dev_private;
4822 int pipe = crtc->pipe;
4823
Daniel Vettere3b95f12013-05-03 11:49:49 +02004824 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4825 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4826 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4827 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004828}
4829
4830static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4831 struct intel_link_m_n *m_n)
4832{
4833 struct drm_device *dev = crtc->base.dev;
4834 struct drm_i915_private *dev_priv = dev->dev_private;
4835 int pipe = crtc->pipe;
4836 enum transcoder transcoder = crtc->config.cpu_transcoder;
4837
4838 if (INTEL_INFO(dev)->gen >= 5) {
4839 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4840 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4841 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4842 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4843 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004844 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4845 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4846 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4847 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004848 }
4849}
4850
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004851static void intel_dp_set_m_n(struct intel_crtc *crtc)
4852{
4853 if (crtc->config.has_pch_encoder)
4854 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4855 else
4856 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4857}
4858
Daniel Vetterf47709a2013-03-28 10:42:02 +01004859static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004860{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004861 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004862 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004863 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004864 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004865 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004866 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004867
Daniel Vetter09153002012-12-12 14:06:44 +01004868 mutex_lock(&dev_priv->dpio_lock);
4869
Daniel Vetterf47709a2013-03-28 10:42:02 +01004870 bestn = crtc->config.dpll.n;
4871 bestm1 = crtc->config.dpll.m1;
4872 bestm2 = crtc->config.dpll.m2;
4873 bestp1 = crtc->config.dpll.p1;
4874 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004875
Jesse Barnes89b667f2013-04-18 14:51:36 -07004876 /* See eDP HDMI DPIO driver vbios notes doc */
4877
4878 /* PLL B needs special handling */
4879 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08004880 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004881
4882 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004883 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004884
4885 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004886 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004887 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004888 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004889
4890 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004891 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004892
4893 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004894 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4895 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4896 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004897 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004898
4899 /*
4900 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4901 * but we don't support that).
4902 * Note: don't use the DAC post divider as it seems unstable.
4903 */
4904 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004905 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004906
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004907 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004908 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004909
Jesse Barnes89b667f2013-04-18 14:51:36 -07004910 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004911 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004912 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004913 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004914 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004915 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004916 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004917 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004918 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004919
Jesse Barnes89b667f2013-04-18 14:51:36 -07004920 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4921 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4922 /* Use SSC source */
4923 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004924 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004925 0x0df40000);
4926 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004927 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004928 0x0df70000);
4929 } else { /* HDMI or VGA */
4930 /* Use bend source */
4931 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004933 0x0df70000);
4934 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004936 0x0df40000);
4937 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004938
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004939 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004940 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4941 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4942 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4943 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004945
Chon Ming Leeab3c7592013-11-07 10:43:30 +08004946 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004947
Jesse Barnes89b667f2013-04-18 14:51:36 -07004948 /* Enable DPIO clock input */
4949 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4950 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07004951 /* We should never disable this, set it here for state tracking */
4952 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07004953 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004954 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004955 crtc->config.dpll_hw_state.dpll = dpll;
4956
Daniel Vetteref1b4602013-06-01 17:17:04 +02004957 dpll_md = (crtc->config.pixel_multiplier - 1)
4958 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004959 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4960
Daniel Vetterf47709a2013-03-28 10:42:02 +01004961 if (crtc->config.has_dp_encoder)
4962 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304963
Daniel Vetter09153002012-12-12 14:06:44 +01004964 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004965}
4966
Daniel Vetterf47709a2013-03-28 10:42:02 +01004967static void i9xx_update_pll(struct intel_crtc *crtc,
4968 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004969 int num_connectors)
4970{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004971 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004972 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004973 u32 dpll;
4974 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004975 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004976
Daniel Vetterf47709a2013-03-28 10:42:02 +01004977 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304978
Daniel Vetterf47709a2013-03-28 10:42:02 +01004979 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4980 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004981
4982 dpll = DPLL_VGA_MODE_DIS;
4983
Daniel Vetterf47709a2013-03-28 10:42:02 +01004984 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004985 dpll |= DPLLB_MODE_LVDS;
4986 else
4987 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004988
Daniel Vetteref1b4602013-06-01 17:17:04 +02004989 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004990 dpll |= (crtc->config.pixel_multiplier - 1)
4991 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004992 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004993
4994 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004995 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004996
Daniel Vetterf47709a2013-03-28 10:42:02 +01004997 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004998 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004999
5000 /* compute bitmask from p1 value */
5001 if (IS_PINEVIEW(dev))
5002 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5003 else {
5004 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5005 if (IS_G4X(dev) && reduced_clock)
5006 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5007 }
5008 switch (clock->p2) {
5009 case 5:
5010 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5011 break;
5012 case 7:
5013 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5014 break;
5015 case 10:
5016 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5017 break;
5018 case 14:
5019 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5020 break;
5021 }
5022 if (INTEL_INFO(dev)->gen >= 4)
5023 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5024
Daniel Vetter09ede542013-04-30 14:01:45 +02005025 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005026 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005027 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005028 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5029 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5030 else
5031 dpll |= PLL_REF_INPUT_DREFCLK;
5032
5033 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005034 crtc->config.dpll_hw_state.dpll = dpll;
5035
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005036 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005037 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5038 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005039 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005040 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005041
5042 if (crtc->config.has_dp_encoder)
5043 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005044}
5045
Daniel Vetterf47709a2013-03-28 10:42:02 +01005046static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005047 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005048 int num_connectors)
5049{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005050 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005051 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005052 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005053 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005054
Daniel Vetterf47709a2013-03-28 10:42:02 +01005055 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305056
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005057 dpll = DPLL_VGA_MODE_DIS;
5058
Daniel Vetterf47709a2013-03-28 10:42:02 +01005059 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005060 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5061 } else {
5062 if (clock->p1 == 2)
5063 dpll |= PLL_P1_DIVIDE_BY_TWO;
5064 else
5065 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5066 if (clock->p2 == 4)
5067 dpll |= PLL_P2_DIVIDE_BY_4;
5068 }
5069
Daniel Vetter4a33e482013-07-06 12:52:05 +02005070 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5071 dpll |= DPLL_DVO_2X_MODE;
5072
Daniel Vetterf47709a2013-03-28 10:42:02 +01005073 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005074 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5075 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5076 else
5077 dpll |= PLL_REF_INPUT_DREFCLK;
5078
5079 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005080 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005081}
5082
Daniel Vetter8a654f32013-06-01 17:16:22 +02005083static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005084{
5085 struct drm_device *dev = intel_crtc->base.dev;
5086 struct drm_i915_private *dev_priv = dev->dev_private;
5087 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005088 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005089 struct drm_display_mode *adjusted_mode =
5090 &intel_crtc->config.adjusted_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005091 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5092
5093 /* We need to be careful not to changed the adjusted mode, for otherwise
5094 * the hw state checker will get angry at the mismatch. */
5095 crtc_vtotal = adjusted_mode->crtc_vtotal;
5096 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005097
5098 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5099 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005100 crtc_vtotal -= 1;
5101 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005102 vsyncshift = adjusted_mode->crtc_hsync_start
5103 - adjusted_mode->crtc_htotal / 2;
5104 } else {
5105 vsyncshift = 0;
5106 }
5107
5108 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005109 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005110
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005111 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005112 (adjusted_mode->crtc_hdisplay - 1) |
5113 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005114 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005115 (adjusted_mode->crtc_hblank_start - 1) |
5116 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005117 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005118 (adjusted_mode->crtc_hsync_start - 1) |
5119 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5120
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005121 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005122 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005123 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005124 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005125 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005126 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005127 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005128 (adjusted_mode->crtc_vsync_start - 1) |
5129 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5130
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005131 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5132 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5133 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5134 * bits. */
5135 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5136 (pipe == PIPE_B || pipe == PIPE_C))
5137 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5138
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005139 /* pipesrc controls the size that is scaled from, which should
5140 * always be the user's requested size.
5141 */
5142 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005143 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5144 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005145}
5146
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005147static void intel_get_pipe_timings(struct intel_crtc *crtc,
5148 struct intel_crtc_config *pipe_config)
5149{
5150 struct drm_device *dev = crtc->base.dev;
5151 struct drm_i915_private *dev_priv = dev->dev_private;
5152 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5153 uint32_t tmp;
5154
5155 tmp = I915_READ(HTOTAL(cpu_transcoder));
5156 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5157 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5158 tmp = I915_READ(HBLANK(cpu_transcoder));
5159 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5160 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5161 tmp = I915_READ(HSYNC(cpu_transcoder));
5162 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5163 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5164
5165 tmp = I915_READ(VTOTAL(cpu_transcoder));
5166 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5167 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5168 tmp = I915_READ(VBLANK(cpu_transcoder));
5169 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5170 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5171 tmp = I915_READ(VSYNC(cpu_transcoder));
5172 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5173 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5174
5175 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5176 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5177 pipe_config->adjusted_mode.crtc_vtotal += 1;
5178 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5179 }
5180
5181 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005182 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5183 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5184
5185 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5186 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005187}
5188
Jesse Barnesbabea612013-06-26 18:57:38 +03005189static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5190 struct intel_crtc_config *pipe_config)
5191{
5192 struct drm_crtc *crtc = &intel_crtc->base;
5193
5194 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5195 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5196 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5197 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5198
5199 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5200 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5201 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5202 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5203
5204 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5205
Damien Lespiau241bfc32013-09-25 16:45:37 +01005206 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
Jesse Barnesbabea612013-06-26 18:57:38 +03005207 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5208}
5209
Daniel Vetter84b046f2013-02-19 18:48:54 +01005210static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5211{
5212 struct drm_device *dev = intel_crtc->base.dev;
5213 struct drm_i915_private *dev_priv = dev->dev_private;
5214 uint32_t pipeconf;
5215
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005216 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005217
Daniel Vetter67c72a12013-09-24 11:46:14 +02005218 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5219 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5220 pipeconf |= PIPECONF_ENABLE;
5221
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005222 if (intel_crtc->config.double_wide)
5223 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005224
Daniel Vetterff9ce462013-04-24 14:57:17 +02005225 /* only g4x and later have fancy bpc/dither controls */
5226 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005227 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5228 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5229 pipeconf |= PIPECONF_DITHER_EN |
5230 PIPECONF_DITHER_TYPE_SP;
5231
5232 switch (intel_crtc->config.pipe_bpp) {
5233 case 18:
5234 pipeconf |= PIPECONF_6BPC;
5235 break;
5236 case 24:
5237 pipeconf |= PIPECONF_8BPC;
5238 break;
5239 case 30:
5240 pipeconf |= PIPECONF_10BPC;
5241 break;
5242 default:
5243 /* Case prevented by intel_choose_pipe_bpp_dither. */
5244 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005245 }
5246 }
5247
5248 if (HAS_PIPE_CXSR(dev)) {
5249 if (intel_crtc->lowfreq_avail) {
5250 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5251 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5252 } else {
5253 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005254 }
5255 }
5256
Daniel Vetter84b046f2013-02-19 18:48:54 +01005257 if (!IS_GEN2(dev) &&
5258 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5259 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5260 else
5261 pipeconf |= PIPECONF_PROGRESSIVE;
5262
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005263 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5264 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005265
Daniel Vetter84b046f2013-02-19 18:48:54 +01005266 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5267 POSTING_READ(PIPECONF(intel_crtc->pipe));
5268}
5269
Eric Anholtf564048e2011-03-30 13:01:02 -07005270static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005271 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005272 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005273{
5274 struct drm_device *dev = crtc->dev;
5275 struct drm_i915_private *dev_priv = dev->dev_private;
5276 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5277 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005278 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005279 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005280 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005281 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005282 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005283 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005284 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005285 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005286 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005287
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005288 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005289 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005290 case INTEL_OUTPUT_LVDS:
5291 is_lvds = true;
5292 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005293 case INTEL_OUTPUT_DSI:
5294 is_dsi = true;
5295 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005296 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005297
Eric Anholtc751ce42010-03-25 11:48:48 -07005298 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005299 }
5300
Jani Nikulaf2335332013-09-13 11:03:09 +03005301 if (is_dsi)
5302 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005303
Jani Nikulaf2335332013-09-13 11:03:09 +03005304 if (!intel_crtc->config.clock_set) {
5305 refclk = i9xx_get_refclk(crtc, num_connectors);
5306
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005307 /*
5308 * Returns a set of divisors for the desired target clock with
5309 * the given refclk, or FALSE. The returned values represent
5310 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5311 * 2) / p1 / p2.
5312 */
5313 limit = intel_limit(crtc, refclk);
5314 ok = dev_priv->display.find_dpll(limit, crtc,
5315 intel_crtc->config.port_clock,
5316 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005317 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005318 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5319 return -EINVAL;
5320 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005321
Jani Nikulaf2335332013-09-13 11:03:09 +03005322 if (is_lvds && dev_priv->lvds_downclock_avail) {
5323 /*
5324 * Ensure we match the reduced clock's P to the target
5325 * clock. If the clocks don't match, we can't switch
5326 * the display clock by using the FP0/FP1. In such case
5327 * we will disable the LVDS downclock feature.
5328 */
5329 has_reduced_clock =
5330 dev_priv->display.find_dpll(limit, crtc,
5331 dev_priv->lvds_downclock,
5332 refclk, &clock,
5333 &reduced_clock);
5334 }
5335 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005336 intel_crtc->config.dpll.n = clock.n;
5337 intel_crtc->config.dpll.m1 = clock.m1;
5338 intel_crtc->config.dpll.m2 = clock.m2;
5339 intel_crtc->config.dpll.p1 = clock.p1;
5340 intel_crtc->config.dpll.p2 = clock.p2;
5341 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005342
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005343 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005344 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305345 has_reduced_clock ? &reduced_clock : NULL,
5346 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005347 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005348 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005349 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005350 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005351 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005352 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005353 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005354
Jani Nikulaf2335332013-09-13 11:03:09 +03005355skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005356 /* Set up the display plane register */
5357 dspcntr = DISPPLANE_GAMMA_ENABLE;
5358
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005359 if (!IS_VALLEYVIEW(dev)) {
5360 if (pipe == 0)
5361 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5362 else
5363 dspcntr |= DISPPLANE_SEL_PIPE_B;
5364 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005365
Daniel Vetter8a654f32013-06-01 17:16:22 +02005366 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005367
5368 /* pipesrc and dspsize control the size that is scaled from,
5369 * which should always be the user's requested size.
5370 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005371 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005372 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5373 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005374 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005375
Daniel Vetter84b046f2013-02-19 18:48:54 +01005376 i9xx_set_pipeconf(intel_crtc);
5377
Eric Anholtf564048e2011-03-30 13:01:02 -07005378 I915_WRITE(DSPCNTR(plane), dspcntr);
5379 POSTING_READ(DSPCNTR(plane));
5380
Daniel Vetter94352cf2012-07-05 22:51:56 +02005381 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005382
Eric Anholtf564048e2011-03-30 13:01:02 -07005383 return ret;
5384}
5385
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005386static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5387 struct intel_crtc_config *pipe_config)
5388{
5389 struct drm_device *dev = crtc->base.dev;
5390 struct drm_i915_private *dev_priv = dev->dev_private;
5391 uint32_t tmp;
5392
5393 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005394 if (!(tmp & PFIT_ENABLE))
5395 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005396
Daniel Vetter06922822013-07-11 13:35:40 +02005397 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005398 if (INTEL_INFO(dev)->gen < 4) {
5399 if (crtc->pipe != PIPE_B)
5400 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005401 } else {
5402 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5403 return;
5404 }
5405
Daniel Vetter06922822013-07-11 13:35:40 +02005406 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005407 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5408 if (INTEL_INFO(dev)->gen < 5)
5409 pipe_config->gmch_pfit.lvds_border_bits =
5410 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5411}
5412
Jesse Barnesacbec812013-09-20 11:29:32 -07005413static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5414 struct intel_crtc_config *pipe_config)
5415{
5416 struct drm_device *dev = crtc->base.dev;
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 int pipe = pipe_config->cpu_transcoder;
5419 intel_clock_t clock;
5420 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005421 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005422
5423 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005424 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005425 mutex_unlock(&dev_priv->dpio_lock);
5426
5427 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5428 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5429 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5430 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5431 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5432
Ville Syrjäläf6466282013-10-14 14:50:31 +03005433 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005434
Ville Syrjäläf6466282013-10-14 14:50:31 +03005435 /* clock.dot is the fast clock */
5436 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005437}
5438
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005439static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5440 struct intel_crtc_config *pipe_config)
5441{
5442 struct drm_device *dev = crtc->base.dev;
5443 struct drm_i915_private *dev_priv = dev->dev_private;
5444 uint32_t tmp;
5445
Daniel Vettere143a212013-07-04 12:01:15 +02005446 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005447 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005448
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005449 tmp = I915_READ(PIPECONF(crtc->pipe));
5450 if (!(tmp & PIPECONF_ENABLE))
5451 return false;
5452
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005453 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5454 switch (tmp & PIPECONF_BPC_MASK) {
5455 case PIPECONF_6BPC:
5456 pipe_config->pipe_bpp = 18;
5457 break;
5458 case PIPECONF_8BPC:
5459 pipe_config->pipe_bpp = 24;
5460 break;
5461 case PIPECONF_10BPC:
5462 pipe_config->pipe_bpp = 30;
5463 break;
5464 default:
5465 break;
5466 }
5467 }
5468
Ville Syrjälä282740f2013-09-04 18:30:03 +03005469 if (INTEL_INFO(dev)->gen < 4)
5470 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5471
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005472 intel_get_pipe_timings(crtc, pipe_config);
5473
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005474 i9xx_get_pfit_config(crtc, pipe_config);
5475
Daniel Vetter6c49f242013-06-06 12:45:25 +02005476 if (INTEL_INFO(dev)->gen >= 4) {
5477 tmp = I915_READ(DPLL_MD(crtc->pipe));
5478 pipe_config->pixel_multiplier =
5479 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5480 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005481 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005482 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5483 tmp = I915_READ(DPLL(crtc->pipe));
5484 pipe_config->pixel_multiplier =
5485 ((tmp & SDVO_MULTIPLIER_MASK)
5486 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5487 } else {
5488 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5489 * port and will be fixed up in the encoder->get_config
5490 * function. */
5491 pipe_config->pixel_multiplier = 1;
5492 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005493 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5494 if (!IS_VALLEYVIEW(dev)) {
5495 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5496 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005497 } else {
5498 /* Mask out read-only status bits. */
5499 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5500 DPLL_PORTC_READY_MASK |
5501 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005502 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005503
Jesse Barnesacbec812013-09-20 11:29:32 -07005504 if (IS_VALLEYVIEW(dev))
5505 vlv_crtc_clock_get(crtc, pipe_config);
5506 else
5507 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005508
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005509 return true;
5510}
5511
Paulo Zanonidde86e22012-12-01 12:04:25 -02005512static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005513{
5514 struct drm_i915_private *dev_priv = dev->dev_private;
5515 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005516 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005517 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005518 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005519 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005520 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005521 bool has_ck505 = false;
5522 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005523
5524 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005525 list_for_each_entry(encoder, &mode_config->encoder_list,
5526 base.head) {
5527 switch (encoder->type) {
5528 case INTEL_OUTPUT_LVDS:
5529 has_panel = true;
5530 has_lvds = true;
5531 break;
5532 case INTEL_OUTPUT_EDP:
5533 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005534 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005535 has_cpu_edp = true;
5536 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005537 }
5538 }
5539
Keith Packard99eb6a02011-09-26 14:29:12 -07005540 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005541 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005542 can_ssc = has_ck505;
5543 } else {
5544 has_ck505 = false;
5545 can_ssc = true;
5546 }
5547
Imre Deak2de69052013-05-08 13:14:04 +03005548 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5549 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005550
5551 /* Ironlake: try to setup display ref clock before DPLL
5552 * enabling. This is only under driver's control after
5553 * PCH B stepping, previous chipset stepping should be
5554 * ignoring this setting.
5555 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005556 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005557
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005558 /* As we must carefully and slowly disable/enable each source in turn,
5559 * compute the final state we want first and check if we need to
5560 * make any changes at all.
5561 */
5562 final = val;
5563 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005564 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005565 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005566 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005567 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5568
5569 final &= ~DREF_SSC_SOURCE_MASK;
5570 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5571 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005572
Keith Packard199e5d72011-09-22 12:01:57 -07005573 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005574 final |= DREF_SSC_SOURCE_ENABLE;
5575
5576 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5577 final |= DREF_SSC1_ENABLE;
5578
5579 if (has_cpu_edp) {
5580 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5581 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5582 else
5583 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5584 } else
5585 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5586 } else {
5587 final |= DREF_SSC_SOURCE_DISABLE;
5588 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5589 }
5590
5591 if (final == val)
5592 return;
5593
5594 /* Always enable nonspread source */
5595 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5596
5597 if (has_ck505)
5598 val |= DREF_NONSPREAD_CK505_ENABLE;
5599 else
5600 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5601
5602 if (has_panel) {
5603 val &= ~DREF_SSC_SOURCE_MASK;
5604 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005605
Keith Packard199e5d72011-09-22 12:01:57 -07005606 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005607 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005608 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005609 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005610 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005611 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005612
5613 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005614 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005615 POSTING_READ(PCH_DREF_CONTROL);
5616 udelay(200);
5617
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005618 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005619
5620 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005621 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005622 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005623 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005624 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005625 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005626 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005627 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005628 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005629 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005630
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005631 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005632 POSTING_READ(PCH_DREF_CONTROL);
5633 udelay(200);
5634 } else {
5635 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5636
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005637 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005638
5639 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005640 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005641
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005642 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005643 POSTING_READ(PCH_DREF_CONTROL);
5644 udelay(200);
5645
5646 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005647 val &= ~DREF_SSC_SOURCE_MASK;
5648 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005649
5650 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005651 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005652
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005653 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005654 POSTING_READ(PCH_DREF_CONTROL);
5655 udelay(200);
5656 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005657
5658 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005659}
5660
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005661static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005662{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005663 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005664
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005665 tmp = I915_READ(SOUTH_CHICKEN2);
5666 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5667 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005668
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005669 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5670 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5671 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005672
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005673 tmp = I915_READ(SOUTH_CHICKEN2);
5674 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5675 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005676
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005677 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5678 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5679 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005680}
5681
5682/* WaMPhyProgramming:hsw */
5683static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5684{
5685 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005686
5687 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5688 tmp &= ~(0xFF << 24);
5689 tmp |= (0x12 << 24);
5690 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5691
Paulo Zanonidde86e22012-12-01 12:04:25 -02005692 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5693 tmp |= (1 << 11);
5694 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5695
5696 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5697 tmp |= (1 << 11);
5698 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5699
Paulo Zanonidde86e22012-12-01 12:04:25 -02005700 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5701 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5702 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5703
5704 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5705 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5706 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5707
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005708 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5709 tmp &= ~(7 << 13);
5710 tmp |= (5 << 13);
5711 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005712
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005713 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5714 tmp &= ~(7 << 13);
5715 tmp |= (5 << 13);
5716 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005717
5718 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5719 tmp &= ~0xFF;
5720 tmp |= 0x1C;
5721 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5722
5723 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5724 tmp &= ~0xFF;
5725 tmp |= 0x1C;
5726 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5727
5728 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5729 tmp &= ~(0xFF << 16);
5730 tmp |= (0x1C << 16);
5731 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5732
5733 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5734 tmp &= ~(0xFF << 16);
5735 tmp |= (0x1C << 16);
5736 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5737
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005738 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5739 tmp |= (1 << 27);
5740 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005741
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005742 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5743 tmp |= (1 << 27);
5744 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005745
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005746 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5747 tmp &= ~(0xF << 28);
5748 tmp |= (4 << 28);
5749 intel_sbi_write(dev_priv, 0x20EC, 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, 0x21EC, SBI_MPHY);
5752 tmp &= ~(0xF << 28);
5753 tmp |= (4 << 28);
5754 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005755}
5756
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005757/* Implements 3 different sequences from BSpec chapter "Display iCLK
5758 * Programming" based on the parameters passed:
5759 * - Sequence to enable CLKOUT_DP
5760 * - Sequence to enable CLKOUT_DP without spread
5761 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5762 */
5763static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5764 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005765{
5766 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005767 uint32_t reg, tmp;
5768
5769 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5770 with_spread = true;
5771 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5772 with_fdi, "LP PCH doesn't have FDI\n"))
5773 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005774
5775 mutex_lock(&dev_priv->dpio_lock);
5776
5777 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5778 tmp &= ~SBI_SSCCTL_DISABLE;
5779 tmp |= SBI_SSCCTL_PATHALT;
5780 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5781
5782 udelay(24);
5783
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005784 if (with_spread) {
5785 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5786 tmp &= ~SBI_SSCCTL_PATHALT;
5787 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005788
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005789 if (with_fdi) {
5790 lpt_reset_fdi_mphy(dev_priv);
5791 lpt_program_fdi_mphy(dev_priv);
5792 }
5793 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005794
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005795 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5796 SBI_GEN0 : SBI_DBUFF0;
5797 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5798 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5799 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005800
5801 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005802}
5803
Paulo Zanoni47701c32013-07-23 11:19:25 -03005804/* Sequence to disable CLKOUT_DP */
5805static void lpt_disable_clkout_dp(struct drm_device *dev)
5806{
5807 struct drm_i915_private *dev_priv = dev->dev_private;
5808 uint32_t reg, tmp;
5809
5810 mutex_lock(&dev_priv->dpio_lock);
5811
5812 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5813 SBI_GEN0 : SBI_DBUFF0;
5814 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5815 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5816 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5817
5818 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5819 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5820 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5821 tmp |= SBI_SSCCTL_PATHALT;
5822 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5823 udelay(32);
5824 }
5825 tmp |= SBI_SSCCTL_DISABLE;
5826 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5827 }
5828
5829 mutex_unlock(&dev_priv->dpio_lock);
5830}
5831
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005832static void lpt_init_pch_refclk(struct drm_device *dev)
5833{
5834 struct drm_mode_config *mode_config = &dev->mode_config;
5835 struct intel_encoder *encoder;
5836 bool has_vga = false;
5837
5838 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5839 switch (encoder->type) {
5840 case INTEL_OUTPUT_ANALOG:
5841 has_vga = true;
5842 break;
5843 }
5844 }
5845
Paulo Zanoni47701c32013-07-23 11:19:25 -03005846 if (has_vga)
5847 lpt_enable_clkout_dp(dev, true, true);
5848 else
5849 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005850}
5851
Paulo Zanonidde86e22012-12-01 12:04:25 -02005852/*
5853 * Initialize reference clocks when the driver loads
5854 */
5855void intel_init_pch_refclk(struct drm_device *dev)
5856{
5857 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5858 ironlake_init_pch_refclk(dev);
5859 else if (HAS_PCH_LPT(dev))
5860 lpt_init_pch_refclk(dev);
5861}
5862
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005863static int ironlake_get_refclk(struct drm_crtc *crtc)
5864{
5865 struct drm_device *dev = crtc->dev;
5866 struct drm_i915_private *dev_priv = dev->dev_private;
5867 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005868 int num_connectors = 0;
5869 bool is_lvds = false;
5870
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005871 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005872 switch (encoder->type) {
5873 case INTEL_OUTPUT_LVDS:
5874 is_lvds = true;
5875 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005876 }
5877 num_connectors++;
5878 }
5879
5880 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5881 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005882 dev_priv->vbt.lvds_ssc_freq);
5883 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005884 }
5885
5886 return 120000;
5887}
5888
Daniel Vetter6ff93602013-04-19 11:24:36 +02005889static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005890{
5891 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5893 int pipe = intel_crtc->pipe;
5894 uint32_t val;
5895
Daniel Vetter78114072013-06-13 00:54:57 +02005896 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005897
Daniel Vetter965e0c42013-03-27 00:44:57 +01005898 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005899 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005900 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005901 break;
5902 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005903 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005904 break;
5905 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005906 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005907 break;
5908 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005909 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005910 break;
5911 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005912 /* Case prevented by intel_choose_pipe_bpp_dither. */
5913 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005914 }
5915
Daniel Vetterd8b32242013-04-25 17:54:44 +02005916 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005917 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5918
Daniel Vetter6ff93602013-04-19 11:24:36 +02005919 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005920 val |= PIPECONF_INTERLACED_ILK;
5921 else
5922 val |= PIPECONF_PROGRESSIVE;
5923
Daniel Vetter50f3b012013-03-27 00:44:56 +01005924 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005925 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005926
Paulo Zanonic8203562012-09-12 10:06:29 -03005927 I915_WRITE(PIPECONF(pipe), val);
5928 POSTING_READ(PIPECONF(pipe));
5929}
5930
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005931/*
5932 * Set up the pipe CSC unit.
5933 *
5934 * Currently only full range RGB to limited range RGB conversion
5935 * is supported, but eventually this should handle various
5936 * RGB<->YCbCr scenarios as well.
5937 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005938static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005939{
5940 struct drm_device *dev = crtc->dev;
5941 struct drm_i915_private *dev_priv = dev->dev_private;
5942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5943 int pipe = intel_crtc->pipe;
5944 uint16_t coeff = 0x7800; /* 1.0 */
5945
5946 /*
5947 * TODO: Check what kind of values actually come out of the pipe
5948 * with these coeff/postoff values and adjust to get the best
5949 * accuracy. Perhaps we even need to take the bpc value into
5950 * consideration.
5951 */
5952
Daniel Vetter50f3b012013-03-27 00:44:56 +01005953 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005954 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5955
5956 /*
5957 * GY/GU and RY/RU should be the other way around according
5958 * to BSpec, but reality doesn't agree. Just set them up in
5959 * a way that results in the correct picture.
5960 */
5961 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5962 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5963
5964 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5965 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5966
5967 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5968 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5969
5970 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5971 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5972 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5973
5974 if (INTEL_INFO(dev)->gen > 6) {
5975 uint16_t postoff = 0;
5976
Daniel Vetter50f3b012013-03-27 00:44:56 +01005977 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005978 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5979
5980 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5981 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5982 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5983
5984 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5985 } else {
5986 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5987
Daniel Vetter50f3b012013-03-27 00:44:56 +01005988 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005989 mode |= CSC_BLACK_SCREEN_OFFSET;
5990
5991 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5992 }
5993}
5994
Daniel Vetter6ff93602013-04-19 11:24:36 +02005995static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005996{
5997 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005999 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006000 uint32_t val;
6001
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006002 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006003
Daniel Vetterd8b32242013-04-25 17:54:44 +02006004 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006005 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6006
Daniel Vetter6ff93602013-04-19 11:24:36 +02006007 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006008 val |= PIPECONF_INTERLACED_ILK;
6009 else
6010 val |= PIPECONF_PROGRESSIVE;
6011
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006012 I915_WRITE(PIPECONF(cpu_transcoder), val);
6013 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006014
6015 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6016 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006017}
6018
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006019static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006020 intel_clock_t *clock,
6021 bool *has_reduced_clock,
6022 intel_clock_t *reduced_clock)
6023{
6024 struct drm_device *dev = crtc->dev;
6025 struct drm_i915_private *dev_priv = dev->dev_private;
6026 struct intel_encoder *intel_encoder;
6027 int refclk;
6028 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006029 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006030
6031 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6032 switch (intel_encoder->type) {
6033 case INTEL_OUTPUT_LVDS:
6034 is_lvds = true;
6035 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006036 }
6037 }
6038
6039 refclk = ironlake_get_refclk(crtc);
6040
6041 /*
6042 * Returns a set of divisors for the desired target clock with the given
6043 * refclk, or FALSE. The returned values represent the clock equation:
6044 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6045 */
6046 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006047 ret = dev_priv->display.find_dpll(limit, crtc,
6048 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006049 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006050 if (!ret)
6051 return false;
6052
6053 if (is_lvds && dev_priv->lvds_downclock_avail) {
6054 /*
6055 * Ensure we match the reduced clock's P to the target clock.
6056 * If the clocks don't match, we can't switch the display clock
6057 * by using the FP0/FP1. In such case we will disable the LVDS
6058 * downclock feature.
6059 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006060 *has_reduced_clock =
6061 dev_priv->display.find_dpll(limit, crtc,
6062 dev_priv->lvds_downclock,
6063 refclk, clock,
6064 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006065 }
6066
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006067 return true;
6068}
6069
Paulo Zanonid4b19312012-11-29 11:29:32 -02006070int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6071{
6072 /*
6073 * Account for spread spectrum to avoid
6074 * oversubscribing the link. Max center spread
6075 * is 2.5%; use 5% for safety's sake.
6076 */
6077 u32 bps = target_clock * bpp * 21 / 20;
6078 return bps / (link_bw * 8) + 1;
6079}
6080
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006081static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006082{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006083 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006084}
6085
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006086static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006087 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006088 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006089{
6090 struct drm_crtc *crtc = &intel_crtc->base;
6091 struct drm_device *dev = crtc->dev;
6092 struct drm_i915_private *dev_priv = dev->dev_private;
6093 struct intel_encoder *intel_encoder;
6094 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006095 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006096 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -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;
6103 case INTEL_OUTPUT_SDVO:
6104 case INTEL_OUTPUT_HDMI:
6105 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006106 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006107 }
6108
6109 num_connectors++;
6110 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006111
Chris Wilsonc1858122010-12-03 21:35:48 +00006112 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006113 factor = 21;
6114 if (is_lvds) {
6115 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006116 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006117 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006118 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006119 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006120 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006121
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006122 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006123 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006124
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006125 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6126 *fp2 |= FP_CB_TUNE;
6127
Chris Wilson5eddb702010-09-11 13:48:45 +01006128 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006129
Eric Anholta07d6782011-03-30 13:01:08 -07006130 if (is_lvds)
6131 dpll |= DPLLB_MODE_LVDS;
6132 else
6133 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006134
Daniel Vetteref1b4602013-06-01 17:17:04 +02006135 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6136 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006137
6138 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006139 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006140 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006141 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006142
Eric Anholta07d6782011-03-30 13:01:08 -07006143 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006144 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006145 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006146 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006147
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006148 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006149 case 5:
6150 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6151 break;
6152 case 7:
6153 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6154 break;
6155 case 10:
6156 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6157 break;
6158 case 14:
6159 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6160 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006161 }
6162
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006163 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006164 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006165 else
6166 dpll |= PLL_REF_INPUT_DREFCLK;
6167
Daniel Vetter959e16d2013-06-05 13:34:21 +02006168 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006169}
6170
Jesse Barnes79e53942008-11-07 14:24:08 -08006171static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006172 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006173 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006174{
6175 struct drm_device *dev = crtc->dev;
6176 struct drm_i915_private *dev_priv = dev->dev_private;
6177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6178 int pipe = intel_crtc->pipe;
6179 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006180 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006181 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006182 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006183 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006184 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006185 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006186 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006187 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006188
6189 for_each_encoder_on_crtc(dev, crtc, encoder) {
6190 switch (encoder->type) {
6191 case INTEL_OUTPUT_LVDS:
6192 is_lvds = true;
6193 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006194 }
6195
6196 num_connectors++;
6197 }
6198
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006199 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6200 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6201
Daniel Vetterff9a6752013-06-01 17:16:21 +02006202 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006203 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006204 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006205 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6206 return -EINVAL;
6207 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006208 /* Compat-code for transition, will disappear. */
6209 if (!intel_crtc->config.clock_set) {
6210 intel_crtc->config.dpll.n = clock.n;
6211 intel_crtc->config.dpll.m1 = clock.m1;
6212 intel_crtc->config.dpll.m2 = clock.m2;
6213 intel_crtc->config.dpll.p1 = clock.p1;
6214 intel_crtc->config.dpll.p2 = clock.p2;
6215 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006216
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006217 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006218 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006219 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006220 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006221 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006222
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006223 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006224 &fp, &reduced_clock,
6225 has_reduced_clock ? &fp2 : NULL);
6226
Daniel Vetter959e16d2013-06-05 13:34:21 +02006227 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006228 intel_crtc->config.dpll_hw_state.fp0 = fp;
6229 if (has_reduced_clock)
6230 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6231 else
6232 intel_crtc->config.dpll_hw_state.fp1 = fp;
6233
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006234 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006235 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006236 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6237 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006238 return -EINVAL;
6239 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006240 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006241 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006242
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006243 if (intel_crtc->config.has_dp_encoder)
6244 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006245
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006246 if (is_lvds && has_reduced_clock && i915_powersave)
6247 intel_crtc->lowfreq_avail = true;
6248 else
6249 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006250
Daniel Vetter8a654f32013-06-01 17:16:22 +02006251 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006252
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006253 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006254 intel_cpu_transcoder_set_m_n(intel_crtc,
6255 &intel_crtc->config.fdi_m_n);
6256 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006257
Daniel Vetter6ff93602013-04-19 11:24:36 +02006258 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006259
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006260 /* Set up the display plane register */
6261 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006262 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006263
Daniel Vetter94352cf2012-07-05 22:51:56 +02006264 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006265
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006266 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006267}
6268
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006269static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6270 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006271{
6272 struct drm_device *dev = crtc->base.dev;
6273 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006274 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006275
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006276 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6277 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6278 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6279 & ~TU_SIZE_MASK;
6280 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6281 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6282 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6283}
6284
6285static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6286 enum transcoder transcoder,
6287 struct intel_link_m_n *m_n)
6288{
6289 struct drm_device *dev = crtc->base.dev;
6290 struct drm_i915_private *dev_priv = dev->dev_private;
6291 enum pipe pipe = crtc->pipe;
6292
6293 if (INTEL_INFO(dev)->gen >= 5) {
6294 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6295 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6296 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6297 & ~TU_SIZE_MASK;
6298 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6299 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6300 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6301 } else {
6302 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6303 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6304 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6305 & ~TU_SIZE_MASK;
6306 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6307 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6308 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6309 }
6310}
6311
6312void intel_dp_get_m_n(struct intel_crtc *crtc,
6313 struct intel_crtc_config *pipe_config)
6314{
6315 if (crtc->config.has_pch_encoder)
6316 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6317 else
6318 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6319 &pipe_config->dp_m_n);
6320}
6321
Daniel Vetter72419202013-04-04 13:28:53 +02006322static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6323 struct intel_crtc_config *pipe_config)
6324{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006325 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6326 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006327}
6328
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006329static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6330 struct intel_crtc_config *pipe_config)
6331{
6332 struct drm_device *dev = crtc->base.dev;
6333 struct drm_i915_private *dev_priv = dev->dev_private;
6334 uint32_t tmp;
6335
6336 tmp = I915_READ(PF_CTL(crtc->pipe));
6337
6338 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006339 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006340 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6341 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006342
6343 /* We currently do not free assignements of panel fitters on
6344 * ivb/hsw (since we don't use the higher upscaling modes which
6345 * differentiates them) so just WARN about this case for now. */
6346 if (IS_GEN7(dev)) {
6347 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6348 PF_PIPE_SEL_IVB(crtc->pipe));
6349 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006350 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006351}
6352
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006353static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6354 struct intel_crtc_config *pipe_config)
6355{
6356 struct drm_device *dev = crtc->base.dev;
6357 struct drm_i915_private *dev_priv = dev->dev_private;
6358 uint32_t tmp;
6359
Daniel Vettere143a212013-07-04 12:01:15 +02006360 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006361 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006362
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006363 tmp = I915_READ(PIPECONF(crtc->pipe));
6364 if (!(tmp & PIPECONF_ENABLE))
6365 return false;
6366
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006367 switch (tmp & PIPECONF_BPC_MASK) {
6368 case PIPECONF_6BPC:
6369 pipe_config->pipe_bpp = 18;
6370 break;
6371 case PIPECONF_8BPC:
6372 pipe_config->pipe_bpp = 24;
6373 break;
6374 case PIPECONF_10BPC:
6375 pipe_config->pipe_bpp = 30;
6376 break;
6377 case PIPECONF_12BPC:
6378 pipe_config->pipe_bpp = 36;
6379 break;
6380 default:
6381 break;
6382 }
6383
Daniel Vetterab9412b2013-05-03 11:49:46 +02006384 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006385 struct intel_shared_dpll *pll;
6386
Daniel Vetter88adfff2013-03-28 10:42:01 +01006387 pipe_config->has_pch_encoder = true;
6388
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006389 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6390 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6391 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006392
6393 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006394
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006395 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006396 pipe_config->shared_dpll =
6397 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006398 } else {
6399 tmp = I915_READ(PCH_DPLL_SEL);
6400 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6401 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6402 else
6403 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6404 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006405
6406 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6407
6408 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6409 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006410
6411 tmp = pipe_config->dpll_hw_state.dpll;
6412 pipe_config->pixel_multiplier =
6413 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6414 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006415
6416 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006417 } else {
6418 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006419 }
6420
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006421 intel_get_pipe_timings(crtc, pipe_config);
6422
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006423 ironlake_get_pfit_config(crtc, pipe_config);
6424
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006425 return true;
6426}
6427
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006428static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6429{
6430 struct drm_device *dev = dev_priv->dev;
6431 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6432 struct intel_crtc *crtc;
6433 unsigned long irqflags;
Paulo Zanonibd633a72013-08-19 13:18:08 -03006434 uint32_t val;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006435
6436 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6437 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6438 pipe_name(crtc->pipe));
6439
6440 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6441 WARN(plls->spll_refcount, "SPLL enabled\n");
6442 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6443 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6444 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6445 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6446 "CPU PWM1 enabled\n");
6447 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6448 "CPU PWM2 enabled\n");
6449 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6450 "PCH PWM1 enabled\n");
6451 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6452 "Utility pin enabled\n");
6453 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6454
6455 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6456 val = I915_READ(DEIMR);
6457 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6458 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6459 val = I915_READ(SDEIMR);
Paulo Zanonibd633a72013-08-19 13:18:08 -03006460 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006461 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6462 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6463}
6464
6465/*
6466 * This function implements pieces of two sequences from BSpec:
6467 * - Sequence for display software to disable LCPLL
6468 * - Sequence for display software to allow package C8+
6469 * The steps implemented here are just the steps that actually touch the LCPLL
6470 * register. Callers should take care of disabling all the display engine
6471 * functions, doing the mode unset, fixing interrupts, etc.
6472 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006473static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6474 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006475{
6476 uint32_t val;
6477
6478 assert_can_disable_lcpll(dev_priv);
6479
6480 val = I915_READ(LCPLL_CTL);
6481
6482 if (switch_to_fclk) {
6483 val |= LCPLL_CD_SOURCE_FCLK;
6484 I915_WRITE(LCPLL_CTL, val);
6485
6486 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6487 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6488 DRM_ERROR("Switching to FCLK failed\n");
6489
6490 val = I915_READ(LCPLL_CTL);
6491 }
6492
6493 val |= LCPLL_PLL_DISABLE;
6494 I915_WRITE(LCPLL_CTL, val);
6495 POSTING_READ(LCPLL_CTL);
6496
6497 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6498 DRM_ERROR("LCPLL still locked\n");
6499
6500 val = I915_READ(D_COMP);
6501 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006502 mutex_lock(&dev_priv->rps.hw_lock);
6503 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6504 DRM_ERROR("Failed to disable D_COMP\n");
6505 mutex_unlock(&dev_priv->rps.hw_lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006506 POSTING_READ(D_COMP);
6507 ndelay(100);
6508
6509 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6510 DRM_ERROR("D_COMP RCOMP still in progress\n");
6511
6512 if (allow_power_down) {
6513 val = I915_READ(LCPLL_CTL);
6514 val |= LCPLL_POWER_DOWN_ALLOW;
6515 I915_WRITE(LCPLL_CTL, val);
6516 POSTING_READ(LCPLL_CTL);
6517 }
6518}
6519
6520/*
6521 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6522 * source.
6523 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03006524static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006525{
6526 uint32_t val;
6527
6528 val = I915_READ(LCPLL_CTL);
6529
6530 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6531 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6532 return;
6533
Paulo Zanoni215733f2013-08-19 13:18:07 -03006534 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6535 * we'll hang the machine! */
6536 dev_priv->uncore.funcs.force_wake_get(dev_priv);
6537
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006538 if (val & LCPLL_POWER_DOWN_ALLOW) {
6539 val &= ~LCPLL_POWER_DOWN_ALLOW;
6540 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006541 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006542 }
6543
6544 val = I915_READ(D_COMP);
6545 val |= D_COMP_COMP_FORCE;
6546 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni515b2392013-09-10 19:36:37 -03006547 mutex_lock(&dev_priv->rps.hw_lock);
6548 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6549 DRM_ERROR("Failed to enable D_COMP\n");
6550 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02006551 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006552
6553 val = I915_READ(LCPLL_CTL);
6554 val &= ~LCPLL_PLL_DISABLE;
6555 I915_WRITE(LCPLL_CTL, val);
6556
6557 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6558 DRM_ERROR("LCPLL not locked yet\n");
6559
6560 if (val & LCPLL_CD_SOURCE_FCLK) {
6561 val = I915_READ(LCPLL_CTL);
6562 val &= ~LCPLL_CD_SOURCE_FCLK;
6563 I915_WRITE(LCPLL_CTL, val);
6564
6565 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6566 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6567 DRM_ERROR("Switching back to LCPLL failed\n");
6568 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03006569
6570 dev_priv->uncore.funcs.force_wake_put(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006571}
6572
Paulo Zanonic67a4702013-08-19 13:18:09 -03006573void hsw_enable_pc8_work(struct work_struct *__work)
6574{
6575 struct drm_i915_private *dev_priv =
6576 container_of(to_delayed_work(__work), struct drm_i915_private,
6577 pc8.enable_work);
6578 struct drm_device *dev = dev_priv->dev;
6579 uint32_t val;
6580
6581 if (dev_priv->pc8.enabled)
6582 return;
6583
6584 DRM_DEBUG_KMS("Enabling package C8+\n");
6585
6586 dev_priv->pc8.enabled = true;
6587
6588 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6589 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6590 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6591 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6592 }
6593
6594 lpt_disable_clkout_dp(dev);
6595 hsw_pc8_disable_interrupts(dev);
6596 hsw_disable_lcpll(dev_priv, true, true);
6597}
6598
6599static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6600{
6601 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6602 WARN(dev_priv->pc8.disable_count < 1,
6603 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6604
6605 dev_priv->pc8.disable_count--;
6606 if (dev_priv->pc8.disable_count != 0)
6607 return;
6608
6609 schedule_delayed_work(&dev_priv->pc8.enable_work,
Paulo Zanoni90058742013-08-19 13:18:11 -03006610 msecs_to_jiffies(i915_pc8_timeout));
Paulo Zanonic67a4702013-08-19 13:18:09 -03006611}
6612
6613static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6614{
6615 struct drm_device *dev = dev_priv->dev;
6616 uint32_t val;
6617
6618 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6619 WARN(dev_priv->pc8.disable_count < 0,
6620 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6621
6622 dev_priv->pc8.disable_count++;
6623 if (dev_priv->pc8.disable_count != 1)
6624 return;
6625
6626 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6627 if (!dev_priv->pc8.enabled)
6628 return;
6629
6630 DRM_DEBUG_KMS("Disabling package C8+\n");
6631
6632 hsw_restore_lcpll(dev_priv);
6633 hsw_pc8_restore_interrupts(dev);
6634 lpt_init_pch_refclk(dev);
6635
6636 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6637 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6638 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6639 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6640 }
6641
6642 intel_prepare_ddi(dev);
6643 i915_gem_init_swizzling(dev);
6644 mutex_lock(&dev_priv->rps.hw_lock);
6645 gen6_update_ring_freq(dev);
6646 mutex_unlock(&dev_priv->rps.hw_lock);
6647 dev_priv->pc8.enabled = false;
6648}
6649
6650void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6651{
6652 mutex_lock(&dev_priv->pc8.lock);
6653 __hsw_enable_package_c8(dev_priv);
6654 mutex_unlock(&dev_priv->pc8.lock);
6655}
6656
6657void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6658{
6659 mutex_lock(&dev_priv->pc8.lock);
6660 __hsw_disable_package_c8(dev_priv);
6661 mutex_unlock(&dev_priv->pc8.lock);
6662}
6663
6664static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6665{
6666 struct drm_device *dev = dev_priv->dev;
6667 struct intel_crtc *crtc;
6668 uint32_t val;
6669
6670 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6671 if (crtc->base.enabled)
6672 return false;
6673
6674 /* This case is still possible since we have the i915.disable_power_well
6675 * parameter and also the KVMr or something else might be requesting the
6676 * power well. */
6677 val = I915_READ(HSW_PWR_WELL_DRIVER);
6678 if (val != 0) {
6679 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6680 return false;
6681 }
6682
6683 return true;
6684}
6685
6686/* Since we're called from modeset_global_resources there's no way to
6687 * symmetrically increase and decrease the refcount, so we use
6688 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6689 * or not.
6690 */
6691static void hsw_update_package_c8(struct drm_device *dev)
6692{
6693 struct drm_i915_private *dev_priv = dev->dev_private;
6694 bool allow;
6695
6696 if (!i915_enable_pc8)
6697 return;
6698
6699 mutex_lock(&dev_priv->pc8.lock);
6700
6701 allow = hsw_can_enable_package_c8(dev_priv);
6702
6703 if (allow == dev_priv->pc8.requirements_met)
6704 goto done;
6705
6706 dev_priv->pc8.requirements_met = allow;
6707
6708 if (allow)
6709 __hsw_enable_package_c8(dev_priv);
6710 else
6711 __hsw_disable_package_c8(dev_priv);
6712
6713done:
6714 mutex_unlock(&dev_priv->pc8.lock);
6715}
6716
6717static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6718{
6719 if (!dev_priv->pc8.gpu_idle) {
6720 dev_priv->pc8.gpu_idle = true;
6721 hsw_enable_package_c8(dev_priv);
6722 }
6723}
6724
6725static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6726{
6727 if (dev_priv->pc8.gpu_idle) {
6728 dev_priv->pc8.gpu_idle = false;
6729 hsw_disable_package_c8(dev_priv);
6730 }
Daniel Vetter94352cf2012-07-05 22:51:56 +02006731}
Eric Anholtf564048e2011-03-30 13:01:02 -07006732
Imre Deak6efdf352013-10-16 17:25:52 +03006733#define for_each_power_domain(domain, mask) \
6734 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6735 if ((1 << (domain)) & (mask))
6736
6737static unsigned long get_pipe_power_domains(struct drm_device *dev,
6738 enum pipe pipe, bool pfit_enabled)
6739{
6740 unsigned long mask;
6741 enum transcoder transcoder;
6742
6743 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6744
6745 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6746 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6747 if (pfit_enabled)
6748 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6749
6750 return mask;
6751}
6752
Imre Deakbaa70702013-10-25 17:36:48 +03006753void intel_display_set_init_power(struct drm_device *dev, bool enable)
6754{
6755 struct drm_i915_private *dev_priv = dev->dev_private;
6756
6757 if (dev_priv->power_domains.init_power_on == enable)
6758 return;
6759
6760 if (enable)
6761 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6762 else
6763 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6764
6765 dev_priv->power_domains.init_power_on = enable;
6766}
6767
Imre Deak4f074122013-10-16 17:25:51 +03006768static void modeset_update_power_wells(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006769{
Imre Deak6efdf352013-10-16 17:25:52 +03006770 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
Jesse Barnes79e53942008-11-07 14:24:08 -08006771 struct intel_crtc *crtc;
6772
Imre Deak6efdf352013-10-16 17:25:52 +03006773 /*
6774 * First get all needed power domains, then put all unneeded, to avoid
6775 * any unnecessary toggling of the power wells.
6776 */
Jesse Barnes79e53942008-11-07 14:24:08 -08006777 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Imre Deak6efdf352013-10-16 17:25:52 +03006778 enum intel_display_power_domain domain;
6779
Jesse Barnes79e53942008-11-07 14:24:08 -08006780 if (!crtc->base.enabled)
6781 continue;
6782
Imre Deak6efdf352013-10-16 17:25:52 +03006783 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6784 crtc->pipe,
6785 crtc->config.pch_pfit.enabled);
6786
6787 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6788 intel_display_power_get(dev, domain);
Jesse Barnes79e53942008-11-07 14:24:08 -08006789 }
6790
Imre Deak6efdf352013-10-16 17:25:52 +03006791 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6792 enum intel_display_power_domain domain;
6793
6794 for_each_power_domain(domain, crtc->enabled_power_domains)
6795 intel_display_power_put(dev, domain);
6796
6797 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6798 }
Imre Deakbaa70702013-10-25 17:36:48 +03006799
6800 intel_display_set_init_power(dev, false);
Imre Deak4f074122013-10-16 17:25:51 +03006801}
Paulo Zanonic67a4702013-08-19 13:18:09 -03006802
Imre Deak4f074122013-10-16 17:25:51 +03006803static void haswell_modeset_global_resources(struct drm_device *dev)
6804{
6805 modeset_update_power_wells(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006806 hsw_update_package_c8(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006807}
6808
6809static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6810 int x, int y,
6811 struct drm_framebuffer *fb)
6812{
6813 struct drm_device *dev = crtc->dev;
6814 struct drm_i915_private *dev_priv = dev->dev_private;
6815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6816 int plane = intel_crtc->plane;
6817 int ret;
6818
6819 if (!intel_ddi_pll_mode_set(crtc))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006820 return -EINVAL;
Eric Anholtbad720f2009-10-22 16:11:14 -07006821
Chris Wilson560b85b2010-08-07 11:01:38 +01006822 if (intel_crtc->config.has_dp_encoder)
6823 intel_dp_set_m_n(intel_crtc);
6824
6825 intel_crtc->lowfreq_avail = false;
6826
6827 intel_set_pipe_timings(intel_crtc);
6828
6829 if (intel_crtc->config.has_pch_encoder) {
6830 intel_cpu_transcoder_set_m_n(intel_crtc,
6831 &intel_crtc->config.fdi_m_n);
6832 }
6833
6834 haswell_set_pipeconf(crtc);
6835
6836 intel_set_pipe_csc(crtc);
6837
6838 /* Set up the display plane register */
6839 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6840 POSTING_READ(DSPCNTR(plane));
6841
6842 ret = intel_pipe_set_base(crtc, x, y, fb);
6843
Chris Wilson560b85b2010-08-07 11:01:38 +01006844 return ret;
6845}
6846
6847static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6848 struct intel_crtc_config *pipe_config)
6849{
6850 struct drm_device *dev = crtc->base.dev;
6851 struct drm_i915_private *dev_priv = dev->dev_private;
6852 enum intel_display_power_domain pfit_domain;
6853 uint32_t tmp;
6854
6855 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6856 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6857
6858 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6859 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6860 enum pipe trans_edp_pipe;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006861 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
Chris Wilson6b383a72010-09-13 13:54:26 +01006862 default:
6863 WARN(1, "unknown pipe linked to edp transcoder\n");
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006864 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6865 case TRANS_DDI_EDP_INPUT_A_ON:
6866 trans_edp_pipe = PIPE_A;
6867 break;
6868 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6869 trans_edp_pipe = PIPE_B;
6870 break;
Chris Wilson560b85b2010-08-07 11:01:38 +01006871 case TRANS_DDI_EDP_INPUT_C_ONOFF:
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006872 trans_edp_pipe = PIPE_C;
6873 break;
6874 }
6875
Chris Wilson6b383a72010-09-13 13:54:26 +01006876 if (trans_edp_pipe == crtc->pipe)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006877 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6878 }
6879
6880 if (!intel_display_power_enabled(dev,
6881 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6882 return false;
6883
6884 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6885 if (!(tmp & PIPECONF_ENABLE))
6886 return false;
6887
6888 /*
6889 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6890 * DDI E. So just check whether this pipe is wired to DDI E and whether
6891 * the PCH transcoder is on.
6892 */
6893 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6894 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6895 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6896 pipe_config->has_pch_encoder = true;
6897
6898 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6899 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6900 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6901
6902 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6903 }
6904
Chris Wilson560b85b2010-08-07 11:01:38 +01006905 intel_get_pipe_timings(crtc, pipe_config);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006906
6907 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6908 if (intel_display_power_enabled(dev, pfit_domain))
Chris Wilson560b85b2010-08-07 11:01:38 +01006909 ironlake_get_pfit_config(crtc, pipe_config);
6910
6911 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6912 (I915_READ(IPS_CTL) & IPS_ENABLE);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006913
6914 pipe_config->pixel_multiplier = 1;
6915
6916 return true;
6917}
Jesse Barnes79e53942008-11-07 14:24:08 -08006918
Chris Wilson05394f32010-11-08 19:18:58 +00006919static int intel_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006920 int x, int y,
6921 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006922{
Daniel Vetter9256aa12012-10-31 19:26:13 +01006923 struct drm_device *dev = crtc->dev;
6924 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtf564048e2011-03-30 13:01:02 -07006925 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006927 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006928 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006929 int ret;
6930
Eric Anholt0b701d22011-03-30 13:01:03 -07006931 drm_vblank_pre_modeset(dev, pipe);
6932
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006933 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6934
Jesse Barnes79e53942008-11-07 14:24:08 -08006935 drm_vblank_post_modeset(dev, pipe);
6936
Daniel Vetter9256aa12012-10-31 19:26:13 +01006937 if (ret != 0)
6938 return ret;
6939
6940 for_each_encoder_on_crtc(dev, crtc, encoder) {
6941 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6942 encoder->base.base.id,
6943 drm_get_encoder_name(&encoder->base),
6944 mode->base.id, mode->name);
Daniel Vetter36f2d1f2013-07-21 21:37:08 +02006945 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01006946 }
6947
6948 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006949}
6950
Jani Nikula1a915102013-10-16 12:34:48 +03006951static struct {
6952 int clock;
6953 u32 config;
6954} hdmi_audio_clock[] = {
6955 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
6956 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
6957 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
6958 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
6959 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
6960 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
6961 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
6962 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
6963 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
6964 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
6965};
6966
6967/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
6968static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
6969{
6970 int i;
6971
6972 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
6973 if (mode->clock == hdmi_audio_clock[i].clock)
6974 break;
6975 }
6976
6977 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
6978 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
6979 i = 1;
6980 }
6981
6982 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
6983 hdmi_audio_clock[i].clock,
6984 hdmi_audio_clock[i].config);
6985
6986 return hdmi_audio_clock[i].config;
6987}
6988
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006989static bool intel_eld_uptodate(struct drm_connector *connector,
6990 int reg_eldv, uint32_t bits_eldv,
6991 int reg_elda, uint32_t bits_elda,
6992 int reg_edid)
6993{
6994 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6995 uint8_t *eld = connector->eld;
6996 uint32_t i;
6997
6998 i = I915_READ(reg_eldv);
6999 i &= bits_eldv;
7000
7001 if (!eld[0])
7002 return !i;
7003
7004 if (!i)
7005 return false;
7006
7007 i = I915_READ(reg_elda);
7008 i &= ~bits_elda;
7009 I915_WRITE(reg_elda, i);
7010
7011 for (i = 0; i < eld[2]; i++)
7012 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7013 return false;
7014
7015 return true;
7016}
7017
Wu Fengguange0dac652011-09-05 14:25:34 +08007018static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007019 struct drm_crtc *crtc,
7020 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007021{
7022 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7023 uint8_t *eld = connector->eld;
7024 uint32_t eldv;
7025 uint32_t len;
7026 uint32_t i;
7027
7028 i = I915_READ(G4X_AUD_VID_DID);
7029
7030 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7031 eldv = G4X_ELDV_DEVCL_DEVBLC;
7032 else
7033 eldv = G4X_ELDV_DEVCTG;
7034
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007035 if (intel_eld_uptodate(connector,
7036 G4X_AUD_CNTL_ST, eldv,
7037 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7038 G4X_HDMIW_HDMIEDID))
7039 return;
7040
Wu Fengguange0dac652011-09-05 14:25:34 +08007041 i = I915_READ(G4X_AUD_CNTL_ST);
7042 i &= ~(eldv | G4X_ELD_ADDR);
7043 len = (i >> 9) & 0x1f; /* ELD buffer size */
7044 I915_WRITE(G4X_AUD_CNTL_ST, i);
7045
7046 if (!eld[0])
7047 return;
7048
7049 len = min_t(uint8_t, eld[2], len);
7050 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7051 for (i = 0; i < len; i++)
7052 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7053
7054 i = I915_READ(G4X_AUD_CNTL_ST);
7055 i |= eldv;
7056 I915_WRITE(G4X_AUD_CNTL_ST, i);
7057}
7058
Wang Xingchao83358c852012-08-16 22:43:37 +08007059static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007060 struct drm_crtc *crtc,
7061 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007062{
7063 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7064 uint8_t *eld = connector->eld;
7065 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007067 uint32_t eldv;
7068 uint32_t i;
7069 int len;
7070 int pipe = to_intel_crtc(crtc)->pipe;
7071 int tmp;
7072
7073 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7074 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7075 int aud_config = HSW_AUD_CFG(pipe);
7076 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7077
7078
7079 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7080
7081 /* Audio output enable */
7082 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7083 tmp = I915_READ(aud_cntrl_st2);
7084 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7085 I915_WRITE(aud_cntrl_st2, tmp);
7086
7087 /* Wait for 1 vertical blank */
7088 intel_wait_for_vblank(dev, pipe);
7089
7090 /* Set ELD valid state */
7091 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007092 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007093 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7094 I915_WRITE(aud_cntrl_st2, tmp);
7095 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007096 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007097
7098 /* Enable HDMI mode */
7099 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007100 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007101 /* clear N_programing_enable and N_value_index */
7102 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7103 I915_WRITE(aud_config, tmp);
7104
7105 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7106
7107 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007108 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007109
7110 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7111 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7112 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7113 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007114 } else {
7115 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7116 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007117
7118 if (intel_eld_uptodate(connector,
7119 aud_cntrl_st2, eldv,
7120 aud_cntl_st, IBX_ELD_ADDRESS,
7121 hdmiw_hdmiedid))
7122 return;
7123
7124 i = I915_READ(aud_cntrl_st2);
7125 i &= ~eldv;
7126 I915_WRITE(aud_cntrl_st2, i);
7127
7128 if (!eld[0])
7129 return;
7130
7131 i = I915_READ(aud_cntl_st);
7132 i &= ~IBX_ELD_ADDRESS;
7133 I915_WRITE(aud_cntl_st, i);
7134 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7135 DRM_DEBUG_DRIVER("port num:%d\n", i);
7136
7137 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7138 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7139 for (i = 0; i < len; i++)
7140 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7141
7142 i = I915_READ(aud_cntrl_st2);
7143 i |= eldv;
7144 I915_WRITE(aud_cntrl_st2, i);
7145
7146}
7147
Wu Fengguange0dac652011-09-05 14:25:34 +08007148static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007149 struct drm_crtc *crtc,
7150 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007151{
7152 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7153 uint8_t *eld = connector->eld;
7154 uint32_t eldv;
7155 uint32_t i;
7156 int len;
7157 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007158 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007159 int aud_cntl_st;
7160 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007161 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007162
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007163 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007164 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7165 aud_config = IBX_AUD_CFG(pipe);
7166 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007167 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007168 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007169 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7170 aud_config = CPT_AUD_CFG(pipe);
7171 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007172 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007173 }
7174
Wang Xingchao9b138a82012-08-09 16:52:18 +08007175 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007176
7177 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08007178 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08007179 if (!i) {
7180 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7181 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007182 eldv = IBX_ELD_VALIDB;
7183 eldv |= IBX_ELD_VALIDB << 4;
7184 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007185 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007186 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007187 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007188 }
7189
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007190 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7191 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7192 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007193 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007194 } else {
7195 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7196 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007197
7198 if (intel_eld_uptodate(connector,
7199 aud_cntrl_st2, eldv,
7200 aud_cntl_st, IBX_ELD_ADDRESS,
7201 hdmiw_hdmiedid))
7202 return;
7203
Wu Fengguange0dac652011-09-05 14:25:34 +08007204 i = I915_READ(aud_cntrl_st2);
7205 i &= ~eldv;
7206 I915_WRITE(aud_cntrl_st2, i);
7207
7208 if (!eld[0])
7209 return;
7210
Wu Fengguange0dac652011-09-05 14:25:34 +08007211 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007212 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007213 I915_WRITE(aud_cntl_st, i);
7214
7215 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7216 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7217 for (i = 0; i < len; i++)
7218 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7219
7220 i = I915_READ(aud_cntrl_st2);
7221 i |= eldv;
7222 I915_WRITE(aud_cntrl_st2, i);
7223}
7224
7225void intel_write_eld(struct drm_encoder *encoder,
7226 struct drm_display_mode *mode)
7227{
7228 struct drm_crtc *crtc = encoder->crtc;
7229 struct drm_connector *connector;
7230 struct drm_device *dev = encoder->dev;
7231 struct drm_i915_private *dev_priv = dev->dev_private;
7232
7233 connector = drm_select_eld(encoder, mode);
7234 if (!connector)
7235 return;
7236
7237 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7238 connector->base.id,
7239 drm_get_connector_name(connector),
7240 connector->encoder->base.id,
7241 drm_get_encoder_name(connector->encoder));
7242
7243 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7244
7245 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007246 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007247}
7248
Jesse Barnes79e53942008-11-07 14:24:08 -08007249static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7250{
7251 struct drm_device *dev = crtc->dev;
7252 struct drm_i915_private *dev_priv = dev->dev_private;
7253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7254 bool visible = base != 0;
7255 u32 cntl;
7256
7257 if (intel_crtc->cursor_visible == visible)
7258 return;
7259
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007260 cntl = I915_READ(_CURACNTR);
Jesse Barnes79e53942008-11-07 14:24:08 -08007261 if (visible) {
7262 /* On these chipsets we can only modify the base whilst
7263 * the cursor is disabled.
7264 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007265 I915_WRITE(_CURABASE, base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007266
7267 cntl &= ~(CURSOR_FORMAT_MASK);
7268 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7269 cntl |= CURSOR_ENABLE |
7270 CURSOR_GAMMA_ENABLE |
7271 CURSOR_FORMAT_ARGB;
7272 } else
7273 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007274 I915_WRITE(_CURACNTR, cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007275
7276 intel_crtc->cursor_visible = visible;
7277}
7278
7279static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7280{
7281 struct drm_device *dev = crtc->dev;
7282 struct drm_i915_private *dev_priv = dev->dev_private;
7283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7284 int pipe = intel_crtc->pipe;
7285 bool visible = base != 0;
7286
7287 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08007288 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007289 if (base) {
7290 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7291 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7292 cntl |= pipe << 28; /* Connect to correct pipe */
7293 } else {
7294 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7295 cntl |= CURSOR_MODE_DISABLE;
7296 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007297 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes79e53942008-11-07 14:24:08 -08007298
7299 intel_crtc->cursor_visible = visible;
7300 }
7301 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007302 I915_WRITE(CURBASE(pipe), base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007303}
7304
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007305static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7306{
7307 struct drm_device *dev = crtc->dev;
7308 struct drm_i915_private *dev_priv = dev->dev_private;
7309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7310 int pipe = intel_crtc->pipe;
7311 bool visible = base != 0;
7312
7313 if (intel_crtc->cursor_visible != visible) {
7314 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7315 if (base) {
7316 cntl &= ~CURSOR_MODE;
7317 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7318 } else {
7319 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7320 cntl |= CURSOR_MODE_DISABLE;
7321 }
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007322 if (IS_HASWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007323 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007324 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7325 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007326 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7327
7328 intel_crtc->cursor_visible = visible;
7329 }
7330 /* and commit changes on next vblank */
7331 I915_WRITE(CURBASE_IVB(pipe), base);
7332}
7333
Jesse Barnes79e53942008-11-07 14:24:08 -08007334/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7335static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7336 bool on)
7337{
7338 struct drm_device *dev = crtc->dev;
7339 struct drm_i915_private *dev_priv = dev->dev_private;
7340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7341 int pipe = intel_crtc->pipe;
7342 int x = intel_crtc->cursor_x;
7343 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007344 u32 base = 0, pos = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007345 bool visible;
7346
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007347 if (on)
Jesse Barnes79e53942008-11-07 14:24:08 -08007348 base = intel_crtc->cursor_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08007349
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007350 if (x >= intel_crtc->config.pipe_src_w)
7351 base = 0;
7352
7353 if (y >= intel_crtc->config.pipe_src_h)
Jesse Barnes79e53942008-11-07 14:24:08 -08007354 base = 0;
7355
7356 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007357 if (x + intel_crtc->cursor_width <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007358 base = 0;
7359
7360 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7361 x = -x;
7362 }
7363 pos |= x << CURSOR_X_SHIFT;
7364
7365 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007366 if (y + intel_crtc->cursor_height <= 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08007367 base = 0;
7368
7369 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7370 y = -y;
7371 }
7372 pos |= y << CURSOR_Y_SHIFT;
7373
7374 visible = base != 0;
7375 if (!visible && !intel_crtc->cursor_visible)
7376 return;
7377
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03007378 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007379 I915_WRITE(CURPOS_IVB(pipe), pos);
7380 ivb_update_cursor(crtc, base);
7381 } else {
7382 I915_WRITE(CURPOS(pipe), pos);
7383 if (IS_845G(dev) || IS_I865G(dev))
7384 i845_update_cursor(crtc, base);
7385 else
7386 i9xx_update_cursor(crtc, base);
7387 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007388}
7389
7390static int intel_crtc_cursor_set(struct drm_crtc *crtc,
7391 struct drm_file *file,
7392 uint32_t handle,
7393 uint32_t width, uint32_t height)
7394{
7395 struct drm_device *dev = crtc->dev;
7396 struct drm_i915_private *dev_priv = dev->dev_private;
7397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007398 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007399 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007400 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007401
Jesse Barnes79e53942008-11-07 14:24:08 -08007402 /* if we want to turn off the cursor ignore width and height */
7403 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007404 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007405 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007406 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007407 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007408 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007409 }
7410
7411 /* Currently we only support 64x64 cursors */
7412 if (width != 64 || height != 64) {
7413 DRM_ERROR("we currently only support 64x64 cursors\n");
7414 return -EINVAL;
7415 }
7416
Chris Wilson05394f32010-11-08 19:18:58 +00007417 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007418 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007419 return -ENOENT;
7420
Chris Wilson05394f32010-11-08 19:18:58 +00007421 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007422 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007423 ret = -ENOMEM;
7424 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007425 }
7426
Dave Airlie71acb5e2008-12-30 20:31:46 +10007427 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007428 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007429 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007430 unsigned alignment;
7431
Chris Wilsond9e86c02010-11-10 16:40:20 +00007432 if (obj->tiling_mode) {
7433 DRM_ERROR("cursor cannot be tiled\n");
7434 ret = -EINVAL;
7435 goto fail_locked;
7436 }
7437
Chris Wilson693db182013-03-05 14:52:39 +00007438 /* Note that the w/a also requires 2 PTE of padding following
7439 * the bo. We currently fill all unused PTE with the shadow
7440 * page and so we should always have valid PTE following the
7441 * cursor preventing the VT-d warning.
7442 */
7443 alignment = 0;
7444 if (need_vtd_wa(dev))
7445 alignment = 64*1024;
7446
7447 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007448 if (ret) {
7449 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007450 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007451 }
7452
Chris Wilsond9e86c02010-11-10 16:40:20 +00007453 ret = i915_gem_object_put_fence(obj);
7454 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007455 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007456 goto fail_unpin;
7457 }
7458
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007459 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007460 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007461 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007462 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007463 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7464 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007465 if (ret) {
7466 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007467 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007468 }
Chris Wilson05394f32010-11-08 19:18:58 +00007469 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007470 }
7471
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007472 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007473 I915_WRITE(CURSIZE, (height << 12) | width);
7474
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007475 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007476 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05007477 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007478 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007479 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7480 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007481 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007482 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007483 }
Jesse Barnes80824002009-09-10 15:28:06 -07007484
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007485 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007486
7487 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007488 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007489 intel_crtc->cursor_width = width;
7490 intel_crtc->cursor_height = height;
7491
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007492 if (intel_crtc->active)
7493 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007494
Jesse Barnes79e53942008-11-07 14:24:08 -08007495 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007496fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007497 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007498fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007499 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007500fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007501 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007502 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007503}
7504
7505static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7506{
Jesse Barnes79e53942008-11-07 14:24:08 -08007507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007508
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007509 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7510 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007511
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007512 if (intel_crtc->active)
7513 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007514
7515 return 0;
7516}
7517
Jesse Barnes79e53942008-11-07 14:24:08 -08007518static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007519 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007520{
James Simmons72034252010-08-03 01:33:19 +01007521 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007523
James Simmons72034252010-08-03 01:33:19 +01007524 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007525 intel_crtc->lut_r[i] = red[i] >> 8;
7526 intel_crtc->lut_g[i] = green[i] >> 8;
7527 intel_crtc->lut_b[i] = blue[i] >> 8;
7528 }
7529
7530 intel_crtc_load_lut(crtc);
7531}
7532
Jesse Barnes79e53942008-11-07 14:24:08 -08007533/* VESA 640x480x72Hz mode to set on the pipe */
7534static struct drm_display_mode load_detect_mode = {
7535 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7536 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7537};
7538
Chris Wilsond2dff872011-04-19 08:36:26 +01007539static struct drm_framebuffer *
7540intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007541 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01007542 struct drm_i915_gem_object *obj)
7543{
7544 struct intel_framebuffer *intel_fb;
7545 int ret;
7546
7547 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7548 if (!intel_fb) {
7549 drm_gem_object_unreference_unlocked(&obj->base);
7550 return ERR_PTR(-ENOMEM);
7551 }
7552
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007553 ret = i915_mutex_lock_interruptible(dev);
7554 if (ret)
7555 goto err;
7556
Chris Wilsond2dff872011-04-19 08:36:26 +01007557 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007558 mutex_unlock(&dev->struct_mutex);
7559 if (ret)
7560 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01007561
7562 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007563err:
7564 drm_gem_object_unreference_unlocked(&obj->base);
7565 kfree(intel_fb);
7566
7567 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01007568}
7569
7570static u32
7571intel_framebuffer_pitch_for_width(int width, int bpp)
7572{
7573 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7574 return ALIGN(pitch, 64);
7575}
7576
7577static u32
7578intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7579{
7580 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7581 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7582}
7583
7584static struct drm_framebuffer *
7585intel_framebuffer_create_for_mode(struct drm_device *dev,
7586 struct drm_display_mode *mode,
7587 int depth, int bpp)
7588{
7589 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00007590 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01007591
7592 obj = i915_gem_alloc_object(dev,
7593 intel_framebuffer_size_for_mode(mode, bpp));
7594 if (obj == NULL)
7595 return ERR_PTR(-ENOMEM);
7596
7597 mode_cmd.width = mode->hdisplay;
7598 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007599 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7600 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00007601 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01007602
7603 return intel_framebuffer_create(dev, &mode_cmd, obj);
7604}
7605
7606static struct drm_framebuffer *
7607mode_fits_in_fbdev(struct drm_device *dev,
7608 struct drm_display_mode *mode)
7609{
Daniel Vetter4520f532013-10-09 09:18:51 +02007610#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01007611 struct drm_i915_private *dev_priv = dev->dev_private;
7612 struct drm_i915_gem_object *obj;
7613 struct drm_framebuffer *fb;
7614
7615 if (dev_priv->fbdev == NULL)
7616 return NULL;
7617
7618 obj = dev_priv->fbdev->ifb.obj;
7619 if (obj == NULL)
7620 return NULL;
7621
7622 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007623 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7624 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007625 return NULL;
7626
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007627 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007628 return NULL;
7629
7630 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02007631#else
7632 return NULL;
7633#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01007634}
7635
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007636bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01007637 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007638 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007639{
7640 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007641 struct intel_encoder *intel_encoder =
7642 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08007643 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007644 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007645 struct drm_crtc *crtc = NULL;
7646 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02007647 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007648 int i = -1;
7649
Chris Wilsond2dff872011-04-19 08:36:26 +01007650 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7651 connector->base.id, drm_get_connector_name(connector),
7652 encoder->base.id, drm_get_encoder_name(encoder));
7653
Jesse Barnes79e53942008-11-07 14:24:08 -08007654 /*
7655 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007656 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007657 * - if the connector already has an assigned crtc, use it (but make
7658 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007659 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007660 * - try to find the first unused crtc that can drive this connector,
7661 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007662 */
7663
7664 /* See if we already have a CRTC for this connector */
7665 if (encoder->crtc) {
7666 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007667
Daniel Vetter7b240562012-12-12 00:35:33 +01007668 mutex_lock(&crtc->mutex);
7669
Daniel Vetter24218aa2012-08-12 19:27:11 +02007670 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007671 old->load_detect_temp = false;
7672
7673 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007674 if (connector->dpms != DRM_MODE_DPMS_ON)
7675 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01007676
Chris Wilson71731882011-04-19 23:10:58 +01007677 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007678 }
7679
7680 /* Find an unused one (if possible) */
7681 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7682 i++;
7683 if (!(encoder->possible_crtcs & (1 << i)))
7684 continue;
7685 if (!possible_crtc->enabled) {
7686 crtc = possible_crtc;
7687 break;
7688 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007689 }
7690
7691 /*
7692 * If we didn't find an unused CRTC, don't use any.
7693 */
7694 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007695 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7696 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007697 }
7698
Daniel Vetter7b240562012-12-12 00:35:33 +01007699 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007700 intel_encoder->new_crtc = to_intel_crtc(crtc);
7701 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007702
7703 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007704 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007705 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007706 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007707
Chris Wilson64927112011-04-20 07:25:26 +01007708 if (!mode)
7709 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007710
Chris Wilsond2dff872011-04-19 08:36:26 +01007711 /* We need a framebuffer large enough to accommodate all accesses
7712 * that the plane may generate whilst we perform load detection.
7713 * We can not rely on the fbcon either being present (we get called
7714 * during its initialisation to detect all boot displays, or it may
7715 * not even exist) or that it is large enough to satisfy the
7716 * requested mode.
7717 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007718 fb = mode_fits_in_fbdev(dev, mode);
7719 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007720 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007721 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7722 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007723 } else
7724 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007725 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007726 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007727 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007728 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007729 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007730
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007731 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007732 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007733 if (old->release_fb)
7734 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007735 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007736 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007737 }
Chris Wilson71731882011-04-19 23:10:58 +01007738
Jesse Barnes79e53942008-11-07 14:24:08 -08007739 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007740 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007741 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007742}
7743
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007744void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007745 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007746{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007747 struct intel_encoder *intel_encoder =
7748 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007749 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007750 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007751
Chris Wilsond2dff872011-04-19 08:36:26 +01007752 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7753 connector->base.id, drm_get_connector_name(connector),
7754 encoder->base.id, drm_get_encoder_name(encoder));
7755
Chris Wilson8261b192011-04-19 23:18:09 +01007756 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007757 to_intel_connector(connector)->new_encoder = NULL;
7758 intel_encoder->new_crtc = NULL;
7759 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007760
Daniel Vetter36206362012-12-10 20:42:17 +01007761 if (old->release_fb) {
7762 drm_framebuffer_unregister_private(old->release_fb);
7763 drm_framebuffer_unreference(old->release_fb);
7764 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007765
Daniel Vetter67c96402013-01-23 16:25:09 +00007766 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007767 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007768 }
7769
Eric Anholtc751ce42010-03-25 11:48:48 -07007770 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007771 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7772 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007773
7774 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007775}
7776
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007777static int i9xx_pll_refclk(struct drm_device *dev,
7778 const struct intel_crtc_config *pipe_config)
7779{
7780 struct drm_i915_private *dev_priv = dev->dev_private;
7781 u32 dpll = pipe_config->dpll_hw_state.dpll;
7782
7783 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7784 return dev_priv->vbt.lvds_ssc_freq * 1000;
7785 else if (HAS_PCH_SPLIT(dev))
7786 return 120000;
7787 else if (!IS_GEN2(dev))
7788 return 96000;
7789 else
7790 return 48000;
7791}
7792
Jesse Barnes79e53942008-11-07 14:24:08 -08007793/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007794static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7795 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007796{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007797 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007798 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007799 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007800 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007801 u32 fp;
7802 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007803 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08007804
7805 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03007806 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007807 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03007808 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08007809
7810 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007811 if (IS_PINEVIEW(dev)) {
7812 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7813 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007814 } else {
7815 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7816 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7817 }
7818
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007819 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007820 if (IS_PINEVIEW(dev))
7821 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7822 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007823 else
7824 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007825 DPLL_FPA01_P1_POST_DIV_SHIFT);
7826
7827 switch (dpll & DPLL_MODE_MASK) {
7828 case DPLLB_MODE_DAC_SERIAL:
7829 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7830 5 : 10;
7831 break;
7832 case DPLLB_MODE_LVDS:
7833 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7834 7 : 14;
7835 break;
7836 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007837 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007838 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007839 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007840 }
7841
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007842 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007843 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007844 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007845 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007846 } else {
7847 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7848
7849 if (is_lvds) {
7850 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7851 DPLL_FPA01_P1_POST_DIV_SHIFT);
7852 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08007853 } else {
7854 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7855 clock.p1 = 2;
7856 else {
7857 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7858 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7859 }
7860 if (dpll & PLL_P2_DIVIDE_BY_4)
7861 clock.p2 = 4;
7862 else
7863 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08007864 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03007865
7866 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007867 }
7868
Ville Syrjälä18442d02013-09-13 16:00:08 +03007869 /*
7870 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01007871 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03007872 * encoder's get_config() function.
7873 */
7874 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007875}
7876
Ville Syrjälä6878da02013-09-13 15:59:11 +03007877int intel_dotclock_calculate(int link_freq,
7878 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007879{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007880 /*
7881 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007882 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007883 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007884 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007885 *
7886 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03007887 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007888 */
7889
Ville Syrjälä6878da02013-09-13 15:59:11 +03007890 if (!m_n->link_n)
7891 return 0;
7892
7893 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7894}
7895
Ville Syrjälä18442d02013-09-13 16:00:08 +03007896static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7897 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03007898{
7899 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007900
7901 /* read out port_clock from the DPLL */
7902 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03007903
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007904 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03007905 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01007906 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03007907 * agree once we know their relationship in the encoder's
7908 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007909 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01007910 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03007911 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7912 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08007913}
7914
7915/** Returns the currently programmed mode of the given pipe. */
7916struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7917 struct drm_crtc *crtc)
7918{
Jesse Barnes548f2452011-02-17 10:40:53 -08007919 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007921 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007922 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007923 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007924 int htot = I915_READ(HTOTAL(cpu_transcoder));
7925 int hsync = I915_READ(HSYNC(cpu_transcoder));
7926 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7927 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03007928 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08007929
7930 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7931 if (!mode)
7932 return NULL;
7933
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007934 /*
7935 * Construct a pipe_config sufficient for getting the clock info
7936 * back out of crtc_clock_get.
7937 *
7938 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7939 * to use a real value here instead.
7940 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03007941 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007942 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03007943 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
7944 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
7945 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007946 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7947
Ville Syrjälä773ae032013-09-23 17:48:20 +03007948 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08007949 mode->hdisplay = (htot & 0xffff) + 1;
7950 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7951 mode->hsync_start = (hsync & 0xffff) + 1;
7952 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7953 mode->vdisplay = (vtot & 0xffff) + 1;
7954 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7955 mode->vsync_start = (vsync & 0xffff) + 1;
7956 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7957
7958 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007959
7960 return mode;
7961}
7962
Daniel Vetter3dec0092010-08-20 21:40:52 +02007963static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007964{
7965 struct drm_device *dev = crtc->dev;
7966 drm_i915_private_t *dev_priv = dev->dev_private;
7967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7968 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007969 int dpll_reg = DPLL(pipe);
7970 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007971
Eric Anholtbad720f2009-10-22 16:11:14 -07007972 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007973 return;
7974
7975 if (!dev_priv->lvds_downclock_avail)
7976 return;
7977
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007978 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007979 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007980 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007981
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007982 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007983
7984 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7985 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007986 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007987
Jesse Barnes652c3932009-08-17 13:31:43 -07007988 dpll = I915_READ(dpll_reg);
7989 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007990 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007991 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007992}
7993
7994static void intel_decrease_pllclock(struct drm_crtc *crtc)
7995{
7996 struct drm_device *dev = crtc->dev;
7997 drm_i915_private_t *dev_priv = dev->dev_private;
7998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007999
Eric Anholtbad720f2009-10-22 16:11:14 -07008000 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008001 return;
8002
8003 if (!dev_priv->lvds_downclock_avail)
8004 return;
8005
8006 /*
8007 * Since this is called by a timer, we should never get here in
8008 * the manual case.
8009 */
8010 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008011 int pipe = intel_crtc->pipe;
8012 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008013 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008014
Zhao Yakui44d98a62009-10-09 11:39:40 +08008015 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008016
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008017 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008018
Chris Wilson074b5e12012-05-02 12:07:06 +01008019 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008020 dpll |= DISPLAY_RATE_SELECT_FPA1;
8021 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008022 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008023 dpll = I915_READ(dpll_reg);
8024 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008025 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008026 }
8027
8028}
8029
Chris Wilsonf047e392012-07-21 12:31:41 +01008030void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008031{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008032 struct drm_i915_private *dev_priv = dev->dev_private;
8033
8034 hsw_package_c8_gpu_busy(dev_priv);
8035 i915_update_gfx_val(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008036}
8037
8038void intel_mark_idle(struct drm_device *dev)
8039{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008040 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008041 struct drm_crtc *crtc;
8042
Paulo Zanonic67a4702013-08-19 13:18:09 -03008043 hsw_package_c8_gpu_idle(dev_priv);
8044
Chris Wilson725a5b52013-01-08 11:02:57 +00008045 if (!i915_powersave)
8046 return;
8047
8048 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8049 if (!crtc->fb)
8050 continue;
8051
8052 intel_decrease_pllclock(crtc);
8053 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008054
8055 if (dev_priv->info->gen >= 6)
8056 gen6_rps_idle(dev->dev_private);
Chris Wilsonf047e392012-07-21 12:31:41 +01008057}
8058
Chris Wilsonc65355b2013-06-06 16:53:41 -03008059void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8060 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008061{
8062 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008063 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008064
8065 if (!i915_powersave)
8066 return;
8067
Jesse Barnes652c3932009-08-17 13:31:43 -07008068 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07008069 if (!crtc->fb)
8070 continue;
8071
Chris Wilsonc65355b2013-06-06 16:53:41 -03008072 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8073 continue;
8074
8075 intel_increase_pllclock(crtc);
8076 if (ring && intel_fbc_enabled(dev))
8077 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008078 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008079}
8080
Jesse Barnes79e53942008-11-07 14:24:08 -08008081static void intel_crtc_destroy(struct drm_crtc *crtc)
8082{
8083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008084 struct drm_device *dev = crtc->dev;
8085 struct intel_unpin_work *work;
8086 unsigned long flags;
8087
8088 spin_lock_irqsave(&dev->event_lock, flags);
8089 work = intel_crtc->unpin_work;
8090 intel_crtc->unpin_work = NULL;
8091 spin_unlock_irqrestore(&dev->event_lock, flags);
8092
8093 if (work) {
8094 cancel_work_sync(&work->work);
8095 kfree(work);
8096 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008097
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008098 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8099
Jesse Barnes79e53942008-11-07 14:24:08 -08008100 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008101
Jesse Barnes79e53942008-11-07 14:24:08 -08008102 kfree(intel_crtc);
8103}
8104
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008105static void intel_unpin_work_fn(struct work_struct *__work)
8106{
8107 struct intel_unpin_work *work =
8108 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008109 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008110
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008111 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008112 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008113 drm_gem_object_unreference(&work->pending_flip_obj->base);
8114 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008115
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008116 intel_update_fbc(dev);
8117 mutex_unlock(&dev->struct_mutex);
8118
8119 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8120 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8121
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008122 kfree(work);
8123}
8124
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008125static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008126 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008127{
8128 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8130 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008131 unsigned long flags;
8132
8133 /* Ignore early vblank irqs */
8134 if (intel_crtc == NULL)
8135 return;
8136
8137 spin_lock_irqsave(&dev->event_lock, flags);
8138 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008139
8140 /* Ensure we don't miss a work->pending update ... */
8141 smp_rmb();
8142
8143 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008144 spin_unlock_irqrestore(&dev->event_lock, flags);
8145 return;
8146 }
8147
Chris Wilsone7d841c2012-12-03 11:36:30 +00008148 /* and that the unpin work is consistent wrt ->pending. */
8149 smp_rmb();
8150
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008151 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008152
Rob Clark45a066e2012-10-08 14:50:40 -05008153 if (work->event)
8154 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008155
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008156 drm_vblank_put(dev, intel_crtc->pipe);
8157
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008158 spin_unlock_irqrestore(&dev->event_lock, flags);
8159
Daniel Vetter2c10d572012-12-20 21:24:07 +01008160 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008161
8162 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008163
8164 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008165}
8166
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008167void intel_finish_page_flip(struct drm_device *dev, int pipe)
8168{
8169 drm_i915_private_t *dev_priv = dev->dev_private;
8170 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8171
Mario Kleiner49b14a52010-12-09 07:00:07 +01008172 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008173}
8174
8175void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8176{
8177 drm_i915_private_t *dev_priv = dev->dev_private;
8178 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8179
Mario Kleiner49b14a52010-12-09 07:00:07 +01008180 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008181}
8182
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008183void intel_prepare_page_flip(struct drm_device *dev, int plane)
8184{
8185 drm_i915_private_t *dev_priv = dev->dev_private;
8186 struct intel_crtc *intel_crtc =
8187 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8188 unsigned long flags;
8189
Chris Wilsone7d841c2012-12-03 11:36:30 +00008190 /* NB: An MMIO update of the plane base pointer will also
8191 * generate a page-flip completion irq, i.e. every modeset
8192 * is also accompanied by a spurious intel_prepare_page_flip().
8193 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008194 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008195 if (intel_crtc->unpin_work)
8196 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008197 spin_unlock_irqrestore(&dev->event_lock, flags);
8198}
8199
Chris Wilsone7d841c2012-12-03 11:36:30 +00008200inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8201{
8202 /* Ensure that the work item is consistent when activating it ... */
8203 smp_wmb();
8204 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8205 /* and that it is marked active as soon as the irq could fire. */
8206 smp_wmb();
8207}
8208
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008209static int intel_gen2_queue_flip(struct drm_device *dev,
8210 struct drm_crtc *crtc,
8211 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008212 struct drm_i915_gem_object *obj,
8213 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008214{
8215 struct drm_i915_private *dev_priv = dev->dev_private;
8216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008217 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008218 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008219 int ret;
8220
Daniel Vetter6d90c952012-04-26 23:28:05 +02008221 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008222 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008223 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008224
Daniel Vetter6d90c952012-04-26 23:28:05 +02008225 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008226 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008227 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008228
8229 /* Can't queue multiple flips, so wait for the previous
8230 * one to finish before executing the next.
8231 */
8232 if (intel_crtc->plane)
8233 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8234 else
8235 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008236 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8237 intel_ring_emit(ring, MI_NOOP);
8238 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8239 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8240 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008241 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008242 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008243
8244 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008245 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008246 return 0;
8247
8248err_unpin:
8249 intel_unpin_fb_obj(obj);
8250err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008251 return ret;
8252}
8253
8254static int intel_gen3_queue_flip(struct drm_device *dev,
8255 struct drm_crtc *crtc,
8256 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008257 struct drm_i915_gem_object *obj,
8258 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008259{
8260 struct drm_i915_private *dev_priv = dev->dev_private;
8261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008262 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008263 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008264 int ret;
8265
Daniel Vetter6d90c952012-04-26 23:28:05 +02008266 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008267 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008268 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008269
Daniel Vetter6d90c952012-04-26 23:28:05 +02008270 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008271 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008272 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008273
8274 if (intel_crtc->plane)
8275 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8276 else
8277 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008278 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8279 intel_ring_emit(ring, MI_NOOP);
8280 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8281 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8282 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008283 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008284 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008285
Chris Wilsone7d841c2012-12-03 11:36:30 +00008286 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008287 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008288 return 0;
8289
8290err_unpin:
8291 intel_unpin_fb_obj(obj);
8292err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008293 return ret;
8294}
8295
8296static int intel_gen4_queue_flip(struct drm_device *dev,
8297 struct drm_crtc *crtc,
8298 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008299 struct drm_i915_gem_object *obj,
8300 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008301{
8302 struct drm_i915_private *dev_priv = dev->dev_private;
8303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8304 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008305 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008306 int ret;
8307
Daniel Vetter6d90c952012-04-26 23:28:05 +02008308 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008309 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008310 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008311
Daniel Vetter6d90c952012-04-26 23:28:05 +02008312 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008313 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008314 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008315
8316 /* i965+ uses the linear or tiled offsets from the
8317 * Display Registers (which do not change across a page-flip)
8318 * so we need only reprogram the base address.
8319 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008320 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8321 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8322 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008323 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008324 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008325 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008326
8327 /* XXX Enabling the panel-fitter across page-flip is so far
8328 * untested on non-native modes, so ignore it for now.
8329 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8330 */
8331 pf = 0;
8332 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008333 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008334
8335 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008336 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008337 return 0;
8338
8339err_unpin:
8340 intel_unpin_fb_obj(obj);
8341err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008342 return ret;
8343}
8344
8345static int intel_gen6_queue_flip(struct drm_device *dev,
8346 struct drm_crtc *crtc,
8347 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008348 struct drm_i915_gem_object *obj,
8349 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008350{
8351 struct drm_i915_private *dev_priv = dev->dev_private;
8352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008353 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008354 uint32_t pf, pipesrc;
8355 int ret;
8356
Daniel Vetter6d90c952012-04-26 23:28:05 +02008357 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008358 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008359 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008360
Daniel Vetter6d90c952012-04-26 23:28:05 +02008361 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008362 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008363 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008364
Daniel Vetter6d90c952012-04-26 23:28:05 +02008365 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8366 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8367 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008368 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008369
Chris Wilson99d9acd2012-04-17 20:37:00 +01008370 /* Contrary to the suggestions in the documentation,
8371 * "Enable Panel Fitter" does not seem to be required when page
8372 * flipping with a non-native mode, and worse causes a normal
8373 * modeset to fail.
8374 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8375 */
8376 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008377 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008378 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008379
8380 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008381 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008382 return 0;
8383
8384err_unpin:
8385 intel_unpin_fb_obj(obj);
8386err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008387 return ret;
8388}
8389
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008390static int intel_gen7_queue_flip(struct drm_device *dev,
8391 struct drm_crtc *crtc,
8392 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008393 struct drm_i915_gem_object *obj,
8394 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008395{
8396 struct drm_i915_private *dev_priv = dev->dev_private;
8397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008398 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008399 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008400 int len, ret;
8401
8402 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008403 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008404 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008405
8406 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8407 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008408 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008409
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008410 switch(intel_crtc->plane) {
8411 case PLANE_A:
8412 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8413 break;
8414 case PLANE_B:
8415 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8416 break;
8417 case PLANE_C:
8418 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8419 break;
8420 default:
8421 WARN_ONCE(1, "unknown plane in flip command\n");
8422 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008423 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008424 }
8425
Chris Wilsonffe74d72013-08-26 20:58:12 +01008426 len = 4;
8427 if (ring->id == RCS)
8428 len += 6;
8429
8430 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008431 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008432 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008433
Chris Wilsonffe74d72013-08-26 20:58:12 +01008434 /* Unmask the flip-done completion message. Note that the bspec says that
8435 * we should do this for both the BCS and RCS, and that we must not unmask
8436 * more than one flip event at any time (or ensure that one flip message
8437 * can be sent by waiting for flip-done prior to queueing new flips).
8438 * Experimentation says that BCS works despite DERRMR masking all
8439 * flip-done completion events and that unmasking all planes at once
8440 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8441 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8442 */
8443 if (ring->id == RCS) {
8444 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8445 intel_ring_emit(ring, DERRMR);
8446 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8447 DERRMR_PIPEB_PRI_FLIP_DONE |
8448 DERRMR_PIPEC_PRI_FLIP_DONE));
8449 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8450 intel_ring_emit(ring, DERRMR);
8451 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8452 }
8453
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008454 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008455 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008456 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008457 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008458
8459 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008460 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008461 return 0;
8462
8463err_unpin:
8464 intel_unpin_fb_obj(obj);
8465err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008466 return ret;
8467}
8468
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008469static int intel_default_queue_flip(struct drm_device *dev,
8470 struct drm_crtc *crtc,
8471 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008472 struct drm_i915_gem_object *obj,
8473 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008474{
8475 return -ENODEV;
8476}
8477
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008478static int intel_crtc_page_flip(struct drm_crtc *crtc,
8479 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008480 struct drm_pending_vblank_event *event,
8481 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008482{
8483 struct drm_device *dev = crtc->dev;
8484 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008485 struct drm_framebuffer *old_fb = crtc->fb;
8486 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8488 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008489 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01008490 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008491
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03008492 /* Can't change pixel format via MI display flips. */
8493 if (fb->pixel_format != crtc->fb->pixel_format)
8494 return -EINVAL;
8495
8496 /*
8497 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8498 * Note that pitch changes could also affect these register.
8499 */
8500 if (INTEL_INFO(dev)->gen > 3 &&
8501 (fb->offsets[0] != crtc->fb->offsets[0] ||
8502 fb->pitches[0] != crtc->fb->pitches[0]))
8503 return -EINVAL;
8504
Daniel Vetterb14c5672013-09-19 12:18:32 +02008505 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008506 if (work == NULL)
8507 return -ENOMEM;
8508
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008509 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008510 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008511 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008512 INIT_WORK(&work->work, intel_unpin_work_fn);
8513
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008514 ret = drm_vblank_get(dev, intel_crtc->pipe);
8515 if (ret)
8516 goto free_work;
8517
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008518 /* We borrow the event spin lock for protecting unpin_work */
8519 spin_lock_irqsave(&dev->event_lock, flags);
8520 if (intel_crtc->unpin_work) {
8521 spin_unlock_irqrestore(&dev->event_lock, flags);
8522 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008523 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01008524
8525 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008526 return -EBUSY;
8527 }
8528 intel_crtc->unpin_work = work;
8529 spin_unlock_irqrestore(&dev->event_lock, flags);
8530
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008531 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8532 flush_workqueue(dev_priv->wq);
8533
Chris Wilson79158102012-05-23 11:13:58 +01008534 ret = i915_mutex_lock_interruptible(dev);
8535 if (ret)
8536 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008537
Jesse Barnes75dfca82010-02-10 15:09:44 -08008538 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00008539 drm_gem_object_reference(&work->old_fb_obj->base);
8540 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008541
8542 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01008543
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008544 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008545
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01008546 work->enable_stall_check = true;
8547
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008548 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02008549 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01008550
Keith Packarded8d1972013-07-22 18:49:58 -07008551 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008552 if (ret)
8553 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008554
Chris Wilson7782de32011-07-08 12:22:41 +01008555 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03008556 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008557 mutex_unlock(&dev->struct_mutex);
8558
Jesse Barnese5510fa2010-07-01 16:48:37 -07008559 trace_i915_flip_request(intel_crtc->plane, obj);
8560
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008561 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01008562
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008563cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008564 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02008565 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00008566 drm_gem_object_unreference(&work->old_fb_obj->base);
8567 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01008568 mutex_unlock(&dev->struct_mutex);
8569
Chris Wilson79158102012-05-23 11:13:58 +01008570cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01008571 spin_lock_irqsave(&dev->event_lock, flags);
8572 intel_crtc->unpin_work = NULL;
8573 spin_unlock_irqrestore(&dev->event_lock, flags);
8574
Jesse Barnes7317c75e62011-08-29 09:45:28 -07008575 drm_vblank_put(dev, intel_crtc->pipe);
8576free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01008577 kfree(work);
8578
8579 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008580}
8581
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008582static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008583 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8584 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008585};
8586
Daniel Vetter50f56112012-07-02 09:35:43 +02008587static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8588 struct drm_crtc *crtc)
8589{
8590 struct drm_device *dev;
8591 struct drm_crtc *tmp;
8592 int crtc_mask = 1;
8593
8594 WARN(!crtc, "checking null crtc?\n");
8595
8596 dev = crtc->dev;
8597
8598 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8599 if (tmp == crtc)
8600 break;
8601 crtc_mask <<= 1;
8602 }
8603
8604 if (encoder->possible_crtcs & crtc_mask)
8605 return true;
8606 return false;
8607}
8608
Daniel Vetter9a935852012-07-05 22:34:27 +02008609/**
8610 * intel_modeset_update_staged_output_state
8611 *
8612 * Updates the staged output configuration state, e.g. after we've read out the
8613 * current hw state.
8614 */
8615static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8616{
8617 struct intel_encoder *encoder;
8618 struct intel_connector *connector;
8619
8620 list_for_each_entry(connector, &dev->mode_config.connector_list,
8621 base.head) {
8622 connector->new_encoder =
8623 to_intel_encoder(connector->base.encoder);
8624 }
8625
8626 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8627 base.head) {
8628 encoder->new_crtc =
8629 to_intel_crtc(encoder->base.crtc);
8630 }
8631}
8632
8633/**
8634 * intel_modeset_commit_output_state
8635 *
8636 * This function copies the stage display pipe configuration to the real one.
8637 */
8638static void intel_modeset_commit_output_state(struct drm_device *dev)
8639{
8640 struct intel_encoder *encoder;
8641 struct intel_connector *connector;
8642
8643 list_for_each_entry(connector, &dev->mode_config.connector_list,
8644 base.head) {
8645 connector->base.encoder = &connector->new_encoder->base;
8646 }
8647
8648 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8649 base.head) {
8650 encoder->base.crtc = &encoder->new_crtc->base;
8651 }
8652}
8653
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008654static void
8655connected_sink_compute_bpp(struct intel_connector * connector,
8656 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008657{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008658 int bpp = pipe_config->pipe_bpp;
8659
8660 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8661 connector->base.base.id,
8662 drm_get_connector_name(&connector->base));
8663
8664 /* Don't use an invalid EDID bpc value */
8665 if (connector->base.display_info.bpc &&
8666 connector->base.display_info.bpc * 3 < bpp) {
8667 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8668 bpp, connector->base.display_info.bpc*3);
8669 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8670 }
8671
8672 /* Clamp bpp to 8 on screens without EDID 1.4 */
8673 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8674 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8675 bpp);
8676 pipe_config->pipe_bpp = 24;
8677 }
8678}
8679
8680static int
8681compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8682 struct drm_framebuffer *fb,
8683 struct intel_crtc_config *pipe_config)
8684{
8685 struct drm_device *dev = crtc->base.dev;
8686 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008687 int bpp;
8688
Daniel Vetterd42264b2013-03-28 16:38:08 +01008689 switch (fb->pixel_format) {
8690 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008691 bpp = 8*3; /* since we go through a colormap */
8692 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008693 case DRM_FORMAT_XRGB1555:
8694 case DRM_FORMAT_ARGB1555:
8695 /* checked in intel_framebuffer_init already */
8696 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8697 return -EINVAL;
8698 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008699 bpp = 6*3; /* min is 18bpp */
8700 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008701 case DRM_FORMAT_XBGR8888:
8702 case DRM_FORMAT_ABGR8888:
8703 /* checked in intel_framebuffer_init already */
8704 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8705 return -EINVAL;
8706 case DRM_FORMAT_XRGB8888:
8707 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008708 bpp = 8*3;
8709 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01008710 case DRM_FORMAT_XRGB2101010:
8711 case DRM_FORMAT_ARGB2101010:
8712 case DRM_FORMAT_XBGR2101010:
8713 case DRM_FORMAT_ABGR2101010:
8714 /* checked in intel_framebuffer_init already */
8715 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01008716 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008717 bpp = 10*3;
8718 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01008719 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008720 default:
8721 DRM_DEBUG_KMS("unsupported depth\n");
8722 return -EINVAL;
8723 }
8724
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008725 pipe_config->pipe_bpp = bpp;
8726
8727 /* Clamp display bpp to EDID value */
8728 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008729 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02008730 if (!connector->new_encoder ||
8731 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008732 continue;
8733
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008734 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008735 }
8736
8737 return bpp;
8738}
8739
Daniel Vetter644db712013-09-19 14:53:58 +02008740static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8741{
8742 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8743 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01008744 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02008745 mode->crtc_hdisplay, mode->crtc_hsync_start,
8746 mode->crtc_hsync_end, mode->crtc_htotal,
8747 mode->crtc_vdisplay, mode->crtc_vsync_start,
8748 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8749}
8750
Daniel Vetterc0b03412013-05-28 12:05:54 +02008751static void intel_dump_pipe_config(struct intel_crtc *crtc,
8752 struct intel_crtc_config *pipe_config,
8753 const char *context)
8754{
8755 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8756 context, pipe_name(crtc->pipe));
8757
8758 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8759 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8760 pipe_config->pipe_bpp, pipe_config->dither);
8761 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8762 pipe_config->has_pch_encoder,
8763 pipe_config->fdi_lanes,
8764 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8765 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8766 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008767 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8768 pipe_config->has_dp_encoder,
8769 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8770 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8771 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008772 DRM_DEBUG_KMS("requested mode:\n");
8773 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8774 DRM_DEBUG_KMS("adjusted mode:\n");
8775 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02008776 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03008777 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008778 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8779 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008780 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8781 pipe_config->gmch_pfit.control,
8782 pipe_config->gmch_pfit.pgm_ratios,
8783 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008784 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02008785 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008786 pipe_config->pch_pfit.size,
8787 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008788 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008789 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008790}
8791
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008792static bool check_encoder_cloning(struct drm_crtc *crtc)
8793{
8794 int num_encoders = 0;
8795 bool uncloneable_encoders = false;
8796 struct intel_encoder *encoder;
8797
8798 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8799 base.head) {
8800 if (&encoder->new_crtc->base != crtc)
8801 continue;
8802
8803 num_encoders++;
8804 if (!encoder->cloneable)
8805 uncloneable_encoders = true;
8806 }
8807
8808 return !(num_encoders > 1 && uncloneable_encoders);
8809}
8810
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008811static struct intel_crtc_config *
8812intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008813 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008814 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008815{
8816 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008817 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008818 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008819 int plane_bpp, ret = -EINVAL;
8820 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008821
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008822 if (!check_encoder_cloning(crtc)) {
8823 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8824 return ERR_PTR(-EINVAL);
8825 }
8826
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008827 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8828 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008829 return ERR_PTR(-ENOMEM);
8830
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008831 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8832 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008833
Daniel Vettere143a212013-07-04 12:01:15 +02008834 pipe_config->cpu_transcoder =
8835 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008836 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008837
Imre Deak2960bc92013-07-30 13:36:32 +03008838 /*
8839 * Sanitize sync polarity flags based on requested ones. If neither
8840 * positive or negative polarity is requested, treat this as meaning
8841 * negative polarity.
8842 */
8843 if (!(pipe_config->adjusted_mode.flags &
8844 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8845 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8846
8847 if (!(pipe_config->adjusted_mode.flags &
8848 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8849 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8850
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008851 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8852 * plane pixel format and any sink constraints into account. Returns the
8853 * source plane bpp so that dithering can be selected on mismatches
8854 * after encoders and crtc also have had their say. */
8855 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8856 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008857 if (plane_bpp < 0)
8858 goto fail;
8859
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03008860 /*
8861 * Determine the real pipe dimensions. Note that stereo modes can
8862 * increase the actual pipe size due to the frame doubling and
8863 * insertion of additional space for blanks between the frame. This
8864 * is stored in the crtc timings. We use the requested mode to do this
8865 * computation to clearly distinguish it from the adjusted mode, which
8866 * can be changed by the connectors in the below retry loop.
8867 */
8868 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8869 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8870 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8871
Daniel Vettere29c22c2013-02-21 00:00:16 +01008872encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008873 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008874 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008875 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008876
Daniel Vetter135c81b2013-07-21 21:37:09 +02008877 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01008878 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02008879
Daniel Vetter7758a112012-07-08 19:40:39 +02008880 /* Pass our mode to the connectors and the CRTC to give them a chance to
8881 * adjust it according to limitations or connector properties, and also
8882 * a chance to reject the mode entirely.
8883 */
8884 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8885 base.head) {
8886
8887 if (&encoder->new_crtc->base != crtc)
8888 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008889
Daniel Vetterefea6e82013-07-21 21:36:59 +02008890 if (!(encoder->compute_config(encoder, pipe_config))) {
8891 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008892 goto fail;
8893 }
8894 }
8895
Daniel Vetterff9a6752013-06-01 17:16:21 +02008896 /* Set default port clock if not overwritten by the encoder. Needs to be
8897 * done afterwards in case the encoder adjusts the mode. */
8898 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01008899 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8900 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008901
Daniel Vettera43f6e02013-06-07 23:10:32 +02008902 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008903 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008904 DRM_DEBUG_KMS("CRTC fixup failed\n");
8905 goto fail;
8906 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008907
8908 if (ret == RETRY) {
8909 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8910 ret = -EINVAL;
8911 goto fail;
8912 }
8913
8914 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8915 retry = false;
8916 goto encoder_retry;
8917 }
8918
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008919 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8920 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8921 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8922
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008923 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008924fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008925 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008926 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008927}
8928
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008929/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8930 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8931static void
8932intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8933 unsigned *prepare_pipes, unsigned *disable_pipes)
8934{
8935 struct intel_crtc *intel_crtc;
8936 struct drm_device *dev = crtc->dev;
8937 struct intel_encoder *encoder;
8938 struct intel_connector *connector;
8939 struct drm_crtc *tmp_crtc;
8940
8941 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8942
8943 /* Check which crtcs have changed outputs connected to them, these need
8944 * to be part of the prepare_pipes mask. We don't (yet) support global
8945 * modeset across multiple crtcs, so modeset_pipes will only have one
8946 * bit set at most. */
8947 list_for_each_entry(connector, &dev->mode_config.connector_list,
8948 base.head) {
8949 if (connector->base.encoder == &connector->new_encoder->base)
8950 continue;
8951
8952 if (connector->base.encoder) {
8953 tmp_crtc = connector->base.encoder->crtc;
8954
8955 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8956 }
8957
8958 if (connector->new_encoder)
8959 *prepare_pipes |=
8960 1 << connector->new_encoder->new_crtc->pipe;
8961 }
8962
8963 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8964 base.head) {
8965 if (encoder->base.crtc == &encoder->new_crtc->base)
8966 continue;
8967
8968 if (encoder->base.crtc) {
8969 tmp_crtc = encoder->base.crtc;
8970
8971 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8972 }
8973
8974 if (encoder->new_crtc)
8975 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8976 }
8977
8978 /* Check for any pipes that will be fully disabled ... */
8979 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8980 base.head) {
8981 bool used = false;
8982
8983 /* Don't try to disable disabled crtcs. */
8984 if (!intel_crtc->base.enabled)
8985 continue;
8986
8987 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8988 base.head) {
8989 if (encoder->new_crtc == intel_crtc)
8990 used = true;
8991 }
8992
8993 if (!used)
8994 *disable_pipes |= 1 << intel_crtc->pipe;
8995 }
8996
8997
8998 /* set_mode is also used to update properties on life display pipes. */
8999 intel_crtc = to_intel_crtc(crtc);
9000 if (crtc->enabled)
9001 *prepare_pipes |= 1 << intel_crtc->pipe;
9002
Daniel Vetterb6c51642013-04-12 18:48:43 +02009003 /*
9004 * For simplicity do a full modeset on any pipe where the output routing
9005 * changed. We could be more clever, but that would require us to be
9006 * more careful with calling the relevant encoder->mode_set functions.
9007 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009008 if (*prepare_pipes)
9009 *modeset_pipes = *prepare_pipes;
9010
9011 /* ... and mask these out. */
9012 *modeset_pipes &= ~(*disable_pipes);
9013 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009014
9015 /*
9016 * HACK: We don't (yet) fully support global modesets. intel_set_config
9017 * obies this rule, but the modeset restore mode of
9018 * intel_modeset_setup_hw_state does not.
9019 */
9020 *modeset_pipes &= 1 << intel_crtc->pipe;
9021 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009022
9023 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9024 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009025}
9026
Daniel Vetterea9d7582012-07-10 10:42:52 +02009027static bool intel_crtc_in_use(struct drm_crtc *crtc)
9028{
9029 struct drm_encoder *encoder;
9030 struct drm_device *dev = crtc->dev;
9031
9032 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9033 if (encoder->crtc == crtc)
9034 return true;
9035
9036 return false;
9037}
9038
9039static void
9040intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9041{
9042 struct intel_encoder *intel_encoder;
9043 struct intel_crtc *intel_crtc;
9044 struct drm_connector *connector;
9045
9046 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9047 base.head) {
9048 if (!intel_encoder->base.crtc)
9049 continue;
9050
9051 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9052
9053 if (prepare_pipes & (1 << intel_crtc->pipe))
9054 intel_encoder->connectors_active = false;
9055 }
9056
9057 intel_modeset_commit_output_state(dev);
9058
9059 /* Update computed state. */
9060 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9061 base.head) {
9062 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9063 }
9064
9065 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9066 if (!connector->encoder || !connector->encoder->crtc)
9067 continue;
9068
9069 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9070
9071 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009072 struct drm_property *dpms_property =
9073 dev->mode_config.dpms_property;
9074
Daniel Vetterea9d7582012-07-10 10:42:52 +02009075 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009076 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009077 dpms_property,
9078 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009079
9080 intel_encoder = to_intel_encoder(connector->encoder);
9081 intel_encoder->connectors_active = true;
9082 }
9083 }
9084
9085}
9086
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009087static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009088{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009089 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009090
9091 if (clock1 == clock2)
9092 return true;
9093
9094 if (!clock1 || !clock2)
9095 return false;
9096
9097 diff = abs(clock1 - clock2);
9098
9099 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9100 return true;
9101
9102 return false;
9103}
9104
Daniel Vetter25c5b262012-07-08 22:08:04 +02009105#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9106 list_for_each_entry((intel_crtc), \
9107 &(dev)->mode_config.crtc_list, \
9108 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009109 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009110
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009111static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009112intel_pipe_config_compare(struct drm_device *dev,
9113 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009114 struct intel_crtc_config *pipe_config)
9115{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009116#define PIPE_CONF_CHECK_X(name) \
9117 if (current_config->name != pipe_config->name) { \
9118 DRM_ERROR("mismatch in " #name " " \
9119 "(expected 0x%08x, found 0x%08x)\n", \
9120 current_config->name, \
9121 pipe_config->name); \
9122 return false; \
9123 }
9124
Daniel Vetter08a24032013-04-19 11:25:34 +02009125#define PIPE_CONF_CHECK_I(name) \
9126 if (current_config->name != pipe_config->name) { \
9127 DRM_ERROR("mismatch in " #name " " \
9128 "(expected %i, found %i)\n", \
9129 current_config->name, \
9130 pipe_config->name); \
9131 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009132 }
9133
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009134#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9135 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009136 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009137 "(expected %i, found %i)\n", \
9138 current_config->name & (mask), \
9139 pipe_config->name & (mask)); \
9140 return false; \
9141 }
9142
Ville Syrjälä5e550652013-09-06 23:29:07 +03009143#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9144 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9145 DRM_ERROR("mismatch in " #name " " \
9146 "(expected %i, found %i)\n", \
9147 current_config->name, \
9148 pipe_config->name); \
9149 return false; \
9150 }
9151
Daniel Vetterbb760062013-06-06 14:55:52 +02009152#define PIPE_CONF_QUIRK(quirk) \
9153 ((current_config->quirks | pipe_config->quirks) & (quirk))
9154
Daniel Vettereccb1402013-05-22 00:50:22 +02009155 PIPE_CONF_CHECK_I(cpu_transcoder);
9156
Daniel Vetter08a24032013-04-19 11:25:34 +02009157 PIPE_CONF_CHECK_I(has_pch_encoder);
9158 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009159 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9160 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9161 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9162 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9163 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009164
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009165 PIPE_CONF_CHECK_I(has_dp_encoder);
9166 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9167 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9168 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9169 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9170 PIPE_CONF_CHECK_I(dp_m_n.tu);
9171
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009172 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9173 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9174 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9175 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9176 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9177 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9178
9179 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9180 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9181 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9182 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9183 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9184 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9185
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009186 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009187
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009188 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9189 DRM_MODE_FLAG_INTERLACE);
9190
Daniel Vetterbb760062013-06-06 14:55:52 +02009191 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9192 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9193 DRM_MODE_FLAG_PHSYNC);
9194 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9195 DRM_MODE_FLAG_NHSYNC);
9196 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9197 DRM_MODE_FLAG_PVSYNC);
9198 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9199 DRM_MODE_FLAG_NVSYNC);
9200 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009201
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009202 PIPE_CONF_CHECK_I(pipe_src_w);
9203 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009204
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009205 PIPE_CONF_CHECK_I(gmch_pfit.control);
9206 /* pfit ratios are autocomputed by the hw on gen4+ */
9207 if (INTEL_INFO(dev)->gen < 4)
9208 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9209 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009210 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9211 if (current_config->pch_pfit.enabled) {
9212 PIPE_CONF_CHECK_I(pch_pfit.pos);
9213 PIPE_CONF_CHECK_I(pch_pfit.size);
9214 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009215
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009216 PIPE_CONF_CHECK_I(ips_enabled);
9217
Ville Syrjälä282740f2013-09-04 18:30:03 +03009218 PIPE_CONF_CHECK_I(double_wide);
9219
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009220 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009221 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009222 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009223 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9224 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009225
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009226 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9227 PIPE_CONF_CHECK_I(pipe_bpp);
9228
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009229 if (!IS_HASWELL(dev)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01009230 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009231 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9232 }
Ville Syrjälä5e550652013-09-06 23:29:07 +03009233
Daniel Vetter66e985c2013-06-05 13:34:20 +02009234#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009235#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009236#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009237#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009238#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009239
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009240 return true;
9241}
9242
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009243static void
9244check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009245{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009246 struct intel_connector *connector;
9247
9248 list_for_each_entry(connector, &dev->mode_config.connector_list,
9249 base.head) {
9250 /* This also checks the encoder/connector hw state with the
9251 * ->get_hw_state callbacks. */
9252 intel_connector_check_state(connector);
9253
9254 WARN(&connector->new_encoder->base != connector->base.encoder,
9255 "connector's staged encoder doesn't match current encoder\n");
9256 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009257}
9258
9259static void
9260check_encoder_state(struct drm_device *dev)
9261{
9262 struct intel_encoder *encoder;
9263 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009264
9265 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9266 base.head) {
9267 bool enabled = false;
9268 bool active = false;
9269 enum pipe pipe, tracked_pipe;
9270
9271 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9272 encoder->base.base.id,
9273 drm_get_encoder_name(&encoder->base));
9274
9275 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9276 "encoder's stage crtc doesn't match current crtc\n");
9277 WARN(encoder->connectors_active && !encoder->base.crtc,
9278 "encoder's active_connectors set, but no crtc\n");
9279
9280 list_for_each_entry(connector, &dev->mode_config.connector_list,
9281 base.head) {
9282 if (connector->base.encoder != &encoder->base)
9283 continue;
9284 enabled = true;
9285 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9286 active = true;
9287 }
9288 WARN(!!encoder->base.crtc != enabled,
9289 "encoder's enabled state mismatch "
9290 "(expected %i, found %i)\n",
9291 !!encoder->base.crtc, enabled);
9292 WARN(active && !encoder->base.crtc,
9293 "active encoder with no crtc\n");
9294
9295 WARN(encoder->connectors_active != active,
9296 "encoder's computed active state doesn't match tracked active state "
9297 "(expected %i, found %i)\n", active, encoder->connectors_active);
9298
9299 active = encoder->get_hw_state(encoder, &pipe);
9300 WARN(active != encoder->connectors_active,
9301 "encoder's hw state doesn't match sw tracking "
9302 "(expected %i, found %i)\n",
9303 encoder->connectors_active, active);
9304
9305 if (!encoder->base.crtc)
9306 continue;
9307
9308 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9309 WARN(active && pipe != tracked_pipe,
9310 "active encoder's pipe doesn't match"
9311 "(expected %i, found %i)\n",
9312 tracked_pipe, pipe);
9313
9314 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009315}
9316
9317static void
9318check_crtc_state(struct drm_device *dev)
9319{
9320 drm_i915_private_t *dev_priv = dev->dev_private;
9321 struct intel_crtc *crtc;
9322 struct intel_encoder *encoder;
9323 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009324
9325 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9326 base.head) {
9327 bool enabled = false;
9328 bool active = false;
9329
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009330 memset(&pipe_config, 0, sizeof(pipe_config));
9331
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009332 DRM_DEBUG_KMS("[CRTC:%d]\n",
9333 crtc->base.base.id);
9334
9335 WARN(crtc->active && !crtc->base.enabled,
9336 "active crtc, but not enabled in sw tracking\n");
9337
9338 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9339 base.head) {
9340 if (encoder->base.crtc != &crtc->base)
9341 continue;
9342 enabled = true;
9343 if (encoder->connectors_active)
9344 active = true;
9345 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009346
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009347 WARN(active != crtc->active,
9348 "crtc's computed active state doesn't match tracked active state "
9349 "(expected %i, found %i)\n", active, crtc->active);
9350 WARN(enabled != crtc->base.enabled,
9351 "crtc's computed enabled state doesn't match tracked enabled state "
9352 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9353
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009354 active = dev_priv->display.get_pipe_config(crtc,
9355 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009356
9357 /* hw state is inconsistent with the pipe A quirk */
9358 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9359 active = crtc->active;
9360
Daniel Vetter6c49f242013-06-06 12:45:25 +02009361 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9362 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009363 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009364 if (encoder->base.crtc != &crtc->base)
9365 continue;
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009366 if (encoder->get_config &&
9367 encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009368 encoder->get_config(encoder, &pipe_config);
9369 }
9370
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009371 WARN(crtc->active != active,
9372 "crtc active state doesn't match with hw state "
9373 "(expected %i, found %i)\n", crtc->active, active);
9374
Daniel Vetterc0b03412013-05-28 12:05:54 +02009375 if (active &&
9376 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9377 WARN(1, "pipe state doesn't match!\n");
9378 intel_dump_pipe_config(crtc, &pipe_config,
9379 "[hw state]");
9380 intel_dump_pipe_config(crtc, &crtc->config,
9381 "[sw state]");
9382 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009383 }
9384}
9385
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009386static void
9387check_shared_dpll_state(struct drm_device *dev)
9388{
9389 drm_i915_private_t *dev_priv = dev->dev_private;
9390 struct intel_crtc *crtc;
9391 struct intel_dpll_hw_state dpll_hw_state;
9392 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009393
9394 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9395 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9396 int enabled_crtcs = 0, active_crtcs = 0;
9397 bool active;
9398
9399 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9400
9401 DRM_DEBUG_KMS("%s\n", pll->name);
9402
9403 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9404
9405 WARN(pll->active > pll->refcount,
9406 "more active pll users than references: %i vs %i\n",
9407 pll->active, pll->refcount);
9408 WARN(pll->active && !pll->on,
9409 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009410 WARN(pll->on && !pll->active,
9411 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009412 WARN(pll->on != active,
9413 "pll on state mismatch (expected %i, found %i)\n",
9414 pll->on, active);
9415
9416 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9417 base.head) {
9418 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9419 enabled_crtcs++;
9420 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9421 active_crtcs++;
9422 }
9423 WARN(pll->active != active_crtcs,
9424 "pll active crtcs mismatch (expected %i, found %i)\n",
9425 pll->active, active_crtcs);
9426 WARN(pll->refcount != enabled_crtcs,
9427 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9428 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009429
9430 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9431 sizeof(dpll_hw_state)),
9432 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009433 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009434}
9435
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009436void
9437intel_modeset_check_state(struct drm_device *dev)
9438{
9439 check_connector_state(dev);
9440 check_encoder_state(dev);
9441 check_crtc_state(dev);
9442 check_shared_dpll_state(dev);
9443}
9444
Ville Syrjälä18442d02013-09-13 16:00:08 +03009445void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9446 int dotclock)
9447{
9448 /*
9449 * FDI already provided one idea for the dotclock.
9450 * Yell if the encoder disagrees.
9451 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01009452 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009453 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +01009454 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009455}
9456
Daniel Vetterf30da182013-04-11 20:22:50 +02009457static int __intel_set_mode(struct drm_crtc *crtc,
9458 struct drm_display_mode *mode,
9459 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02009460{
9461 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02009462 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009463 struct drm_display_mode *saved_mode, *saved_hwmode;
9464 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009465 struct intel_crtc *intel_crtc;
9466 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009467 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02009468
Daniel Vettera1e22652013-09-21 00:35:38 +02009469 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009470 if (!saved_mode)
9471 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07009472 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02009473
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009474 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02009475 &prepare_pipes, &disable_pipes);
9476
Tim Gardner3ac18232012-12-07 07:54:26 -07009477 *saved_hwmode = crtc->hwmode;
9478 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009479
Daniel Vetter25c5b262012-07-08 22:08:04 +02009480 /* Hack: Because we don't (yet) support global modeset on multiple
9481 * crtcs, we don't keep track of the new mode for more than one crtc.
9482 * Hence simply check whether any bit is set in modeset_pipes in all the
9483 * pieces of code that are not yet converted to deal with mutliple crtcs
9484 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009485 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009486 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009487 if (IS_ERR(pipe_config)) {
9488 ret = PTR_ERR(pipe_config);
9489 pipe_config = NULL;
9490
Tim Gardner3ac18232012-12-07 07:54:26 -07009491 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02009492 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02009493 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9494 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02009495 }
9496
Jesse Barnes30a970c2013-11-04 13:48:12 -08009497 /*
9498 * See if the config requires any additional preparation, e.g.
9499 * to adjust global state with pipes off. We need to do this
9500 * here so we can get the modeset_pipe updated config for the new
9501 * mode set on this crtc. For other crtcs we need to use the
9502 * adjusted_mode bits in the crtc directly.
9503 */
Ville Syrjäläc164f832013-11-05 22:34:12 +02009504 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08009505 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9506 modeset_pipes, pipe_config);
9507
Ville Syrjäläc164f832013-11-05 22:34:12 +02009508 /* may have added more to prepare_pipes than we should */
9509 prepare_pipes &= ~disable_pipes;
9510 }
9511
Daniel Vetter460da9162013-03-27 00:44:51 +01009512 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9513 intel_crtc_disable(&intel_crtc->base);
9514
Daniel Vetterea9d7582012-07-10 10:42:52 +02009515 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9516 if (intel_crtc->base.enabled)
9517 dev_priv->display.crtc_disable(&intel_crtc->base);
9518 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009519
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02009520 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9521 * to set it here already despite that we pass it down the callchain.
9522 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009523 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02009524 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009525 /* mode_set/enable/disable functions rely on a correct pipe
9526 * config. */
9527 to_intel_crtc(crtc)->config = *pipe_config;
9528 }
Daniel Vetter7758a112012-07-08 19:40:39 +02009529
Daniel Vetterea9d7582012-07-10 10:42:52 +02009530 /* Only after disabling all output pipelines that will be changed can we
9531 * update the the output configuration. */
9532 intel_modeset_update_state(dev, prepare_pipes);
9533
Daniel Vetter47fab732012-10-26 10:58:18 +02009534 if (dev_priv->display.modeset_global_resources)
9535 dev_priv->display.modeset_global_resources(dev);
9536
Daniel Vettera6778b32012-07-02 09:56:42 +02009537 /* Set up the DPLL and any encoders state that needs to adjust or depend
9538 * on the DPLL.
9539 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009540 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009541 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009542 x, y, fb);
9543 if (ret)
9544 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02009545 }
9546
9547 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02009548 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9549 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02009550
Daniel Vetter25c5b262012-07-08 22:08:04 +02009551 if (modeset_pipes) {
9552 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009553 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009554
Daniel Vetter25c5b262012-07-08 22:08:04 +02009555 /* Calculate and store various constants which
9556 * are later needed by vblank and swap-completion
9557 * timestamping. They are derived from true hwmode.
9558 */
9559 drm_calc_timestamping_constants(crtc);
9560 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009561
9562 /* FIXME: add subpixel order */
9563done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009564 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07009565 crtc->hwmode = *saved_hwmode;
9566 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02009567 }
9568
Tim Gardner3ac18232012-12-07 07:54:26 -07009569out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009570 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07009571 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02009572 return ret;
9573}
9574
Damien Lespiaue7457a92013-08-08 22:28:59 +01009575static int intel_set_mode(struct drm_crtc *crtc,
9576 struct drm_display_mode *mode,
9577 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +02009578{
9579 int ret;
9580
9581 ret = __intel_set_mode(crtc, mode, x, y, fb);
9582
9583 if (ret == 0)
9584 intel_modeset_check_state(crtc->dev);
9585
9586 return ret;
9587}
9588
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009589void intel_crtc_restore_mode(struct drm_crtc *crtc)
9590{
9591 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9592}
9593
Daniel Vetter25c5b262012-07-08 22:08:04 +02009594#undef for_each_intel_crtc_masked
9595
Daniel Vetterd9e55602012-07-04 22:16:09 +02009596static void intel_set_config_free(struct intel_set_config *config)
9597{
9598 if (!config)
9599 return;
9600
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009601 kfree(config->save_connector_encoders);
9602 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02009603 kfree(config);
9604}
9605
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009606static int intel_set_config_save_state(struct drm_device *dev,
9607 struct intel_set_config *config)
9608{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009609 struct drm_encoder *encoder;
9610 struct drm_connector *connector;
9611 int count;
9612
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009613 config->save_encoder_crtcs =
9614 kcalloc(dev->mode_config.num_encoder,
9615 sizeof(struct drm_crtc *), GFP_KERNEL);
9616 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009617 return -ENOMEM;
9618
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009619 config->save_connector_encoders =
9620 kcalloc(dev->mode_config.num_connector,
9621 sizeof(struct drm_encoder *), GFP_KERNEL);
9622 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009623 return -ENOMEM;
9624
9625 /* Copy data. Note that driver private data is not affected.
9626 * Should anything bad happen only the expected state is
9627 * restored, not the drivers personal bookkeeping.
9628 */
9629 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009630 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009631 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009632 }
9633
9634 count = 0;
9635 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02009636 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009637 }
9638
9639 return 0;
9640}
9641
9642static void intel_set_config_restore_state(struct drm_device *dev,
9643 struct intel_set_config *config)
9644{
Daniel Vetter9a935852012-07-05 22:34:27 +02009645 struct intel_encoder *encoder;
9646 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009647 int count;
9648
9649 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009650 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9651 encoder->new_crtc =
9652 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009653 }
9654
9655 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02009656 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9657 connector->new_encoder =
9658 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009659 }
9660}
9661
Imre Deake3de42b2013-05-03 19:44:07 +02009662static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01009663is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02009664{
9665 int i;
9666
Chris Wilson2e57f472013-07-17 12:14:40 +01009667 if (set->num_connectors == 0)
9668 return false;
9669
9670 if (WARN_ON(set->connectors == NULL))
9671 return false;
9672
9673 for (i = 0; i < set->num_connectors; i++)
9674 if (set->connectors[i]->encoder &&
9675 set->connectors[i]->encoder->crtc == set->crtc &&
9676 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02009677 return true;
9678
9679 return false;
9680}
9681
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009682static void
9683intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9684 struct intel_set_config *config)
9685{
9686
9687 /* We should be able to check here if the fb has the same properties
9688 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01009689 if (is_crtc_connector_off(set)) {
9690 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009691 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009692 /* If we have no fb then treat it as a full mode set */
9693 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03009694 struct intel_crtc *intel_crtc =
9695 to_intel_crtc(set->crtc);
9696
9697 if (intel_crtc->active && i915_fastboot) {
9698 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9699 config->fb_changed = true;
9700 } else {
9701 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9702 config->mode_changed = true;
9703 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009704 } else if (set->fb == NULL) {
9705 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01009706 } else if (set->fb->pixel_format !=
9707 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009708 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009709 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009710 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02009711 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009712 }
9713
Daniel Vetter835c5872012-07-10 18:11:08 +02009714 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009715 config->fb_changed = true;
9716
9717 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9718 DRM_DEBUG_KMS("modes are different, full mode set\n");
9719 drm_mode_debug_printmodeline(&set->crtc->mode);
9720 drm_mode_debug_printmodeline(set->mode);
9721 config->mode_changed = true;
9722 }
Chris Wilsona1d95702013-08-13 18:48:47 +01009723
9724 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9725 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009726}
9727
Daniel Vetter2e431052012-07-04 22:42:15 +02009728static int
Daniel Vetter9a935852012-07-05 22:34:27 +02009729intel_modeset_stage_output_state(struct drm_device *dev,
9730 struct drm_mode_set *set,
9731 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02009732{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02009733 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009734 struct intel_connector *connector;
9735 struct intel_encoder *encoder;
Paulo Zanonif3f08572013-08-12 14:56:53 -03009736 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02009737
Damien Lespiau9abdda72013-02-13 13:29:23 +00009738 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02009739 * of connectors. For paranoia, double-check this. */
9740 WARN_ON(!set->fb && (set->num_connectors != 0));
9741 WARN_ON(set->fb && (set->num_connectors == 0));
9742
Daniel Vetter9a935852012-07-05 22:34:27 +02009743 list_for_each_entry(connector, &dev->mode_config.connector_list,
9744 base.head) {
9745 /* Otherwise traverse passed in connector list and get encoders
9746 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009747 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009748 if (set->connectors[ro] == &connector->base) {
9749 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02009750 break;
9751 }
9752 }
9753
Daniel Vetter9a935852012-07-05 22:34:27 +02009754 /* If we disable the crtc, disable all its connectors. Also, if
9755 * the connector is on the changing crtc but not on the new
9756 * connector list, disable it. */
9757 if ((!set->fb || ro == set->num_connectors) &&
9758 connector->base.encoder &&
9759 connector->base.encoder->crtc == set->crtc) {
9760 connector->new_encoder = NULL;
9761
9762 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9763 connector->base.base.id,
9764 drm_get_connector_name(&connector->base));
9765 }
9766
9767
9768 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009769 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009770 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009771 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009772 }
9773 /* connector->new_encoder is now updated for all connectors. */
9774
9775 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +02009776 list_for_each_entry(connector, &dev->mode_config.connector_list,
9777 base.head) {
9778 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02009779 continue;
9780
Daniel Vetter9a935852012-07-05 22:34:27 +02009781 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02009782
9783 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009784 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02009785 new_crtc = set->crtc;
9786 }
9787
9788 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02009789 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9790 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009791 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02009792 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009793 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9794
9795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9796 connector->base.base.id,
9797 drm_get_connector_name(&connector->base),
9798 new_crtc->base.id);
9799 }
9800
9801 /* Check for any encoders that needs to be disabled. */
9802 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9803 base.head) {
9804 list_for_each_entry(connector,
9805 &dev->mode_config.connector_list,
9806 base.head) {
9807 if (connector->new_encoder == encoder) {
9808 WARN_ON(!connector->new_encoder->new_crtc);
9809
9810 goto next_encoder;
9811 }
9812 }
9813 encoder->new_crtc = NULL;
9814next_encoder:
9815 /* Only now check for crtc changes so we don't miss encoders
9816 * that will be disabled. */
9817 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02009818 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009819 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02009820 }
9821 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009822 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02009823
Daniel Vetter2e431052012-07-04 22:42:15 +02009824 return 0;
9825}
9826
9827static int intel_crtc_set_config(struct drm_mode_set *set)
9828{
9829 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009830 struct drm_mode_set save_set;
9831 struct intel_set_config *config;
9832 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009833
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009834 BUG_ON(!set);
9835 BUG_ON(!set->crtc);
9836 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009837
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009838 /* Enforce sane interface api - has been abused by the fb helper. */
9839 BUG_ON(!set->mode && set->fb);
9840 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009841
Daniel Vetter2e431052012-07-04 22:42:15 +02009842 if (set->fb) {
9843 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9844 set->crtc->base.id, set->fb->base.id,
9845 (int)set->num_connectors, set->x, set->y);
9846 } else {
9847 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009848 }
9849
9850 dev = set->crtc->dev;
9851
9852 ret = -ENOMEM;
9853 config = kzalloc(sizeof(*config), GFP_KERNEL);
9854 if (!config)
9855 goto out_config;
9856
9857 ret = intel_set_config_save_state(dev, config);
9858 if (ret)
9859 goto out_config;
9860
9861 save_set.crtc = set->crtc;
9862 save_set.mode = &set->crtc->mode;
9863 save_set.x = set->crtc->x;
9864 save_set.y = set->crtc->y;
9865 save_set.fb = set->crtc->fb;
9866
9867 /* Compute whether we need a full modeset, only an fb base update or no
9868 * change at all. In the future we might also check whether only the
9869 * mode changed, e.g. for LVDS where we only change the panel fitter in
9870 * such cases. */
9871 intel_set_config_compute_mode_changes(set, config);
9872
Daniel Vetter9a935852012-07-05 22:34:27 +02009873 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009874 if (ret)
9875 goto fail;
9876
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009877 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009878 ret = intel_set_mode(set->crtc, set->mode,
9879 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009880 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009881 intel_crtc_wait_for_pending_flips(set->crtc);
9882
Daniel Vetter4f660f42012-07-02 09:47:37 +02009883 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009884 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009885 }
9886
Chris Wilson2d05eae2013-05-03 17:36:25 +01009887 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009888 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9889 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009890fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009891 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009892
Chris Wilson2d05eae2013-05-03 17:36:25 +01009893 /* Try to restore the config */
9894 if (config->mode_changed &&
9895 intel_set_mode(save_set.crtc, save_set.mode,
9896 save_set.x, save_set.y, save_set.fb))
9897 DRM_ERROR("failed to restore config after modeset failure\n");
9898 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009899
Daniel Vetterd9e55602012-07-04 22:16:09 +02009900out_config:
9901 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009902 return ret;
9903}
9904
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009905static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009906 .cursor_set = intel_crtc_cursor_set,
9907 .cursor_move = intel_crtc_cursor_move,
9908 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009909 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009910 .destroy = intel_crtc_destroy,
9911 .page_flip = intel_crtc_page_flip,
9912};
9913
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009914static void intel_cpu_pll_init(struct drm_device *dev)
9915{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009916 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009917 intel_ddi_pll_init(dev);
9918}
9919
Daniel Vetter53589012013-06-05 13:34:16 +02009920static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9921 struct intel_shared_dpll *pll,
9922 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009923{
Daniel Vetter53589012013-06-05 13:34:16 +02009924 uint32_t val;
9925
9926 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009927 hw_state->dpll = val;
9928 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9929 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02009930
9931 return val & DPLL_VCO_ENABLE;
9932}
9933
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009934static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9935 struct intel_shared_dpll *pll)
9936{
9937 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9938 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9939}
9940
Daniel Vettere7b903d2013-06-05 13:34:14 +02009941static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9942 struct intel_shared_dpll *pll)
9943{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009944 /* PCH refclock must be enabled first */
9945 assert_pch_refclk_enabled(dev_priv);
9946
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009947 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9948
9949 /* Wait for the clocks to stabilize. */
9950 POSTING_READ(PCH_DPLL(pll->id));
9951 udelay(150);
9952
9953 /* The pixel multiplier can only be updated once the
9954 * DPLL is enabled and the clocks are stable.
9955 *
9956 * So write it again.
9957 */
9958 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9959 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009960 udelay(200);
9961}
9962
9963static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9964 struct intel_shared_dpll *pll)
9965{
9966 struct drm_device *dev = dev_priv->dev;
9967 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009968
9969 /* Make sure no transcoder isn't still depending on us. */
9970 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9971 if (intel_crtc_to_shared_dpll(crtc) == pll)
9972 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9973 }
9974
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009975 I915_WRITE(PCH_DPLL(pll->id), 0);
9976 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009977 udelay(200);
9978}
9979
Daniel Vetter46edb022013-06-05 13:34:12 +02009980static char *ibx_pch_dpll_names[] = {
9981 "PCH DPLL A",
9982 "PCH DPLL B",
9983};
9984
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009985static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009986{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009987 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009988 int i;
9989
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009990 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009991
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009992 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02009993 dev_priv->shared_dplls[i].id = i;
9994 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009995 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009996 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9997 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009998 dev_priv->shared_dplls[i].get_hw_state =
9999 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010000 }
10001}
10002
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010003static void intel_shared_dpll_init(struct drm_device *dev)
10004{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010005 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010006
10007 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10008 ibx_pch_dpll_init(dev);
10009 else
10010 dev_priv->num_shared_dpll = 0;
10011
10012 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10013 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
10014 dev_priv->num_shared_dpll);
10015}
10016
Hannes Ederb358d0a2008-12-18 21:18:47 +010010017static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010018{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010019 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010020 struct intel_crtc *intel_crtc;
10021 int i;
10022
Daniel Vetter955382f2013-09-19 14:05:45 +020010023 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010024 if (intel_crtc == NULL)
10025 return;
10026
10027 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10028
10029 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010030 for (i = 0; i < 256; i++) {
10031 intel_crtc->lut_r[i] = i;
10032 intel_crtc->lut_g[i] = i;
10033 intel_crtc->lut_b[i] = i;
10034 }
10035
Jesse Barnes80824002009-09-10 15:28:06 -070010036 /* Swap pipes & planes for FBC on pre-965 */
10037 intel_crtc->pipe = pipe;
10038 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +010010039 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010040 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010041 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010042 }
10043
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010044 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10045 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10046 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10047 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10048
Jesse Barnes79e53942008-11-07 14:24:08 -080010049 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010050}
10051
Carl Worth08d7b3d2009-04-29 14:43:54 -070010052int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010053 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010054{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010055 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010056 struct drm_mode_object *drmmode_obj;
10057 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010058
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010059 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10060 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010061
Daniel Vetterc05422d2009-08-11 16:05:30 +020010062 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10063 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010064
Daniel Vetterc05422d2009-08-11 16:05:30 +020010065 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010066 DRM_ERROR("no such CRTC id\n");
10067 return -EINVAL;
10068 }
10069
Daniel Vetterc05422d2009-08-11 16:05:30 +020010070 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10071 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010072
Daniel Vetterc05422d2009-08-11 16:05:30 +020010073 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010074}
10075
Daniel Vetter66a92782012-07-12 20:08:18 +020010076static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010077{
Daniel Vetter66a92782012-07-12 20:08:18 +020010078 struct drm_device *dev = encoder->base.dev;
10079 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010080 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010081 int entry = 0;
10082
Daniel Vetter66a92782012-07-12 20:08:18 +020010083 list_for_each_entry(source_encoder,
10084 &dev->mode_config.encoder_list, base.head) {
10085
10086 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010087 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +020010088
10089 /* Intel hw has only one MUX where enocoders could be cloned. */
10090 if (encoder->cloneable && source_encoder->cloneable)
10091 index_mask |= (1 << entry);
10092
Jesse Barnes79e53942008-11-07 14:24:08 -080010093 entry++;
10094 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010095
Jesse Barnes79e53942008-11-07 14:24:08 -080010096 return index_mask;
10097}
10098
Chris Wilson4d302442010-12-14 19:21:29 +000010099static bool has_edp_a(struct drm_device *dev)
10100{
10101 struct drm_i915_private *dev_priv = dev->dev_private;
10102
10103 if (!IS_MOBILE(dev))
10104 return false;
10105
10106 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10107 return false;
10108
10109 if (IS_GEN5(dev) &&
10110 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10111 return false;
10112
10113 return true;
10114}
10115
Jesse Barnes79e53942008-11-07 14:24:08 -080010116static void intel_setup_outputs(struct drm_device *dev)
10117{
Eric Anholt725e30a2009-01-22 13:01:02 -080010118 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010119 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010120 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010121
Daniel Vetterc9093352013-06-06 22:22:47 +020010122 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010123
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010124 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010125 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010126
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010127 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010128 int found;
10129
10130 /* Haswell uses DDI functions to detect digital outputs */
10131 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10132 /* DDI A only supports eDP */
10133 if (found)
10134 intel_ddi_init(dev, PORT_A);
10135
10136 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10137 * register */
10138 found = I915_READ(SFUSE_STRAP);
10139
10140 if (found & SFUSE_STRAP_DDIB_DETECTED)
10141 intel_ddi_init(dev, PORT_B);
10142 if (found & SFUSE_STRAP_DDIC_DETECTED)
10143 intel_ddi_init(dev, PORT_C);
10144 if (found & SFUSE_STRAP_DDID_DETECTED)
10145 intel_ddi_init(dev, PORT_D);
10146 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010147 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +020010148 dpd_is_edp = intel_dpd_is_edp(dev);
10149
10150 if (has_edp_a(dev))
10151 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010152
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010153 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010154 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010155 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010156 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010157 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010158 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010159 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010160 }
10161
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010162 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010163 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010164
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010165 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010166 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010167
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010168 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010169 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010170
Daniel Vetter270b3042012-10-27 15:52:05 +020010171 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010172 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010173 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010174 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10175 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10176 PORT_B);
10177 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10178 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10179 }
10180
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010181 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10182 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10183 PORT_C);
10184 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10185 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10186 PORT_C);
10187 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010188
Jani Nikula3cfca972013-08-27 15:12:26 +030010189 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010190 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010191 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010192
Paulo Zanonie2debe92013-02-18 19:00:27 -030010193 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010194 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010195 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010196 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10197 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010198 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010199 }
Ma Ling27185ae2009-08-24 13:50:23 +080010200
Imre Deake7281ea2013-05-08 13:14:08 +030010201 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010202 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010203 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010204
10205 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010206
Paulo Zanonie2debe92013-02-18 19:00:27 -030010207 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010208 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010209 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010210 }
Ma Ling27185ae2009-08-24 13:50:23 +080010211
Paulo Zanonie2debe92013-02-18 19:00:27 -030010212 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010213
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010214 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10215 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010216 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010217 }
Imre Deake7281ea2013-05-08 13:14:08 +030010218 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010219 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010220 }
Ma Ling27185ae2009-08-24 13:50:23 +080010221
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010222 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010223 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010224 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010225 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010226 intel_dvo_init(dev);
10227
Zhenyu Wang103a1962009-11-27 11:44:36 +080010228 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010229 intel_tv_init(dev);
10230
Chris Wilson4ef69c72010-09-09 15:14:28 +010010231 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10232 encoder->base.possible_crtcs = encoder->crtc_mask;
10233 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010234 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010235 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010236
Paulo Zanonidde86e22012-12-01 12:04:25 -020010237 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010238
10239 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010240}
10241
Chris Wilsonddfe1562013-08-06 17:43:07 +010010242void intel_framebuffer_fini(struct intel_framebuffer *fb)
10243{
10244 drm_framebuffer_cleanup(&fb->base);
Daniel Vetter80075d42013-10-09 21:23:52 +020010245 WARN_ON(!fb->obj->framebuffer_references--);
Chris Wilsonddfe1562013-08-06 17:43:07 +010010246 drm_gem_object_unreference_unlocked(&fb->obj->base);
10247}
10248
Jesse Barnes79e53942008-11-07 14:24:08 -080010249static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10250{
10251 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010252
Chris Wilsonddfe1562013-08-06 17:43:07 +010010253 intel_framebuffer_fini(intel_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010254 kfree(intel_fb);
10255}
10256
10257static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010258 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010259 unsigned int *handle)
10260{
10261 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010262 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010263
Chris Wilson05394f32010-11-08 19:18:58 +000010264 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010265}
10266
10267static const struct drm_framebuffer_funcs intel_fb_funcs = {
10268 .destroy = intel_user_framebuffer_destroy,
10269 .create_handle = intel_user_framebuffer_create_handle,
10270};
10271
Dave Airlie38651672010-03-30 05:34:13 +000010272int intel_framebuffer_init(struct drm_device *dev,
10273 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010274 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +000010275 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010276{
Daniel Vetter53155c02013-10-09 21:55:33 +020010277 int aligned_height, tile_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010278 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010279 int ret;
10280
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010281 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10282
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010283 if (obj->tiling_mode == I915_TILING_Y) {
10284 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010285 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010286 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010287
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010288 if (mode_cmd->pitches[0] & 63) {
10289 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10290 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010291 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010292 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010293
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010294 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10295 pitch_limit = 32*1024;
10296 } else if (INTEL_INFO(dev)->gen >= 4) {
10297 if (obj->tiling_mode)
10298 pitch_limit = 16*1024;
10299 else
10300 pitch_limit = 32*1024;
10301 } else if (INTEL_INFO(dev)->gen >= 3) {
10302 if (obj->tiling_mode)
10303 pitch_limit = 8*1024;
10304 else
10305 pitch_limit = 16*1024;
10306 } else
10307 /* XXX DSPC is limited to 4k tiled */
10308 pitch_limit = 8*1024;
10309
10310 if (mode_cmd->pitches[0] > pitch_limit) {
10311 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10312 obj->tiling_mode ? "tiled" : "linear",
10313 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010314 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010315 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010316
10317 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010318 mode_cmd->pitches[0] != obj->stride) {
10319 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10320 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010321 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010322 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010323
Ville Syrjälä57779d02012-10-31 17:50:14 +020010324 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010325 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010326 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010327 case DRM_FORMAT_RGB565:
10328 case DRM_FORMAT_XRGB8888:
10329 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010330 break;
10331 case DRM_FORMAT_XRGB1555:
10332 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010333 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010334 DRM_DEBUG("unsupported pixel format: %s\n",
10335 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010336 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010337 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020010338 break;
10339 case DRM_FORMAT_XBGR8888:
10340 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010341 case DRM_FORMAT_XRGB2101010:
10342 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010343 case DRM_FORMAT_XBGR2101010:
10344 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010345 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010346 DRM_DEBUG("unsupported pixel format: %s\n",
10347 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010348 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010349 }
Jesse Barnesb5626742011-06-24 12:19:27 -070010350 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020010351 case DRM_FORMAT_YUYV:
10352 case DRM_FORMAT_UYVY:
10353 case DRM_FORMAT_YVYU:
10354 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010355 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010356 DRM_DEBUG("unsupported pixel format: %s\n",
10357 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020010358 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010359 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010360 break;
10361 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000010362 DRM_DEBUG("unsupported pixel format: %s\n",
10363 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010010364 return -EINVAL;
10365 }
10366
Ville Syrjälä90f9a332012-10-31 17:50:19 +020010367 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10368 if (mode_cmd->offsets[0] != 0)
10369 return -EINVAL;
10370
Daniel Vetter53155c02013-10-09 21:55:33 +020010371 tile_height = IS_GEN2(dev) ? 16 : 8;
10372 aligned_height = ALIGN(mode_cmd->height,
10373 obj->tiling_mode ? tile_height : 1);
10374 /* FIXME drm helper for size checks (especially planar formats)? */
10375 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10376 return -EINVAL;
10377
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010378 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10379 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020010380 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010010381
Jesse Barnes79e53942008-11-07 14:24:08 -080010382 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10383 if (ret) {
10384 DRM_ERROR("framebuffer init failed %d\n", ret);
10385 return ret;
10386 }
10387
Jesse Barnes79e53942008-11-07 14:24:08 -080010388 return 0;
10389}
10390
Jesse Barnes79e53942008-11-07 14:24:08 -080010391static struct drm_framebuffer *
10392intel_user_framebuffer_create(struct drm_device *dev,
10393 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010394 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080010395{
Chris Wilson05394f32010-11-08 19:18:58 +000010396 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010397
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010398 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10399 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000010400 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010010401 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080010402
Chris Wilsond2dff872011-04-19 08:36:26 +010010403 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080010404}
10405
Daniel Vetter4520f532013-10-09 09:18:51 +020010406#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020010407static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020010408{
10409}
10410#endif
10411
Jesse Barnes79e53942008-11-07 14:24:08 -080010412static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010413 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020010414 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080010415};
10416
Jesse Barnese70236a2009-09-21 10:42:27 -070010417/* Set up chip specific display functions */
10418static void intel_init_display(struct drm_device *dev)
10419{
10420 struct drm_i915_private *dev_priv = dev->dev_private;
10421
Daniel Vetteree9300b2013-06-03 22:40:22 +020010422 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10423 dev_priv->display.find_dpll = g4x_find_best_dpll;
10424 else if (IS_VALLEYVIEW(dev))
10425 dev_priv->display.find_dpll = vlv_find_best_dpll;
10426 else if (IS_PINEVIEW(dev))
10427 dev_priv->display.find_dpll = pnv_find_best_dpll;
10428 else
10429 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10430
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010431 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010432 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010433 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020010434 dev_priv->display.crtc_enable = haswell_crtc_enable;
10435 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010436 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010437 dev_priv->display.update_plane = ironlake_update_plane;
10438 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010439 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010440 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010441 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10442 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010443 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010444 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070010445 } else if (IS_VALLEYVIEW(dev)) {
10446 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10447 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10448 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10449 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10450 dev_priv->display.off = i9xx_crtc_off;
10451 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010452 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010453 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070010454 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020010455 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10456 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010457 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -070010458 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070010459 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010460
Jesse Barnese70236a2009-09-21 10:42:27 -070010461 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070010462 if (IS_VALLEYVIEW(dev))
10463 dev_priv->display.get_display_clock_speed =
10464 valleyview_get_display_clock_speed;
10465 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070010466 dev_priv->display.get_display_clock_speed =
10467 i945_get_display_clock_speed;
10468 else if (IS_I915G(dev))
10469 dev_priv->display.get_display_clock_speed =
10470 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010471 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010472 dev_priv->display.get_display_clock_speed =
10473 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020010474 else if (IS_PINEVIEW(dev))
10475 dev_priv->display.get_display_clock_speed =
10476 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070010477 else if (IS_I915GM(dev))
10478 dev_priv->display.get_display_clock_speed =
10479 i915gm_get_display_clock_speed;
10480 else if (IS_I865G(dev))
10481 dev_priv->display.get_display_clock_speed =
10482 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020010483 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070010484 dev_priv->display.get_display_clock_speed =
10485 i855_get_display_clock_speed;
10486 else /* 852, 830 */
10487 dev_priv->display.get_display_clock_speed =
10488 i830_get_display_clock_speed;
10489
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080010490 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010010491 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010492 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010493 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080010494 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070010495 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010496 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -070010497 } else if (IS_IVYBRIDGE(dev)) {
10498 /* FIXME: detect B0+ stepping and use auto training */
10499 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080010500 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020010501 dev_priv->display.modeset_global_resources =
10502 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030010503 } else if (IS_HASWELL(dev)) {
10504 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080010505 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020010506 dev_priv->display.modeset_global_resources =
10507 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020010508 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070010509 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080010510 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080010511 } else if (IS_VALLEYVIEW(dev)) {
10512 dev_priv->display.modeset_global_resources =
10513 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070010514 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010515
10516 /* Default just returns -ENODEV to indicate unsupported */
10517 dev_priv->display.queue_flip = intel_default_queue_flip;
10518
10519 switch (INTEL_INFO(dev)->gen) {
10520 case 2:
10521 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10522 break;
10523
10524 case 3:
10525 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10526 break;
10527
10528 case 4:
10529 case 5:
10530 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10531 break;
10532
10533 case 6:
10534 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10535 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010536 case 7:
10537 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10538 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010539 }
Jesse Barnese70236a2009-09-21 10:42:27 -070010540}
10541
Jesse Barnesb690e962010-07-19 13:53:12 -070010542/*
10543 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10544 * resume, or other times. This quirk makes sure that's the case for
10545 * affected systems.
10546 */
Akshay Joshi0206e352011-08-16 15:34:10 -040010547static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070010548{
10549 struct drm_i915_private *dev_priv = dev->dev_private;
10550
10551 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010552 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010553}
10554
Keith Packard435793d2011-07-12 14:56:22 -070010555/*
10556 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10557 */
10558static void quirk_ssc_force_disable(struct drm_device *dev)
10559{
10560 struct drm_i915_private *dev_priv = dev->dev_private;
10561 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010562 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070010563}
10564
Carsten Emde4dca20e2012-03-15 15:56:26 +010010565/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010010566 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10567 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010010568 */
10569static void quirk_invert_brightness(struct drm_device *dev)
10570{
10571 struct drm_i915_private *dev_priv = dev->dev_private;
10572 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020010573 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070010574}
10575
Kamal Mostafae85843b2013-07-19 15:02:01 -070010576/*
10577 * Some machines (Dell XPS13) suffer broken backlight controls if
10578 * BLM_PCH_PWM_ENABLE is set.
10579 */
10580static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
10581{
10582 struct drm_i915_private *dev_priv = dev->dev_private;
10583 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
10584 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
10585}
10586
Jesse Barnesb690e962010-07-19 13:53:12 -070010587struct intel_quirk {
10588 int device;
10589 int subsystem_vendor;
10590 int subsystem_device;
10591 void (*hook)(struct drm_device *dev);
10592};
10593
Egbert Eich5f85f1762012-10-14 15:46:38 +020010594/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10595struct intel_dmi_quirk {
10596 void (*hook)(struct drm_device *dev);
10597 const struct dmi_system_id (*dmi_id_list)[];
10598};
10599
10600static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10601{
10602 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10603 return 1;
10604}
10605
10606static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10607 {
10608 .dmi_id_list = &(const struct dmi_system_id[]) {
10609 {
10610 .callback = intel_dmi_reverse_brightness,
10611 .ident = "NCR Corporation",
10612 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10613 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10614 },
10615 },
10616 { } /* terminating entry */
10617 },
10618 .hook = quirk_invert_brightness,
10619 },
10620};
10621
Ben Widawskyc43b5632012-04-16 14:07:40 -070010622static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070010623 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040010624 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070010625
Jesse Barnesb690e962010-07-19 13:53:12 -070010626 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10627 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10628
Jesse Barnesb690e962010-07-19 13:53:12 -070010629 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10630 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10631
Chris Wilsona4945f92013-10-08 11:16:59 +010010632 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020010633 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070010634
10635 /* Lenovo U160 cannot use SSC on LVDS */
10636 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020010637
10638 /* Sony Vaio Y cannot use SSC on LVDS */
10639 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010010640
Jani Nikulaee1452d2013-09-20 15:05:30 +030010641 /*
10642 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10643 * seem to use inverted backlight PWM.
10644 */
10645 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
Kamal Mostafae85843b2013-07-19 15:02:01 -070010646
10647 /* Dell XPS13 HD Sandy Bridge */
10648 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
10649 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
10650 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
Jesse Barnesb690e962010-07-19 13:53:12 -070010651};
10652
10653static void intel_init_quirks(struct drm_device *dev)
10654{
10655 struct pci_dev *d = dev->pdev;
10656 int i;
10657
10658 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10659 struct intel_quirk *q = &intel_quirks[i];
10660
10661 if (d->device == q->device &&
10662 (d->subsystem_vendor == q->subsystem_vendor ||
10663 q->subsystem_vendor == PCI_ANY_ID) &&
10664 (d->subsystem_device == q->subsystem_device ||
10665 q->subsystem_device == PCI_ANY_ID))
10666 q->hook(dev);
10667 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020010668 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10669 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10670 intel_dmi_quirks[i].hook(dev);
10671 }
Jesse Barnesb690e962010-07-19 13:53:12 -070010672}
10673
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010674/* Disable the VGA plane that we never use */
10675static void i915_disable_vga(struct drm_device *dev)
10676{
10677 struct drm_i915_private *dev_priv = dev->dev_private;
10678 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010679 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010680
10681 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070010682 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010683 sr1 = inb(VGA_SR_DATA);
10684 outb(sr1 | 1<<5, VGA_SR_DATA);
10685 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10686 udelay(300);
10687
10688 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10689 POSTING_READ(vga_reg);
10690}
10691
Daniel Vetterf8175862012-04-10 15:50:11 +020010692void intel_modeset_init_hw(struct drm_device *dev)
10693{
Jesse Barnesf6071162013-10-01 10:41:38 -070010694 struct drm_i915_private *dev_priv = dev->dev_private;
10695
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030010696 intel_prepare_ddi(dev);
10697
Daniel Vetterf8175862012-04-10 15:50:11 +020010698 intel_init_clock_gating(dev);
10699
Jesse Barnesf6071162013-10-01 10:41:38 -070010700 /* Enable the CRI clock source so we can get at the display */
10701 if (IS_VALLEYVIEW(dev))
10702 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10703 DPLL_INTEGRATED_CRI_CLK_VLV);
10704
Jesse Barnes40e9cf62013-10-03 11:35:46 -070010705 intel_init_dpio(dev);
10706
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010707 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010708 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +020010709 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +020010710}
10711
Imre Deak7d708ee2013-04-17 14:04:50 +030010712void intel_modeset_suspend_hw(struct drm_device *dev)
10713{
10714 intel_suspend_hw(dev);
10715}
10716
Jesse Barnes79e53942008-11-07 14:24:08 -080010717void intel_modeset_init(struct drm_device *dev)
10718{
Jesse Barnes652c3932009-08-17 13:31:43 -070010719 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010720 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010721
10722 drm_mode_config_init(dev);
10723
10724 dev->mode_config.min_width = 0;
10725 dev->mode_config.min_height = 0;
10726
Dave Airlie019d96c2011-09-29 16:20:42 +010010727 dev->mode_config.preferred_depth = 24;
10728 dev->mode_config.prefer_shadow = 1;
10729
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020010730 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080010731
Jesse Barnesb690e962010-07-19 13:53:12 -070010732 intel_init_quirks(dev);
10733
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030010734 intel_init_pm(dev);
10735
Ben Widawskye3c74752013-04-05 13:12:39 -070010736 if (INTEL_INFO(dev)->num_pipes == 0)
10737 return;
10738
Jesse Barnese70236a2009-09-21 10:42:27 -070010739 intel_init_display(dev);
10740
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010741 if (IS_GEN2(dev)) {
10742 dev->mode_config.max_width = 2048;
10743 dev->mode_config.max_height = 2048;
10744 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070010745 dev->mode_config.max_width = 4096;
10746 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080010747 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010748 dev->mode_config.max_width = 8192;
10749 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080010750 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -080010751 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010752
Zhao Yakui28c97732009-10-09 11:39:41 +080010753 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010754 INTEL_INFO(dev)->num_pipes,
10755 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080010756
Damien Lespiau08e2a7d2013-07-11 20:10:54 +010010757 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010758 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010759 for (j = 0; j < dev_priv->num_plane; j++) {
10760 ret = intel_plane_init(dev, i, j);
10761 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030010762 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10763 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070010764 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010765 }
10766
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010767 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010768 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010769
Jesse Barnes9cce37f2010-08-13 15:11:26 -070010770 /* Just disable it once at startup */
10771 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010772 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000010773
10774 /* Just in case the BIOS is doing something questionable. */
10775 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010776}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080010777
Daniel Vetter24929352012-07-02 20:28:59 +020010778static void
10779intel_connector_break_all_links(struct intel_connector *connector)
10780{
10781 connector->base.dpms = DRM_MODE_DPMS_OFF;
10782 connector->base.encoder = NULL;
10783 connector->encoder->connectors_active = false;
10784 connector->encoder->base.crtc = NULL;
10785}
10786
Daniel Vetter7fad7982012-07-04 17:51:47 +020010787static void intel_enable_pipe_a(struct drm_device *dev)
10788{
10789 struct intel_connector *connector;
10790 struct drm_connector *crt = NULL;
10791 struct intel_load_detect_pipe load_detect_temp;
10792
10793 /* We can't just switch on the pipe A, we need to set things up with a
10794 * proper mode and output configuration. As a gross hack, enable pipe A
10795 * by enabling the load detect pipe once. */
10796 list_for_each_entry(connector,
10797 &dev->mode_config.connector_list,
10798 base.head) {
10799 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10800 crt = &connector->base;
10801 break;
10802 }
10803 }
10804
10805 if (!crt)
10806 return;
10807
10808 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10809 intel_release_load_detect_pipe(crt, &load_detect_temp);
10810
10811
10812}
10813
Daniel Vetterfa555832012-10-10 23:14:00 +020010814static bool
10815intel_check_plane_mapping(struct intel_crtc *crtc)
10816{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010817 struct drm_device *dev = crtc->base.dev;
10818 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010819 u32 reg, val;
10820
Ben Widawsky7eb552a2013-03-13 14:05:41 -070010821 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020010822 return true;
10823
10824 reg = DSPCNTR(!crtc->plane);
10825 val = I915_READ(reg);
10826
10827 if ((val & DISPLAY_PLANE_ENABLE) &&
10828 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10829 return false;
10830
10831 return true;
10832}
10833
Daniel Vetter24929352012-07-02 20:28:59 +020010834static void intel_sanitize_crtc(struct intel_crtc *crtc)
10835{
10836 struct drm_device *dev = crtc->base.dev;
10837 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020010838 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020010839
Daniel Vetter24929352012-07-02 20:28:59 +020010840 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020010841 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020010842 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10843
10844 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020010845 * disable the crtc (and hence change the state) if it is wrong. Note
10846 * that gen4+ has a fixed plane -> pipe mapping. */
10847 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020010848 struct intel_connector *connector;
10849 bool plane;
10850
Daniel Vetter24929352012-07-02 20:28:59 +020010851 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10852 crtc->base.base.id);
10853
10854 /* Pipe has the wrong plane attached and the plane is active.
10855 * Temporarily change the plane mapping and disable everything
10856 * ... */
10857 plane = crtc->plane;
10858 crtc->plane = !plane;
10859 dev_priv->display.crtc_disable(&crtc->base);
10860 crtc->plane = plane;
10861
10862 /* ... and break all links. */
10863 list_for_each_entry(connector, &dev->mode_config.connector_list,
10864 base.head) {
10865 if (connector->encoder->base.crtc != &crtc->base)
10866 continue;
10867
10868 intel_connector_break_all_links(connector);
10869 }
10870
10871 WARN_ON(crtc->active);
10872 crtc->base.enabled = false;
10873 }
Daniel Vetter24929352012-07-02 20:28:59 +020010874
Daniel Vetter7fad7982012-07-04 17:51:47 +020010875 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10876 crtc->pipe == PIPE_A && !crtc->active) {
10877 /* BIOS forgot to enable pipe A, this mostly happens after
10878 * resume. Force-enable the pipe to fix this, the update_dpms
10879 * call below we restore the pipe to the right state, but leave
10880 * the required bits on. */
10881 intel_enable_pipe_a(dev);
10882 }
10883
Daniel Vetter24929352012-07-02 20:28:59 +020010884 /* Adjust the state of the output pipe according to whether we
10885 * have active connectors/encoders. */
10886 intel_crtc_update_dpms(&crtc->base);
10887
10888 if (crtc->active != crtc->base.enabled) {
10889 struct intel_encoder *encoder;
10890
10891 /* This can happen either due to bugs in the get_hw_state
10892 * functions or because the pipe is force-enabled due to the
10893 * pipe A quirk. */
10894 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10895 crtc->base.base.id,
10896 crtc->base.enabled ? "enabled" : "disabled",
10897 crtc->active ? "enabled" : "disabled");
10898
10899 crtc->base.enabled = crtc->active;
10900
10901 /* Because we only establish the connector -> encoder ->
10902 * crtc links if something is active, this means the
10903 * crtc is now deactivated. Break the links. connector
10904 * -> encoder links are only establish when things are
10905 * actually up, hence no need to break them. */
10906 WARN_ON(crtc->active);
10907
10908 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10909 WARN_ON(encoder->connectors_active);
10910 encoder->base.crtc = NULL;
10911 }
10912 }
10913}
10914
10915static void intel_sanitize_encoder(struct intel_encoder *encoder)
10916{
10917 struct intel_connector *connector;
10918 struct drm_device *dev = encoder->base.dev;
10919
10920 /* We need to check both for a crtc link (meaning that the
10921 * encoder is active and trying to read from a pipe) and the
10922 * pipe itself being active. */
10923 bool has_active_crtc = encoder->base.crtc &&
10924 to_intel_crtc(encoder->base.crtc)->active;
10925
10926 if (encoder->connectors_active && !has_active_crtc) {
10927 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10928 encoder->base.base.id,
10929 drm_get_encoder_name(&encoder->base));
10930
10931 /* Connector is active, but has no active pipe. This is
10932 * fallout from our resume register restoring. Disable
10933 * the encoder manually again. */
10934 if (encoder->base.crtc) {
10935 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10936 encoder->base.base.id,
10937 drm_get_encoder_name(&encoder->base));
10938 encoder->disable(encoder);
10939 }
10940
10941 /* Inconsistent output/port/pipe state happens presumably due to
10942 * a bug in one of the get_hw_state functions. Or someplace else
10943 * in our code, like the register restore mess on resume. Clamp
10944 * things to off as a safer default. */
10945 list_for_each_entry(connector,
10946 &dev->mode_config.connector_list,
10947 base.head) {
10948 if (connector->encoder != encoder)
10949 continue;
10950
10951 intel_connector_break_all_links(connector);
10952 }
10953 }
10954 /* Enabled encoders without active connectors will be fixed in
10955 * the crtc fixup. */
10956}
10957
Daniel Vetter44cec742013-01-25 17:53:21 +010010958void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010959{
10960 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010961 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010962
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010963 /* This function can be called both from intel_modeset_setup_hw_state or
10964 * at a very early point in our resume sequence, where the power well
10965 * structures are not yet restored. Since this function is at a very
10966 * paranoid "someone might have enabled VGA while we were not looking"
10967 * level, just check if the power well is enabled instead of trying to
10968 * follow the "don't touch the power well if we don't need it" policy
10969 * the rest of the driver uses. */
10970 if (HAS_POWER_WELL(dev) &&
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -030010971 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030010972 return;
10973
Ville Syrjäläe1553fa2013-10-04 20:32:25 +030010974 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010975 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020010976 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010977 }
10978}
10979
Daniel Vetter30e984d2013-06-05 13:34:17 +020010980static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020010981{
10982 struct drm_i915_private *dev_priv = dev->dev_private;
10983 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020010984 struct intel_crtc *crtc;
10985 struct intel_encoder *encoder;
10986 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020010987 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020010988
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010989 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10990 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010010991 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020010992
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010993 crtc->active = dev_priv->display.get_pipe_config(crtc,
10994 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010995
10996 crtc->base.enabled = crtc->active;
Ville Syrjälä4c445e02013-10-09 17:24:58 +030010997 crtc->primary_enabled = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020010998
10999 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11000 crtc->base.base.id,
11001 crtc->active ? "enabled" : "disabled");
11002 }
11003
Daniel Vetter53589012013-06-05 13:34:16 +020011004 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011005 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011006 intel_ddi_setup_hw_pll_state(dev);
11007
Daniel Vetter53589012013-06-05 13:34:16 +020011008 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11009 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11010
11011 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11012 pll->active = 0;
11013 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11014 base.head) {
11015 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11016 pll->active++;
11017 }
11018 pll->refcount = pll->active;
11019
Daniel Vetter35c95372013-07-17 06:55:04 +020011020 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11021 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011022 }
11023
Daniel Vetter24929352012-07-02 20:28:59 +020011024 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11025 base.head) {
11026 pipe = 0;
11027
11028 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011029 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11030 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070011031 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011032 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011033 } else {
11034 encoder->base.crtc = NULL;
11035 }
11036
11037 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011038 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011039 encoder->base.base.id,
11040 drm_get_encoder_name(&encoder->base),
11041 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011042 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011043 }
11044
11045 list_for_each_entry(connector, &dev->mode_config.connector_list,
11046 base.head) {
11047 if (connector->get_hw_state(connector)) {
11048 connector->base.dpms = DRM_MODE_DPMS_ON;
11049 connector->encoder->connectors_active = true;
11050 connector->base.encoder = &connector->encoder->base;
11051 } else {
11052 connector->base.dpms = DRM_MODE_DPMS_OFF;
11053 connector->base.encoder = NULL;
11054 }
11055 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11056 connector->base.base.id,
11057 drm_get_connector_name(&connector->base),
11058 connector->base.encoder ? "enabled" : "disabled");
11059 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011060}
11061
11062/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11063 * and i915 state tracking structures. */
11064void intel_modeset_setup_hw_state(struct drm_device *dev,
11065 bool force_restore)
11066{
11067 struct drm_i915_private *dev_priv = dev->dev_private;
11068 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011069 struct intel_crtc *crtc;
11070 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011071 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011072
11073 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011074
Jesse Barnesbabea612013-06-26 18:57:38 +030011075 /*
11076 * Now that we have the config, copy it to each CRTC struct
11077 * Note that this could go away if we move to using crtc_config
11078 * checking everywhere.
11079 */
11080 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11081 base.head) {
11082 if (crtc->active && i915_fastboot) {
11083 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11084
11085 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11086 crtc->base.base.id);
11087 drm_mode_debug_printmodeline(&crtc->base.mode);
11088 }
11089 }
11090
Daniel Vetter24929352012-07-02 20:28:59 +020011091 /* HW state is read out, now we need to sanitize this mess. */
11092 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11093 base.head) {
11094 intel_sanitize_encoder(encoder);
11095 }
11096
11097 for_each_pipe(pipe) {
11098 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11099 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011100 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011101 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011102
Daniel Vetter35c95372013-07-17 06:55:04 +020011103 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11104 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11105
11106 if (!pll->on || pll->active)
11107 continue;
11108
11109 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11110
11111 pll->disable(dev_priv, pll);
11112 pll->on = false;
11113 }
11114
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011115 if (IS_HASWELL(dev))
11116 ilk_wm_get_hw_state(dev);
11117
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011118 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011119 i915_redisable_vga(dev);
11120
Daniel Vetterf30da182013-04-11 20:22:50 +020011121 /*
11122 * We need to use raw interfaces for restoring state to avoid
11123 * checking (bogus) intermediate states.
11124 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011125 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011126 struct drm_crtc *crtc =
11127 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011128
11129 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11130 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011131 }
11132 } else {
11133 intel_modeset_update_staged_output_state(dev);
11134 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011135
11136 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020011137
11138 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011139}
11140
11141void intel_modeset_gem_init(struct drm_device *dev)
11142{
Chris Wilson1833b132012-05-09 11:56:28 +010011143 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011144
11145 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011146
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011147 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080011148}
11149
11150void intel_modeset_cleanup(struct drm_device *dev)
11151{
Jesse Barnes652c3932009-08-17 13:31:43 -070011152 struct drm_i915_private *dev_priv = dev->dev_private;
11153 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011154 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011155
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011156 /*
11157 * Interrupts and polling as the first thing to avoid creating havoc.
11158 * Too much stuff here (turning of rps, connectors, ...) would
11159 * experience fancy races otherwise.
11160 */
11161 drm_irq_uninstall(dev);
11162 cancel_work_sync(&dev_priv->hotplug_work);
11163 /*
11164 * Due to the hpd irq storm handling the hotplug work can re-arm the
11165 * poll handlers. Hence disable polling after hpd handling is shut down.
11166 */
Keith Packardf87ea762010-10-03 19:36:26 -070011167 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011168
Jesse Barnes652c3932009-08-17 13:31:43 -070011169 mutex_lock(&dev->struct_mutex);
11170
Jesse Barnes723bfd72010-10-07 16:01:13 -070011171 intel_unregister_dsm_handler();
11172
Jesse Barnes652c3932009-08-17 13:31:43 -070011173 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11174 /* Skip inactive CRTCs */
11175 if (!crtc->fb)
11176 continue;
11177
Daniel Vetter3dec0092010-08-20 21:40:52 +020011178 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011179 }
11180
Chris Wilson973d04f2011-07-08 12:22:37 +010011181 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011182
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011183 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011184
Daniel Vetter930ebb42012-06-29 23:32:16 +020011185 ironlake_teardown_rc6(dev);
11186
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011187 mutex_unlock(&dev->struct_mutex);
11188
Chris Wilson1630fe72011-07-08 12:22:42 +010011189 /* flush any delayed tasks or pending work */
11190 flush_scheduled_work();
11191
Jani Nikuladc652f92013-04-12 15:18:38 +030011192 /* destroy backlight, if any, before the connectors */
11193 intel_panel_destroy_backlight(dev);
11194
Paulo Zanonid9255d52013-09-26 20:05:59 -030011195 /* destroy the sysfs files before encoders/connectors */
11196 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
11197 drm_sysfs_connector_remove(connector);
11198
Jesse Barnes79e53942008-11-07 14:24:08 -080011199 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011200
11201 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011202}
11203
Dave Airlie28d52042009-09-21 14:33:58 +100011204/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011205 * Return which encoder is currently attached for connector.
11206 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011207struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011208{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011209 return &intel_attached_encoder(connector)->base;
11210}
Jesse Barnes79e53942008-11-07 14:24:08 -080011211
Chris Wilsondf0e9242010-09-09 16:20:55 +010011212void intel_connector_attach_encoder(struct intel_connector *connector,
11213 struct intel_encoder *encoder)
11214{
11215 connector->encoder = encoder;
11216 drm_mode_connector_attach_encoder(&connector->base,
11217 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011218}
Dave Airlie28d52042009-09-21 14:33:58 +100011219
11220/*
11221 * set vga decode state - true == enable VGA decode
11222 */
11223int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11224{
11225 struct drm_i915_private *dev_priv = dev->dev_private;
11226 u16 gmch_ctrl;
11227
11228 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11229 if (state)
11230 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11231 else
11232 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11233 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11234 return 0;
11235}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011236
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011237struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011238
11239 u32 power_well_driver;
11240
Chris Wilson63b66e52013-08-08 15:12:06 +020011241 int num_transcoders;
11242
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011243 struct intel_cursor_error_state {
11244 u32 control;
11245 u32 position;
11246 u32 base;
11247 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010011248 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011249
11250 struct intel_pipe_error_state {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011251 u32 source;
Damien Lespiau52331302012-08-15 19:23:25 +010011252 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011253
11254 struct intel_plane_error_state {
11255 u32 control;
11256 u32 stride;
11257 u32 size;
11258 u32 pos;
11259 u32 addr;
11260 u32 surface;
11261 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010011262 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020011263
11264 struct intel_transcoder_error_state {
11265 enum transcoder cpu_transcoder;
11266
11267 u32 conf;
11268
11269 u32 htotal;
11270 u32 hblank;
11271 u32 hsync;
11272 u32 vtotal;
11273 u32 vblank;
11274 u32 vsync;
11275 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011276};
11277
11278struct intel_display_error_state *
11279intel_display_capture_error_state(struct drm_device *dev)
11280{
Akshay Joshi0206e352011-08-16 15:34:10 -040011281 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011282 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020011283 int transcoders[] = {
11284 TRANSCODER_A,
11285 TRANSCODER_B,
11286 TRANSCODER_C,
11287 TRANSCODER_EDP,
11288 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011289 int i;
11290
Chris Wilson63b66e52013-08-08 15:12:06 +020011291 if (INTEL_INFO(dev)->num_pipes == 0)
11292 return NULL;
11293
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011294 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011295 if (error == NULL)
11296 return NULL;
11297
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011298 if (HAS_POWER_WELL(dev))
11299 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11300
Damien Lespiau52331302012-08-15 19:23:25 +010011301 for_each_pipe(i) {
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011302 if (!intel_display_power_enabled(dev, POWER_DOMAIN_PIPE(i)))
11303 continue;
11304
Paulo Zanonia18c4c32013-03-06 20:03:12 -030011305 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11306 error->cursor[i].control = I915_READ(CURCNTR(i));
11307 error->cursor[i].position = I915_READ(CURPOS(i));
11308 error->cursor[i].base = I915_READ(CURBASE(i));
11309 } else {
11310 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11311 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11312 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11313 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011314
11315 error->plane[i].control = I915_READ(DSPCNTR(i));
11316 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011317 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030011318 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011319 error->plane[i].pos = I915_READ(DSPPOS(i));
11320 }
Paulo Zanonica291362013-03-06 20:03:14 -030011321 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11322 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011323 if (INTEL_INFO(dev)->gen >= 4) {
11324 error->plane[i].surface = I915_READ(DSPSURF(i));
11325 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11326 }
11327
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011328 error->pipe[i].source = I915_READ(PIPESRC(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020011329 }
11330
11331 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11332 if (HAS_DDI(dev_priv->dev))
11333 error->num_transcoders++; /* Account for eDP. */
11334
11335 for (i = 0; i < error->num_transcoders; i++) {
11336 enum transcoder cpu_transcoder = transcoders[i];
11337
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020011338 if (!intel_display_power_enabled(dev,
11339 POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
11340 continue;
11341
Chris Wilson63b66e52013-08-08 15:12:06 +020011342 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11343
11344 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11345 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11346 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11347 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11348 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11349 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11350 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011351 }
11352
11353 return error;
11354}
11355
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011356#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11357
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011358void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011359intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011360 struct drm_device *dev,
11361 struct intel_display_error_state *error)
11362{
11363 int i;
11364
Chris Wilson63b66e52013-08-08 15:12:06 +020011365 if (!error)
11366 return;
11367
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011368 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011369 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011370 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030011371 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010011372 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011373 err_printf(m, "Pipe [%d]:\n", i);
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011374 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011375
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011376 err_printf(m, "Plane [%d]:\n", i);
11377 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11378 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011379 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011380 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11381 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030011382 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030011383 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011384 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011385 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011386 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11387 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011388 }
11389
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030011390 err_printf(m, "Cursor [%d]:\n", i);
11391 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11392 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11393 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011394 }
Chris Wilson63b66e52013-08-08 15:12:06 +020011395
11396 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010011397 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020011398 transcoder_name(error->transcoder[i].cpu_transcoder));
11399 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11400 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11401 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11402 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11403 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11404 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11405 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11406 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000011407}