blob: 13330fd7b21ccab7d86f75cf86d2f0e9ad97004a [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
Chon Ming Leeef9348c2014-04-09 13:28:18 +030044#define DIV_ROUND_CLOSEST_ULL(ll, d) \
45 ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
46
Daniel Vetter3dec0092010-08-20 21:40:52 +020047static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010048static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080049
Jesse Barnesf1f644d2013-06-27 00:39:25 +030050static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
51 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030052static void ironlake_pch_clock_get(struct intel_crtc *crtc,
53 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030054
Damien Lespiaue7457a92013-08-08 22:28:59 +010055static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
56 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080057static int intel_framebuffer_init(struct drm_device *dev,
58 struct intel_framebuffer *ifb,
59 struct drm_mode_fb_cmd2 *mode_cmd,
60 struct drm_i915_gem_object *obj);
Damien Lespiaue7457a92013-08-08 22:28:59 +010061
Jesse Barnes79e53942008-11-07 14:24:08 -080062typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040063 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080064} intel_range_t;
65
66typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 int dot_limit;
68 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080069} intel_p2_t;
70
Ma Lingd4906092009-03-18 20:13:27 +080071typedef struct intel_limit intel_limit_t;
72struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040073 intel_range_t dot, vco, n, m, m1, m2, p, p1;
74 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080075};
Jesse Barnes79e53942008-11-07 14:24:08 -080076
Daniel Vetterd2acd212012-10-20 20:57:43 +020077int
78intel_pch_rawclk(struct drm_device *dev)
79{
80 struct drm_i915_private *dev_priv = dev->dev_private;
81
82 WARN_ON(!HAS_PCH_SPLIT(dev));
83
84 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
85}
86
Chris Wilson021357a2010-09-07 20:54:59 +010087static inline u32 /* units of 100MHz */
88intel_fdi_link_freq(struct drm_device *dev)
89{
Chris Wilson8b99e682010-10-13 09:59:17 +010090 if (IS_GEN5(dev)) {
91 struct drm_i915_private *dev_priv = dev->dev_private;
92 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
93 } else
94 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010095}
96
Daniel Vetter5d536e22013-07-06 12:52:06 +020097static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040098 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +020099 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200100 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400101 .m = { .min = 96, .max = 140 },
102 .m1 = { .min = 18, .max = 26 },
103 .m2 = { .min = 6, .max = 16 },
104 .p = { .min = 4, .max = 128 },
105 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700106 .p2 = { .dot_limit = 165000,
107 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700108};
109
Daniel Vetter5d536e22013-07-06 12:52:06 +0200110static const intel_limit_t intel_limits_i8xx_dvo = {
111 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200112 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200113 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200114 .m = { .min = 96, .max = 140 },
115 .m1 = { .min = 18, .max = 26 },
116 .m2 = { .min = 6, .max = 16 },
117 .p = { .min = 4, .max = 128 },
118 .p1 = { .min = 2, .max = 33 },
119 .p2 = { .dot_limit = 165000,
120 .p2_slow = 4, .p2_fast = 4 },
121};
122
Keith Packarde4b36692009-06-05 19:22:17 -0700123static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400124 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200125 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200126 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400127 .m = { .min = 96, .max = 140 },
128 .m1 = { .min = 18, .max = 26 },
129 .m2 = { .min = 6, .max = 16 },
130 .p = { .min = 4, .max = 128 },
131 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700132 .p2 = { .dot_limit = 165000,
133 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700134};
Eric Anholt273e27c2011-03-30 13:01:10 -0700135
Keith Packarde4b36692009-06-05 19:22:17 -0700136static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .dot = { .min = 20000, .max = 400000 },
138 .vco = { .min = 1400000, .max = 2800000 },
139 .n = { .min = 1, .max = 6 },
140 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100141 .m1 = { .min = 8, .max = 18 },
142 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400143 .p = { .min = 5, .max = 80 },
144 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700145 .p2 = { .dot_limit = 200000,
146 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700147};
148
149static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .dot = { .min = 20000, .max = 400000 },
151 .vco = { .min = 1400000, .max = 2800000 },
152 .n = { .min = 1, .max = 6 },
153 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100154 .m1 = { .min = 8, .max = 18 },
155 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400156 .p = { .min = 7, .max = 98 },
157 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .p2 = { .dot_limit = 112000,
159 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700160};
161
Eric Anholt273e27c2011-03-30 13:01:10 -0700162
Keith Packarde4b36692009-06-05 19:22:17 -0700163static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700164 .dot = { .min = 25000, .max = 270000 },
165 .vco = { .min = 1750000, .max = 3500000},
166 .n = { .min = 1, .max = 4 },
167 .m = { .min = 104, .max = 138 },
168 .m1 = { .min = 17, .max = 23 },
169 .m2 = { .min = 5, .max = 11 },
170 .p = { .min = 10, .max = 30 },
171 .p1 = { .min = 1, .max = 3},
172 .p2 = { .dot_limit = 270000,
173 .p2_slow = 10,
174 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800175 },
Keith Packarde4b36692009-06-05 19:22:17 -0700176};
177
178static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700179 .dot = { .min = 22000, .max = 400000 },
180 .vco = { .min = 1750000, .max = 3500000},
181 .n = { .min = 1, .max = 4 },
182 .m = { .min = 104, .max = 138 },
183 .m1 = { .min = 16, .max = 23 },
184 .m2 = { .min = 5, .max = 11 },
185 .p = { .min = 5, .max = 80 },
186 .p1 = { .min = 1, .max = 8},
187 .p2 = { .dot_limit = 165000,
188 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700189};
190
191static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700192 .dot = { .min = 20000, .max = 115000 },
193 .vco = { .min = 1750000, .max = 3500000 },
194 .n = { .min = 1, .max = 3 },
195 .m = { .min = 104, .max = 138 },
196 .m1 = { .min = 17, .max = 23 },
197 .m2 = { .min = 5, .max = 11 },
198 .p = { .min = 28, .max = 112 },
199 .p1 = { .min = 2, .max = 8 },
200 .p2 = { .dot_limit = 0,
201 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800202 },
Keith Packarde4b36692009-06-05 19:22:17 -0700203};
204
205static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700206 .dot = { .min = 80000, .max = 224000 },
207 .vco = { .min = 1750000, .max = 3500000 },
208 .n = { .min = 1, .max = 3 },
209 .m = { .min = 104, .max = 138 },
210 .m1 = { .min = 17, .max = 23 },
211 .m2 = { .min = 5, .max = 11 },
212 .p = { .min = 14, .max = 42 },
213 .p1 = { .min = 2, .max = 6 },
214 .p2 = { .dot_limit = 0,
215 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800216 },
Keith Packarde4b36692009-06-05 19:22:17 -0700217};
218
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500219static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .dot = { .min = 20000, .max = 400000},
221 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700222 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400223 .n = { .min = 3, .max = 6 },
224 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700225 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400226 .m1 = { .min = 0, .max = 0 },
227 .m2 = { .min = 0, .max = 254 },
228 .p = { .min = 5, .max = 80 },
229 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700230 .p2 = { .dot_limit = 200000,
231 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700232};
233
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500234static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400235 .dot = { .min = 20000, .max = 400000 },
236 .vco = { .min = 1700000, .max = 3500000 },
237 .n = { .min = 3, .max = 6 },
238 .m = { .min = 2, .max = 256 },
239 .m1 = { .min = 0, .max = 0 },
240 .m2 = { .min = 0, .max = 254 },
241 .p = { .min = 7, .max = 112 },
242 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700243 .p2 = { .dot_limit = 112000,
244 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700245};
246
Eric Anholt273e27c2011-03-30 13:01:10 -0700247/* Ironlake / Sandybridge
248 *
249 * We calculate clock using (register_value + 2) for N/M1/M2, so here
250 * the range value for them is (actual_value - 2).
251 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800252static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700253 .dot = { .min = 25000, .max = 350000 },
254 .vco = { .min = 1760000, .max = 3510000 },
255 .n = { .min = 1, .max = 5 },
256 .m = { .min = 79, .max = 127 },
257 .m1 = { .min = 12, .max = 22 },
258 .m2 = { .min = 5, .max = 9 },
259 .p = { .min = 5, .max = 80 },
260 .p1 = { .min = 1, .max = 8 },
261 .p2 = { .dot_limit = 225000,
262 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700263};
264
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800265static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .dot = { .min = 25000, .max = 350000 },
267 .vco = { .min = 1760000, .max = 3510000 },
268 .n = { .min = 1, .max = 3 },
269 .m = { .min = 79, .max = 118 },
270 .m1 = { .min = 12, .max = 22 },
271 .m2 = { .min = 5, .max = 9 },
272 .p = { .min = 28, .max = 112 },
273 .p1 = { .min = 2, .max = 8 },
274 .p2 = { .dot_limit = 225000,
275 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800276};
277
278static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700279 .dot = { .min = 25000, .max = 350000 },
280 .vco = { .min = 1760000, .max = 3510000 },
281 .n = { .min = 1, .max = 3 },
282 .m = { .min = 79, .max = 127 },
283 .m1 = { .min = 12, .max = 22 },
284 .m2 = { .min = 5, .max = 9 },
285 .p = { .min = 14, .max = 56 },
286 .p1 = { .min = 2, .max = 8 },
287 .p2 = { .dot_limit = 225000,
288 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800289};
290
Eric Anholt273e27c2011-03-30 13:01:10 -0700291/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800292static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700293 .dot = { .min = 25000, .max = 350000 },
294 .vco = { .min = 1760000, .max = 3510000 },
295 .n = { .min = 1, .max = 2 },
296 .m = { .min = 79, .max = 126 },
297 .m1 = { .min = 12, .max = 22 },
298 .m2 = { .min = 5, .max = 9 },
299 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400300 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .p2 = { .dot_limit = 225000,
302 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800303};
304
305static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700306 .dot = { .min = 25000, .max = 350000 },
307 .vco = { .min = 1760000, .max = 3510000 },
308 .n = { .min = 1, .max = 3 },
309 .m = { .min = 79, .max = 126 },
310 .m1 = { .min = 12, .max = 22 },
311 .m2 = { .min = 5, .max = 9 },
312 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400313 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700314 .p2 = { .dot_limit = 225000,
315 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800316};
317
Ville Syrjälädc730512013-09-24 21:26:30 +0300318static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300319 /*
320 * These are the data rate limits (measured in fast clocks)
321 * since those are the strictest limits we have. The fast
322 * clock and actual rate limits are more relaxed, so checking
323 * them would make no difference.
324 */
325 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200326 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700327 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700328 .m1 = { .min = 2, .max = 3 },
329 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300330 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300331 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700332};
333
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300334static const intel_limit_t intel_limits_chv = {
335 /*
336 * These are the data rate limits (measured in fast clocks)
337 * since those are the strictest limits we have. The fast
338 * clock and actual rate limits are more relaxed, so checking
339 * them would make no difference.
340 */
341 .dot = { .min = 25000 * 5, .max = 540000 * 5},
342 .vco = { .min = 4860000, .max = 6700000 },
343 .n = { .min = 1, .max = 1 },
344 .m1 = { .min = 2, .max = 2 },
345 .m2 = { .min = 24 << 22, .max = 175 << 22 },
346 .p1 = { .min = 2, .max = 4 },
347 .p2 = { .p2_slow = 1, .p2_fast = 14 },
348};
349
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300350static void vlv_clock(int refclk, intel_clock_t *clock)
351{
352 clock->m = clock->m1 * clock->m2;
353 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200354 if (WARN_ON(clock->n == 0 || clock->p == 0))
355 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300356 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
357 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300358}
359
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300360/**
361 * Returns whether any output on the specified pipe is of the specified type
362 */
363static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
364{
365 struct drm_device *dev = crtc->dev;
366 struct intel_encoder *encoder;
367
368 for_each_encoder_on_crtc(dev, crtc, encoder)
369 if (encoder->type == type)
370 return true;
371
372 return false;
373}
374
Chris Wilson1b894b52010-12-14 20:04:54 +0000375static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
376 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800377{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800378 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800379 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800380
381 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100382 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000383 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800384 limit = &intel_limits_ironlake_dual_lvds_100m;
385 else
386 limit = &intel_limits_ironlake_dual_lvds;
387 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000388 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800389 limit = &intel_limits_ironlake_single_lvds_100m;
390 else
391 limit = &intel_limits_ironlake_single_lvds;
392 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200393 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800394 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800395
396 return limit;
397}
398
Ma Ling044c7c42009-03-18 20:13:23 +0800399static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
400{
401 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800402 const intel_limit_t *limit;
403
404 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100405 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700406 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800407 else
Keith Packarde4b36692009-06-05 19:22:17 -0700408 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800409 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
410 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700411 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800412 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700413 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800414 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700415 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800416
417 return limit;
418}
419
Chris Wilson1b894b52010-12-14 20:04:54 +0000420static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800421{
422 struct drm_device *dev = crtc->dev;
423 const intel_limit_t *limit;
424
Eric Anholtbad720f2009-10-22 16:11:14 -0700425 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000426 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800427 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800428 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500429 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800430 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500431 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800432 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500433 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300434 } else if (IS_CHERRYVIEW(dev)) {
435 limit = &intel_limits_chv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700436 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300437 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100438 } else if (!IS_GEN2(dev)) {
439 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
440 limit = &intel_limits_i9xx_lvds;
441 else
442 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800443 } else {
444 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700445 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200446 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700447 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200448 else
449 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800450 }
451 return limit;
452}
453
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500454/* m1 is reserved as 0 in Pineview, n is a ring counter */
455static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800456{
Shaohua Li21778322009-02-23 15:19:16 +0800457 clock->m = clock->m2 + 2;
458 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200459 if (WARN_ON(clock->n == 0 || clock->p == 0))
460 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300461 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
462 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800463}
464
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200465static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
466{
467 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
468}
469
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200470static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800471{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200472 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800473 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200474 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
475 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300476 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
477 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800478}
479
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300480static void chv_clock(int refclk, intel_clock_t *clock)
481{
482 clock->m = clock->m1 * clock->m2;
483 clock->p = clock->p1 * clock->p2;
484 if (WARN_ON(clock->n == 0 || clock->p == 0))
485 return;
486 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
487 clock->n << 22);
488 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
489}
490
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800491#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800492/**
493 * Returns whether the given set of divisors are valid for a given refclk with
494 * the given connectors.
495 */
496
Chris Wilson1b894b52010-12-14 20:04:54 +0000497static bool intel_PLL_is_valid(struct drm_device *dev,
498 const intel_limit_t *limit,
499 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800500{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300501 if (clock->n < limit->n.min || limit->n.max < clock->n)
502 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800503 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400504 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400506 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400508 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300509
510 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
511 if (clock->m1 <= clock->m2)
512 INTELPllInvalid("m1 <= m2\n");
513
514 if (!IS_VALLEYVIEW(dev)) {
515 if (clock->p < limit->p.min || limit->p.max < clock->p)
516 INTELPllInvalid("p out of range\n");
517 if (clock->m < limit->m.min || limit->m.max < clock->m)
518 INTELPllInvalid("m out of range\n");
519 }
520
Jesse Barnes79e53942008-11-07 14:24:08 -0800521 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400522 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800523 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
524 * connector, etc., rather than just a single range.
525 */
526 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400527 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800528
529 return true;
530}
531
Ma Lingd4906092009-03-18 20:13:27 +0800532static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200533i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800534 int target, int refclk, intel_clock_t *match_clock,
535 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800536{
537 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800538 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800539 int err = target;
540
Daniel Vettera210b022012-11-26 17:22:08 +0100541 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800542 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100543 * For LVDS just rely on its current settings for dual-channel.
544 * We haven't figured out how to reliably set up different
545 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800546 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100547 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 clock.p2 = limit->p2.p2_fast;
549 else
550 clock.p2 = limit->p2.p2_slow;
551 } else {
552 if (target < limit->p2.dot_limit)
553 clock.p2 = limit->p2.p2_slow;
554 else
555 clock.p2 = limit->p2.p2_fast;
556 }
557
Akshay Joshi0206e352011-08-16 15:34:10 -0400558 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800559
Zhao Yakui42158662009-11-20 11:24:18 +0800560 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
561 clock.m1++) {
562 for (clock.m2 = limit->m2.min;
563 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200564 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800565 break;
566 for (clock.n = limit->n.min;
567 clock.n <= limit->n.max; clock.n++) {
568 for (clock.p1 = limit->p1.min;
569 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800570 int this_err;
571
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200572 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000573 if (!intel_PLL_is_valid(dev, limit,
574 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800575 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800576 if (match_clock &&
577 clock.p != match_clock->p)
578 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800579
580 this_err = abs(clock.dot - target);
581 if (this_err < err) {
582 *best_clock = clock;
583 err = this_err;
584 }
585 }
586 }
587 }
588 }
589
590 return (err != target);
591}
592
Ma Lingd4906092009-03-18 20:13:27 +0800593static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200594pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
595 int target, int refclk, intel_clock_t *match_clock,
596 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200597{
598 struct drm_device *dev = crtc->dev;
599 intel_clock_t clock;
600 int err = target;
601
602 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
603 /*
604 * For LVDS just rely on its current settings for dual-channel.
605 * We haven't figured out how to reliably set up different
606 * single/dual channel state, if we even can.
607 */
608 if (intel_is_dual_link_lvds(dev))
609 clock.p2 = limit->p2.p2_fast;
610 else
611 clock.p2 = limit->p2.p2_slow;
612 } else {
613 if (target < limit->p2.dot_limit)
614 clock.p2 = limit->p2.p2_slow;
615 else
616 clock.p2 = limit->p2.p2_fast;
617 }
618
619 memset(best_clock, 0, sizeof(*best_clock));
620
621 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
622 clock.m1++) {
623 for (clock.m2 = limit->m2.min;
624 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200625 for (clock.n = limit->n.min;
626 clock.n <= limit->n.max; clock.n++) {
627 for (clock.p1 = limit->p1.min;
628 clock.p1 <= limit->p1.max; clock.p1++) {
629 int this_err;
630
631 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 if (!intel_PLL_is_valid(dev, limit,
633 &clock))
634 continue;
635 if (match_clock &&
636 clock.p != match_clock->p)
637 continue;
638
639 this_err = abs(clock.dot - target);
640 if (this_err < err) {
641 *best_clock = clock;
642 err = this_err;
643 }
644 }
645 }
646 }
647 }
648
649 return (err != target);
650}
651
Ma Lingd4906092009-03-18 20:13:27 +0800652static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200653g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
654 int target, int refclk, intel_clock_t *match_clock,
655 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800656{
657 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800658 intel_clock_t clock;
659 int max_n;
660 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400661 /* approximately equals target * 0.00585 */
662 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800663 found = false;
664
665 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100666 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800667 clock.p2 = limit->p2.p2_fast;
668 else
669 clock.p2 = limit->p2.p2_slow;
670 } else {
671 if (target < limit->p2.dot_limit)
672 clock.p2 = limit->p2.p2_slow;
673 else
674 clock.p2 = limit->p2.p2_fast;
675 }
676
677 memset(best_clock, 0, sizeof(*best_clock));
678 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200679 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800680 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200681 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800682 for (clock.m1 = limit->m1.max;
683 clock.m1 >= limit->m1.min; clock.m1--) {
684 for (clock.m2 = limit->m2.max;
685 clock.m2 >= limit->m2.min; clock.m2--) {
686 for (clock.p1 = limit->p1.max;
687 clock.p1 >= limit->p1.min; clock.p1--) {
688 int this_err;
689
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200690 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000691 if (!intel_PLL_is_valid(dev, limit,
692 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800693 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000694
695 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800696 if (this_err < err_most) {
697 *best_clock = clock;
698 err_most = this_err;
699 max_n = clock.n;
700 found = true;
701 }
702 }
703 }
704 }
705 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800706 return found;
707}
Ma Lingd4906092009-03-18 20:13:27 +0800708
Zhenyu Wang2c072452009-06-05 15:38:42 +0800709static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200710vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
711 int target, int refclk, intel_clock_t *match_clock,
712 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700713{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300714 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300715 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300716 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300717 /* min update 19.2 MHz */
718 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300719 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700720
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300721 target *= 5; /* fast clock */
722
723 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700724
725 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300726 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300727 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300728 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300729 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300730 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700731 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300732 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300733 unsigned int ppm, diff;
734
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300735 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
736 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300737
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300738 vlv_clock(refclk, &clock);
739
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300740 if (!intel_PLL_is_valid(dev, limit,
741 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300742 continue;
743
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300744 diff = abs(clock.dot - target);
745 ppm = div_u64(1000000ULL * diff, target);
746
747 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300748 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300749 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300750 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300751 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300752
Ville Syrjäläc6861222013-09-24 21:26:21 +0300753 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300754 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300755 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300756 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700757 }
758 }
759 }
760 }
761 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700762
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300763 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700764}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700765
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300766static bool
767chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
770{
771 struct drm_device *dev = crtc->dev;
772 intel_clock_t clock;
773 uint64_t m2;
774 int found = false;
775
776 memset(best_clock, 0, sizeof(*best_clock));
777
778 /*
779 * Based on hardware doc, the n always set to 1, and m1 always
780 * set to 2. If requires to support 200Mhz refclk, we need to
781 * revisit this because n may not 1 anymore.
782 */
783 clock.n = 1, clock.m1 = 2;
784 target *= 5; /* fast clock */
785
786 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
787 for (clock.p2 = limit->p2.p2_fast;
788 clock.p2 >= limit->p2.p2_slow;
789 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
790
791 clock.p = clock.p1 * clock.p2;
792
793 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
794 clock.n) << 22, refclk * clock.m1);
795
796 if (m2 > INT_MAX/clock.m1)
797 continue;
798
799 clock.m2 = m2;
800
801 chv_clock(refclk, &clock);
802
803 if (!intel_PLL_is_valid(dev, limit, &clock))
804 continue;
805
806 /* based on hardware requirement, prefer bigger p
807 */
808 if (clock.p > best_clock->p) {
809 *best_clock = clock;
810 found = true;
811 }
812 }
813 }
814
815 return found;
816}
817
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300818bool intel_crtc_active(struct drm_crtc *crtc)
819{
820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
821
822 /* Be paranoid as we can arrive here with only partial
823 * state retrieved from the hardware during setup.
824 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100825 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300826 * as Haswell has gained clock readout/fastboot support.
827 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000828 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300829 * properly reconstruct framebuffers.
830 */
Matt Roperf4510a22014-04-01 15:22:40 -0700831 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100832 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300833}
834
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200835enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
836 enum pipe pipe)
837{
838 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
840
Daniel Vetter3b117c82013-04-17 20:15:07 +0200841 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200842}
843
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200844static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300845{
846 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200847 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300848
849 frame = I915_READ(frame_reg);
850
851 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
Jesse Barnes93937072014-04-04 16:12:09 -0700852 WARN(1, "vblank wait timed out\n");
Paulo Zanonia928d532012-05-04 17:18:15 -0300853}
854
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700855/**
856 * intel_wait_for_vblank - wait for vblank on a given pipe
857 * @dev: drm device
858 * @pipe: pipe to wait for
859 *
860 * Wait for vblank to occur on a given pipe. Needed for various bits of
861 * mode setting code.
862 */
863void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800864{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700865 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800866 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700867
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200868 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
869 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300870 return;
871 }
872
Chris Wilson300387c2010-09-05 20:25:43 +0100873 /* Clear existing vblank status. Note this will clear any other
874 * sticky status fields as well.
875 *
876 * This races with i915_driver_irq_handler() with the result
877 * that either function could miss a vblank event. Here it is not
878 * fatal, as we will either wait upon the next vblank interrupt or
879 * timeout. Generally speaking intel_wait_for_vblank() is only
880 * called during modeset at which time the GPU should be idle and
881 * should *not* be performing page flips and thus not waiting on
882 * vblanks...
883 * Currently, the result of us stealing a vblank from the irq
884 * handler is that a single frame will be skipped during swapbuffers.
885 */
886 I915_WRITE(pipestat_reg,
887 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
888
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700889 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100890 if (wait_for(I915_READ(pipestat_reg) &
891 PIPE_VBLANK_INTERRUPT_STATUS,
892 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700893 DRM_DEBUG_KMS("vblank wait timed out\n");
894}
895
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300896static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
897{
898 struct drm_i915_private *dev_priv = dev->dev_private;
899 u32 reg = PIPEDSL(pipe);
900 u32 line1, line2;
901 u32 line_mask;
902
903 if (IS_GEN2(dev))
904 line_mask = DSL_LINEMASK_GEN2;
905 else
906 line_mask = DSL_LINEMASK_GEN3;
907
908 line1 = I915_READ(reg) & line_mask;
909 mdelay(5);
910 line2 = I915_READ(reg) & line_mask;
911
912 return line1 == line2;
913}
914
Keith Packardab7ad7f2010-10-03 00:33:06 -0700915/*
916 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700917 * @dev: drm device
918 * @pipe: pipe to wait for
919 *
920 * After disabling a pipe, we can't wait for vblank in the usual way,
921 * spinning on the vblank interrupt status bit, since we won't actually
922 * see an interrupt when the pipe is disabled.
923 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700924 * On Gen4 and above:
925 * wait for the pipe register state bit to turn off
926 *
927 * Otherwise:
928 * wait for the display line value to settle (it usually
929 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100930 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700931 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100932void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700933{
934 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200935 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
936 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700937
Keith Packardab7ad7f2010-10-03 00:33:06 -0700938 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200939 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700940
Keith Packardab7ad7f2010-10-03 00:33:06 -0700941 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100942 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
943 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200944 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700945 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700946 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300947 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200948 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700949 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800950}
951
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000952/*
953 * ibx_digital_port_connected - is the specified port connected?
954 * @dev_priv: i915 private structure
955 * @port: the port to test
956 *
957 * Returns true if @port is connected, false otherwise.
958 */
959bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
960 struct intel_digital_port *port)
961{
962 u32 bit;
963
Damien Lespiauc36346e2012-12-13 16:09:03 +0000964 if (HAS_PCH_IBX(dev_priv->dev)) {
965 switch(port->port) {
966 case PORT_B:
967 bit = SDE_PORTB_HOTPLUG;
968 break;
969 case PORT_C:
970 bit = SDE_PORTC_HOTPLUG;
971 break;
972 case PORT_D:
973 bit = SDE_PORTD_HOTPLUG;
974 break;
975 default:
976 return true;
977 }
978 } else {
979 switch(port->port) {
980 case PORT_B:
981 bit = SDE_PORTB_HOTPLUG_CPT;
982 break;
983 case PORT_C:
984 bit = SDE_PORTC_HOTPLUG_CPT;
985 break;
986 case PORT_D:
987 bit = SDE_PORTD_HOTPLUG_CPT;
988 break;
989 default:
990 return true;
991 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000992 }
993
994 return I915_READ(SDEISR) & bit;
995}
996
Jesse Barnesb24e7172011-01-04 15:09:30 -0800997static const char *state_string(bool enabled)
998{
999 return enabled ? "on" : "off";
1000}
1001
1002/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001003void assert_pll(struct drm_i915_private *dev_priv,
1004 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001005{
1006 int reg;
1007 u32 val;
1008 bool cur_state;
1009
1010 reg = DPLL(pipe);
1011 val = I915_READ(reg);
1012 cur_state = !!(val & DPLL_VCO_ENABLE);
1013 WARN(cur_state != state,
1014 "PLL state assertion failure (expected %s, current %s)\n",
1015 state_string(state), state_string(cur_state));
1016}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001017
Jani Nikula23538ef2013-08-27 15:12:22 +03001018/* XXX: the dsi pll is shared between MIPI DSI ports */
1019static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1020{
1021 u32 val;
1022 bool cur_state;
1023
1024 mutex_lock(&dev_priv->dpio_lock);
1025 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1026 mutex_unlock(&dev_priv->dpio_lock);
1027
1028 cur_state = val & DSI_PLL_VCO_EN;
1029 WARN(cur_state != state,
1030 "DSI PLL state assertion failure (expected %s, current %s)\n",
1031 state_string(state), state_string(cur_state));
1032}
1033#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1034#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1035
Daniel Vetter55607e82013-06-16 21:42:39 +02001036struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001037intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001038{
Daniel Vettere2b78262013-06-07 23:10:03 +02001039 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1040
Daniel Vettera43f6e02013-06-07 23:10:32 +02001041 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001042 return NULL;
1043
Daniel Vettera43f6e02013-06-07 23:10:32 +02001044 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001045}
1046
Jesse Barnesb24e7172011-01-04 15:09:30 -08001047/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001048void assert_shared_dpll(struct drm_i915_private *dev_priv,
1049 struct intel_shared_dpll *pll,
1050 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001051{
Jesse Barnes040484a2011-01-03 12:14:26 -08001052 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001053 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001054
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001055 if (HAS_PCH_LPT(dev_priv->dev)) {
1056 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1057 return;
1058 }
1059
Chris Wilson92b27b02012-05-20 18:10:50 +01001060 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001061 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001062 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001063
Daniel Vetter53589012013-06-05 13:34:16 +02001064 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001065 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001066 "%s assertion failure (expected %s, current %s)\n",
1067 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001068}
Jesse Barnes040484a2011-01-03 12:14:26 -08001069
1070static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
1072{
1073 int reg;
1074 u32 val;
1075 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001076 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1077 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001078
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001079 if (HAS_DDI(dev_priv->dev)) {
1080 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001081 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001082 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001083 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001084 } else {
1085 reg = FDI_TX_CTL(pipe);
1086 val = I915_READ(reg);
1087 cur_state = !!(val & FDI_TX_ENABLE);
1088 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001089 WARN(cur_state != state,
1090 "FDI TX state assertion failure (expected %s, current %s)\n",
1091 state_string(state), state_string(cur_state));
1092}
1093#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1094#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1095
1096static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1097 enum pipe pipe, bool state)
1098{
1099 int reg;
1100 u32 val;
1101 bool cur_state;
1102
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001103 reg = FDI_RX_CTL(pipe);
1104 val = I915_READ(reg);
1105 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001106 WARN(cur_state != state,
1107 "FDI RX state assertion failure (expected %s, current %s)\n",
1108 state_string(state), state_string(cur_state));
1109}
1110#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1111#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1112
1113static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1114 enum pipe pipe)
1115{
1116 int reg;
1117 u32 val;
1118
1119 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001120 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001121 return;
1122
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001123 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001124 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001125 return;
1126
Jesse Barnes040484a2011-01-03 12:14:26 -08001127 reg = FDI_TX_CTL(pipe);
1128 val = I915_READ(reg);
1129 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1130}
1131
Daniel Vetter55607e82013-06-16 21:42:39 +02001132void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1133 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001134{
1135 int reg;
1136 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001137 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001138
1139 reg = FDI_RX_CTL(pipe);
1140 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001141 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1142 WARN(cur_state != state,
1143 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1144 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001145}
1146
Jesse Barnesea0760c2011-01-04 15:09:32 -08001147static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1148 enum pipe pipe)
1149{
1150 int pp_reg, lvds_reg;
1151 u32 val;
1152 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001153 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001154
1155 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1156 pp_reg = PCH_PP_CONTROL;
1157 lvds_reg = PCH_LVDS;
1158 } else {
1159 pp_reg = PP_CONTROL;
1160 lvds_reg = LVDS;
1161 }
1162
1163 val = I915_READ(pp_reg);
1164 if (!(val & PANEL_POWER_ON) ||
1165 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1166 locked = false;
1167
1168 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1169 panel_pipe = PIPE_B;
1170
1171 WARN(panel_pipe == pipe && locked,
1172 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001173 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001174}
1175
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001176static void assert_cursor(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
1178{
1179 struct drm_device *dev = dev_priv->dev;
1180 bool cur_state;
1181
Paulo Zanonid9d82082014-02-27 16:30:56 -03001182 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001183 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001184 else if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001185 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001186 else
1187 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001188
1189 WARN(cur_state != state,
1190 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1191 pipe_name(pipe), state_string(state), state_string(cur_state));
1192}
1193#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1194#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1195
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001196void assert_pipe(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001198{
1199 int reg;
1200 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001201 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001202 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1203 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001204
Daniel Vetter8e636782012-01-22 01:36:48 +01001205 /* if we need the pipe A quirk it must be always on */
1206 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1207 state = true;
1208
Imre Deakda7e29b2014-02-18 00:02:02 +02001209 if (!intel_display_power_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001210 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001211 cur_state = false;
1212 } else {
1213 reg = PIPECONF(cpu_transcoder);
1214 val = I915_READ(reg);
1215 cur_state = !!(val & PIPECONF_ENABLE);
1216 }
1217
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001218 WARN(cur_state != state,
1219 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001220 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001221}
1222
Chris Wilson931872f2012-01-16 23:01:13 +00001223static void assert_plane(struct drm_i915_private *dev_priv,
1224 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001225{
1226 int reg;
1227 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001228 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001229
1230 reg = DSPCNTR(plane);
1231 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001232 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1233 WARN(cur_state != state,
1234 "plane %c assertion failure (expected %s, current %s)\n",
1235 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001236}
1237
Chris Wilson931872f2012-01-16 23:01:13 +00001238#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1239#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1240
Jesse Barnesb24e7172011-01-04 15:09:30 -08001241static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1242 enum pipe pipe)
1243{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001244 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001245 int reg, i;
1246 u32 val;
1247 int cur_pipe;
1248
Ville Syrjälä653e1022013-06-04 13:49:05 +03001249 /* Primary planes are fixed to pipes on gen4+ */
1250 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001251 reg = DSPCNTR(pipe);
1252 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001253 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001254 "plane %c assertion failure, should be disabled but not\n",
1255 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001256 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001257 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001258
Jesse Barnesb24e7172011-01-04 15:09:30 -08001259 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001260 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001261 reg = DSPCNTR(i);
1262 val = I915_READ(reg);
1263 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1264 DISPPLANE_SEL_PIPE_SHIFT;
1265 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001266 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1267 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001268 }
1269}
1270
Jesse Barnes19332d72013-03-28 09:55:38 -07001271static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1272 enum pipe pipe)
1273{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001274 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001275 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001276 u32 val;
1277
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001278 if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001279 for_each_sprite(pipe, sprite) {
1280 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001281 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001282 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001284 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001285 }
1286 } else if (INTEL_INFO(dev)->gen >= 7) {
1287 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001288 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001289 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001290 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001291 plane_name(pipe), pipe_name(pipe));
1292 } else if (INTEL_INFO(dev)->gen >= 5) {
1293 reg = DVSCNTR(pipe);
1294 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001295 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001296 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1297 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001298 }
1299}
1300
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001301static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001302{
1303 u32 val;
1304 bool enabled;
1305
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001306 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001307
Jesse Barnes92f25842011-01-04 15:09:34 -08001308 val = I915_READ(PCH_DREF_CONTROL);
1309 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1310 DREF_SUPERSPREAD_SOURCE_MASK));
1311 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1312}
1313
Daniel Vetterab9412b2013-05-03 11:49:46 +02001314static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1315 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001316{
1317 int reg;
1318 u32 val;
1319 bool enabled;
1320
Daniel Vetterab9412b2013-05-03 11:49:46 +02001321 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001322 val = I915_READ(reg);
1323 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001324 WARN(enabled,
1325 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1326 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001327}
1328
Keith Packard4e634382011-08-06 10:39:45 -07001329static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1330 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001331{
1332 if ((val & DP_PORT_EN) == 0)
1333 return false;
1334
1335 if (HAS_PCH_CPT(dev_priv->dev)) {
1336 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1337 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1338 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1339 return false;
1340 } else {
1341 if ((val & DP_PIPE_MASK) != (pipe << 30))
1342 return false;
1343 }
1344 return true;
1345}
1346
Keith Packard1519b992011-08-06 10:35:34 -07001347static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1348 enum pipe pipe, u32 val)
1349{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001350 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001351 return false;
1352
1353 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001354 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001355 return false;
1356 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001357 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001358 return false;
1359 }
1360 return true;
1361}
1362
1363static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe, u32 val)
1365{
1366 if ((val & LVDS_PORT_EN) == 0)
1367 return false;
1368
1369 if (HAS_PCH_CPT(dev_priv->dev)) {
1370 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1371 return false;
1372 } else {
1373 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1374 return false;
1375 }
1376 return true;
1377}
1378
1379static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1380 enum pipe pipe, u32 val)
1381{
1382 if ((val & ADPA_DAC_ENABLE) == 0)
1383 return false;
1384 if (HAS_PCH_CPT(dev_priv->dev)) {
1385 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1386 return false;
1387 } else {
1388 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1389 return false;
1390 }
1391 return true;
1392}
1393
Jesse Barnes291906f2011-02-02 12:28:03 -08001394static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001395 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001396{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001397 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001398 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001399 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001400 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001401
Daniel Vetter75c5da22012-09-10 21:58:29 +02001402 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1403 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001404 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001405}
1406
1407static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1408 enum pipe pipe, int reg)
1409{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001410 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001411 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001412 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001413 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001414
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001415 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001416 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001417 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001418}
1419
1420static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1421 enum pipe pipe)
1422{
1423 int reg;
1424 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001425
Keith Packardf0575e92011-07-25 22:12:43 -07001426 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1427 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1428 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001429
1430 reg = PCH_ADPA;
1431 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001432 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001433 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001434 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001435
1436 reg = PCH_LVDS;
1437 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001438 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001439 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001440 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001441
Paulo Zanonie2debe92013-02-18 19:00:27 -03001442 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1443 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1444 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001445}
1446
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001447static void intel_init_dpio(struct drm_device *dev)
1448{
1449 struct drm_i915_private *dev_priv = dev->dev_private;
1450
1451 if (!IS_VALLEYVIEW(dev))
1452 return;
1453
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001454 /*
1455 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1456 * CHV x1 PHY (DP/HDMI D)
1457 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1458 */
1459 if (IS_CHERRYVIEW(dev)) {
1460 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1461 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1462 } else {
1463 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1464 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001465}
1466
1467static void intel_reset_dpio(struct drm_device *dev)
1468{
1469 struct drm_i915_private *dev_priv = dev->dev_private;
1470
1471 if (!IS_VALLEYVIEW(dev))
1472 return;
1473
Imre Deake5cbfbf2014-01-09 17:08:16 +02001474 /*
1475 * Enable the CRI clock source so we can get at the display and the
1476 * reference clock for VGA hotplug / manual detection.
1477 */
Imre Deak404faab2014-01-09 17:08:15 +02001478 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
Imre Deake5cbfbf2014-01-09 17:08:16 +02001479 DPLL_REFA_CLK_ENABLE_VLV |
Imre Deak404faab2014-01-09 17:08:15 +02001480 DPLL_INTEGRATED_CRI_CLK_VLV);
1481
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001482 if (IS_CHERRYVIEW(dev)) {
1483 enum dpio_phy phy;
1484 u32 val;
1485
1486 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1487 /* Poll for phypwrgood signal */
1488 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1489 PHY_POWERGOOD(phy), 1))
1490 DRM_ERROR("Display PHY %d is not power up\n", phy);
1491
1492 /*
1493 * Deassert common lane reset for PHY.
1494 *
1495 * This should only be done on init and resume from S3
1496 * with both PLLs disabled, or we risk losing DPIO and
1497 * PLL synchronization.
1498 */
1499 val = I915_READ(DISPLAY_PHY_CONTROL);
1500 I915_WRITE(DISPLAY_PHY_CONTROL,
1501 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1502 }
1503
1504 } else {
1505 /*
1506 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1507 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1508 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1509 * b. The other bits such as sfr settings / modesel may all
1510 * be set to 0.
1511 *
1512 * This should only be done on init and resume from S3 with
1513 * both PLLs disabled, or we risk losing DPIO and PLL
1514 * synchronization.
1515 */
1516 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1517 }
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001518}
1519
Daniel Vetter426115c2013-07-11 22:13:42 +02001520static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001521{
Daniel Vetter426115c2013-07-11 22:13:42 +02001522 struct drm_device *dev = crtc->base.dev;
1523 struct drm_i915_private *dev_priv = dev->dev_private;
1524 int reg = DPLL(crtc->pipe);
1525 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001526
Daniel Vetter426115c2013-07-11 22:13:42 +02001527 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001528
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001529 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001530 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1531
1532 /* PLL is protected by panel, make sure we can write it */
1533 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001534 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001535
Daniel Vetter426115c2013-07-11 22:13:42 +02001536 I915_WRITE(reg, dpll);
1537 POSTING_READ(reg);
1538 udelay(150);
1539
1540 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1541 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1542
1543 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1544 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001545
1546 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001547 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001548 POSTING_READ(reg);
1549 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001550 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001551 POSTING_READ(reg);
1552 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001553 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001554 POSTING_READ(reg);
1555 udelay(150); /* wait for warmup */
1556}
1557
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001558static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001559{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001560 struct drm_device *dev = crtc->base.dev;
1561 struct drm_i915_private *dev_priv = dev->dev_private;
1562 int reg = DPLL(crtc->pipe);
1563 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001564
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001565 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001566
1567 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001568 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001569
1570 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001571 if (IS_MOBILE(dev) && !IS_I830(dev))
1572 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001573
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001574 I915_WRITE(reg, dpll);
1575
1576 /* Wait for the clocks to stabilize. */
1577 POSTING_READ(reg);
1578 udelay(150);
1579
1580 if (INTEL_INFO(dev)->gen >= 4) {
1581 I915_WRITE(DPLL_MD(crtc->pipe),
1582 crtc->config.dpll_hw_state.dpll_md);
1583 } else {
1584 /* The pixel multiplier can only be updated once the
1585 * DPLL is enabled and the clocks are stable.
1586 *
1587 * So write it again.
1588 */
1589 I915_WRITE(reg, dpll);
1590 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001591
1592 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001593 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001594 POSTING_READ(reg);
1595 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001596 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001597 POSTING_READ(reg);
1598 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001599 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001600 POSTING_READ(reg);
1601 udelay(150); /* wait for warmup */
1602}
1603
1604/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001605 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001606 * @dev_priv: i915 private structure
1607 * @pipe: pipe PLL to disable
1608 *
1609 * Disable the PLL for @pipe, making sure the pipe is off first.
1610 *
1611 * Note! This is for pre-ILK only.
1612 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001613static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001614{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615 /* Don't disable pipe A or pipe A PLLs if needed */
1616 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1617 return;
1618
1619 /* Make sure the pipe isn't still relying on us */
1620 assert_pipe_disabled(dev_priv, pipe);
1621
Daniel Vetter50b44a42013-06-05 13:34:33 +02001622 I915_WRITE(DPLL(pipe), 0);
1623 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001624}
1625
Jesse Barnesf6071162013-10-01 10:41:38 -07001626static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1627{
1628 u32 val = 0;
1629
1630 /* Make sure the pipe isn't still relying on us */
1631 assert_pipe_disabled(dev_priv, pipe);
1632
Imre Deake5cbfbf2014-01-09 17:08:16 +02001633 /*
1634 * Leave integrated clock source and reference clock enabled for pipe B.
1635 * The latter is needed for VGA hotplug / manual detection.
1636 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001637 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001638 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001639 I915_WRITE(DPLL(pipe), val);
1640 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001641
1642}
1643
1644static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1645{
1646 int dpll = DPLL(pipe);
1647 u32 val;
1648
1649 /* Set PLL en = 0 */
1650 val = I915_READ(dpll);
1651 val &= ~DPLL_VCO_ENABLE;
1652 I915_WRITE(dpll, val);
1653
Jesse Barnesf6071162013-10-01 10:41:38 -07001654}
1655
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001656void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1657 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001658{
1659 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001660 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001661
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001662 switch (dport->port) {
1663 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001664 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001665 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001666 break;
1667 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001668 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001669 dpll_reg = DPLL(0);
1670 break;
1671 case PORT_D:
1672 port_mask = DPLL_PORTD_READY_MASK;
1673 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001674 break;
1675 default:
1676 BUG();
1677 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001678
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001679 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001680 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001681 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001682}
1683
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001685 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001686 * @dev_priv: i915 private structure
1687 * @pipe: pipe PLL to enable
1688 *
1689 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1690 * drives the transcoder clock.
1691 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001692static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001693{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001694 struct drm_device *dev = crtc->base.dev;
1695 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001696 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001697
Chris Wilson48da64a2012-05-13 20:16:12 +01001698 /* PCH PLLs only available on ILK, SNB and IVB */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001699 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001700 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001701 return;
1702
1703 if (WARN_ON(pll->refcount == 0))
1704 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001705
Daniel Vetter46edb022013-06-05 13:34:12 +02001706 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1707 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001708 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001709
Daniel Vettercdbd2312013-06-05 13:34:03 +02001710 if (pll->active++) {
1711 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001712 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001713 return;
1714 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001715 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001716
Daniel Vetter46edb022013-06-05 13:34:12 +02001717 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001718 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001719 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001720}
1721
Daniel Vettere2b78262013-06-07 23:10:03 +02001722static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001723{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001724 struct drm_device *dev = crtc->base.dev;
1725 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001726 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001727
Jesse Barnes92f25842011-01-04 15:09:34 -08001728 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001729 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001730 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001731 return;
1732
Chris Wilson48da64a2012-05-13 20:16:12 +01001733 if (WARN_ON(pll->refcount == 0))
1734 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001735
Daniel Vetter46edb022013-06-05 13:34:12 +02001736 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1737 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001738 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001739
Chris Wilson48da64a2012-05-13 20:16:12 +01001740 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001741 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001742 return;
1743 }
1744
Daniel Vettere9d69442013-06-05 13:34:15 +02001745 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001746 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001747 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001748 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001749
Daniel Vetter46edb022013-06-05 13:34:12 +02001750 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001751 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001752 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001753}
1754
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001755static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1756 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001757{
Daniel Vetter23670b322012-11-01 09:15:30 +01001758 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001759 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001761 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001762
1763 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001764 BUG_ON(INTEL_INFO(dev)->gen < 5);
Jesse Barnes040484a2011-01-03 12:14:26 -08001765
1766 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001767 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001768 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001769
1770 /* FDI must be feeding us bits for PCH ports */
1771 assert_fdi_tx_enabled(dev_priv, pipe);
1772 assert_fdi_rx_enabled(dev_priv, pipe);
1773
Daniel Vetter23670b322012-11-01 09:15:30 +01001774 if (HAS_PCH_CPT(dev)) {
1775 /* Workaround: Set the timing override bit before enabling the
1776 * pch transcoder. */
1777 reg = TRANS_CHICKEN2(pipe);
1778 val = I915_READ(reg);
1779 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1780 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001781 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001782
Daniel Vetterab9412b2013-05-03 11:49:46 +02001783 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001784 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001785 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001786
1787 if (HAS_PCH_IBX(dev_priv->dev)) {
1788 /*
1789 * make the BPC in transcoder be consistent with
1790 * that in pipeconf reg.
1791 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001792 val &= ~PIPECONF_BPC_MASK;
1793 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001794 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001795
1796 val &= ~TRANS_INTERLACE_MASK;
1797 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001798 if (HAS_PCH_IBX(dev_priv->dev) &&
1799 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1800 val |= TRANS_LEGACY_INTERLACED_ILK;
1801 else
1802 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001803 else
1804 val |= TRANS_PROGRESSIVE;
1805
Jesse Barnes040484a2011-01-03 12:14:26 -08001806 I915_WRITE(reg, val | TRANS_ENABLE);
1807 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001808 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001809}
1810
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001811static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001812 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001813{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001814 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001815
1816 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001817 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001818
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001819 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001820 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001821 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001822
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001823 /* Workaround: set timing override bit. */
1824 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001825 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001826 I915_WRITE(_TRANSA_CHICKEN2, val);
1827
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001828 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001829 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001830
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001831 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1832 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001833 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001834 else
1835 val |= TRANS_PROGRESSIVE;
1836
Daniel Vetterab9412b2013-05-03 11:49:46 +02001837 I915_WRITE(LPT_TRANSCONF, val);
1838 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001839 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001840}
1841
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001842static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1843 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001844{
Daniel Vetter23670b322012-11-01 09:15:30 +01001845 struct drm_device *dev = dev_priv->dev;
1846 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001847
1848 /* FDI relies on the transcoder */
1849 assert_fdi_tx_disabled(dev_priv, pipe);
1850 assert_fdi_rx_disabled(dev_priv, pipe);
1851
Jesse Barnes291906f2011-02-02 12:28:03 -08001852 /* Ports must be off as well */
1853 assert_pch_ports_disabled(dev_priv, pipe);
1854
Daniel Vetterab9412b2013-05-03 11:49:46 +02001855 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001856 val = I915_READ(reg);
1857 val &= ~TRANS_ENABLE;
1858 I915_WRITE(reg, val);
1859 /* wait for PCH transcoder off, transcoder state */
1860 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001861 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001862
1863 if (!HAS_PCH_IBX(dev)) {
1864 /* Workaround: Clear the timing override chicken bit again. */
1865 reg = TRANS_CHICKEN2(pipe);
1866 val = I915_READ(reg);
1867 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1868 I915_WRITE(reg, val);
1869 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001870}
1871
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001872static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001873{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874 u32 val;
1875
Daniel Vetterab9412b2013-05-03 11:49:46 +02001876 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001878 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001879 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001880 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001881 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001882
1883 /* Workaround: clear timing override bit. */
1884 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001885 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001886 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001887}
1888
1889/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001890 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001891 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001892 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001893 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001894 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001895 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001896static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001897{
Paulo Zanoni03722642014-01-17 13:51:09 -02001898 struct drm_device *dev = crtc->base.dev;
1899 struct drm_i915_private *dev_priv = dev->dev_private;
1900 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001901 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1902 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001903 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001904 int reg;
1905 u32 val;
1906
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001907 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001908 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001909 assert_sprites_disabled(dev_priv, pipe);
1910
Paulo Zanoni681e5812012-12-06 11:12:38 -02001911 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001912 pch_transcoder = TRANSCODER_A;
1913 else
1914 pch_transcoder = pipe;
1915
Jesse Barnesb24e7172011-01-04 15:09:30 -08001916 /*
1917 * A pipe without a PLL won't actually be able to drive bits from
1918 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1919 * need the check.
1920 */
1921 if (!HAS_PCH_SPLIT(dev_priv->dev))
Paulo Zanonifbf32182014-01-17 13:51:11 -02001922 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001923 assert_dsi_pll_enabled(dev_priv);
1924 else
1925 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001926 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02001927 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001928 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001929 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001930 assert_fdi_tx_pll_enabled(dev_priv,
1931 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001932 }
1933 /* FIXME: assert CPU port conditions for SNB+ */
1934 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001935
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001936 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001937 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001938 if (val & PIPECONF_ENABLE) {
1939 WARN_ON(!(pipe == PIPE_A &&
1940 dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson00d70b12011-03-17 07:18:29 +00001941 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001942 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001943
1944 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001945 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001946}
1947
1948/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001949 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001950 * @dev_priv: i915 private structure
1951 * @pipe: pipe to disable
1952 *
1953 * Disable @pipe, making sure that various hardware specific requirements
1954 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1955 *
1956 * @pipe should be %PIPE_A or %PIPE_B.
1957 *
1958 * Will wait until the pipe has shut down before returning.
1959 */
1960static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1961 enum pipe pipe)
1962{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001963 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1964 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001965 int reg;
1966 u32 val;
1967
1968 /*
1969 * Make sure planes won't keep trying to pump pixels to us,
1970 * or we might hang the display.
1971 */
1972 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001973 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001974 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975
1976 /* Don't disable pipe A or pipe A PLLs if needed */
1977 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1978 return;
1979
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001980 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001982 if ((val & PIPECONF_ENABLE) == 0)
1983 return;
1984
1985 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001986 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1987}
1988
Keith Packardd74362c2011-07-28 14:47:14 -07001989/*
1990 * Plane regs are double buffered, going from enabled->disabled needs a
1991 * trigger in order to latch. The display address reg provides this.
1992 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001993void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1994 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07001995{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001996 struct drm_device *dev = dev_priv->dev;
1997 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03001998
1999 I915_WRITE(reg, I915_READ(reg));
2000 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002001}
2002
Jesse Barnesb24e7172011-01-04 15:09:30 -08002003/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002004 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002005 * @dev_priv: i915 private structure
2006 * @plane: plane to enable
2007 * @pipe: pipe being fed
2008 *
2009 * Enable @plane on @pipe, making sure that @pipe is running first.
2010 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002011static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2012 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002013{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002014 struct intel_crtc *intel_crtc =
2015 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002016 int reg;
2017 u32 val;
2018
2019 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2020 assert_pipe_enabled(dev_priv, pipe);
2021
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002022 if (intel_crtc->primary_enabled)
2023 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002024
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002025 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002026
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027 reg = DSPCNTR(plane);
2028 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002029 WARN_ON(val & DISPLAY_PLANE_ENABLE);
Chris Wilson00d70b12011-03-17 07:18:29 +00002030
2031 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002032 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033 intel_wait_for_vblank(dev_priv->dev, pipe);
2034}
2035
Jesse Barnesb24e7172011-01-04 15:09:30 -08002036/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002037 * intel_disable_primary_hw_plane - disable the primary hardware plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002038 * @dev_priv: i915 private structure
2039 * @plane: plane to disable
2040 * @pipe: pipe consuming the data
2041 *
2042 * Disable @plane; should be an independent operation.
2043 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002044static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2045 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002046{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002047 struct intel_crtc *intel_crtc =
2048 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049 int reg;
2050 u32 val;
2051
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002052 if (!intel_crtc->primary_enabled)
2053 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002054
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002055 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002056
Jesse Barnesb24e7172011-01-04 15:09:30 -08002057 reg = DSPCNTR(plane);
2058 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002059 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
Chris Wilson00d70b12011-03-17 07:18:29 +00002060
2061 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002062 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002063 intel_wait_for_vblank(dev_priv->dev, pipe);
2064}
2065
Chris Wilson693db182013-03-05 14:52:39 +00002066static bool need_vtd_wa(struct drm_device *dev)
2067{
2068#ifdef CONFIG_INTEL_IOMMU
2069 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2070 return true;
2071#endif
2072 return false;
2073}
2074
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002075static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2076{
2077 int tile_height;
2078
2079 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2080 return ALIGN(height, tile_height);
2081}
2082
Chris Wilson127bd2a2010-07-23 23:32:05 +01002083int
Chris Wilson48b956c2010-09-14 12:50:34 +01002084intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002085 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002086 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002087{
Chris Wilsonce453d82011-02-21 14:43:56 +00002088 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002089 u32 alignment;
2090 int ret;
2091
Chris Wilson05394f32010-11-08 19:18:58 +00002092 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002093 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002094 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2095 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002096 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002097 alignment = 4 * 1024;
2098 else
2099 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002100 break;
2101 case I915_TILING_X:
2102 /* pin() will align the object as required by fence */
2103 alignment = 0;
2104 break;
2105 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002106 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002107 return -EINVAL;
2108 default:
2109 BUG();
2110 }
2111
Chris Wilson693db182013-03-05 14:52:39 +00002112 /* Note that the w/a also requires 64 PTE of padding following the
2113 * bo. We currently fill all unused PTE with the shadow page and so
2114 * we should always have valid PTE following the scanout preventing
2115 * the VT-d warning.
2116 */
2117 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2118 alignment = 256 * 1024;
2119
Chris Wilsonce453d82011-02-21 14:43:56 +00002120 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002121 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002122 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002123 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002124
2125 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2126 * fence, whereas 965+ only requires a fence if using
2127 * framebuffer compression. For simplicity, we always install
2128 * a fence as the cost is not that onerous.
2129 */
Chris Wilson06d98132012-04-17 15:31:24 +01002130 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002131 if (ret)
2132 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002133
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002134 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002135
Chris Wilsonce453d82011-02-21 14:43:56 +00002136 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002137 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002138
2139err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002140 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002141err_interruptible:
2142 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002143 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002144}
2145
Chris Wilson1690e1e2011-12-14 13:57:08 +01002146void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2147{
2148 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002149 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002150}
2151
Daniel Vetterc2c75132012-07-05 12:17:30 +02002152/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2153 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002154unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2155 unsigned int tiling_mode,
2156 unsigned int cpp,
2157 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002158{
Chris Wilsonbc752862013-02-21 20:04:31 +00002159 if (tiling_mode != I915_TILING_NONE) {
2160 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002161
Chris Wilsonbc752862013-02-21 20:04:31 +00002162 tile_rows = *y / 8;
2163 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002164
Chris Wilsonbc752862013-02-21 20:04:31 +00002165 tiles = *x / (512/cpp);
2166 *x %= 512/cpp;
2167
2168 return tile_rows * pitch * 8 + tiles * 4096;
2169 } else {
2170 unsigned int offset;
2171
2172 offset = *y * pitch + *x * cpp;
2173 *y = 0;
2174 *x = (offset & 4095) / cpp;
2175 return offset & -4096;
2176 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002177}
2178
Jesse Barnes46f297f2014-03-07 08:57:48 -08002179int intel_format_to_fourcc(int format)
2180{
2181 switch (format) {
2182 case DISPPLANE_8BPP:
2183 return DRM_FORMAT_C8;
2184 case DISPPLANE_BGRX555:
2185 return DRM_FORMAT_XRGB1555;
2186 case DISPPLANE_BGRX565:
2187 return DRM_FORMAT_RGB565;
2188 default:
2189 case DISPPLANE_BGRX888:
2190 return DRM_FORMAT_XRGB8888;
2191 case DISPPLANE_RGBX888:
2192 return DRM_FORMAT_XBGR8888;
2193 case DISPPLANE_BGRX101010:
2194 return DRM_FORMAT_XRGB2101010;
2195 case DISPPLANE_RGBX101010:
2196 return DRM_FORMAT_XBGR2101010;
2197 }
2198}
2199
Jesse Barnes484b41d2014-03-07 08:57:55 -08002200static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002201 struct intel_plane_config *plane_config)
2202{
2203 struct drm_device *dev = crtc->base.dev;
2204 struct drm_i915_gem_object *obj = NULL;
2205 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2206 u32 base = plane_config->base;
2207
Chris Wilsonff2652e2014-03-10 08:07:02 +00002208 if (plane_config->size == 0)
2209 return false;
2210
Jesse Barnes46f297f2014-03-07 08:57:48 -08002211 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2212 plane_config->size);
2213 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002214 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002215
2216 if (plane_config->tiled) {
2217 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002218 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002219 }
2220
Dave Airlie66e514c2014-04-03 07:51:54 +10002221 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2222 mode_cmd.width = crtc->base.primary->fb->width;
2223 mode_cmd.height = crtc->base.primary->fb->height;
2224 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002225
2226 mutex_lock(&dev->struct_mutex);
2227
Dave Airlie66e514c2014-04-03 07:51:54 +10002228 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002229 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002230 DRM_DEBUG_KMS("intel fb init failed\n");
2231 goto out_unref_obj;
2232 }
2233
2234 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002235
2236 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2237 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002238
2239out_unref_obj:
2240 drm_gem_object_unreference(&obj->base);
2241 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002242 return false;
2243}
2244
2245static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2246 struct intel_plane_config *plane_config)
2247{
2248 struct drm_device *dev = intel_crtc->base.dev;
2249 struct drm_crtc *c;
2250 struct intel_crtc *i;
2251 struct intel_framebuffer *fb;
2252
Dave Airlie66e514c2014-04-03 07:51:54 +10002253 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002254 return;
2255
2256 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2257 return;
2258
Dave Airlie66e514c2014-04-03 07:51:54 +10002259 kfree(intel_crtc->base.primary->fb);
2260 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002261
2262 /*
2263 * Failed to alloc the obj, check to see if we should share
2264 * an fb with another CRTC instead
2265 */
2266 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
2267 i = to_intel_crtc(c);
2268
2269 if (c == &intel_crtc->base)
2270 continue;
2271
Dave Airlie66e514c2014-04-03 07:51:54 +10002272 if (!i->active || !c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002273 continue;
2274
Dave Airlie66e514c2014-04-03 07:51:54 +10002275 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002276 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002277 drm_framebuffer_reference(c->primary->fb);
2278 intel_crtc->base.primary->fb = c->primary->fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002279 break;
2280 }
2281 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002282}
2283
Matt Roper262ca2b2014-03-18 17:22:55 -07002284static int i9xx_update_primary_plane(struct drm_crtc *crtc,
2285 struct drm_framebuffer *fb,
2286 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002287{
2288 struct drm_device *dev = crtc->dev;
2289 struct drm_i915_private *dev_priv = dev->dev_private;
2290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2291 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002292 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002293 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002294 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002295 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002296 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002297
Jesse Barnes81255562010-08-02 12:07:50 -07002298 intel_fb = to_intel_framebuffer(fb);
2299 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002300
Chris Wilson5eddb702010-09-11 13:48:45 +01002301 reg = DSPCNTR(plane);
2302 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002303 /* Mask out pixel format bits in case we change it */
2304 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002305 switch (fb->pixel_format) {
2306 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002307 dspcntr |= DISPPLANE_8BPP;
2308 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002309 case DRM_FORMAT_XRGB1555:
2310 case DRM_FORMAT_ARGB1555:
2311 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002312 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002313 case DRM_FORMAT_RGB565:
2314 dspcntr |= DISPPLANE_BGRX565;
2315 break;
2316 case DRM_FORMAT_XRGB8888:
2317 case DRM_FORMAT_ARGB8888:
2318 dspcntr |= DISPPLANE_BGRX888;
2319 break;
2320 case DRM_FORMAT_XBGR8888:
2321 case DRM_FORMAT_ABGR8888:
2322 dspcntr |= DISPPLANE_RGBX888;
2323 break;
2324 case DRM_FORMAT_XRGB2101010:
2325 case DRM_FORMAT_ARGB2101010:
2326 dspcntr |= DISPPLANE_BGRX101010;
2327 break;
2328 case DRM_FORMAT_XBGR2101010:
2329 case DRM_FORMAT_ABGR2101010:
2330 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002331 break;
2332 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002333 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002334 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002335
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002336 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002337 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002338 dspcntr |= DISPPLANE_TILED;
2339 else
2340 dspcntr &= ~DISPPLANE_TILED;
2341 }
2342
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002343 if (IS_G4X(dev))
2344 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2345
Chris Wilson5eddb702010-09-11 13:48:45 +01002346 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002347
Daniel Vettere506a0c2012-07-05 12:17:29 +02002348 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002349
Daniel Vetterc2c75132012-07-05 12:17:30 +02002350 if (INTEL_INFO(dev)->gen >= 4) {
2351 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002352 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2353 fb->bits_per_pixel / 8,
2354 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002355 linear_offset -= intel_crtc->dspaddr_offset;
2356 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002357 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002358 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002359
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002360 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2361 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2362 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002363 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002364 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002365 I915_WRITE(DSPSURF(plane),
2366 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002367 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002368 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002369 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002370 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002371 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002372
Jesse Barnes17638cd2011-06-24 12:19:23 -07002373 return 0;
2374}
2375
Matt Roper262ca2b2014-03-18 17:22:55 -07002376static int ironlake_update_primary_plane(struct drm_crtc *crtc,
2377 struct drm_framebuffer *fb,
2378 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002379{
2380 struct drm_device *dev = crtc->dev;
2381 struct drm_i915_private *dev_priv = dev->dev_private;
2382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2383 struct intel_framebuffer *intel_fb;
2384 struct drm_i915_gem_object *obj;
2385 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002386 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002387 u32 dspcntr;
2388 u32 reg;
2389
Jesse Barnes17638cd2011-06-24 12:19:23 -07002390 intel_fb = to_intel_framebuffer(fb);
2391 obj = intel_fb->obj;
2392
2393 reg = DSPCNTR(plane);
2394 dspcntr = I915_READ(reg);
2395 /* Mask out pixel format bits in case we change it */
2396 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002397 switch (fb->pixel_format) {
2398 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002399 dspcntr |= DISPPLANE_8BPP;
2400 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002401 case DRM_FORMAT_RGB565:
2402 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002403 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002404 case DRM_FORMAT_XRGB8888:
2405 case DRM_FORMAT_ARGB8888:
2406 dspcntr |= DISPPLANE_BGRX888;
2407 break;
2408 case DRM_FORMAT_XBGR8888:
2409 case DRM_FORMAT_ABGR8888:
2410 dspcntr |= DISPPLANE_RGBX888;
2411 break;
2412 case DRM_FORMAT_XRGB2101010:
2413 case DRM_FORMAT_ARGB2101010:
2414 dspcntr |= DISPPLANE_BGRX101010;
2415 break;
2416 case DRM_FORMAT_XBGR2101010:
2417 case DRM_FORMAT_ABGR2101010:
2418 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002419 break;
2420 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002421 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002422 }
2423
2424 if (obj->tiling_mode != I915_TILING_NONE)
2425 dspcntr |= DISPPLANE_TILED;
2426 else
2427 dspcntr &= ~DISPPLANE_TILED;
2428
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002429 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002430 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2431 else
2432 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002433
2434 I915_WRITE(reg, dspcntr);
2435
Daniel Vettere506a0c2012-07-05 12:17:29 +02002436 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002437 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002438 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2439 fb->bits_per_pixel / 8,
2440 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002441 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002442
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002443 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2444 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2445 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002446 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002447 I915_WRITE(DSPSURF(plane),
2448 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002449 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002450 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2451 } else {
2452 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2453 I915_WRITE(DSPLINOFF(plane), linear_offset);
2454 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002455 POSTING_READ(reg);
2456
2457 return 0;
2458}
2459
2460/* Assume fb object is pinned & idle & fenced and just update base pointers */
2461static int
2462intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2463 int x, int y, enum mode_set_atomic state)
2464{
2465 struct drm_device *dev = crtc->dev;
2466 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002467
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002468 if (dev_priv->display.disable_fbc)
2469 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002470 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002471
Matt Roper262ca2b2014-03-18 17:22:55 -07002472 return dev_priv->display.update_primary_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002473}
2474
Ville Syrjälä96a02912013-02-18 19:08:49 +02002475void intel_display_handle_reset(struct drm_device *dev)
2476{
2477 struct drm_i915_private *dev_priv = dev->dev_private;
2478 struct drm_crtc *crtc;
2479
2480 /*
2481 * Flips in the rings have been nuked by the reset,
2482 * so complete all pending flips so that user space
2483 * will get its events and not get stuck.
2484 *
2485 * Also update the base address of all primary
2486 * planes to the the last fb to make sure we're
2487 * showing the correct fb after a reset.
2488 *
2489 * Need to make two loops over the crtcs so that we
2490 * don't try to grab a crtc mutex before the
2491 * pending_flip_queue really got woken up.
2492 */
2493
2494 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2496 enum plane plane = intel_crtc->plane;
2497
2498 intel_prepare_page_flip(dev, plane);
2499 intel_finish_page_flip_plane(dev, plane);
2500 }
2501
2502 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2504
2505 mutex_lock(&crtc->mutex);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002506 /*
2507 * FIXME: Once we have proper support for primary planes (and
2508 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002509 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002510 */
Matt Roperf4510a22014-04-01 15:22:40 -07002511 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002512 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002513 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002514 crtc->x,
2515 crtc->y);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002516 mutex_unlock(&crtc->mutex);
2517 }
2518}
2519
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002520static int
Chris Wilson14667a42012-04-03 17:58:35 +01002521intel_finish_fb(struct drm_framebuffer *old_fb)
2522{
2523 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2524 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2525 bool was_interruptible = dev_priv->mm.interruptible;
2526 int ret;
2527
Chris Wilson14667a42012-04-03 17:58:35 +01002528 /* Big Hammer, we also need to ensure that any pending
2529 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2530 * current scanout is retired before unpinning the old
2531 * framebuffer.
2532 *
2533 * This should only fail upon a hung GPU, in which case we
2534 * can safely continue.
2535 */
2536 dev_priv->mm.interruptible = false;
2537 ret = i915_gem_object_finish_gpu(obj);
2538 dev_priv->mm.interruptible = was_interruptible;
2539
2540 return ret;
2541}
2542
Chris Wilson7d5e3792014-03-04 13:15:08 +00002543static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2544{
2545 struct drm_device *dev = crtc->dev;
2546 struct drm_i915_private *dev_priv = dev->dev_private;
2547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2548 unsigned long flags;
2549 bool pending;
2550
2551 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2552 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2553 return false;
2554
2555 spin_lock_irqsave(&dev->event_lock, flags);
2556 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2557 spin_unlock_irqrestore(&dev->event_lock, flags);
2558
2559 return pending;
2560}
2561
Chris Wilson14667a42012-04-03 17:58:35 +01002562static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002563intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002564 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002565{
2566 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002567 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002569 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002570 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002571
Chris Wilson7d5e3792014-03-04 13:15:08 +00002572 if (intel_crtc_has_pending_flip(crtc)) {
2573 DRM_ERROR("pipe is still busy with an old pageflip\n");
2574 return -EBUSY;
2575 }
2576
Jesse Barnes79e53942008-11-07 14:24:08 -08002577 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002578 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002579 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002580 return 0;
2581 }
2582
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002583 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002584 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2585 plane_name(intel_crtc->plane),
2586 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002587 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002588 }
2589
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002590 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002591 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002592 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002593 NULL);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002594 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002595 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002596 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002597 return ret;
2598 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002599
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002600 /*
2601 * Update pipe size and adjust fitter if needed: the reason for this is
2602 * that in compute_mode_changes we check the native mode (not the pfit
2603 * mode) to see if we can flip rather than do a full mode set. In the
2604 * fastboot case, we'll flip, but if we don't update the pipesrc and
2605 * pfit state, we'll end up with a big fb scanned out into the wrong
2606 * sized surface.
2607 *
2608 * To fix this properly, we need to hoist the checks up into
2609 * compute_mode_changes (or above), check the actual pfit state and
2610 * whether the platform allows pfit disable with pipe active, and only
2611 * then update the pipesrc and pfit state, even on the flip path.
2612 */
Jani Nikulad330a952014-01-21 11:24:25 +02002613 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002614 const struct drm_display_mode *adjusted_mode =
2615 &intel_crtc->config.adjusted_mode;
2616
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002617 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002618 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2619 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002620 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002621 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2622 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2623 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2624 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2625 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2626 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002627 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2628 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002629 }
2630
Matt Roper262ca2b2014-03-18 17:22:55 -07002631 ret = dev_priv->display.update_primary_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002632 if (ret) {
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002633 mutex_lock(&dev->struct_mutex);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002634 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002635 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002636 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002637 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002638 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002639
Matt Roperf4510a22014-04-01 15:22:40 -07002640 old_fb = crtc->primary->fb;
2641 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002642 crtc->x = x;
2643 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002644
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002645 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002646 if (intel_crtc->active && old_fb != fb)
2647 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002648 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002649 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002650 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002651 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002652
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002653 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002654 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002655 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002656 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002657
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002658 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002659}
2660
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002661static void intel_fdi_normal_train(struct drm_crtc *crtc)
2662{
2663 struct drm_device *dev = crtc->dev;
2664 struct drm_i915_private *dev_priv = dev->dev_private;
2665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2666 int pipe = intel_crtc->pipe;
2667 u32 reg, temp;
2668
2669 /* enable normal train */
2670 reg = FDI_TX_CTL(pipe);
2671 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002672 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002673 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2674 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002675 } else {
2676 temp &= ~FDI_LINK_TRAIN_NONE;
2677 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002678 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002679 I915_WRITE(reg, temp);
2680
2681 reg = FDI_RX_CTL(pipe);
2682 temp = I915_READ(reg);
2683 if (HAS_PCH_CPT(dev)) {
2684 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2685 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2686 } else {
2687 temp &= ~FDI_LINK_TRAIN_NONE;
2688 temp |= FDI_LINK_TRAIN_NONE;
2689 }
2690 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2691
2692 /* wait one idle pattern time */
2693 POSTING_READ(reg);
2694 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002695
2696 /* IVB wants error correction enabled */
2697 if (IS_IVYBRIDGE(dev))
2698 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2699 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002700}
2701
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002702static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002703{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002704 return crtc->base.enabled && crtc->active &&
2705 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002706}
2707
Daniel Vetter01a415f2012-10-27 15:58:40 +02002708static void ivb_modeset_global_resources(struct drm_device *dev)
2709{
2710 struct drm_i915_private *dev_priv = dev->dev_private;
2711 struct intel_crtc *pipe_B_crtc =
2712 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2713 struct intel_crtc *pipe_C_crtc =
2714 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2715 uint32_t temp;
2716
Daniel Vetter1e833f42013-02-19 22:31:57 +01002717 /*
2718 * When everything is off disable fdi C so that we could enable fdi B
2719 * with all lanes. Note that we don't care about enabled pipes without
2720 * an enabled pch encoder.
2721 */
2722 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2723 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002724 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2725 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2726
2727 temp = I915_READ(SOUTH_CHICKEN1);
2728 temp &= ~FDI_BC_BIFURCATION_SELECT;
2729 DRM_DEBUG_KMS("disabling fdi C rx\n");
2730 I915_WRITE(SOUTH_CHICKEN1, temp);
2731 }
2732}
2733
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002734/* The FDI link training functions for ILK/Ibexpeak. */
2735static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2736{
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
2739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2740 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002741 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002742
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002743 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002744 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002745
Adam Jacksone1a44742010-06-25 15:32:14 -04002746 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2747 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 reg = FDI_RX_IMR(pipe);
2749 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002750 temp &= ~FDI_RX_SYMBOL_LOCK;
2751 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002752 I915_WRITE(reg, temp);
2753 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002754 udelay(150);
2755
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002756 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002757 reg = FDI_TX_CTL(pipe);
2758 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002759 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2760 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002761 temp &= ~FDI_LINK_TRAIN_NONE;
2762 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002763 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002764
Chris Wilson5eddb702010-09-11 13:48:45 +01002765 reg = FDI_RX_CTL(pipe);
2766 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002767 temp &= ~FDI_LINK_TRAIN_NONE;
2768 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002769 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2770
2771 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002772 udelay(150);
2773
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002774 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002775 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2776 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2777 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002778
Chris Wilson5eddb702010-09-11 13:48:45 +01002779 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002780 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002781 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002782 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2783
2784 if ((temp & FDI_RX_BIT_LOCK)) {
2785 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002786 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002787 break;
2788 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002789 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002790 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002791 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002792
2793 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002794 reg = FDI_TX_CTL(pipe);
2795 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002796 temp &= ~FDI_LINK_TRAIN_NONE;
2797 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002798 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002799
Chris Wilson5eddb702010-09-11 13:48:45 +01002800 reg = FDI_RX_CTL(pipe);
2801 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002802 temp &= ~FDI_LINK_TRAIN_NONE;
2803 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002804 I915_WRITE(reg, temp);
2805
2806 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002807 udelay(150);
2808
Chris Wilson5eddb702010-09-11 13:48:45 +01002809 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002810 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002811 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002812 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2813
2814 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002815 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002816 DRM_DEBUG_KMS("FDI train 2 done.\n");
2817 break;
2818 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002819 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002820 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002821 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002822
2823 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002824
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002825}
2826
Akshay Joshi0206e352011-08-16 15:34:10 -04002827static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002828 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2829 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2830 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2831 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2832};
2833
2834/* The FDI link training functions for SNB/Cougarpoint. */
2835static void gen6_fdi_link_train(struct drm_crtc *crtc)
2836{
2837 struct drm_device *dev = crtc->dev;
2838 struct drm_i915_private *dev_priv = dev->dev_private;
2839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2840 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002841 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002842
Adam Jacksone1a44742010-06-25 15:32:14 -04002843 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2844 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002845 reg = FDI_RX_IMR(pipe);
2846 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002847 temp &= ~FDI_RX_SYMBOL_LOCK;
2848 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002849 I915_WRITE(reg, temp);
2850
2851 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002852 udelay(150);
2853
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002854 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002855 reg = FDI_TX_CTL(pipe);
2856 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002857 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2858 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002859 temp &= ~FDI_LINK_TRAIN_NONE;
2860 temp |= FDI_LINK_TRAIN_PATTERN_1;
2861 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2862 /* SNB-B */
2863 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002864 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002865
Daniel Vetterd74cf322012-10-26 10:58:13 +02002866 I915_WRITE(FDI_RX_MISC(pipe),
2867 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2868
Chris Wilson5eddb702010-09-11 13:48:45 +01002869 reg = FDI_RX_CTL(pipe);
2870 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002871 if (HAS_PCH_CPT(dev)) {
2872 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2873 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2874 } else {
2875 temp &= ~FDI_LINK_TRAIN_NONE;
2876 temp |= FDI_LINK_TRAIN_PATTERN_1;
2877 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002878 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2879
2880 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002881 udelay(150);
2882
Akshay Joshi0206e352011-08-16 15:34:10 -04002883 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002884 reg = FDI_TX_CTL(pipe);
2885 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002886 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2887 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002888 I915_WRITE(reg, temp);
2889
2890 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002891 udelay(500);
2892
Sean Paulfa37d392012-03-02 12:53:39 -05002893 for (retry = 0; retry < 5; retry++) {
2894 reg = FDI_RX_IIR(pipe);
2895 temp = I915_READ(reg);
2896 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2897 if (temp & FDI_RX_BIT_LOCK) {
2898 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2899 DRM_DEBUG_KMS("FDI train 1 done.\n");
2900 break;
2901 }
2902 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002903 }
Sean Paulfa37d392012-03-02 12:53:39 -05002904 if (retry < 5)
2905 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002906 }
2907 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002908 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002909
2910 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002911 reg = FDI_TX_CTL(pipe);
2912 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002913 temp &= ~FDI_LINK_TRAIN_NONE;
2914 temp |= FDI_LINK_TRAIN_PATTERN_2;
2915 if (IS_GEN6(dev)) {
2916 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2917 /* SNB-B */
2918 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2919 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002920 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002921
Chris Wilson5eddb702010-09-11 13:48:45 +01002922 reg = FDI_RX_CTL(pipe);
2923 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002924 if (HAS_PCH_CPT(dev)) {
2925 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2926 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2927 } else {
2928 temp &= ~FDI_LINK_TRAIN_NONE;
2929 temp |= FDI_LINK_TRAIN_PATTERN_2;
2930 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002931 I915_WRITE(reg, temp);
2932
2933 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002934 udelay(150);
2935
Akshay Joshi0206e352011-08-16 15:34:10 -04002936 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002937 reg = FDI_TX_CTL(pipe);
2938 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002939 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2940 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002941 I915_WRITE(reg, temp);
2942
2943 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002944 udelay(500);
2945
Sean Paulfa37d392012-03-02 12:53:39 -05002946 for (retry = 0; retry < 5; retry++) {
2947 reg = FDI_RX_IIR(pipe);
2948 temp = I915_READ(reg);
2949 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2950 if (temp & FDI_RX_SYMBOL_LOCK) {
2951 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2952 DRM_DEBUG_KMS("FDI train 2 done.\n");
2953 break;
2954 }
2955 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002956 }
Sean Paulfa37d392012-03-02 12:53:39 -05002957 if (retry < 5)
2958 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002959 }
2960 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002961 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002962
2963 DRM_DEBUG_KMS("FDI train done.\n");
2964}
2965
Jesse Barnes357555c2011-04-28 15:09:55 -07002966/* Manual link training for Ivy Bridge A0 parts */
2967static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2968{
2969 struct drm_device *dev = crtc->dev;
2970 struct drm_i915_private *dev_priv = dev->dev_private;
2971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2972 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07002973 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07002974
2975 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2976 for train result */
2977 reg = FDI_RX_IMR(pipe);
2978 temp = I915_READ(reg);
2979 temp &= ~FDI_RX_SYMBOL_LOCK;
2980 temp &= ~FDI_RX_BIT_LOCK;
2981 I915_WRITE(reg, temp);
2982
2983 POSTING_READ(reg);
2984 udelay(150);
2985
Daniel Vetter01a415f2012-10-27 15:58:40 +02002986 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2987 I915_READ(FDI_RX_IIR(pipe)));
2988
Jesse Barnes139ccd32013-08-19 11:04:55 -07002989 /* Try each vswing and preemphasis setting twice before moving on */
2990 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2991 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07002992 reg = FDI_TX_CTL(pipe);
2993 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07002994 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2995 temp &= ~FDI_TX_ENABLE;
2996 I915_WRITE(reg, temp);
2997
2998 reg = FDI_RX_CTL(pipe);
2999 temp = I915_READ(reg);
3000 temp &= ~FDI_LINK_TRAIN_AUTO;
3001 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3002 temp &= ~FDI_RX_ENABLE;
3003 I915_WRITE(reg, temp);
3004
3005 /* enable CPU FDI TX and PCH FDI RX */
3006 reg = FDI_TX_CTL(pipe);
3007 temp = I915_READ(reg);
3008 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3009 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3010 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003011 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003012 temp |= snb_b_fdi_train_param[j/2];
3013 temp |= FDI_COMPOSITE_SYNC;
3014 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3015
3016 I915_WRITE(FDI_RX_MISC(pipe),
3017 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3018
3019 reg = FDI_RX_CTL(pipe);
3020 temp = I915_READ(reg);
3021 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3022 temp |= FDI_COMPOSITE_SYNC;
3023 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3024
3025 POSTING_READ(reg);
3026 udelay(1); /* should be 0.5us */
3027
3028 for (i = 0; i < 4; i++) {
3029 reg = FDI_RX_IIR(pipe);
3030 temp = I915_READ(reg);
3031 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3032
3033 if (temp & FDI_RX_BIT_LOCK ||
3034 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3035 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3036 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3037 i);
3038 break;
3039 }
3040 udelay(1); /* should be 0.5us */
3041 }
3042 if (i == 4) {
3043 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3044 continue;
3045 }
3046
3047 /* Train 2 */
3048 reg = FDI_TX_CTL(pipe);
3049 temp = I915_READ(reg);
3050 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3051 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3052 I915_WRITE(reg, temp);
3053
3054 reg = FDI_RX_CTL(pipe);
3055 temp = I915_READ(reg);
3056 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3057 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003058 I915_WRITE(reg, temp);
3059
3060 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003061 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003062
Jesse Barnes139ccd32013-08-19 11:04:55 -07003063 for (i = 0; i < 4; i++) {
3064 reg = FDI_RX_IIR(pipe);
3065 temp = I915_READ(reg);
3066 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003067
Jesse Barnes139ccd32013-08-19 11:04:55 -07003068 if (temp & FDI_RX_SYMBOL_LOCK ||
3069 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3070 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3071 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3072 i);
3073 goto train_done;
3074 }
3075 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003076 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003077 if (i == 4)
3078 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003079 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003080
Jesse Barnes139ccd32013-08-19 11:04:55 -07003081train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003082 DRM_DEBUG_KMS("FDI train done.\n");
3083}
3084
Daniel Vetter88cefb62012-08-12 19:27:14 +02003085static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003086{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003087 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003088 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003089 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003090 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003091
Jesse Barnesc64e3112010-09-10 11:27:03 -07003092
Jesse Barnes0e23b992010-09-10 11:10:00 -07003093 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003094 reg = FDI_RX_CTL(pipe);
3095 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003096 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3097 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003098 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003099 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3100
3101 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003102 udelay(200);
3103
3104 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003105 temp = I915_READ(reg);
3106 I915_WRITE(reg, temp | FDI_PCDCLK);
3107
3108 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003109 udelay(200);
3110
Paulo Zanoni20749732012-11-23 15:30:38 -02003111 /* Enable CPU FDI TX PLL, always on for Ironlake */
3112 reg = FDI_TX_CTL(pipe);
3113 temp = I915_READ(reg);
3114 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3115 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003116
Paulo Zanoni20749732012-11-23 15:30:38 -02003117 POSTING_READ(reg);
3118 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003119 }
3120}
3121
Daniel Vetter88cefb62012-08-12 19:27:14 +02003122static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3123{
3124 struct drm_device *dev = intel_crtc->base.dev;
3125 struct drm_i915_private *dev_priv = dev->dev_private;
3126 int pipe = intel_crtc->pipe;
3127 u32 reg, temp;
3128
3129 /* Switch from PCDclk to Rawclk */
3130 reg = FDI_RX_CTL(pipe);
3131 temp = I915_READ(reg);
3132 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3133
3134 /* Disable CPU FDI TX PLL */
3135 reg = FDI_TX_CTL(pipe);
3136 temp = I915_READ(reg);
3137 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3138
3139 POSTING_READ(reg);
3140 udelay(100);
3141
3142 reg = FDI_RX_CTL(pipe);
3143 temp = I915_READ(reg);
3144 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3145
3146 /* Wait for the clocks to turn off. */
3147 POSTING_READ(reg);
3148 udelay(100);
3149}
3150
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003151static void ironlake_fdi_disable(struct drm_crtc *crtc)
3152{
3153 struct drm_device *dev = crtc->dev;
3154 struct drm_i915_private *dev_priv = dev->dev_private;
3155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3156 int pipe = intel_crtc->pipe;
3157 u32 reg, temp;
3158
3159 /* disable CPU FDI tx and PCH FDI rx */
3160 reg = FDI_TX_CTL(pipe);
3161 temp = I915_READ(reg);
3162 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3163 POSTING_READ(reg);
3164
3165 reg = FDI_RX_CTL(pipe);
3166 temp = I915_READ(reg);
3167 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003168 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003169 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3170
3171 POSTING_READ(reg);
3172 udelay(100);
3173
3174 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003175 if (HAS_PCH_IBX(dev)) {
3176 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003177 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003178
3179 /* still set train pattern 1 */
3180 reg = FDI_TX_CTL(pipe);
3181 temp = I915_READ(reg);
3182 temp &= ~FDI_LINK_TRAIN_NONE;
3183 temp |= FDI_LINK_TRAIN_PATTERN_1;
3184 I915_WRITE(reg, temp);
3185
3186 reg = FDI_RX_CTL(pipe);
3187 temp = I915_READ(reg);
3188 if (HAS_PCH_CPT(dev)) {
3189 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3190 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3191 } else {
3192 temp &= ~FDI_LINK_TRAIN_NONE;
3193 temp |= FDI_LINK_TRAIN_PATTERN_1;
3194 }
3195 /* BPC in FDI rx is consistent with that in PIPECONF */
3196 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003197 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003198 I915_WRITE(reg, temp);
3199
3200 POSTING_READ(reg);
3201 udelay(100);
3202}
3203
Chris Wilson5dce5b932014-01-20 10:17:36 +00003204bool intel_has_pending_fb_unpin(struct drm_device *dev)
3205{
3206 struct intel_crtc *crtc;
3207
3208 /* Note that we don't need to be called with mode_config.lock here
3209 * as our list of CRTC objects is static for the lifetime of the
3210 * device and so cannot disappear as we iterate. Similarly, we can
3211 * happily treat the predicates as racy, atomic checks as userspace
3212 * cannot claim and pin a new fb without at least acquring the
3213 * struct_mutex and so serialising with us.
3214 */
3215 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3216 if (atomic_read(&crtc->unpin_work_count) == 0)
3217 continue;
3218
3219 if (crtc->unpin_work)
3220 intel_wait_for_vblank(dev, crtc->pipe);
3221
3222 return true;
3223 }
3224
3225 return false;
3226}
3227
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003228static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3229{
Chris Wilson0f911282012-04-17 10:05:38 +01003230 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003231 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003232
Matt Roperf4510a22014-04-01 15:22:40 -07003233 if (crtc->primary->fb == NULL)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003234 return;
3235
Daniel Vetter2c10d572012-12-20 21:24:07 +01003236 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3237
Chris Wilson5bb61642012-09-27 21:25:58 +01003238 wait_event(dev_priv->pending_flip_queue,
3239 !intel_crtc_has_pending_flip(crtc));
3240
Chris Wilson0f911282012-04-17 10:05:38 +01003241 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07003242 intel_finish_fb(crtc->primary->fb);
Chris Wilson0f911282012-04-17 10:05:38 +01003243 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003244}
3245
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003246/* Program iCLKIP clock to the desired frequency */
3247static void lpt_program_iclkip(struct drm_crtc *crtc)
3248{
3249 struct drm_device *dev = crtc->dev;
3250 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003251 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003252 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3253 u32 temp;
3254
Daniel Vetter09153002012-12-12 14:06:44 +01003255 mutex_lock(&dev_priv->dpio_lock);
3256
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003257 /* It is necessary to ungate the pixclk gate prior to programming
3258 * the divisors, and gate it back when it is done.
3259 */
3260 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3261
3262 /* Disable SSCCTL */
3263 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003264 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3265 SBI_SSCCTL_DISABLE,
3266 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003267
3268 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003269 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003270 auxdiv = 1;
3271 divsel = 0x41;
3272 phaseinc = 0x20;
3273 } else {
3274 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003275 * but the adjusted_mode->crtc_clock in in KHz. To get the
3276 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003277 * convert the virtual clock precision to KHz here for higher
3278 * precision.
3279 */
3280 u32 iclk_virtual_root_freq = 172800 * 1000;
3281 u32 iclk_pi_range = 64;
3282 u32 desired_divisor, msb_divisor_value, pi_value;
3283
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003284 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003285 msb_divisor_value = desired_divisor / iclk_pi_range;
3286 pi_value = desired_divisor % iclk_pi_range;
3287
3288 auxdiv = 0;
3289 divsel = msb_divisor_value - 2;
3290 phaseinc = pi_value;
3291 }
3292
3293 /* This should not happen with any sane values */
3294 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3295 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3296 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3297 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3298
3299 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 +03003300 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003301 auxdiv,
3302 divsel,
3303 phasedir,
3304 phaseinc);
3305
3306 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003307 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003308 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3309 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3310 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3311 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3312 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3313 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003314 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003315
3316 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003317 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003318 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3319 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003320 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003321
3322 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003323 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003324 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003325 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003326
3327 /* Wait for initialization time */
3328 udelay(24);
3329
3330 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003331
3332 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003333}
3334
Daniel Vetter275f01b22013-05-03 11:49:47 +02003335static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3336 enum pipe pch_transcoder)
3337{
3338 struct drm_device *dev = crtc->base.dev;
3339 struct drm_i915_private *dev_priv = dev->dev_private;
3340 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3341
3342 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3343 I915_READ(HTOTAL(cpu_transcoder)));
3344 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3345 I915_READ(HBLANK(cpu_transcoder)));
3346 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3347 I915_READ(HSYNC(cpu_transcoder)));
3348
3349 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3350 I915_READ(VTOTAL(cpu_transcoder)));
3351 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3352 I915_READ(VBLANK(cpu_transcoder)));
3353 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3354 I915_READ(VSYNC(cpu_transcoder)));
3355 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3356 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3357}
3358
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003359static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3360{
3361 struct drm_i915_private *dev_priv = dev->dev_private;
3362 uint32_t temp;
3363
3364 temp = I915_READ(SOUTH_CHICKEN1);
3365 if (temp & FDI_BC_BIFURCATION_SELECT)
3366 return;
3367
3368 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3369 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3370
3371 temp |= FDI_BC_BIFURCATION_SELECT;
3372 DRM_DEBUG_KMS("enabling fdi C rx\n");
3373 I915_WRITE(SOUTH_CHICKEN1, temp);
3374 POSTING_READ(SOUTH_CHICKEN1);
3375}
3376
3377static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3378{
3379 struct drm_device *dev = intel_crtc->base.dev;
3380 struct drm_i915_private *dev_priv = dev->dev_private;
3381
3382 switch (intel_crtc->pipe) {
3383 case PIPE_A:
3384 break;
3385 case PIPE_B:
3386 if (intel_crtc->config.fdi_lanes > 2)
3387 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3388 else
3389 cpt_enable_fdi_bc_bifurcation(dev);
3390
3391 break;
3392 case PIPE_C:
3393 cpt_enable_fdi_bc_bifurcation(dev);
3394
3395 break;
3396 default:
3397 BUG();
3398 }
3399}
3400
Jesse Barnesf67a5592011-01-05 10:31:48 -08003401/*
3402 * Enable PCH resources required for PCH ports:
3403 * - PCH PLLs
3404 * - FDI training & RX/TX
3405 * - update transcoder timings
3406 * - DP transcoding bits
3407 * - transcoder
3408 */
3409static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003410{
3411 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003412 struct drm_i915_private *dev_priv = dev->dev_private;
3413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3414 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003415 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003416
Daniel Vetterab9412b2013-05-03 11:49:46 +02003417 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003418
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003419 if (IS_IVYBRIDGE(dev))
3420 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3421
Daniel Vettercd986ab2012-10-26 10:58:12 +02003422 /* Write the TU size bits before fdi link training, so that error
3423 * detection works. */
3424 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3425 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3426
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003427 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003428 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003429
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003430 /* We need to program the right clock selection before writing the pixel
3431 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003432 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003433 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003434
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003435 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003436 temp |= TRANS_DPLL_ENABLE(pipe);
3437 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003438 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003439 temp |= sel;
3440 else
3441 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003442 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003443 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003444
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003445 /* XXX: pch pll's can be enabled any time before we enable the PCH
3446 * transcoder, and we actually should do this to not upset any PCH
3447 * transcoder that already use the clock when we share it.
3448 *
3449 * Note that enable_shared_dpll tries to do the right thing, but
3450 * get_shared_dpll unconditionally resets the pll - we need that to have
3451 * the right LVDS enable sequence. */
3452 ironlake_enable_shared_dpll(intel_crtc);
3453
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003454 /* set transcoder timing, panel must allow it */
3455 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003456 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003457
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003458 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003459
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003460 /* For PCH DP, enable TRANS_DP_CTL */
3461 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003462 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3463 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003464 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = TRANS_DP_CTL(pipe);
3466 temp = I915_READ(reg);
3467 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003468 TRANS_DP_SYNC_MASK |
3469 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 temp |= (TRANS_DP_OUTPUT_ENABLE |
3471 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003472 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003473
3474 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003476 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003478
3479 switch (intel_trans_dp_port_sel(crtc)) {
3480 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003482 break;
3483 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003485 break;
3486 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003488 break;
3489 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003490 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003491 }
3492
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003494 }
3495
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003496 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003497}
3498
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003499static void lpt_pch_enable(struct drm_crtc *crtc)
3500{
3501 struct drm_device *dev = crtc->dev;
3502 struct drm_i915_private *dev_priv = dev->dev_private;
3503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003504 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003505
Daniel Vetterab9412b2013-05-03 11:49:46 +02003506 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003507
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003508 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003509
Paulo Zanoni0540e482012-10-31 18:12:40 -02003510 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003511 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003512
Paulo Zanoni937bb612012-10-31 18:12:47 -02003513 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003514}
3515
Daniel Vettere2b78262013-06-07 23:10:03 +02003516static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003517{
Daniel Vettere2b78262013-06-07 23:10:03 +02003518 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003519
3520 if (pll == NULL)
3521 return;
3522
3523 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003524 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003525 return;
3526 }
3527
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003528 if (--pll->refcount == 0) {
3529 WARN_ON(pll->on);
3530 WARN_ON(pll->active);
3531 }
3532
Daniel Vettera43f6e02013-06-07 23:10:32 +02003533 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003534}
3535
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003536static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003537{
Daniel Vettere2b78262013-06-07 23:10:03 +02003538 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3539 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3540 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003541
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003542 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003543 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3544 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003545 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003546 }
3547
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003548 if (HAS_PCH_IBX(dev_priv->dev)) {
3549 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003550 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003551 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003552
Daniel Vetter46edb022013-06-05 13:34:12 +02003553 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3554 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003555
3556 goto found;
3557 }
3558
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003559 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3560 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003561
3562 /* Only want to check enabled timings first */
3563 if (pll->refcount == 0)
3564 continue;
3565
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003566 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3567 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003568 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003569 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003570 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003571
3572 goto found;
3573 }
3574 }
3575
3576 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003577 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3578 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003579 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003580 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3581 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003582 goto found;
3583 }
3584 }
3585
3586 return NULL;
3587
3588found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003589 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003590 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3591 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003592
Daniel Vettercdbd2312013-06-05 13:34:03 +02003593 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003594 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3595 sizeof(pll->hw_state));
3596
Daniel Vetter46edb022013-06-05 13:34:12 +02003597 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003598 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003599 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003600
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003601 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003602 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003603 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003604
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003605 return pll;
3606}
3607
Daniel Vettera1520312013-05-03 11:49:50 +02003608static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003609{
3610 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003611 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003612 u32 temp;
3613
3614 temp = I915_READ(dslreg);
3615 udelay(500);
3616 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003617 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003618 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003619 }
3620}
3621
Jesse Barnesb074cec2013-04-25 12:55:02 -07003622static void ironlake_pfit_enable(struct intel_crtc *crtc)
3623{
3624 struct drm_device *dev = crtc->base.dev;
3625 struct drm_i915_private *dev_priv = dev->dev_private;
3626 int pipe = crtc->pipe;
3627
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003628 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003629 /* Force use of hard-coded filter coefficients
3630 * as some pre-programmed values are broken,
3631 * e.g. x201.
3632 */
3633 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3634 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3635 PF_PIPE_SEL_IVB(pipe));
3636 else
3637 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3638 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3639 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003640 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003641}
3642
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003643static void intel_enable_planes(struct drm_crtc *crtc)
3644{
3645 struct drm_device *dev = crtc->dev;
3646 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003647 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003648 struct intel_plane *intel_plane;
3649
Matt Roperaf2b6532014-04-01 15:22:32 -07003650 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3651 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003652 if (intel_plane->pipe == pipe)
3653 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003654 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003655}
3656
3657static void intel_disable_planes(struct drm_crtc *crtc)
3658{
3659 struct drm_device *dev = crtc->dev;
3660 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003661 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003662 struct intel_plane *intel_plane;
3663
Matt Roperaf2b6532014-04-01 15:22:32 -07003664 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3665 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003666 if (intel_plane->pipe == pipe)
3667 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003668 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003669}
3670
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003671void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003672{
3673 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3674
3675 if (!crtc->config.ips_enabled)
3676 return;
3677
3678 /* We can only enable IPS after we enable a plane and wait for a vblank.
3679 * We guarantee that the plane is enabled by calling intel_enable_ips
3680 * only after intel_enable_plane. And intel_enable_plane already waits
3681 * for a vblank, so all we need to do here is to enable the IPS bit. */
3682 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003683 if (IS_BROADWELL(crtc->base.dev)) {
3684 mutex_lock(&dev_priv->rps.hw_lock);
3685 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3686 mutex_unlock(&dev_priv->rps.hw_lock);
3687 /* Quoting Art Runyan: "its not safe to expect any particular
3688 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003689 * mailbox." Moreover, the mailbox may return a bogus state,
3690 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003691 */
3692 } else {
3693 I915_WRITE(IPS_CTL, IPS_ENABLE);
3694 /* The bit only becomes 1 in the next vblank, so this wait here
3695 * is essentially intel_wait_for_vblank. If we don't have this
3696 * and don't wait for vblanks until the end of crtc_enable, then
3697 * the HW state readout code will complain that the expected
3698 * IPS_CTL value is not the one we read. */
3699 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3700 DRM_ERROR("Timed out waiting for IPS enable\n");
3701 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003702}
3703
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003704void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003705{
3706 struct drm_device *dev = crtc->base.dev;
3707 struct drm_i915_private *dev_priv = dev->dev_private;
3708
3709 if (!crtc->config.ips_enabled)
3710 return;
3711
3712 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003713 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003714 mutex_lock(&dev_priv->rps.hw_lock);
3715 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3716 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003717 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3718 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3719 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003720 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003721 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003722 POSTING_READ(IPS_CTL);
3723 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003724
3725 /* We need to wait for a vblank before we can disable the plane. */
3726 intel_wait_for_vblank(dev, crtc->pipe);
3727}
3728
3729/** Loads the palette/gamma unit for the CRTC with the prepared values */
3730static void intel_crtc_load_lut(struct drm_crtc *crtc)
3731{
3732 struct drm_device *dev = crtc->dev;
3733 struct drm_i915_private *dev_priv = dev->dev_private;
3734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3735 enum pipe pipe = intel_crtc->pipe;
3736 int palreg = PALETTE(pipe);
3737 int i;
3738 bool reenable_ips = false;
3739
3740 /* The clocks have to be on to load the palette. */
3741 if (!crtc->enabled || !intel_crtc->active)
3742 return;
3743
3744 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3745 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3746 assert_dsi_pll_enabled(dev_priv);
3747 else
3748 assert_pll_enabled(dev_priv, pipe);
3749 }
3750
3751 /* use legacy palette for Ironlake */
3752 if (HAS_PCH_SPLIT(dev))
3753 palreg = LGC_PALETTE(pipe);
3754
3755 /* Workaround : Do not read or write the pipe palette/gamma data while
3756 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3757 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003758 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003759 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3760 GAMMA_MODE_MODE_SPLIT)) {
3761 hsw_disable_ips(intel_crtc);
3762 reenable_ips = true;
3763 }
3764
3765 for (i = 0; i < 256; i++) {
3766 I915_WRITE(palreg + 4 * i,
3767 (intel_crtc->lut_r[i] << 16) |
3768 (intel_crtc->lut_g[i] << 8) |
3769 intel_crtc->lut_b[i]);
3770 }
3771
3772 if (reenable_ips)
3773 hsw_enable_ips(intel_crtc);
3774}
3775
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003776static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3777{
3778 if (!enable && intel_crtc->overlay) {
3779 struct drm_device *dev = intel_crtc->base.dev;
3780 struct drm_i915_private *dev_priv = dev->dev_private;
3781
3782 mutex_lock(&dev->struct_mutex);
3783 dev_priv->mm.interruptible = false;
3784 (void) intel_overlay_switch_off(intel_crtc->overlay);
3785 dev_priv->mm.interruptible = true;
3786 mutex_unlock(&dev->struct_mutex);
3787 }
3788
3789 /* Let userspace switch the overlay on again. In most cases userspace
3790 * has to recompute where to put it anyway.
3791 */
3792}
3793
3794/**
3795 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3796 * cursor plane briefly if not already running after enabling the display
3797 * plane.
3798 * This workaround avoids occasional blank screens when self refresh is
3799 * enabled.
3800 */
3801static void
3802g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3803{
3804 u32 cntl = I915_READ(CURCNTR(pipe));
3805
3806 if ((cntl & CURSOR_MODE) == 0) {
3807 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3808
3809 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3810 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3811 intel_wait_for_vblank(dev_priv->dev, pipe);
3812 I915_WRITE(CURCNTR(pipe), cntl);
3813 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3814 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3815 }
3816}
3817
3818static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003819{
3820 struct drm_device *dev = crtc->dev;
3821 struct drm_i915_private *dev_priv = dev->dev_private;
3822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3823 int pipe = intel_crtc->pipe;
3824 int plane = intel_crtc->plane;
3825
3826 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3827 intel_enable_planes(crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003828 /* The fixup needs to happen before cursor is enabled */
3829 if (IS_G4X(dev))
3830 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003831 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003832 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003833
3834 hsw_enable_ips(intel_crtc);
3835
3836 mutex_lock(&dev->struct_mutex);
3837 intel_update_fbc(dev);
3838 mutex_unlock(&dev->struct_mutex);
3839}
3840
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003841static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003842{
3843 struct drm_device *dev = crtc->dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3846 int pipe = intel_crtc->pipe;
3847 int plane = intel_crtc->plane;
3848
3849 intel_crtc_wait_for_pending_flips(crtc);
3850 drm_vblank_off(dev, pipe);
3851
3852 if (dev_priv->fbc.plane == plane)
3853 intel_disable_fbc(dev);
3854
3855 hsw_disable_ips(intel_crtc);
3856
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003857 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003858 intel_crtc_update_cursor(crtc, false);
3859 intel_disable_planes(crtc);
3860 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
3861}
3862
Jesse Barnesf67a5592011-01-05 10:31:48 -08003863static void ironlake_crtc_enable(struct drm_crtc *crtc)
3864{
3865 struct drm_device *dev = crtc->dev;
3866 struct drm_i915_private *dev_priv = dev->dev_private;
3867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003868 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003869 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003870
Daniel Vetter08a48462012-07-02 11:43:47 +02003871 WARN_ON(!crtc->enabled);
3872
Jesse Barnesf67a5592011-01-05 10:31:48 -08003873 if (intel_crtc->active)
3874 return;
3875
3876 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003877
3878 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3879 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3880
Daniel Vetterf6736a12013-06-05 13:34:30 +02003881 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003882 if (encoder->pre_enable)
3883 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003884
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003885 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003886 /* Note: FDI PLL enabling _must_ be done before we enable the
3887 * cpu pipes, hence this is separate from all the other fdi/pch
3888 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003889 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003890 } else {
3891 assert_fdi_tx_disabled(dev_priv, pipe);
3892 assert_fdi_rx_disabled(dev_priv, pipe);
3893 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003894
Jesse Barnesb074cec2013-04-25 12:55:02 -07003895 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003896
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003897 /*
3898 * On ILK+ LUT must be loaded before the pipe is running but with
3899 * clocks enabled
3900 */
3901 intel_crtc_load_lut(crtc);
3902
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03003903 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02003904 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003905
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003906 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003907 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003908
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003909 for_each_encoder_on_crtc(dev, crtc, encoder)
3910 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003911
3912 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003913 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003914
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003915 intel_crtc_enable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003916
Daniel Vetter6ce94102012-10-04 19:20:03 +02003917 /*
3918 * There seems to be a race in PCH platform hw (at least on some
3919 * outputs) where an enabled pipe still completes any pageflip right
3920 * away (as if the pipe is off) instead of waiting for vblank. As soon
3921 * as the first vblank happend, everything works as expected. Hence just
3922 * wait for one vblank before returning to avoid strange things
3923 * happening.
3924 */
3925 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003926}
3927
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003928/* IPS only exists on ULT machines and is tied to pipe A. */
3929static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3930{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003931 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003932}
3933
Paulo Zanonie4916942013-09-20 16:21:19 -03003934/*
3935 * This implements the workaround described in the "notes" section of the mode
3936 * set sequence documentation. When going from no pipes or single pipe to
3937 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3938 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3939 */
3940static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3941{
3942 struct drm_device *dev = crtc->base.dev;
3943 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3944
3945 /* We want to get the other_active_crtc only if there's only 1 other
3946 * active crtc. */
3947 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3948 if (!crtc_it->active || crtc_it == crtc)
3949 continue;
3950
3951 if (other_active_crtc)
3952 return;
3953
3954 other_active_crtc = crtc_it;
3955 }
3956 if (!other_active_crtc)
3957 return;
3958
3959 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3960 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3961}
3962
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003963static void haswell_crtc_enable(struct drm_crtc *crtc)
3964{
3965 struct drm_device *dev = crtc->dev;
3966 struct drm_i915_private *dev_priv = dev->dev_private;
3967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3968 struct intel_encoder *encoder;
3969 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003970
3971 WARN_ON(!crtc->enabled);
3972
3973 if (intel_crtc->active)
3974 return;
3975
3976 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003977
3978 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3979 if (intel_crtc->config.has_pch_encoder)
3980 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3981
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003982 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003983 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003984
3985 for_each_encoder_on_crtc(dev, crtc, encoder)
3986 if (encoder->pre_enable)
3987 encoder->pre_enable(encoder);
3988
Paulo Zanoni1f544382012-10-24 11:32:00 -02003989 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003990
Jesse Barnesb074cec2013-04-25 12:55:02 -07003991 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003992
3993 /*
3994 * On ILK+ LUT must be loaded before the pipe is running but with
3995 * clocks enabled
3996 */
3997 intel_crtc_load_lut(crtc);
3998
Paulo Zanoni1f544382012-10-24 11:32:00 -02003999 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004000 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004001
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004002 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004003 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004004
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004005 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004006 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004007
Jani Nikula8807e552013-08-30 19:40:32 +03004008 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004009 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004010 intel_opregion_notify_encoder(encoder, true);
4011 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004012
Paulo Zanonie4916942013-09-20 16:21:19 -03004013 /* If we change the relative order between pipe/planes enabling, we need
4014 * to change the workaround. */
4015 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004016 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004017}
4018
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004019static void ironlake_pfit_disable(struct intel_crtc *crtc)
4020{
4021 struct drm_device *dev = crtc->base.dev;
4022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 int pipe = crtc->pipe;
4024
4025 /* To avoid upsetting the power well on haswell only disable the pfit if
4026 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004027 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004028 I915_WRITE(PF_CTL(pipe), 0);
4029 I915_WRITE(PF_WIN_POS(pipe), 0);
4030 I915_WRITE(PF_WIN_SZ(pipe), 0);
4031 }
4032}
4033
Jesse Barnes6be4a602010-09-10 10:26:01 -07004034static void ironlake_crtc_disable(struct drm_crtc *crtc)
4035{
4036 struct drm_device *dev = crtc->dev;
4037 struct drm_i915_private *dev_priv = dev->dev_private;
4038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004039 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004040 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004041 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004042
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004043 if (!intel_crtc->active)
4044 return;
4045
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004046 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004047
Daniel Vetterea9d7582012-07-10 10:42:52 +02004048 for_each_encoder_on_crtc(dev, crtc, encoder)
4049 encoder->disable(encoder);
4050
Daniel Vetterd925c592013-06-05 13:34:04 +02004051 if (intel_crtc->config.has_pch_encoder)
4052 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4053
Jesse Barnesb24e7172011-01-04 15:09:30 -08004054 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004055
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004056 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004057
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004058 for_each_encoder_on_crtc(dev, crtc, encoder)
4059 if (encoder->post_disable)
4060 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004061
Daniel Vetterd925c592013-06-05 13:34:04 +02004062 if (intel_crtc->config.has_pch_encoder) {
4063 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004064
Daniel Vetterd925c592013-06-05 13:34:04 +02004065 ironlake_disable_pch_transcoder(dev_priv, pipe);
4066 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004067
Daniel Vetterd925c592013-06-05 13:34:04 +02004068 if (HAS_PCH_CPT(dev)) {
4069 /* disable TRANS_DP_CTL */
4070 reg = TRANS_DP_CTL(pipe);
4071 temp = I915_READ(reg);
4072 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4073 TRANS_DP_PORT_SEL_MASK);
4074 temp |= TRANS_DP_PORT_SEL_NONE;
4075 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004076
Daniel Vetterd925c592013-06-05 13:34:04 +02004077 /* disable DPLL_SEL */
4078 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004079 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004080 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004081 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004082
4083 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004084 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004085
4086 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004087 }
4088
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004089 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004090 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004091
4092 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004093 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004094 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004095}
4096
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004097static void haswell_crtc_disable(struct drm_crtc *crtc)
4098{
4099 struct drm_device *dev = crtc->dev;
4100 struct drm_i915_private *dev_priv = dev->dev_private;
4101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4102 struct intel_encoder *encoder;
4103 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004104 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004105
4106 if (!intel_crtc->active)
4107 return;
4108
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004109 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004110
Jani Nikula8807e552013-08-30 19:40:32 +03004111 for_each_encoder_on_crtc(dev, crtc, encoder) {
4112 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004113 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004114 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004115
Paulo Zanoni86642812013-04-12 17:57:57 -03004116 if (intel_crtc->config.has_pch_encoder)
4117 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004118 intel_disable_pipe(dev_priv, pipe);
4119
Paulo Zanoniad80a812012-10-24 16:06:19 -02004120 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004121
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004122 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004123
Paulo Zanoni1f544382012-10-24 11:32:00 -02004124 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004125
4126 for_each_encoder_on_crtc(dev, crtc, encoder)
4127 if (encoder->post_disable)
4128 encoder->post_disable(encoder);
4129
Daniel Vetter88adfff2013-03-28 10:42:01 +01004130 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004131 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004132 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004133 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004134 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004135
4136 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004137 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004138
4139 mutex_lock(&dev->struct_mutex);
4140 intel_update_fbc(dev);
4141 mutex_unlock(&dev->struct_mutex);
4142}
4143
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004144static void ironlake_crtc_off(struct drm_crtc *crtc)
4145{
4146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004147 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004148}
4149
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004150static void haswell_crtc_off(struct drm_crtc *crtc)
4151{
4152 intel_ddi_put_crtc_pll(crtc);
4153}
4154
Jesse Barnes2dd24552013-04-25 12:55:01 -07004155static void i9xx_pfit_enable(struct intel_crtc *crtc)
4156{
4157 struct drm_device *dev = crtc->base.dev;
4158 struct drm_i915_private *dev_priv = dev->dev_private;
4159 struct intel_crtc_config *pipe_config = &crtc->config;
4160
Daniel Vetter328d8e82013-05-08 10:36:31 +02004161 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004162 return;
4163
Daniel Vetterc0b03412013-05-28 12:05:54 +02004164 /*
4165 * The panel fitter should only be adjusted whilst the pipe is disabled,
4166 * according to register description and PRM.
4167 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004168 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4169 assert_pipe_disabled(dev_priv, crtc->pipe);
4170
Jesse Barnesb074cec2013-04-25 12:55:02 -07004171 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4172 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004173
4174 /* Border color in case we don't scale up to the full screen. Black by
4175 * default, change to something else for debugging. */
4176 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004177}
4178
Imre Deak77d22dc2014-03-05 16:20:52 +02004179#define for_each_power_domain(domain, mask) \
4180 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4181 if ((1 << (domain)) & (mask))
4182
Imre Deak319be8a2014-03-04 19:22:57 +02004183enum intel_display_power_domain
4184intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004185{
Imre Deak319be8a2014-03-04 19:22:57 +02004186 struct drm_device *dev = intel_encoder->base.dev;
4187 struct intel_digital_port *intel_dig_port;
4188
4189 switch (intel_encoder->type) {
4190 case INTEL_OUTPUT_UNKNOWN:
4191 /* Only DDI platforms should ever use this output type */
4192 WARN_ON_ONCE(!HAS_DDI(dev));
4193 case INTEL_OUTPUT_DISPLAYPORT:
4194 case INTEL_OUTPUT_HDMI:
4195 case INTEL_OUTPUT_EDP:
4196 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4197 switch (intel_dig_port->port) {
4198 case PORT_A:
4199 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4200 case PORT_B:
4201 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4202 case PORT_C:
4203 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4204 case PORT_D:
4205 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4206 default:
4207 WARN_ON_ONCE(1);
4208 return POWER_DOMAIN_PORT_OTHER;
4209 }
4210 case INTEL_OUTPUT_ANALOG:
4211 return POWER_DOMAIN_PORT_CRT;
4212 case INTEL_OUTPUT_DSI:
4213 return POWER_DOMAIN_PORT_DSI;
4214 default:
4215 return POWER_DOMAIN_PORT_OTHER;
4216 }
4217}
4218
4219static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4220{
4221 struct drm_device *dev = crtc->dev;
4222 struct intel_encoder *intel_encoder;
4223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4224 enum pipe pipe = intel_crtc->pipe;
4225 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
Imre Deak77d22dc2014-03-05 16:20:52 +02004226 unsigned long mask;
4227 enum transcoder transcoder;
4228
4229 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4230
4231 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4232 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4233 if (pfit_enabled)
4234 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4235
Imre Deak319be8a2014-03-04 19:22:57 +02004236 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4237 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4238
Imre Deak77d22dc2014-03-05 16:20:52 +02004239 return mask;
4240}
4241
4242void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4243 bool enable)
4244{
4245 if (dev_priv->power_domains.init_power_on == enable)
4246 return;
4247
4248 if (enable)
4249 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4250 else
4251 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4252
4253 dev_priv->power_domains.init_power_on = enable;
4254}
4255
4256static void modeset_update_crtc_power_domains(struct drm_device *dev)
4257{
4258 struct drm_i915_private *dev_priv = dev->dev_private;
4259 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4260 struct intel_crtc *crtc;
4261
4262 /*
4263 * First get all needed power domains, then put all unneeded, to avoid
4264 * any unnecessary toggling of the power wells.
4265 */
4266 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
4267 enum intel_display_power_domain domain;
4268
4269 if (!crtc->base.enabled)
4270 continue;
4271
Imre Deak319be8a2014-03-04 19:22:57 +02004272 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004273
4274 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4275 intel_display_power_get(dev_priv, domain);
4276 }
4277
4278 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
4279 enum intel_display_power_domain domain;
4280
4281 for_each_power_domain(domain, crtc->enabled_power_domains)
4282 intel_display_power_put(dev_priv, domain);
4283
4284 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4285 }
4286
4287 intel_display_set_init_power(dev_priv, false);
4288}
4289
Jesse Barnes586f49d2013-11-04 16:06:59 -08004290int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004291{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004292 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004293
Jesse Barnes586f49d2013-11-04 16:06:59 -08004294 /* Obtain SKU information */
4295 mutex_lock(&dev_priv->dpio_lock);
4296 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4297 CCK_FUSE_HPLL_FREQ_MASK;
4298 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004299
Jesse Barnes586f49d2013-11-04 16:06:59 -08004300 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08004301}
4302
4303/* Adjust CDclk dividers to allow high res or save power if possible */
4304static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4305{
4306 struct drm_i915_private *dev_priv = dev->dev_private;
4307 u32 val, cmd;
4308
Imre Deakd60c4472014-03-27 17:45:10 +02004309 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4310 dev_priv->vlv_cdclk_freq = cdclk;
4311
Jesse Barnes30a970c2013-11-04 13:48:12 -08004312 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4313 cmd = 2;
4314 else if (cdclk == 266)
4315 cmd = 1;
4316 else
4317 cmd = 0;
4318
4319 mutex_lock(&dev_priv->rps.hw_lock);
4320 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4321 val &= ~DSPFREQGUAR_MASK;
4322 val |= (cmd << DSPFREQGUAR_SHIFT);
4323 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4324 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4325 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4326 50)) {
4327 DRM_ERROR("timed out waiting for CDclk change\n");
4328 }
4329 mutex_unlock(&dev_priv->rps.hw_lock);
4330
4331 if (cdclk == 400) {
4332 u32 divider, vco;
4333
4334 vco = valleyview_get_vco(dev_priv);
4335 divider = ((vco << 1) / cdclk) - 1;
4336
4337 mutex_lock(&dev_priv->dpio_lock);
4338 /* adjust cdclk divider */
4339 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4340 val &= ~0xf;
4341 val |= divider;
4342 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4343 mutex_unlock(&dev_priv->dpio_lock);
4344 }
4345
4346 mutex_lock(&dev_priv->dpio_lock);
4347 /* adjust self-refresh exit latency value */
4348 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4349 val &= ~0x7f;
4350
4351 /*
4352 * For high bandwidth configs, we set a higher latency in the bunit
4353 * so that the core display fetch happens in time to avoid underruns.
4354 */
4355 if (cdclk == 400)
4356 val |= 4500 / 250; /* 4.5 usec */
4357 else
4358 val |= 3000 / 250; /* 3.0 usec */
4359 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4360 mutex_unlock(&dev_priv->dpio_lock);
4361
4362 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4363 intel_i2c_reset(dev);
4364}
4365
Imre Deakd60c4472014-03-27 17:45:10 +02004366int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004367{
4368 int cur_cdclk, vco;
4369 int divider;
4370
4371 vco = valleyview_get_vco(dev_priv);
4372
4373 mutex_lock(&dev_priv->dpio_lock);
4374 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4375 mutex_unlock(&dev_priv->dpio_lock);
4376
4377 divider &= 0xf;
4378
4379 cur_cdclk = (vco << 1) / (divider + 1);
4380
4381 return cur_cdclk;
4382}
4383
4384static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4385 int max_pixclk)
4386{
Jesse Barnes30a970c2013-11-04 13:48:12 -08004387 /*
4388 * Really only a few cases to deal with, as only 4 CDclks are supported:
4389 * 200MHz
4390 * 267MHz
4391 * 320MHz
4392 * 400MHz
4393 * So we check to see whether we're above 90% of the lower bin and
4394 * adjust if needed.
4395 */
4396 if (max_pixclk > 288000) {
4397 return 400;
4398 } else if (max_pixclk > 240000) {
4399 return 320;
4400 } else
4401 return 266;
4402 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4403}
4404
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004405/* compute the max pixel clock for new configuration */
4406static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004407{
4408 struct drm_device *dev = dev_priv->dev;
4409 struct intel_crtc *intel_crtc;
4410 int max_pixclk = 0;
4411
4412 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4413 base.head) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004414 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004415 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004416 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004417 }
4418
4419 return max_pixclk;
4420}
4421
4422static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004423 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004424{
4425 struct drm_i915_private *dev_priv = dev->dev_private;
4426 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004427 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004428
Imre Deakd60c4472014-03-27 17:45:10 +02004429 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4430 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004431 return;
4432
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004433 /* disable/enable all currently active pipes while we change cdclk */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004434 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4435 base.head)
4436 if (intel_crtc->base.enabled)
4437 *prepare_pipes |= (1 << intel_crtc->pipe);
4438}
4439
4440static void valleyview_modeset_global_resources(struct drm_device *dev)
4441{
4442 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004443 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004444 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4445
Imre Deakd60c4472014-03-27 17:45:10 +02004446 if (req_cdclk != dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004447 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak77961eb2014-03-05 16:20:56 +02004448 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004449}
4450
Jesse Barnes89b667f2013-04-18 14:51:36 -07004451static void valleyview_crtc_enable(struct drm_crtc *crtc)
4452{
4453 struct drm_device *dev = crtc->dev;
4454 struct drm_i915_private *dev_priv = dev->dev_private;
4455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4456 struct intel_encoder *encoder;
4457 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03004458 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004459
4460 WARN_ON(!crtc->enabled);
4461
4462 if (intel_crtc->active)
4463 return;
4464
4465 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004466
Jesse Barnes89b667f2013-04-18 14:51:36 -07004467 for_each_encoder_on_crtc(dev, crtc, encoder)
4468 if (encoder->pre_pll_enable)
4469 encoder->pre_pll_enable(encoder);
4470
Jani Nikula23538ef2013-08-27 15:12:22 +03004471 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4472
Jani Nikulae9fd1c02013-08-27 15:12:23 +03004473 if (!is_dsi)
4474 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004475
4476 for_each_encoder_on_crtc(dev, crtc, encoder)
4477 if (encoder->pre_enable)
4478 encoder->pre_enable(encoder);
4479
Jesse Barnes2dd24552013-04-25 12:55:01 -07004480 i9xx_pfit_enable(intel_crtc);
4481
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004482 intel_crtc_load_lut(crtc);
4483
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004484 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004485 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004486 intel_wait_for_vblank(dev_priv->dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004487 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004488
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004489 intel_crtc_enable_planes(crtc);
Jani Nikula50049452013-07-30 12:20:32 +03004490
4491 for_each_encoder_on_crtc(dev, crtc, encoder)
4492 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004493}
4494
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004495static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004496{
4497 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004498 struct drm_i915_private *dev_priv = dev->dev_private;
4499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004500 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004501 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004502
Daniel Vetter08a48462012-07-02 11:43:47 +02004503 WARN_ON(!crtc->enabled);
4504
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004505 if (intel_crtc->active)
4506 return;
4507
4508 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004509
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004510 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004511 if (encoder->pre_enable)
4512 encoder->pre_enable(encoder);
4513
Daniel Vetterf6736a12013-06-05 13:34:30 +02004514 i9xx_enable_pll(intel_crtc);
4515
Jesse Barnes2dd24552013-04-25 12:55:01 -07004516 i9xx_pfit_enable(intel_crtc);
4517
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004518 intel_crtc_load_lut(crtc);
4519
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004520 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004521 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004522 intel_wait_for_vblank(dev_priv->dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004523 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004524
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004525 intel_crtc_enable_planes(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004526
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004527 for_each_encoder_on_crtc(dev, crtc, encoder)
4528 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004529}
4530
Daniel Vetter87476d62013-04-11 16:29:06 +02004531static void i9xx_pfit_disable(struct intel_crtc *crtc)
4532{
4533 struct drm_device *dev = crtc->base.dev;
4534 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004535
4536 if (!crtc->config.gmch_pfit.control)
4537 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004538
4539 assert_pipe_disabled(dev_priv, crtc->pipe);
4540
Daniel Vetter328d8e82013-05-08 10:36:31 +02004541 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4542 I915_READ(PFIT_CONTROL));
4543 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004544}
4545
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004546static void i9xx_crtc_disable(struct drm_crtc *crtc)
4547{
4548 struct drm_device *dev = crtc->dev;
4549 struct drm_i915_private *dev_priv = dev->dev_private;
4550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004551 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004552 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004553
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004554 if (!intel_crtc->active)
4555 return;
4556
Daniel Vetterea9d7582012-07-10 10:42:52 +02004557 for_each_encoder_on_crtc(dev, crtc, encoder)
4558 encoder->disable(encoder);
4559
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004560 intel_crtc_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03004561
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004562 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08004563 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004564
Daniel Vetter87476d62013-04-11 16:29:06 +02004565 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004566
Jesse Barnes89b667f2013-04-18 14:51:36 -07004567 for_each_encoder_on_crtc(dev, crtc, encoder)
4568 if (encoder->post_disable)
4569 encoder->post_disable(encoder);
4570
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004571 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4572 if (IS_CHERRYVIEW(dev))
4573 chv_disable_pll(dev_priv, pipe);
4574 else if (IS_VALLEYVIEW(dev))
4575 vlv_disable_pll(dev_priv, pipe);
4576 else
4577 i9xx_disable_pll(dev_priv, pipe);
4578 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004579
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004580 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004581 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004582
Chris Wilson6b383a72010-09-13 13:54:26 +01004583 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004584}
4585
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004586static void i9xx_crtc_off(struct drm_crtc *crtc)
4587{
4588}
4589
Daniel Vetter976f8a22012-07-08 22:34:21 +02004590static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4591 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004592{
4593 struct drm_device *dev = crtc->dev;
4594 struct drm_i915_master_private *master_priv;
4595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004597
4598 if (!dev->primary->master)
4599 return;
4600
4601 master_priv = dev->primary->master->driver_priv;
4602 if (!master_priv->sarea_priv)
4603 return;
4604
Jesse Barnes79e53942008-11-07 14:24:08 -08004605 switch (pipe) {
4606 case 0:
4607 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4608 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4609 break;
4610 case 1:
4611 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4612 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4613 break;
4614 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004615 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004616 break;
4617 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004618}
4619
Daniel Vetter976f8a22012-07-08 22:34:21 +02004620/**
4621 * Sets the power management mode of the pipe and plane.
4622 */
4623void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004624{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004625 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004626 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004627 struct intel_encoder *intel_encoder;
4628 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004629
Daniel Vetter976f8a22012-07-08 22:34:21 +02004630 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4631 enable |= intel_encoder->connectors_active;
4632
4633 if (enable)
4634 dev_priv->display.crtc_enable(crtc);
4635 else
4636 dev_priv->display.crtc_disable(crtc);
4637
4638 intel_crtc_update_sarea(crtc, enable);
4639}
4640
Daniel Vetter976f8a22012-07-08 22:34:21 +02004641static void intel_crtc_disable(struct drm_crtc *crtc)
4642{
4643 struct drm_device *dev = crtc->dev;
4644 struct drm_connector *connector;
4645 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08004646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004647
4648 /* crtc should still be enabled when we disable it. */
4649 WARN_ON(!crtc->enabled);
4650
4651 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03004652 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004653 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004654 dev_priv->display.off(crtc);
4655
Chris Wilson931872f2012-01-16 23:01:13 +00004656 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004657 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004658 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004659
Matt Roperf4510a22014-04-01 15:22:40 -07004660 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01004661 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004662 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004663 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004664 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004665 }
4666
4667 /* Update computed state. */
4668 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4669 if (!connector->encoder || !connector->encoder->crtc)
4670 continue;
4671
4672 if (connector->encoder->crtc != crtc)
4673 continue;
4674
4675 connector->dpms = DRM_MODE_DPMS_OFF;
4676 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004677 }
4678}
4679
Chris Wilsonea5b2132010-08-04 13:50:23 +01004680void intel_encoder_destroy(struct drm_encoder *encoder)
4681{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004682 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004683
Chris Wilsonea5b2132010-08-04 13:50:23 +01004684 drm_encoder_cleanup(encoder);
4685 kfree(intel_encoder);
4686}
4687
Damien Lespiau92373292013-08-08 22:28:57 +01004688/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004689 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4690 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004691static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004692{
4693 if (mode == DRM_MODE_DPMS_ON) {
4694 encoder->connectors_active = true;
4695
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004696 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004697 } else {
4698 encoder->connectors_active = false;
4699
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004700 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004701 }
4702}
4703
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004704/* Cross check the actual hw state with our own modeset state tracking (and it's
4705 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004706static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004707{
4708 if (connector->get_hw_state(connector)) {
4709 struct intel_encoder *encoder = connector->encoder;
4710 struct drm_crtc *crtc;
4711 bool encoder_enabled;
4712 enum pipe pipe;
4713
4714 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4715 connector->base.base.id,
4716 drm_get_connector_name(&connector->base));
4717
4718 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4719 "wrong connector dpms state\n");
4720 WARN(connector->base.encoder != &encoder->base,
4721 "active connector not linked to encoder\n");
4722 WARN(!encoder->connectors_active,
4723 "encoder->connectors_active not set\n");
4724
4725 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4726 WARN(!encoder_enabled, "encoder not enabled\n");
4727 if (WARN_ON(!encoder->base.crtc))
4728 return;
4729
4730 crtc = encoder->base.crtc;
4731
4732 WARN(!crtc->enabled, "crtc not enabled\n");
4733 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4734 WARN(pipe != to_intel_crtc(crtc)->pipe,
4735 "encoder active on the wrong pipe\n");
4736 }
4737}
4738
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004739/* Even simpler default implementation, if there's really no special case to
4740 * consider. */
4741void intel_connector_dpms(struct drm_connector *connector, int mode)
4742{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004743 /* All the simple cases only support two dpms states. */
4744 if (mode != DRM_MODE_DPMS_ON)
4745 mode = DRM_MODE_DPMS_OFF;
4746
4747 if (mode == connector->dpms)
4748 return;
4749
4750 connector->dpms = mode;
4751
4752 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01004753 if (connector->encoder)
4754 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004755
Daniel Vetterb9805142012-08-31 17:37:33 +02004756 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004757}
4758
Daniel Vetterf0947c32012-07-02 13:10:34 +02004759/* Simple connector->get_hw_state implementation for encoders that support only
4760 * one connector and no cloning and hence the encoder state determines the state
4761 * of the connector. */
4762bool intel_connector_get_hw_state(struct intel_connector *connector)
4763{
Daniel Vetter24929352012-07-02 20:28:59 +02004764 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004765 struct intel_encoder *encoder = connector->encoder;
4766
4767 return encoder->get_hw_state(encoder, &pipe);
4768}
4769
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004770static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4771 struct intel_crtc_config *pipe_config)
4772{
4773 struct drm_i915_private *dev_priv = dev->dev_private;
4774 struct intel_crtc *pipe_B_crtc =
4775 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4776
4777 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4778 pipe_name(pipe), pipe_config->fdi_lanes);
4779 if (pipe_config->fdi_lanes > 4) {
4780 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4781 pipe_name(pipe), pipe_config->fdi_lanes);
4782 return false;
4783 }
4784
Paulo Zanonibafb6552013-11-02 21:07:44 -07004785 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004786 if (pipe_config->fdi_lanes > 2) {
4787 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4788 pipe_config->fdi_lanes);
4789 return false;
4790 } else {
4791 return true;
4792 }
4793 }
4794
4795 if (INTEL_INFO(dev)->num_pipes == 2)
4796 return true;
4797
4798 /* Ivybridge 3 pipe is really complicated */
4799 switch (pipe) {
4800 case PIPE_A:
4801 return true;
4802 case PIPE_B:
4803 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4804 pipe_config->fdi_lanes > 2) {
4805 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4806 pipe_name(pipe), pipe_config->fdi_lanes);
4807 return false;
4808 }
4809 return true;
4810 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004811 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004812 pipe_B_crtc->config.fdi_lanes <= 2) {
4813 if (pipe_config->fdi_lanes > 2) {
4814 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4815 pipe_name(pipe), pipe_config->fdi_lanes);
4816 return false;
4817 }
4818 } else {
4819 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4820 return false;
4821 }
4822 return true;
4823 default:
4824 BUG();
4825 }
4826}
4827
Daniel Vettere29c22c2013-02-21 00:00:16 +01004828#define RETRY 1
4829static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4830 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004831{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004832 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004833 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004834 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004835 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004836
Daniel Vettere29c22c2013-02-21 00:00:16 +01004837retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004838 /* FDI is a binary signal running at ~2.7GHz, encoding
4839 * each output octet as 10 bits. The actual frequency
4840 * is stored as a divider into a 100MHz clock, and the
4841 * mode pixel clock is stored in units of 1KHz.
4842 * Hence the bw of each lane in terms of the mode signal
4843 * is:
4844 */
4845 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4846
Damien Lespiau241bfc32013-09-25 16:45:37 +01004847 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004848
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004849 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004850 pipe_config->pipe_bpp);
4851
4852 pipe_config->fdi_lanes = lane;
4853
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004854 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004855 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004856
Daniel Vettere29c22c2013-02-21 00:00:16 +01004857 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4858 intel_crtc->pipe, pipe_config);
4859 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4860 pipe_config->pipe_bpp -= 2*3;
4861 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4862 pipe_config->pipe_bpp);
4863 needs_recompute = true;
4864 pipe_config->bw_constrained = true;
4865
4866 goto retry;
4867 }
4868
4869 if (needs_recompute)
4870 return RETRY;
4871
4872 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004873}
4874
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004875static void hsw_compute_ips_config(struct intel_crtc *crtc,
4876 struct intel_crtc_config *pipe_config)
4877{
Jani Nikulad330a952014-01-21 11:24:25 +02004878 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004879 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07004880 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004881}
4882
Daniel Vettera43f6e02013-06-07 23:10:32 +02004883static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004884 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004885{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004886 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004887 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004888
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004889 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004890 if (INTEL_INFO(dev)->gen < 4) {
4891 struct drm_i915_private *dev_priv = dev->dev_private;
4892 int clock_limit =
4893 dev_priv->display.get_display_clock_speed(dev);
4894
4895 /*
4896 * Enable pixel doubling when the dot clock
4897 * is > 90% of the (display) core speed.
4898 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03004899 * GDG double wide on either pipe,
4900 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004901 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03004902 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01004903 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004904 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03004905 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03004906 }
4907
Damien Lespiau241bfc32013-09-25 16:45:37 +01004908 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004909 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004910 }
Chris Wilson89749352010-09-12 18:25:19 +01004911
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03004912 /*
4913 * Pipe horizontal size must be even in:
4914 * - DVO ganged mode
4915 * - LVDS dual channel mode
4916 * - Double wide pipe
4917 */
4918 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4919 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4920 pipe_config->pipe_src_w &= ~1;
4921
Damien Lespiau8693a822013-05-03 18:48:11 +01004922 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4923 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004924 */
4925 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4926 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004927 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004928
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004929 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004930 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004931 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004932 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4933 * for lvds. */
4934 pipe_config->pipe_bpp = 8*3;
4935 }
4936
Damien Lespiauf5adf942013-06-24 18:29:34 +01004937 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004938 hsw_compute_ips_config(crtc, pipe_config);
4939
4940 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4941 * clock survives for now. */
4942 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4943 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004944
Daniel Vetter877d48d2013-04-19 11:24:43 +02004945 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004946 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004947
Daniel Vettere29c22c2013-02-21 00:00:16 +01004948 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004949}
4950
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004951static int valleyview_get_display_clock_speed(struct drm_device *dev)
4952{
4953 return 400000; /* FIXME */
4954}
4955
Jesse Barnese70236a2009-09-21 10:42:27 -07004956static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004957{
Jesse Barnese70236a2009-09-21 10:42:27 -07004958 return 400000;
4959}
Jesse Barnes79e53942008-11-07 14:24:08 -08004960
Jesse Barnese70236a2009-09-21 10:42:27 -07004961static int i915_get_display_clock_speed(struct drm_device *dev)
4962{
4963 return 333000;
4964}
Jesse Barnes79e53942008-11-07 14:24:08 -08004965
Jesse Barnese70236a2009-09-21 10:42:27 -07004966static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4967{
4968 return 200000;
4969}
Jesse Barnes79e53942008-11-07 14:24:08 -08004970
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004971static int pnv_get_display_clock_speed(struct drm_device *dev)
4972{
4973 u16 gcfgc = 0;
4974
4975 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4976
4977 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4978 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4979 return 267000;
4980 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4981 return 333000;
4982 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4983 return 444000;
4984 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4985 return 200000;
4986 default:
4987 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4988 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4989 return 133000;
4990 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4991 return 167000;
4992 }
4993}
4994
Jesse Barnese70236a2009-09-21 10:42:27 -07004995static int i915gm_get_display_clock_speed(struct drm_device *dev)
4996{
4997 u16 gcfgc = 0;
4998
4999 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5000
5001 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005002 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005003 else {
5004 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5005 case GC_DISPLAY_CLOCK_333_MHZ:
5006 return 333000;
5007 default:
5008 case GC_DISPLAY_CLOCK_190_200_MHZ:
5009 return 190000;
5010 }
5011 }
5012}
Jesse Barnes79e53942008-11-07 14:24:08 -08005013
Jesse Barnese70236a2009-09-21 10:42:27 -07005014static int i865_get_display_clock_speed(struct drm_device *dev)
5015{
5016 return 266000;
5017}
5018
5019static int i855_get_display_clock_speed(struct drm_device *dev)
5020{
5021 u16 hpllcc = 0;
5022 /* Assume that the hardware is in the high speed state. This
5023 * should be the default.
5024 */
5025 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5026 case GC_CLOCK_133_200:
5027 case GC_CLOCK_100_200:
5028 return 200000;
5029 case GC_CLOCK_166_250:
5030 return 250000;
5031 case GC_CLOCK_100_133:
5032 return 133000;
5033 }
5034
5035 /* Shouldn't happen */
5036 return 0;
5037}
5038
5039static int i830_get_display_clock_speed(struct drm_device *dev)
5040{
5041 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005042}
5043
Zhenyu Wang2c072452009-06-05 15:38:42 +08005044static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005045intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005046{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005047 while (*num > DATA_LINK_M_N_MASK ||
5048 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005049 *num >>= 1;
5050 *den >>= 1;
5051 }
5052}
5053
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005054static void compute_m_n(unsigned int m, unsigned int n,
5055 uint32_t *ret_m, uint32_t *ret_n)
5056{
5057 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5058 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5059 intel_reduce_m_n_ratio(ret_m, ret_n);
5060}
5061
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005062void
5063intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5064 int pixel_clock, int link_clock,
5065 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005066{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005067 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005068
5069 compute_m_n(bits_per_pixel * pixel_clock,
5070 link_clock * nlanes * 8,
5071 &m_n->gmch_m, &m_n->gmch_n);
5072
5073 compute_m_n(pixel_clock, link_clock,
5074 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005075}
5076
Chris Wilsona7615032011-01-12 17:04:08 +00005077static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5078{
Jani Nikulad330a952014-01-21 11:24:25 +02005079 if (i915.panel_use_ssc >= 0)
5080 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005081 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005082 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005083}
5084
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005085static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5086{
5087 struct drm_device *dev = crtc->dev;
5088 struct drm_i915_private *dev_priv = dev->dev_private;
5089 int refclk;
5090
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005091 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005092 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005093 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005094 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005095 refclk = dev_priv->vbt.lvds_ssc_freq;
5096 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005097 } else if (!IS_GEN2(dev)) {
5098 refclk = 96000;
5099 } else {
5100 refclk = 48000;
5101 }
5102
5103 return refclk;
5104}
5105
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005106static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005107{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005108 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005109}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005110
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005111static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5112{
5113 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005114}
5115
Daniel Vetterf47709a2013-03-28 10:42:02 +01005116static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005117 intel_clock_t *reduced_clock)
5118{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005119 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005120 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005121 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005122 u32 fp, fp2 = 0;
5123
5124 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005125 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005126 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005127 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005128 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005129 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005130 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005131 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005132 }
5133
5134 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005135 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005136
Daniel Vetterf47709a2013-03-28 10:42:02 +01005137 crtc->lowfreq_avail = false;
5138 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005139 reduced_clock && i915.powersave) {
Jesse Barnesa7516a02011-12-15 12:30:37 -08005140 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005141 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005142 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005143 } else {
5144 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005145 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005146 }
5147}
5148
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005149static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5150 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005151{
5152 u32 reg_val;
5153
5154 /*
5155 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5156 * and set it to a reasonable value instead.
5157 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005158 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005159 reg_val &= 0xffffff00;
5160 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005161 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005162
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005163 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005164 reg_val &= 0x8cffffff;
5165 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005166 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005167
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005168 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005169 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005170 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005171
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005172 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005173 reg_val &= 0x00ffffff;
5174 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005175 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005176}
5177
Daniel Vetterb5518422013-05-03 11:49:48 +02005178static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5179 struct intel_link_m_n *m_n)
5180{
5181 struct drm_device *dev = crtc->base.dev;
5182 struct drm_i915_private *dev_priv = dev->dev_private;
5183 int pipe = crtc->pipe;
5184
Daniel Vettere3b95f12013-05-03 11:49:49 +02005185 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5186 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5187 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5188 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005189}
5190
5191static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5192 struct intel_link_m_n *m_n)
5193{
5194 struct drm_device *dev = crtc->base.dev;
5195 struct drm_i915_private *dev_priv = dev->dev_private;
5196 int pipe = crtc->pipe;
5197 enum transcoder transcoder = crtc->config.cpu_transcoder;
5198
5199 if (INTEL_INFO(dev)->gen >= 5) {
5200 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5201 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5202 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5203 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5204 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005205 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5206 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5207 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5208 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005209 }
5210}
5211
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005212static void intel_dp_set_m_n(struct intel_crtc *crtc)
5213{
5214 if (crtc->config.has_pch_encoder)
5215 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5216 else
5217 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5218}
5219
Daniel Vetterf47709a2013-03-28 10:42:02 +01005220static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005221{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005222 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005223 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005224 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005225 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005226 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005227 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005228
Daniel Vetter09153002012-12-12 14:06:44 +01005229 mutex_lock(&dev_priv->dpio_lock);
5230
Daniel Vetterf47709a2013-03-28 10:42:02 +01005231 bestn = crtc->config.dpll.n;
5232 bestm1 = crtc->config.dpll.m1;
5233 bestm2 = crtc->config.dpll.m2;
5234 bestp1 = crtc->config.dpll.p1;
5235 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005236
Jesse Barnes89b667f2013-04-18 14:51:36 -07005237 /* See eDP HDMI DPIO driver vbios notes doc */
5238
5239 /* PLL B needs special handling */
5240 if (pipe)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005241 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005242
5243 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005244 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005245
5246 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005247 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005248 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005249 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005250
5251 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005252 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005253
5254 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005255 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5256 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5257 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005258 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005259
5260 /*
5261 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5262 * but we don't support that).
5263 * Note: don't use the DAC post divider as it seems unstable.
5264 */
5265 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005267
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005268 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005270
Jesse Barnes89b667f2013-04-18 14:51:36 -07005271 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005272 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005273 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005274 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005275 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005276 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005277 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005278 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005279 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005280
Jesse Barnes89b667f2013-04-18 14:51:36 -07005281 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5282 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5283 /* Use SSC source */
5284 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005286 0x0df40000);
5287 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005288 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005289 0x0df70000);
5290 } else { /* HDMI or VGA */
5291 /* Use bend source */
5292 if (!pipe)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005293 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005294 0x0df70000);
5295 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005296 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005297 0x0df40000);
5298 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005299
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005300 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005301 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5302 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5303 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5304 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005305 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005306
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005308
Imre Deake5cbfbf2014-01-09 17:08:16 +02005309 /*
5310 * Enable DPIO clock input. We should never disable the reference
5311 * clock for pipe B, since VGA hotplug / manual detection depends
5312 * on it.
5313 */
Jesse Barnes89b667f2013-04-18 14:51:36 -07005314 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5315 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07005316 /* We should never disable this, set it here for state tracking */
5317 if (pipe == PIPE_B)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005318 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005319 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005320 crtc->config.dpll_hw_state.dpll = dpll;
5321
Daniel Vetteref1b4602013-06-01 17:17:04 +02005322 dpll_md = (crtc->config.pixel_multiplier - 1)
5323 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005324 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5325
Daniel Vetter09153002012-12-12 14:06:44 +01005326 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005327}
5328
Daniel Vetterf47709a2013-03-28 10:42:02 +01005329static void i9xx_update_pll(struct intel_crtc *crtc,
5330 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005331 int num_connectors)
5332{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005333 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005334 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005335 u32 dpll;
5336 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005337 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005338
Daniel Vetterf47709a2013-03-28 10:42:02 +01005339 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305340
Daniel Vetterf47709a2013-03-28 10:42:02 +01005341 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5342 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005343
5344 dpll = DPLL_VGA_MODE_DIS;
5345
Daniel Vetterf47709a2013-03-28 10:42:02 +01005346 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005347 dpll |= DPLLB_MODE_LVDS;
5348 else
5349 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005350
Daniel Vetteref1b4602013-06-01 17:17:04 +02005351 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005352 dpll |= (crtc->config.pixel_multiplier - 1)
5353 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005354 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005355
5356 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005357 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005358
Daniel Vetterf47709a2013-03-28 10:42:02 +01005359 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005360 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005361
5362 /* compute bitmask from p1 value */
5363 if (IS_PINEVIEW(dev))
5364 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5365 else {
5366 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5367 if (IS_G4X(dev) && reduced_clock)
5368 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5369 }
5370 switch (clock->p2) {
5371 case 5:
5372 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5373 break;
5374 case 7:
5375 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5376 break;
5377 case 10:
5378 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5379 break;
5380 case 14:
5381 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5382 break;
5383 }
5384 if (INTEL_INFO(dev)->gen >= 4)
5385 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5386
Daniel Vetter09ede542013-04-30 14:01:45 +02005387 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005388 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005389 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005390 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5391 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5392 else
5393 dpll |= PLL_REF_INPUT_DREFCLK;
5394
5395 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005396 crtc->config.dpll_hw_state.dpll = dpll;
5397
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005398 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005399 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5400 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005401 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005402 }
5403}
5404
Daniel Vetterf47709a2013-03-28 10:42:02 +01005405static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005406 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005407 int num_connectors)
5408{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005409 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005410 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005411 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005412 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005413
Daniel Vetterf47709a2013-03-28 10:42:02 +01005414 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305415
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005416 dpll = DPLL_VGA_MODE_DIS;
5417
Daniel Vetterf47709a2013-03-28 10:42:02 +01005418 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005419 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5420 } else {
5421 if (clock->p1 == 2)
5422 dpll |= PLL_P1_DIVIDE_BY_TWO;
5423 else
5424 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5425 if (clock->p2 == 4)
5426 dpll |= PLL_P2_DIVIDE_BY_4;
5427 }
5428
Daniel Vetter4a33e482013-07-06 12:52:05 +02005429 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5430 dpll |= DPLL_DVO_2X_MODE;
5431
Daniel Vetterf47709a2013-03-28 10:42:02 +01005432 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005433 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5434 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5435 else
5436 dpll |= PLL_REF_INPUT_DREFCLK;
5437
5438 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005439 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005440}
5441
Daniel Vetter8a654f32013-06-01 17:16:22 +02005442static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005443{
5444 struct drm_device *dev = intel_crtc->base.dev;
5445 struct drm_i915_private *dev_priv = dev->dev_private;
5446 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005447 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005448 struct drm_display_mode *adjusted_mode =
5449 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005450 uint32_t crtc_vtotal, crtc_vblank_end;
5451 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005452
5453 /* We need to be careful not to changed the adjusted mode, for otherwise
5454 * the hw state checker will get angry at the mismatch. */
5455 crtc_vtotal = adjusted_mode->crtc_vtotal;
5456 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005457
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005458 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005459 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005460 crtc_vtotal -= 1;
5461 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005462
5463 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5464 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5465 else
5466 vsyncshift = adjusted_mode->crtc_hsync_start -
5467 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005468 if (vsyncshift < 0)
5469 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005470 }
5471
5472 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005473 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005474
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005475 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005476 (adjusted_mode->crtc_hdisplay - 1) |
5477 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005478 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005479 (adjusted_mode->crtc_hblank_start - 1) |
5480 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005481 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005482 (adjusted_mode->crtc_hsync_start - 1) |
5483 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5484
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005485 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005486 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005487 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005488 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005489 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005490 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005491 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005492 (adjusted_mode->crtc_vsync_start - 1) |
5493 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5494
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005495 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5496 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5497 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5498 * bits. */
5499 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5500 (pipe == PIPE_B || pipe == PIPE_C))
5501 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5502
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005503 /* pipesrc controls the size that is scaled from, which should
5504 * always be the user's requested size.
5505 */
5506 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005507 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5508 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005509}
5510
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005511static void intel_get_pipe_timings(struct intel_crtc *crtc,
5512 struct intel_crtc_config *pipe_config)
5513{
5514 struct drm_device *dev = crtc->base.dev;
5515 struct drm_i915_private *dev_priv = dev->dev_private;
5516 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5517 uint32_t tmp;
5518
5519 tmp = I915_READ(HTOTAL(cpu_transcoder));
5520 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5521 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5522 tmp = I915_READ(HBLANK(cpu_transcoder));
5523 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5524 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5525 tmp = I915_READ(HSYNC(cpu_transcoder));
5526 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5527 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5528
5529 tmp = I915_READ(VTOTAL(cpu_transcoder));
5530 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5531 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5532 tmp = I915_READ(VBLANK(cpu_transcoder));
5533 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5534 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5535 tmp = I915_READ(VSYNC(cpu_transcoder));
5536 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5537 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5538
5539 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5540 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5541 pipe_config->adjusted_mode.crtc_vtotal += 1;
5542 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5543 }
5544
5545 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005546 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5547 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5548
5549 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5550 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005551}
5552
Daniel Vetterf6a83282014-02-11 15:28:57 -08005553void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5554 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03005555{
Daniel Vetterf6a83282014-02-11 15:28:57 -08005556 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5557 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5558 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5559 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005560
Daniel Vetterf6a83282014-02-11 15:28:57 -08005561 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5562 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5563 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5564 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005565
Daniel Vetterf6a83282014-02-11 15:28:57 -08005566 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005567
Daniel Vetterf6a83282014-02-11 15:28:57 -08005568 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5569 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005570}
5571
Daniel Vetter84b046f2013-02-19 18:48:54 +01005572static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5573{
5574 struct drm_device *dev = intel_crtc->base.dev;
5575 struct drm_i915_private *dev_priv = dev->dev_private;
5576 uint32_t pipeconf;
5577
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005578 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005579
Daniel Vetter67c72a12013-09-24 11:46:14 +02005580 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5581 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5582 pipeconf |= PIPECONF_ENABLE;
5583
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005584 if (intel_crtc->config.double_wide)
5585 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005586
Daniel Vetterff9ce462013-04-24 14:57:17 +02005587 /* only g4x and later have fancy bpc/dither controls */
5588 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005589 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5590 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5591 pipeconf |= PIPECONF_DITHER_EN |
5592 PIPECONF_DITHER_TYPE_SP;
5593
5594 switch (intel_crtc->config.pipe_bpp) {
5595 case 18:
5596 pipeconf |= PIPECONF_6BPC;
5597 break;
5598 case 24:
5599 pipeconf |= PIPECONF_8BPC;
5600 break;
5601 case 30:
5602 pipeconf |= PIPECONF_10BPC;
5603 break;
5604 default:
5605 /* Case prevented by intel_choose_pipe_bpp_dither. */
5606 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005607 }
5608 }
5609
5610 if (HAS_PIPE_CXSR(dev)) {
5611 if (intel_crtc->lowfreq_avail) {
5612 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5613 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5614 } else {
5615 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005616 }
5617 }
5618
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02005619 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5620 if (INTEL_INFO(dev)->gen < 4 ||
5621 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5622 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5623 else
5624 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5625 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01005626 pipeconf |= PIPECONF_PROGRESSIVE;
5627
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005628 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5629 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005630
Daniel Vetter84b046f2013-02-19 18:48:54 +01005631 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5632 POSTING_READ(PIPECONF(intel_crtc->pipe));
5633}
5634
Eric Anholtf564048e2011-03-30 13:01:02 -07005635static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005636 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005637 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005638{
5639 struct drm_device *dev = crtc->dev;
5640 struct drm_i915_private *dev_priv = dev->dev_private;
5641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5642 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005643 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005644 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005645 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005646 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02005647 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005648 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005649 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005650 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005651 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005652
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005653 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005654 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005655 case INTEL_OUTPUT_LVDS:
5656 is_lvds = true;
5657 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005658 case INTEL_OUTPUT_DSI:
5659 is_dsi = true;
5660 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005661 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005662
Eric Anholtc751ce42010-03-25 11:48:48 -07005663 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005664 }
5665
Jani Nikulaf2335332013-09-13 11:03:09 +03005666 if (is_dsi)
5667 goto skip_dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08005668
Jani Nikulaf2335332013-09-13 11:03:09 +03005669 if (!intel_crtc->config.clock_set) {
5670 refclk = i9xx_get_refclk(crtc, num_connectors);
5671
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005672 /*
5673 * Returns a set of divisors for the desired target clock with
5674 * the given refclk, or FALSE. The returned values represent
5675 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5676 * 2) / p1 / p2.
5677 */
5678 limit = intel_limit(crtc, refclk);
5679 ok = dev_priv->display.find_dpll(limit, crtc,
5680 intel_crtc->config.port_clock,
5681 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005682 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005683 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5684 return -EINVAL;
5685 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005686
Jani Nikulaf2335332013-09-13 11:03:09 +03005687 if (is_lvds && dev_priv->lvds_downclock_avail) {
5688 /*
5689 * Ensure we match the reduced clock's P to the target
5690 * clock. If the clocks don't match, we can't switch
5691 * the display clock by using the FP0/FP1. In such case
5692 * we will disable the LVDS downclock feature.
5693 */
5694 has_reduced_clock =
5695 dev_priv->display.find_dpll(limit, crtc,
5696 dev_priv->lvds_downclock,
5697 refclk, &clock,
5698 &reduced_clock);
5699 }
5700 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01005701 intel_crtc->config.dpll.n = clock.n;
5702 intel_crtc->config.dpll.m1 = clock.m1;
5703 intel_crtc->config.dpll.m2 = clock.m2;
5704 intel_crtc->config.dpll.p1 = clock.p1;
5705 intel_crtc->config.dpll.p2 = clock.p2;
5706 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005707
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005708 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02005709 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305710 has_reduced_clock ? &reduced_clock : NULL,
5711 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005712 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03005713 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005714 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01005715 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005716 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07005717 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005718 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005719
Jani Nikulaf2335332013-09-13 11:03:09 +03005720skip_dpll:
Eric Anholtf564048e2011-03-30 13:01:02 -07005721 /* Set up the display plane register */
5722 dspcntr = DISPPLANE_GAMMA_ENABLE;
5723
Jesse Barnesda6ecc52013-03-08 10:46:00 -08005724 if (!IS_VALLEYVIEW(dev)) {
5725 if (pipe == 0)
5726 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5727 else
5728 dspcntr |= DISPPLANE_SEL_PIPE_B;
5729 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005730
Ville Syrjälä2070f002014-03-31 18:21:25 +03005731 if (intel_crtc->config.has_dp_encoder)
5732 intel_dp_set_m_n(intel_crtc);
5733
Daniel Vetter8a654f32013-06-01 17:16:22 +02005734 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07005735
5736 /* pipesrc and dspsize control the size that is scaled from,
5737 * which should always be the user's requested size.
5738 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005739 I915_WRITE(DSPSIZE(plane),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005740 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5741 (intel_crtc->config.pipe_src_w - 1));
Eric Anholt929c77f2011-03-30 13:01:04 -07005742 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005743
Daniel Vetter84b046f2013-02-19 18:48:54 +01005744 i9xx_set_pipeconf(intel_crtc);
5745
Eric Anholtf564048e2011-03-30 13:01:02 -07005746 I915_WRITE(DSPCNTR(plane), dspcntr);
5747 POSTING_READ(DSPCNTR(plane));
5748
Daniel Vetter94352cf2012-07-05 22:51:56 +02005749 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07005750
Eric Anholtf564048e2011-03-30 13:01:02 -07005751 return ret;
5752}
5753
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005754static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5755 struct intel_crtc_config *pipe_config)
5756{
5757 struct drm_device *dev = crtc->base.dev;
5758 struct drm_i915_private *dev_priv = dev->dev_private;
5759 uint32_t tmp;
5760
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02005761 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
5762 return;
5763
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005764 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02005765 if (!(tmp & PFIT_ENABLE))
5766 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005767
Daniel Vetter06922822013-07-11 13:35:40 +02005768 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005769 if (INTEL_INFO(dev)->gen < 4) {
5770 if (crtc->pipe != PIPE_B)
5771 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005772 } else {
5773 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5774 return;
5775 }
5776
Daniel Vetter06922822013-07-11 13:35:40 +02005777 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005778 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5779 if (INTEL_INFO(dev)->gen < 5)
5780 pipe_config->gmch_pfit.lvds_border_bits =
5781 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5782}
5783
Jesse Barnesacbec812013-09-20 11:29:32 -07005784static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5785 struct intel_crtc_config *pipe_config)
5786{
5787 struct drm_device *dev = crtc->base.dev;
5788 struct drm_i915_private *dev_priv = dev->dev_private;
5789 int pipe = pipe_config->cpu_transcoder;
5790 intel_clock_t clock;
5791 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07005792 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07005793
5794 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005795 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07005796 mutex_unlock(&dev_priv->dpio_lock);
5797
5798 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5799 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5800 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5801 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5802 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5803
Ville Syrjäläf6466282013-10-14 14:50:31 +03005804 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07005805
Ville Syrjäläf6466282013-10-14 14:50:31 +03005806 /* clock.dot is the fast clock */
5807 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07005808}
5809
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005810static void i9xx_get_plane_config(struct intel_crtc *crtc,
5811 struct intel_plane_config *plane_config)
5812{
5813 struct drm_device *dev = crtc->base.dev;
5814 struct drm_i915_private *dev_priv = dev->dev_private;
5815 u32 val, base, offset;
5816 int pipe = crtc->pipe, plane = crtc->plane;
5817 int fourcc, pixel_format;
5818 int aligned_height;
5819
Dave Airlie66e514c2014-04-03 07:51:54 +10005820 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
5821 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005822 DRM_DEBUG_KMS("failed to alloc fb\n");
5823 return;
5824 }
5825
5826 val = I915_READ(DSPCNTR(plane));
5827
5828 if (INTEL_INFO(dev)->gen >= 4)
5829 if (val & DISPPLANE_TILED)
5830 plane_config->tiled = true;
5831
5832 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
5833 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10005834 crtc->base.primary->fb->pixel_format = fourcc;
5835 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005836 drm_format_plane_cpp(fourcc, 0) * 8;
5837
5838 if (INTEL_INFO(dev)->gen >= 4) {
5839 if (plane_config->tiled)
5840 offset = I915_READ(DSPTILEOFF(plane));
5841 else
5842 offset = I915_READ(DSPLINOFF(plane));
5843 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
5844 } else {
5845 base = I915_READ(DSPADDR(plane));
5846 }
5847 plane_config->base = base;
5848
5849 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10005850 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
5851 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005852
5853 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10005854 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005855
Dave Airlie66e514c2014-04-03 07:51:54 +10005856 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005857 plane_config->tiled);
5858
Dave Airlie66e514c2014-04-03 07:51:54 +10005859 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005860 aligned_height, PAGE_SIZE);
5861
5862 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10005863 pipe, plane, crtc->base.primary->fb->width,
5864 crtc->base.primary->fb->height,
5865 crtc->base.primary->fb->bits_per_pixel, base,
5866 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08005867 plane_config->size);
5868
5869}
5870
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005871static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5872 struct intel_crtc_config *pipe_config)
5873{
5874 struct drm_device *dev = crtc->base.dev;
5875 struct drm_i915_private *dev_priv = dev->dev_private;
5876 uint32_t tmp;
5877
Imre Deakb5482bd2014-03-05 16:20:55 +02005878 if (!intel_display_power_enabled(dev_priv,
5879 POWER_DOMAIN_PIPE(crtc->pipe)))
5880 return false;
5881
Daniel Vettere143a212013-07-04 12:01:15 +02005882 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005883 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005884
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005885 tmp = I915_READ(PIPECONF(crtc->pipe));
5886 if (!(tmp & PIPECONF_ENABLE))
5887 return false;
5888
Ville Syrjälä42571ae2013-09-06 23:29:00 +03005889 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5890 switch (tmp & PIPECONF_BPC_MASK) {
5891 case PIPECONF_6BPC:
5892 pipe_config->pipe_bpp = 18;
5893 break;
5894 case PIPECONF_8BPC:
5895 pipe_config->pipe_bpp = 24;
5896 break;
5897 case PIPECONF_10BPC:
5898 pipe_config->pipe_bpp = 30;
5899 break;
5900 default:
5901 break;
5902 }
5903 }
5904
Ville Syrjälä282740f2013-09-04 18:30:03 +03005905 if (INTEL_INFO(dev)->gen < 4)
5906 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5907
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005908 intel_get_pipe_timings(crtc, pipe_config);
5909
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005910 i9xx_get_pfit_config(crtc, pipe_config);
5911
Daniel Vetter6c49f242013-06-06 12:45:25 +02005912 if (INTEL_INFO(dev)->gen >= 4) {
5913 tmp = I915_READ(DPLL_MD(crtc->pipe));
5914 pipe_config->pixel_multiplier =
5915 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5916 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005917 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005918 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5919 tmp = I915_READ(DPLL(crtc->pipe));
5920 pipe_config->pixel_multiplier =
5921 ((tmp & SDVO_MULTIPLIER_MASK)
5922 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5923 } else {
5924 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5925 * port and will be fixed up in the encoder->get_config
5926 * function. */
5927 pipe_config->pixel_multiplier = 1;
5928 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005929 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5930 if (!IS_VALLEYVIEW(dev)) {
5931 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5932 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005933 } else {
5934 /* Mask out read-only status bits. */
5935 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5936 DPLL_PORTC_READY_MASK |
5937 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005938 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005939
Jesse Barnesacbec812013-09-20 11:29:32 -07005940 if (IS_VALLEYVIEW(dev))
5941 vlv_crtc_clock_get(crtc, pipe_config);
5942 else
5943 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03005944
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005945 return true;
5946}
5947
Paulo Zanonidde86e22012-12-01 12:04:25 -02005948static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005949{
5950 struct drm_i915_private *dev_priv = dev->dev_private;
5951 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005952 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005953 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005954 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005955 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005956 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005957 bool has_ck505 = false;
5958 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005959
5960 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005961 list_for_each_entry(encoder, &mode_config->encoder_list,
5962 base.head) {
5963 switch (encoder->type) {
5964 case INTEL_OUTPUT_LVDS:
5965 has_panel = true;
5966 has_lvds = true;
5967 break;
5968 case INTEL_OUTPUT_EDP:
5969 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005970 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005971 has_cpu_edp = true;
5972 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005973 }
5974 }
5975
Keith Packard99eb6a02011-09-26 14:29:12 -07005976 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005977 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005978 can_ssc = has_ck505;
5979 } else {
5980 has_ck505 = false;
5981 can_ssc = true;
5982 }
5983
Imre Deak2de69052013-05-08 13:14:04 +03005984 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5985 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005986
5987 /* Ironlake: try to setup display ref clock before DPLL
5988 * enabling. This is only under driver's control after
5989 * PCH B stepping, previous chipset stepping should be
5990 * ignoring this setting.
5991 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005992 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005993
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005994 /* As we must carefully and slowly disable/enable each source in turn,
5995 * compute the final state we want first and check if we need to
5996 * make any changes at all.
5997 */
5998 final = val;
5999 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006000 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006001 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006002 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006003 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6004
6005 final &= ~DREF_SSC_SOURCE_MASK;
6006 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6007 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006008
Keith Packard199e5d72011-09-22 12:01:57 -07006009 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006010 final |= DREF_SSC_SOURCE_ENABLE;
6011
6012 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6013 final |= DREF_SSC1_ENABLE;
6014
6015 if (has_cpu_edp) {
6016 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6017 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6018 else
6019 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6020 } else
6021 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6022 } else {
6023 final |= DREF_SSC_SOURCE_DISABLE;
6024 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6025 }
6026
6027 if (final == val)
6028 return;
6029
6030 /* Always enable nonspread source */
6031 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6032
6033 if (has_ck505)
6034 val |= DREF_NONSPREAD_CK505_ENABLE;
6035 else
6036 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6037
6038 if (has_panel) {
6039 val &= ~DREF_SSC_SOURCE_MASK;
6040 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006041
Keith Packard199e5d72011-09-22 12:01:57 -07006042 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006043 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006044 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006045 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006046 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006047 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006048
6049 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006050 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006051 POSTING_READ(PCH_DREF_CONTROL);
6052 udelay(200);
6053
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006054 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006055
6056 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006057 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006058 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006059 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006060 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006061 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07006062 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006063 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006064 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006065 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006066
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006067 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006068 POSTING_READ(PCH_DREF_CONTROL);
6069 udelay(200);
6070 } else {
6071 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6072
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006073 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006074
6075 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006076 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006077
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006078 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006079 POSTING_READ(PCH_DREF_CONTROL);
6080 udelay(200);
6081
6082 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006083 val &= ~DREF_SSC_SOURCE_MASK;
6084 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006085
6086 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006087 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006088
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006089 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006090 POSTING_READ(PCH_DREF_CONTROL);
6091 udelay(200);
6092 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006093
6094 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006095}
6096
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006097static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006098{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006099 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006100
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006101 tmp = I915_READ(SOUTH_CHICKEN2);
6102 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6103 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006104
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006105 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6106 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6107 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006108
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006109 tmp = I915_READ(SOUTH_CHICKEN2);
6110 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6111 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006112
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006113 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6114 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6115 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006116}
6117
6118/* WaMPhyProgramming:hsw */
6119static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6120{
6121 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006122
6123 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6124 tmp &= ~(0xFF << 24);
6125 tmp |= (0x12 << 24);
6126 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6127
Paulo Zanonidde86e22012-12-01 12:04:25 -02006128 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6129 tmp |= (1 << 11);
6130 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6131
6132 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6133 tmp |= (1 << 11);
6134 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6135
Paulo Zanonidde86e22012-12-01 12:04:25 -02006136 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6137 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6138 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6139
6140 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6141 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6142 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6143
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006144 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6145 tmp &= ~(7 << 13);
6146 tmp |= (5 << 13);
6147 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006148
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006149 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6150 tmp &= ~(7 << 13);
6151 tmp |= (5 << 13);
6152 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006153
6154 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6155 tmp &= ~0xFF;
6156 tmp |= 0x1C;
6157 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6158
6159 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6160 tmp &= ~0xFF;
6161 tmp |= 0x1C;
6162 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6163
6164 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6165 tmp &= ~(0xFF << 16);
6166 tmp |= (0x1C << 16);
6167 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6168
6169 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6170 tmp &= ~(0xFF << 16);
6171 tmp |= (0x1C << 16);
6172 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6173
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006174 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6175 tmp |= (1 << 27);
6176 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006177
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006178 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6179 tmp |= (1 << 27);
6180 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006181
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006182 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6183 tmp &= ~(0xF << 28);
6184 tmp |= (4 << 28);
6185 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006186
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006187 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6188 tmp &= ~(0xF << 28);
6189 tmp |= (4 << 28);
6190 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006191}
6192
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006193/* Implements 3 different sequences from BSpec chapter "Display iCLK
6194 * Programming" based on the parameters passed:
6195 * - Sequence to enable CLKOUT_DP
6196 * - Sequence to enable CLKOUT_DP without spread
6197 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6198 */
6199static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6200 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006201{
6202 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006203 uint32_t reg, tmp;
6204
6205 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6206 with_spread = true;
6207 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6208 with_fdi, "LP PCH doesn't have FDI\n"))
6209 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006210
6211 mutex_lock(&dev_priv->dpio_lock);
6212
6213 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6214 tmp &= ~SBI_SSCCTL_DISABLE;
6215 tmp |= SBI_SSCCTL_PATHALT;
6216 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6217
6218 udelay(24);
6219
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006220 if (with_spread) {
6221 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6222 tmp &= ~SBI_SSCCTL_PATHALT;
6223 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006224
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006225 if (with_fdi) {
6226 lpt_reset_fdi_mphy(dev_priv);
6227 lpt_program_fdi_mphy(dev_priv);
6228 }
6229 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006230
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006231 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6232 SBI_GEN0 : SBI_DBUFF0;
6233 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6234 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6235 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006236
6237 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006238}
6239
Paulo Zanoni47701c32013-07-23 11:19:25 -03006240/* Sequence to disable CLKOUT_DP */
6241static void lpt_disable_clkout_dp(struct drm_device *dev)
6242{
6243 struct drm_i915_private *dev_priv = dev->dev_private;
6244 uint32_t reg, tmp;
6245
6246 mutex_lock(&dev_priv->dpio_lock);
6247
6248 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6249 SBI_GEN0 : SBI_DBUFF0;
6250 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6251 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6252 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6253
6254 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6255 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6256 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6257 tmp |= SBI_SSCCTL_PATHALT;
6258 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6259 udelay(32);
6260 }
6261 tmp |= SBI_SSCCTL_DISABLE;
6262 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6263 }
6264
6265 mutex_unlock(&dev_priv->dpio_lock);
6266}
6267
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006268static void lpt_init_pch_refclk(struct drm_device *dev)
6269{
6270 struct drm_mode_config *mode_config = &dev->mode_config;
6271 struct intel_encoder *encoder;
6272 bool has_vga = false;
6273
6274 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6275 switch (encoder->type) {
6276 case INTEL_OUTPUT_ANALOG:
6277 has_vga = true;
6278 break;
6279 }
6280 }
6281
Paulo Zanoni47701c32013-07-23 11:19:25 -03006282 if (has_vga)
6283 lpt_enable_clkout_dp(dev, true, true);
6284 else
6285 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006286}
6287
Paulo Zanonidde86e22012-12-01 12:04:25 -02006288/*
6289 * Initialize reference clocks when the driver loads
6290 */
6291void intel_init_pch_refclk(struct drm_device *dev)
6292{
6293 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6294 ironlake_init_pch_refclk(dev);
6295 else if (HAS_PCH_LPT(dev))
6296 lpt_init_pch_refclk(dev);
6297}
6298
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006299static int ironlake_get_refclk(struct drm_crtc *crtc)
6300{
6301 struct drm_device *dev = crtc->dev;
6302 struct drm_i915_private *dev_priv = dev->dev_private;
6303 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006304 int num_connectors = 0;
6305 bool is_lvds = false;
6306
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02006307 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006308 switch (encoder->type) {
6309 case INTEL_OUTPUT_LVDS:
6310 is_lvds = true;
6311 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006312 }
6313 num_connectors++;
6314 }
6315
6316 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006317 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006318 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006319 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006320 }
6321
6322 return 120000;
6323}
6324
Daniel Vetter6ff93602013-04-19 11:24:36 +02006325static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006326{
6327 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6329 int pipe = intel_crtc->pipe;
6330 uint32_t val;
6331
Daniel Vetter78114072013-06-13 00:54:57 +02006332 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006333
Daniel Vetter965e0c42013-03-27 00:44:57 +01006334 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006335 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006336 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006337 break;
6338 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006339 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006340 break;
6341 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006342 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006343 break;
6344 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006345 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006346 break;
6347 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006348 /* Case prevented by intel_choose_pipe_bpp_dither. */
6349 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006350 }
6351
Daniel Vetterd8b32242013-04-25 17:54:44 +02006352 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006353 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6354
Daniel Vetter6ff93602013-04-19 11:24:36 +02006355 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006356 val |= PIPECONF_INTERLACED_ILK;
6357 else
6358 val |= PIPECONF_PROGRESSIVE;
6359
Daniel Vetter50f3b012013-03-27 00:44:56 +01006360 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006361 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006362
Paulo Zanonic8203562012-09-12 10:06:29 -03006363 I915_WRITE(PIPECONF(pipe), val);
6364 POSTING_READ(PIPECONF(pipe));
6365}
6366
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006367/*
6368 * Set up the pipe CSC unit.
6369 *
6370 * Currently only full range RGB to limited range RGB conversion
6371 * is supported, but eventually this should handle various
6372 * RGB<->YCbCr scenarios as well.
6373 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006374static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006375{
6376 struct drm_device *dev = crtc->dev;
6377 struct drm_i915_private *dev_priv = dev->dev_private;
6378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6379 int pipe = intel_crtc->pipe;
6380 uint16_t coeff = 0x7800; /* 1.0 */
6381
6382 /*
6383 * TODO: Check what kind of values actually come out of the pipe
6384 * with these coeff/postoff values and adjust to get the best
6385 * accuracy. Perhaps we even need to take the bpc value into
6386 * consideration.
6387 */
6388
Daniel Vetter50f3b012013-03-27 00:44:56 +01006389 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006390 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6391
6392 /*
6393 * GY/GU and RY/RU should be the other way around according
6394 * to BSpec, but reality doesn't agree. Just set them up in
6395 * a way that results in the correct picture.
6396 */
6397 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6398 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6399
6400 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6401 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6402
6403 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6404 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6405
6406 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6407 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6408 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6409
6410 if (INTEL_INFO(dev)->gen > 6) {
6411 uint16_t postoff = 0;
6412
Daniel Vetter50f3b012013-03-27 00:44:56 +01006413 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006414 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006415
6416 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6417 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6418 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6419
6420 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6421 } else {
6422 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6423
Daniel Vetter50f3b012013-03-27 00:44:56 +01006424 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006425 mode |= CSC_BLACK_SCREEN_OFFSET;
6426
6427 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6428 }
6429}
6430
Daniel Vetter6ff93602013-04-19 11:24:36 +02006431static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006432{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006433 struct drm_device *dev = crtc->dev;
6434 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006436 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006437 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006438 uint32_t val;
6439
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006440 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006441
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006442 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006443 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6444
Daniel Vetter6ff93602013-04-19 11:24:36 +02006445 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006446 val |= PIPECONF_INTERLACED_ILK;
6447 else
6448 val |= PIPECONF_PROGRESSIVE;
6449
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006450 I915_WRITE(PIPECONF(cpu_transcoder), val);
6451 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006452
6453 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6454 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006455
6456 if (IS_BROADWELL(dev)) {
6457 val = 0;
6458
6459 switch (intel_crtc->config.pipe_bpp) {
6460 case 18:
6461 val |= PIPEMISC_DITHER_6_BPC;
6462 break;
6463 case 24:
6464 val |= PIPEMISC_DITHER_8_BPC;
6465 break;
6466 case 30:
6467 val |= PIPEMISC_DITHER_10_BPC;
6468 break;
6469 case 36:
6470 val |= PIPEMISC_DITHER_12_BPC;
6471 break;
6472 default:
6473 /* Case prevented by pipe_config_set_bpp. */
6474 BUG();
6475 }
6476
6477 if (intel_crtc->config.dither)
6478 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6479
6480 I915_WRITE(PIPEMISC(pipe), val);
6481 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006482}
6483
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006484static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006485 intel_clock_t *clock,
6486 bool *has_reduced_clock,
6487 intel_clock_t *reduced_clock)
6488{
6489 struct drm_device *dev = crtc->dev;
6490 struct drm_i915_private *dev_priv = dev->dev_private;
6491 struct intel_encoder *intel_encoder;
6492 int refclk;
6493 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006494 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006495
6496 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6497 switch (intel_encoder->type) {
6498 case INTEL_OUTPUT_LVDS:
6499 is_lvds = true;
6500 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006501 }
6502 }
6503
6504 refclk = ironlake_get_refclk(crtc);
6505
6506 /*
6507 * Returns a set of divisors for the desired target clock with the given
6508 * refclk, or FALSE. The returned values represent the clock equation:
6509 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6510 */
6511 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006512 ret = dev_priv->display.find_dpll(limit, crtc,
6513 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006514 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006515 if (!ret)
6516 return false;
6517
6518 if (is_lvds && dev_priv->lvds_downclock_avail) {
6519 /*
6520 * Ensure we match the reduced clock's P to the target clock.
6521 * If the clocks don't match, we can't switch the display clock
6522 * by using the FP0/FP1. In such case we will disable the LVDS
6523 * downclock feature.
6524 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006525 *has_reduced_clock =
6526 dev_priv->display.find_dpll(limit, crtc,
6527 dev_priv->lvds_downclock,
6528 refclk, clock,
6529 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006530 }
6531
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006532 return true;
6533}
6534
Paulo Zanonid4b19312012-11-29 11:29:32 -02006535int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6536{
6537 /*
6538 * Account for spread spectrum to avoid
6539 * oversubscribing the link. Max center spread
6540 * is 2.5%; use 5% for safety's sake.
6541 */
6542 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02006543 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02006544}
6545
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006546static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006547{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006548 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006549}
6550
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006551static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006552 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006553 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006554{
6555 struct drm_crtc *crtc = &intel_crtc->base;
6556 struct drm_device *dev = crtc->dev;
6557 struct drm_i915_private *dev_priv = dev->dev_private;
6558 struct intel_encoder *intel_encoder;
6559 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006560 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006561 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006562
6563 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6564 switch (intel_encoder->type) {
6565 case INTEL_OUTPUT_LVDS:
6566 is_lvds = true;
6567 break;
6568 case INTEL_OUTPUT_SDVO:
6569 case INTEL_OUTPUT_HDMI:
6570 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006571 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006572 }
6573
6574 num_connectors++;
6575 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006576
Chris Wilsonc1858122010-12-03 21:35:48 +00006577 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006578 factor = 21;
6579 if (is_lvds) {
6580 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006581 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006582 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006583 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006584 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006585 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006586
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006587 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006588 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006589
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006590 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6591 *fp2 |= FP_CB_TUNE;
6592
Chris Wilson5eddb702010-09-11 13:48:45 +01006593 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006594
Eric Anholta07d6782011-03-30 13:01:08 -07006595 if (is_lvds)
6596 dpll |= DPLLB_MODE_LVDS;
6597 else
6598 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006599
Daniel Vetteref1b4602013-06-01 17:17:04 +02006600 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6601 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006602
6603 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006604 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006605 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006606 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006607
Eric Anholta07d6782011-03-30 13:01:08 -07006608 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006609 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006610 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006611 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006612
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006613 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006614 case 5:
6615 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6616 break;
6617 case 7:
6618 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6619 break;
6620 case 10:
6621 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6622 break;
6623 case 14:
6624 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6625 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006626 }
6627
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006628 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006629 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006630 else
6631 dpll |= PLL_REF_INPUT_DREFCLK;
6632
Daniel Vetter959e16d2013-06-05 13:34:21 +02006633 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006634}
6635
Jesse Barnes79e53942008-11-07 14:24:08 -08006636static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006637 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006638 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006639{
6640 struct drm_device *dev = crtc->dev;
6641 struct drm_i915_private *dev_priv = dev->dev_private;
6642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6643 int pipe = intel_crtc->pipe;
6644 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006645 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006646 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006647 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006648 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006649 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006650 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006651 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006652 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006653
6654 for_each_encoder_on_crtc(dev, crtc, encoder) {
6655 switch (encoder->type) {
6656 case INTEL_OUTPUT_LVDS:
6657 is_lvds = true;
6658 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006659 }
6660
6661 num_connectors++;
6662 }
6663
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006664 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6665 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6666
Daniel Vetterff9a6752013-06-01 17:16:21 +02006667 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006668 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006669 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006670 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6671 return -EINVAL;
6672 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006673 /* Compat-code for transition, will disappear. */
6674 if (!intel_crtc->config.clock_set) {
6675 intel_crtc->config.dpll.n = clock.n;
6676 intel_crtc->config.dpll.m1 = clock.m1;
6677 intel_crtc->config.dpll.m2 = clock.m2;
6678 intel_crtc->config.dpll.p1 = clock.p1;
6679 intel_crtc->config.dpll.p2 = clock.p2;
6680 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006681
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006682 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006683 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006684 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006685 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006686 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006687
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006688 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006689 &fp, &reduced_clock,
6690 has_reduced_clock ? &fp2 : NULL);
6691
Daniel Vetter959e16d2013-06-05 13:34:21 +02006692 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02006693 intel_crtc->config.dpll_hw_state.fp0 = fp;
6694 if (has_reduced_clock)
6695 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6696 else
6697 intel_crtc->config.dpll_hw_state.fp1 = fp;
6698
Daniel Vetterb89a1d32013-06-05 13:34:24 +02006699 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006700 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03006701 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6702 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07006703 return -EINVAL;
6704 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006705 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02006706 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006707
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006708 if (intel_crtc->config.has_dp_encoder)
6709 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006710
Jani Nikulad330a952014-01-21 11:24:25 +02006711 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02006712 intel_crtc->lowfreq_avail = true;
6713 else
6714 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02006715
Daniel Vetter8a654f32013-06-01 17:16:22 +02006716 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006717
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006718 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006719 intel_cpu_transcoder_set_m_n(intel_crtc,
6720 &intel_crtc->config.fdi_m_n);
6721 }
Chris Wilson5eddb702010-09-11 13:48:45 +01006722
Daniel Vetter6ff93602013-04-19 11:24:36 +02006723 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006724
Paulo Zanonia1f9e772012-09-12 10:06:32 -03006725 /* Set up the display plane register */
6726 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006727 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006728
Daniel Vetter94352cf2012-07-05 22:51:56 +02006729 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006730
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006731 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006732}
6733
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006734static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6735 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02006736{
6737 struct drm_device *dev = crtc->base.dev;
6738 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006739 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02006740
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006741 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6742 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6743 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6744 & ~TU_SIZE_MASK;
6745 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6746 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6747 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6748}
6749
6750static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6751 enum transcoder transcoder,
6752 struct intel_link_m_n *m_n)
6753{
6754 struct drm_device *dev = crtc->base.dev;
6755 struct drm_i915_private *dev_priv = dev->dev_private;
6756 enum pipe pipe = crtc->pipe;
6757
6758 if (INTEL_INFO(dev)->gen >= 5) {
6759 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6760 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6761 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6762 & ~TU_SIZE_MASK;
6763 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6764 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6765 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6766 } else {
6767 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6768 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6769 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6770 & ~TU_SIZE_MASK;
6771 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6772 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6773 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6774 }
6775}
6776
6777void intel_dp_get_m_n(struct intel_crtc *crtc,
6778 struct intel_crtc_config *pipe_config)
6779{
6780 if (crtc->config.has_pch_encoder)
6781 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6782 else
6783 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6784 &pipe_config->dp_m_n);
6785}
6786
Daniel Vetter72419202013-04-04 13:28:53 +02006787static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6788 struct intel_crtc_config *pipe_config)
6789{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03006790 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6791 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02006792}
6793
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006794static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6795 struct intel_crtc_config *pipe_config)
6796{
6797 struct drm_device *dev = crtc->base.dev;
6798 struct drm_i915_private *dev_priv = dev->dev_private;
6799 uint32_t tmp;
6800
6801 tmp = I915_READ(PF_CTL(crtc->pipe));
6802
6803 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01006804 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006805 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6806 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02006807
6808 /* We currently do not free assignements of panel fitters on
6809 * ivb/hsw (since we don't use the higher upscaling modes which
6810 * differentiates them) so just WARN about this case for now. */
6811 if (IS_GEN7(dev)) {
6812 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6813 PF_PIPE_SEL_IVB(crtc->pipe));
6814 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006815 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006816}
6817
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006818static void ironlake_get_plane_config(struct intel_crtc *crtc,
6819 struct intel_plane_config *plane_config)
6820{
6821 struct drm_device *dev = crtc->base.dev;
6822 struct drm_i915_private *dev_priv = dev->dev_private;
6823 u32 val, base, offset;
6824 int pipe = crtc->pipe, plane = crtc->plane;
6825 int fourcc, pixel_format;
6826 int aligned_height;
6827
Dave Airlie66e514c2014-04-03 07:51:54 +10006828 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6829 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006830 DRM_DEBUG_KMS("failed to alloc fb\n");
6831 return;
6832 }
6833
6834 val = I915_READ(DSPCNTR(plane));
6835
6836 if (INTEL_INFO(dev)->gen >= 4)
6837 if (val & DISPPLANE_TILED)
6838 plane_config->tiled = true;
6839
6840 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6841 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006842 crtc->base.primary->fb->pixel_format = fourcc;
6843 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006844 drm_format_plane_cpp(fourcc, 0) * 8;
6845
6846 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6847 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
6848 offset = I915_READ(DSPOFFSET(plane));
6849 } else {
6850 if (plane_config->tiled)
6851 offset = I915_READ(DSPTILEOFF(plane));
6852 else
6853 offset = I915_READ(DSPLINOFF(plane));
6854 }
6855 plane_config->base = base;
6856
6857 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006858 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6859 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006860
6861 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006862 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006863
Dave Airlie66e514c2014-04-03 07:51:54 +10006864 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006865 plane_config->tiled);
6866
Dave Airlie66e514c2014-04-03 07:51:54 +10006867 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006868 aligned_height, PAGE_SIZE);
6869
6870 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10006871 pipe, plane, crtc->base.primary->fb->width,
6872 crtc->base.primary->fb->height,
6873 crtc->base.primary->fb->bits_per_pixel, base,
6874 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08006875 plane_config->size);
6876}
6877
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006878static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6879 struct intel_crtc_config *pipe_config)
6880{
6881 struct drm_device *dev = crtc->base.dev;
6882 struct drm_i915_private *dev_priv = dev->dev_private;
6883 uint32_t tmp;
6884
Daniel Vettere143a212013-07-04 12:01:15 +02006885 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006886 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006887
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006888 tmp = I915_READ(PIPECONF(crtc->pipe));
6889 if (!(tmp & PIPECONF_ENABLE))
6890 return false;
6891
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006892 switch (tmp & PIPECONF_BPC_MASK) {
6893 case PIPECONF_6BPC:
6894 pipe_config->pipe_bpp = 18;
6895 break;
6896 case PIPECONF_8BPC:
6897 pipe_config->pipe_bpp = 24;
6898 break;
6899 case PIPECONF_10BPC:
6900 pipe_config->pipe_bpp = 30;
6901 break;
6902 case PIPECONF_12BPC:
6903 pipe_config->pipe_bpp = 36;
6904 break;
6905 default:
6906 break;
6907 }
6908
Daniel Vetterab9412b2013-05-03 11:49:46 +02006909 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02006910 struct intel_shared_dpll *pll;
6911
Daniel Vetter88adfff2013-03-28 10:42:01 +01006912 pipe_config->has_pch_encoder = true;
6913
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006914 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6915 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6916 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006917
6918 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006919
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006920 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02006921 pipe_config->shared_dpll =
6922 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006923 } else {
6924 tmp = I915_READ(PCH_DPLL_SEL);
6925 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6926 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6927 else
6928 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6929 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02006930
6931 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6932
6933 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6934 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02006935
6936 tmp = pipe_config->dpll_hw_state.dpll;
6937 pipe_config->pixel_multiplier =
6938 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6939 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03006940
6941 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02006942 } else {
6943 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006944 }
6945
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006946 intel_get_pipe_timings(crtc, pipe_config);
6947
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006948 ironlake_get_pfit_config(crtc, pipe_config);
6949
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006950 return true;
6951}
6952
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006953static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6954{
6955 struct drm_device *dev = dev_priv->dev;
6956 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6957 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006958
6959 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
Paulo Zanoni798183c2013-12-06 20:29:01 -02006960 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006961 pipe_name(crtc->pipe));
6962
6963 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6964 WARN(plls->spll_refcount, "SPLL enabled\n");
6965 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6966 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6967 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6968 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6969 "CPU PWM1 enabled\n");
6970 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6971 "CPU PWM2 enabled\n");
6972 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6973 "PCH PWM1 enabled\n");
6974 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6975 "Utility pin enabled\n");
6976 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6977
Paulo Zanoni9926ada2014-04-01 19:39:47 -03006978 /*
6979 * In theory we can still leave IRQs enabled, as long as only the HPD
6980 * interrupts remain enabled. We used to check for that, but since it's
6981 * gen-specific and since we only disable LCPLL after we fully disable
6982 * the interrupts, the check below should be enough.
6983 */
6984 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03006985}
6986
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03006987static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
6988{
6989 struct drm_device *dev = dev_priv->dev;
6990
6991 if (IS_HASWELL(dev)) {
6992 mutex_lock(&dev_priv->rps.hw_lock);
6993 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
6994 val))
6995 DRM_ERROR("Failed to disable D_COMP\n");
6996 mutex_unlock(&dev_priv->rps.hw_lock);
6997 } else {
6998 I915_WRITE(D_COMP, val);
6999 }
7000 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007001}
7002
7003/*
7004 * This function implements pieces of two sequences from BSpec:
7005 * - Sequence for display software to disable LCPLL
7006 * - Sequence for display software to allow package C8+
7007 * The steps implemented here are just the steps that actually touch the LCPLL
7008 * register. Callers should take care of disabling all the display engine
7009 * functions, doing the mode unset, fixing interrupts, etc.
7010 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007011static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7012 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007013{
7014 uint32_t val;
7015
7016 assert_can_disable_lcpll(dev_priv);
7017
7018 val = I915_READ(LCPLL_CTL);
7019
7020 if (switch_to_fclk) {
7021 val |= LCPLL_CD_SOURCE_FCLK;
7022 I915_WRITE(LCPLL_CTL, val);
7023
7024 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7025 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7026 DRM_ERROR("Switching to FCLK failed\n");
7027
7028 val = I915_READ(LCPLL_CTL);
7029 }
7030
7031 val |= LCPLL_PLL_DISABLE;
7032 I915_WRITE(LCPLL_CTL, val);
7033 POSTING_READ(LCPLL_CTL);
7034
7035 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7036 DRM_ERROR("LCPLL still locked\n");
7037
7038 val = I915_READ(D_COMP);
7039 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007040 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007041 ndelay(100);
7042
7043 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7044 DRM_ERROR("D_COMP RCOMP still in progress\n");
7045
7046 if (allow_power_down) {
7047 val = I915_READ(LCPLL_CTL);
7048 val |= LCPLL_POWER_DOWN_ALLOW;
7049 I915_WRITE(LCPLL_CTL, val);
7050 POSTING_READ(LCPLL_CTL);
7051 }
7052}
7053
7054/*
7055 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7056 * source.
7057 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007058static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007059{
7060 uint32_t val;
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007061 unsigned long irqflags;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007062
7063 val = I915_READ(LCPLL_CTL);
7064
7065 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7066 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7067 return;
7068
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007069 /*
7070 * Make sure we're not on PC8 state before disabling PC8, otherwise
7071 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7072 *
7073 * The other problem is that hsw_restore_lcpll() is called as part of
7074 * the runtime PM resume sequence, so we can't just call
7075 * gen6_gt_force_wake_get() because that function calls
7076 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7077 * while we are on the resume sequence. So to solve this problem we have
7078 * to call special forcewake code that doesn't touch runtime PM and
7079 * doesn't enable the forcewake delayed work.
7080 */
7081 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7082 if (dev_priv->uncore.forcewake_count++ == 0)
7083 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7084 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007085
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007086 if (val & LCPLL_POWER_DOWN_ALLOW) {
7087 val &= ~LCPLL_POWER_DOWN_ALLOW;
7088 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007089 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007090 }
7091
7092 val = I915_READ(D_COMP);
7093 val |= D_COMP_COMP_FORCE;
7094 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007095 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007096
7097 val = I915_READ(LCPLL_CTL);
7098 val &= ~LCPLL_PLL_DISABLE;
7099 I915_WRITE(LCPLL_CTL, val);
7100
7101 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7102 DRM_ERROR("LCPLL not locked yet\n");
7103
7104 if (val & LCPLL_CD_SOURCE_FCLK) {
7105 val = I915_READ(LCPLL_CTL);
7106 val &= ~LCPLL_CD_SOURCE_FCLK;
7107 I915_WRITE(LCPLL_CTL, val);
7108
7109 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7110 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7111 DRM_ERROR("Switching back to LCPLL failed\n");
7112 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007113
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007114 /* See the big comment above. */
7115 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7116 if (--dev_priv->uncore.forcewake_count == 0)
7117 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7118 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007119}
7120
Paulo Zanoni765dab62014-03-07 20:08:18 -03007121/*
7122 * Package states C8 and deeper are really deep PC states that can only be
7123 * reached when all the devices on the system allow it, so even if the graphics
7124 * device allows PC8+, it doesn't mean the system will actually get to these
7125 * states. Our driver only allows PC8+ when going into runtime PM.
7126 *
7127 * The requirements for PC8+ are that all the outputs are disabled, the power
7128 * well is disabled and most interrupts are disabled, and these are also
7129 * requirements for runtime PM. When these conditions are met, we manually do
7130 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7131 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7132 * hang the machine.
7133 *
7134 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7135 * the state of some registers, so when we come back from PC8+ we need to
7136 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7137 * need to take care of the registers kept by RC6. Notice that this happens even
7138 * if we don't put the device in PCI D3 state (which is what currently happens
7139 * because of the runtime PM support).
7140 *
7141 * For more, read "Display Sequences for Package C8" on the hardware
7142 * documentation.
7143 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007144void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007145{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007146 struct drm_device *dev = dev_priv->dev;
7147 uint32_t val;
7148
Paulo Zanonic67a4702013-08-19 13:18:09 -03007149 DRM_DEBUG_KMS("Enabling package C8+\n");
7150
Paulo Zanonic67a4702013-08-19 13:18:09 -03007151 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7152 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7153 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7154 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7155 }
7156
7157 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007158 hsw_disable_lcpll(dev_priv, true, true);
7159}
7160
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007161void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007162{
7163 struct drm_device *dev = dev_priv->dev;
7164 uint32_t val;
7165
Paulo Zanonic67a4702013-08-19 13:18:09 -03007166 DRM_DEBUG_KMS("Disabling package C8+\n");
7167
7168 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007169 lpt_init_pch_refclk(dev);
7170
7171 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7172 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7173 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7174 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7175 }
7176
7177 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007178}
7179
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007180static void snb_modeset_global_resources(struct drm_device *dev)
7181{
7182 modeset_update_crtc_power_domains(dev);
7183}
7184
Imre Deak4f074122013-10-16 17:25:51 +03007185static void haswell_modeset_global_resources(struct drm_device *dev)
7186{
Paulo Zanonida723562013-12-19 11:54:51 -02007187 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007188}
7189
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007190static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007191 int x, int y,
7192 struct drm_framebuffer *fb)
7193{
7194 struct drm_device *dev = crtc->dev;
7195 struct drm_i915_private *dev_priv = dev->dev_private;
7196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007197 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007198 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007199
Paulo Zanoni566b7342013-11-25 15:27:08 -02007200 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007201 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02007202 intel_ddi_pll_enable(intel_crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007203
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007204 if (intel_crtc->config.has_dp_encoder)
7205 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007206
7207 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007208
Daniel Vetter8a654f32013-06-01 17:16:22 +02007209 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007210
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01007211 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01007212 intel_cpu_transcoder_set_m_n(intel_crtc,
7213 &intel_crtc->config.fdi_m_n);
7214 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007215
Daniel Vetter6ff93602013-04-19 11:24:36 +02007216 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007217
Daniel Vetter50f3b012013-03-27 00:44:56 +01007218 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007219
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007220 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007221 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007222 POSTING_READ(DSPCNTR(plane));
7223
7224 ret = intel_pipe_set_base(crtc, x, y, fb);
7225
Jesse Barnes79e53942008-11-07 14:24:08 -08007226 return ret;
7227}
7228
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007229static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7230 struct intel_crtc_config *pipe_config)
7231{
7232 struct drm_device *dev = crtc->base.dev;
7233 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007234 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007235 uint32_t tmp;
7236
Imre Deakb5482bd2014-03-05 16:20:55 +02007237 if (!intel_display_power_enabled(dev_priv,
7238 POWER_DOMAIN_PIPE(crtc->pipe)))
7239 return false;
7240
Daniel Vettere143a212013-07-04 12:01:15 +02007241 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007242 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7243
Daniel Vettereccb1402013-05-22 00:50:22 +02007244 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7245 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7246 enum pipe trans_edp_pipe;
7247 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7248 default:
7249 WARN(1, "unknown pipe linked to edp transcoder\n");
7250 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7251 case TRANS_DDI_EDP_INPUT_A_ON:
7252 trans_edp_pipe = PIPE_A;
7253 break;
7254 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7255 trans_edp_pipe = PIPE_B;
7256 break;
7257 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7258 trans_edp_pipe = PIPE_C;
7259 break;
7260 }
7261
7262 if (trans_edp_pipe == crtc->pipe)
7263 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7264 }
7265
Imre Deakda7e29b2014-02-18 00:02:02 +02007266 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007267 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007268 return false;
7269
Daniel Vettereccb1402013-05-22 00:50:22 +02007270 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007271 if (!(tmp & PIPECONF_ENABLE))
7272 return false;
7273
Daniel Vetter88adfff2013-03-28 10:42:01 +01007274 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03007275 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01007276 * DDI E. So just check whether this pipe is wired to DDI E and whether
7277 * the PCH transcoder is on.
7278 */
Daniel Vettereccb1402013-05-22 00:50:22 +02007279 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01007280 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02007281 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01007282 pipe_config->has_pch_encoder = true;
7283
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007284 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7285 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7286 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007287
7288 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007289 }
7290
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007291 intel_get_pipe_timings(crtc, pipe_config);
7292
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007293 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007294 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007295 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007296
Jesse Barnese59150d2014-01-07 13:30:45 -08007297 if (IS_HASWELL(dev))
7298 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7299 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007300
Daniel Vetter6c49f242013-06-06 12:45:25 +02007301 pipe_config->pixel_multiplier = 1;
7302
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007303 return true;
7304}
7305
Eric Anholtf564048e2011-03-30 13:01:02 -07007306static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07007307 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007308 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07007309{
7310 struct drm_device *dev = crtc->dev;
7311 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01007312 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07007313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007314 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07007315 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07007316 int ret;
7317
Eric Anholt0b701d22011-03-30 13:01:03 -07007318 drm_vblank_pre_modeset(dev, pipe);
7319
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007320 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7321
Jesse Barnes79e53942008-11-07 14:24:08 -08007322 drm_vblank_post_modeset(dev, pipe);
7323
Daniel Vetter9256aa12012-10-31 19:26:13 +01007324 if (ret != 0)
7325 return ret;
7326
7327 for_each_encoder_on_crtc(dev, crtc, encoder) {
7328 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7329 encoder->base.base.id,
7330 drm_get_encoder_name(&encoder->base),
7331 mode->base.id, mode->name);
Daniel Vetter0d56bf02014-04-24 23:54:37 +02007332
7333 if (encoder->mode_set)
7334 encoder->mode_set(encoder);
Daniel Vetter9256aa12012-10-31 19:26:13 +01007335 }
7336
7337 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007338}
7339
Jani Nikula1a915102013-10-16 12:34:48 +03007340static struct {
7341 int clock;
7342 u32 config;
7343} hdmi_audio_clock[] = {
7344 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7345 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7346 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7347 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7348 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7349 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7350 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7351 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7352 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7353 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7354};
7355
7356/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7357static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7358{
7359 int i;
7360
7361 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7362 if (mode->clock == hdmi_audio_clock[i].clock)
7363 break;
7364 }
7365
7366 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7367 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7368 i = 1;
7369 }
7370
7371 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7372 hdmi_audio_clock[i].clock,
7373 hdmi_audio_clock[i].config);
7374
7375 return hdmi_audio_clock[i].config;
7376}
7377
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007378static bool intel_eld_uptodate(struct drm_connector *connector,
7379 int reg_eldv, uint32_t bits_eldv,
7380 int reg_elda, uint32_t bits_elda,
7381 int reg_edid)
7382{
7383 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7384 uint8_t *eld = connector->eld;
7385 uint32_t i;
7386
7387 i = I915_READ(reg_eldv);
7388 i &= bits_eldv;
7389
7390 if (!eld[0])
7391 return !i;
7392
7393 if (!i)
7394 return false;
7395
7396 i = I915_READ(reg_elda);
7397 i &= ~bits_elda;
7398 I915_WRITE(reg_elda, i);
7399
7400 for (i = 0; i < eld[2]; i++)
7401 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7402 return false;
7403
7404 return true;
7405}
7406
Wu Fengguange0dac652011-09-05 14:25:34 +08007407static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007408 struct drm_crtc *crtc,
7409 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007410{
7411 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7412 uint8_t *eld = connector->eld;
7413 uint32_t eldv;
7414 uint32_t len;
7415 uint32_t i;
7416
7417 i = I915_READ(G4X_AUD_VID_DID);
7418
7419 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7420 eldv = G4X_ELDV_DEVCL_DEVBLC;
7421 else
7422 eldv = G4X_ELDV_DEVCTG;
7423
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007424 if (intel_eld_uptodate(connector,
7425 G4X_AUD_CNTL_ST, eldv,
7426 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7427 G4X_HDMIW_HDMIEDID))
7428 return;
7429
Wu Fengguange0dac652011-09-05 14:25:34 +08007430 i = I915_READ(G4X_AUD_CNTL_ST);
7431 i &= ~(eldv | G4X_ELD_ADDR);
7432 len = (i >> 9) & 0x1f; /* ELD buffer size */
7433 I915_WRITE(G4X_AUD_CNTL_ST, i);
7434
7435 if (!eld[0])
7436 return;
7437
7438 len = min_t(uint8_t, eld[2], len);
7439 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7440 for (i = 0; i < len; i++)
7441 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7442
7443 i = I915_READ(G4X_AUD_CNTL_ST);
7444 i |= eldv;
7445 I915_WRITE(G4X_AUD_CNTL_ST, i);
7446}
7447
Wang Xingchao83358c852012-08-16 22:43:37 +08007448static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007449 struct drm_crtc *crtc,
7450 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007451{
7452 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7453 uint8_t *eld = connector->eld;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08007455 uint32_t eldv;
7456 uint32_t i;
7457 int len;
7458 int pipe = to_intel_crtc(crtc)->pipe;
7459 int tmp;
7460
7461 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7462 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7463 int aud_config = HSW_AUD_CFG(pipe);
7464 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7465
Wang Xingchao83358c852012-08-16 22:43:37 +08007466 /* Audio output enable */
7467 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7468 tmp = I915_READ(aud_cntrl_st2);
7469 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7470 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007471 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007472
Daniel Vetterc7905792014-04-16 16:56:09 +02007473 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007474
7475 /* Set ELD valid state */
7476 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007477 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007478 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7479 I915_WRITE(aud_cntrl_st2, tmp);
7480 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007481 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007482
7483 /* Enable HDMI mode */
7484 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007485 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007486 /* clear N_programing_enable and N_value_index */
7487 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7488 I915_WRITE(aud_config, tmp);
7489
7490 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7491
7492 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08007493 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08007494
7495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7496 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7497 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7498 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007499 } else {
7500 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7501 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007502
7503 if (intel_eld_uptodate(connector,
7504 aud_cntrl_st2, eldv,
7505 aud_cntl_st, IBX_ELD_ADDRESS,
7506 hdmiw_hdmiedid))
7507 return;
7508
7509 i = I915_READ(aud_cntrl_st2);
7510 i &= ~eldv;
7511 I915_WRITE(aud_cntrl_st2, i);
7512
7513 if (!eld[0])
7514 return;
7515
7516 i = I915_READ(aud_cntl_st);
7517 i &= ~IBX_ELD_ADDRESS;
7518 I915_WRITE(aud_cntl_st, i);
7519 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7520 DRM_DEBUG_DRIVER("port num:%d\n", i);
7521
7522 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7523 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7524 for (i = 0; i < len; i++)
7525 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7526
7527 i = I915_READ(aud_cntrl_st2);
7528 i |= eldv;
7529 I915_WRITE(aud_cntrl_st2, i);
7530
7531}
7532
Wu Fengguange0dac652011-09-05 14:25:34 +08007533static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007534 struct drm_crtc *crtc,
7535 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007536{
7537 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7538 uint8_t *eld = connector->eld;
7539 uint32_t eldv;
7540 uint32_t i;
7541 int len;
7542 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007543 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007544 int aud_cntl_st;
7545 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007546 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007547
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007548 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007549 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7550 aud_config = IBX_AUD_CFG(pipe);
7551 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007552 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007553 } else if (IS_VALLEYVIEW(connector->dev)) {
7554 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7555 aud_config = VLV_AUD_CFG(pipe);
7556 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7557 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007558 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007559 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7560 aud_config = CPT_AUD_CFG(pipe);
7561 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007562 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007563 }
7564
Wang Xingchao9b138a82012-08-09 16:52:18 +08007565 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007566
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007567 if (IS_VALLEYVIEW(connector->dev)) {
7568 struct intel_encoder *intel_encoder;
7569 struct intel_digital_port *intel_dig_port;
7570
7571 intel_encoder = intel_attached_encoder(connector);
7572 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7573 i = intel_dig_port->port;
7574 } else {
7575 i = I915_READ(aud_cntl_st);
7576 i = (i >> 29) & DIP_PORT_SEL_MASK;
7577 /* DIP_Port_Select, 0x1 = PortB */
7578 }
7579
Wu Fengguange0dac652011-09-05 14:25:34 +08007580 if (!i) {
7581 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7582 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007583 eldv = IBX_ELD_VALIDB;
7584 eldv |= IBX_ELD_VALIDB << 4;
7585 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007586 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007587 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007588 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007589 }
7590
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007591 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7592 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7593 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007594 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007595 } else {
7596 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7597 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007598
7599 if (intel_eld_uptodate(connector,
7600 aud_cntrl_st2, eldv,
7601 aud_cntl_st, IBX_ELD_ADDRESS,
7602 hdmiw_hdmiedid))
7603 return;
7604
Wu Fengguange0dac652011-09-05 14:25:34 +08007605 i = I915_READ(aud_cntrl_st2);
7606 i &= ~eldv;
7607 I915_WRITE(aud_cntrl_st2, i);
7608
7609 if (!eld[0])
7610 return;
7611
Wu Fengguange0dac652011-09-05 14:25:34 +08007612 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007613 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007614 I915_WRITE(aud_cntl_st, i);
7615
7616 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7617 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7618 for (i = 0; i < len; i++)
7619 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7620
7621 i = I915_READ(aud_cntrl_st2);
7622 i |= eldv;
7623 I915_WRITE(aud_cntrl_st2, i);
7624}
7625
7626void intel_write_eld(struct drm_encoder *encoder,
7627 struct drm_display_mode *mode)
7628{
7629 struct drm_crtc *crtc = encoder->crtc;
7630 struct drm_connector *connector;
7631 struct drm_device *dev = encoder->dev;
7632 struct drm_i915_private *dev_priv = dev->dev_private;
7633
7634 connector = drm_select_eld(encoder, mode);
7635 if (!connector)
7636 return;
7637
7638 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7639 connector->base.id,
7640 drm_get_connector_name(connector),
7641 connector->encoder->base.id,
7642 drm_get_encoder_name(connector->encoder));
7643
7644 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7645
7646 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007647 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007648}
7649
Chris Wilson560b85b2010-08-07 11:01:38 +01007650static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7651{
7652 struct drm_device *dev = crtc->dev;
7653 struct drm_i915_private *dev_priv = dev->dev_private;
7654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7655 bool visible = base != 0;
7656 u32 cntl;
7657
7658 if (intel_crtc->cursor_visible == visible)
7659 return;
7660
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007661 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01007662 if (visible) {
7663 /* On these chipsets we can only modify the base whilst
7664 * the cursor is disabled.
7665 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007666 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01007667
7668 cntl &= ~(CURSOR_FORMAT_MASK);
7669 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7670 cntl |= CURSOR_ENABLE |
7671 CURSOR_GAMMA_ENABLE |
7672 CURSOR_FORMAT_ARGB;
7673 } else
7674 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007675 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01007676
7677 intel_crtc->cursor_visible = visible;
7678}
7679
7680static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7681{
7682 struct drm_device *dev = crtc->dev;
7683 struct drm_i915_private *dev_priv = dev->dev_private;
7684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7685 int pipe = intel_crtc->pipe;
7686 bool visible = base != 0;
7687
7688 if (intel_crtc->cursor_visible != visible) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307689 int16_t width = intel_crtc->cursor_width;
Jesse Barnes548f2452011-02-17 10:40:53 -08007690 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01007691 if (base) {
7692 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307693 cntl |= MCURSOR_GAMMA_ENABLE;
7694
7695 switch (width) {
7696 case 64:
7697 cntl |= CURSOR_MODE_64_ARGB_AX;
7698 break;
7699 case 128:
7700 cntl |= CURSOR_MODE_128_ARGB_AX;
7701 break;
7702 case 256:
7703 cntl |= CURSOR_MODE_256_ARGB_AX;
7704 break;
7705 default:
7706 WARN_ON(1);
7707 return;
7708 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007709 cntl |= pipe << 28; /* Connect to correct pipe */
7710 } else {
7711 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7712 cntl |= CURSOR_MODE_DISABLE;
7713 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007714 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01007715
7716 intel_crtc->cursor_visible = visible;
7717 }
7718 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007719 POSTING_READ(CURCNTR(pipe));
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007720 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007721 POSTING_READ(CURBASE(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01007722}
7723
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007724static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7725{
7726 struct drm_device *dev = crtc->dev;
7727 struct drm_i915_private *dev_priv = dev->dev_private;
7728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7729 int pipe = intel_crtc->pipe;
7730 bool visible = base != 0;
7731
7732 if (intel_crtc->cursor_visible != visible) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307733 int16_t width = intel_crtc->cursor_width;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007734 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7735 if (base) {
7736 cntl &= ~CURSOR_MODE;
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307737 cntl |= MCURSOR_GAMMA_ENABLE;
7738 switch (width) {
7739 case 64:
7740 cntl |= CURSOR_MODE_64_ARGB_AX;
7741 break;
7742 case 128:
7743 cntl |= CURSOR_MODE_128_ARGB_AX;
7744 break;
7745 case 256:
7746 cntl |= CURSOR_MODE_256_ARGB_AX;
7747 break;
7748 default:
7749 WARN_ON(1);
7750 return;
7751 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007752 } else {
7753 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7754 cntl |= CURSOR_MODE_DISABLE;
7755 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007756 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007757 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007758 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7759 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007760 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7761
7762 intel_crtc->cursor_visible = visible;
7763 }
7764 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007765 POSTING_READ(CURCNTR_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007766 I915_WRITE(CURBASE_IVB(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007767 POSTING_READ(CURBASE_IVB(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007768}
7769
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007770/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01007771static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7772 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007773{
7774 struct drm_device *dev = crtc->dev;
7775 struct drm_i915_private *dev_priv = dev->dev_private;
7776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7777 int pipe = intel_crtc->pipe;
7778 int x = intel_crtc->cursor_x;
7779 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007780 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007781 bool visible;
7782
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007783 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007784 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007785
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03007786 if (x >= intel_crtc->config.pipe_src_w)
7787 base = 0;
7788
7789 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007790 base = 0;
7791
7792 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007793 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007794 base = 0;
7795
7796 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7797 x = -x;
7798 }
7799 pos |= x << CURSOR_X_SHIFT;
7800
7801 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03007802 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007803 base = 0;
7804
7805 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7806 y = -y;
7807 }
7808 pos |= y << CURSOR_Y_SHIFT;
7809
7810 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01007811 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007812 return;
7813
Paulo Zanonib3dc6852013-11-02 21:07:33 -07007814 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007815 I915_WRITE(CURPOS_IVB(pipe), pos);
7816 ivb_update_cursor(crtc, base);
7817 } else {
7818 I915_WRITE(CURPOS(pipe), pos);
7819 if (IS_845G(dev) || IS_I865G(dev))
7820 i845_update_cursor(crtc, base);
7821 else
7822 i9xx_update_cursor(crtc, base);
7823 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007824}
7825
Jesse Barnes79e53942008-11-07 14:24:08 -08007826static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00007827 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08007828 uint32_t handle,
7829 uint32_t width, uint32_t height)
7830{
7831 struct drm_device *dev = crtc->dev;
7832 struct drm_i915_private *dev_priv = dev->dev_private;
7833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00007834 struct drm_i915_gem_object *obj;
Chris Wilson64f962e2014-03-26 12:38:15 +00007835 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007836 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007837 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007838
Jesse Barnes79e53942008-11-07 14:24:08 -08007839 /* if we want to turn off the cursor ignore width and height */
7840 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007841 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007842 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00007843 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10007844 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007845 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08007846 }
7847
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307848 /* Check for which cursor types we support */
7849 if (!((width == 64 && height == 64) ||
7850 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
7851 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
7852 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08007853 return -EINVAL;
7854 }
7855
Chris Wilson05394f32010-11-08 19:18:58 +00007856 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00007857 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08007858 return -ENOENT;
7859
Chris Wilson05394f32010-11-08 19:18:58 +00007860 if (obj->base.size < width * height * 4) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01007861 DRM_DEBUG_KMS("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10007862 ret = -ENOMEM;
7863 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08007864 }
7865
Dave Airlie71acb5e2008-12-30 20:31:46 +10007866 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007867 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00007868 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00007869 unsigned alignment;
7870
Chris Wilsond9e86c02010-11-10 16:40:20 +00007871 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01007872 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007873 ret = -EINVAL;
7874 goto fail_locked;
7875 }
7876
Chris Wilson693db182013-03-05 14:52:39 +00007877 /* Note that the w/a also requires 2 PTE of padding following
7878 * the bo. We currently fill all unused PTE with the shadow
7879 * page and so we should always have valid PTE following the
7880 * cursor preventing the VT-d warning.
7881 */
7882 alignment = 0;
7883 if (need_vtd_wa(dev))
7884 alignment = 64*1024;
7885
7886 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01007887 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01007888 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01007889 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007890 }
7891
Chris Wilsond9e86c02010-11-10 16:40:20 +00007892 ret = i915_gem_object_put_fence(obj);
7893 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01007894 DRM_DEBUG_KMS("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00007895 goto fail_unpin;
7896 }
7897
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007898 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007899 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007900 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00007901 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01007902 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7903 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10007904 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01007905 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007906 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10007907 }
Chris Wilson05394f32010-11-08 19:18:58 +00007908 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007909 }
7910
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007911 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04007912 I915_WRITE(CURSIZE, (height << 12) | width);
7913
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007914 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007915 if (intel_crtc->cursor_bo) {
Damien Lespiau3d13ef22014-02-07 19:12:47 +00007916 if (INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00007917 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10007918 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7919 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01007920 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00007921 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007922 }
Jesse Barnes80824002009-09-10 15:28:06 -07007923
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007924 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007925
Chris Wilson64f962e2014-03-26 12:38:15 +00007926 old_width = intel_crtc->cursor_width;
7927
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007928 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00007929 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01007930 intel_crtc->cursor_width = width;
7931 intel_crtc->cursor_height = height;
7932
Chris Wilson64f962e2014-03-26 12:38:15 +00007933 if (intel_crtc->active) {
7934 if (old_width != width)
7935 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007936 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00007937 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05007938
Jesse Barnes79e53942008-11-07 14:24:08 -08007939 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01007940fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01007941 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05007942fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10007943 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00007944fail:
Chris Wilson05394f32010-11-08 19:18:58 +00007945 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10007946 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08007947}
7948
7949static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7950{
Jesse Barnes79e53942008-11-07 14:24:08 -08007951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007952
Ville Syrjälä92e76c82013-10-21 19:01:58 +03007953 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7954 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07007955
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03007956 if (intel_crtc->active)
7957 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08007958
7959 return 0;
7960}
7961
Jesse Barnes79e53942008-11-07 14:24:08 -08007962static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01007963 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08007964{
James Simmons72034252010-08-03 01:33:19 +01007965 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007967
James Simmons72034252010-08-03 01:33:19 +01007968 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007969 intel_crtc->lut_r[i] = red[i] >> 8;
7970 intel_crtc->lut_g[i] = green[i] >> 8;
7971 intel_crtc->lut_b[i] = blue[i] >> 8;
7972 }
7973
7974 intel_crtc_load_lut(crtc);
7975}
7976
Jesse Barnes79e53942008-11-07 14:24:08 -08007977/* VESA 640x480x72Hz mode to set on the pipe */
7978static struct drm_display_mode load_detect_mode = {
7979 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7980 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7981};
7982
Daniel Vettera8bb6812014-02-10 18:00:39 +01007983struct drm_framebuffer *
7984__intel_framebuffer_create(struct drm_device *dev,
7985 struct drm_mode_fb_cmd2 *mode_cmd,
7986 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01007987{
7988 struct intel_framebuffer *intel_fb;
7989 int ret;
7990
7991 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7992 if (!intel_fb) {
7993 drm_gem_object_unreference_unlocked(&obj->base);
7994 return ERR_PTR(-ENOMEM);
7995 }
7996
7997 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02007998 if (ret)
7999 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008000
8001 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008002err:
8003 drm_gem_object_unreference_unlocked(&obj->base);
8004 kfree(intel_fb);
8005
8006 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008007}
8008
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008009static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008010intel_framebuffer_create(struct drm_device *dev,
8011 struct drm_mode_fb_cmd2 *mode_cmd,
8012 struct drm_i915_gem_object *obj)
8013{
8014 struct drm_framebuffer *fb;
8015 int ret;
8016
8017 ret = i915_mutex_lock_interruptible(dev);
8018 if (ret)
8019 return ERR_PTR(ret);
8020 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8021 mutex_unlock(&dev->struct_mutex);
8022
8023 return fb;
8024}
8025
Chris Wilsond2dff872011-04-19 08:36:26 +01008026static u32
8027intel_framebuffer_pitch_for_width(int width, int bpp)
8028{
8029 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8030 return ALIGN(pitch, 64);
8031}
8032
8033static u32
8034intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8035{
8036 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8037 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8038}
8039
8040static struct drm_framebuffer *
8041intel_framebuffer_create_for_mode(struct drm_device *dev,
8042 struct drm_display_mode *mode,
8043 int depth, int bpp)
8044{
8045 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008046 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008047
8048 obj = i915_gem_alloc_object(dev,
8049 intel_framebuffer_size_for_mode(mode, bpp));
8050 if (obj == NULL)
8051 return ERR_PTR(-ENOMEM);
8052
8053 mode_cmd.width = mode->hdisplay;
8054 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008055 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8056 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008057 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008058
8059 return intel_framebuffer_create(dev, &mode_cmd, obj);
8060}
8061
8062static struct drm_framebuffer *
8063mode_fits_in_fbdev(struct drm_device *dev,
8064 struct drm_display_mode *mode)
8065{
Daniel Vetter4520f532013-10-09 09:18:51 +02008066#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008067 struct drm_i915_private *dev_priv = dev->dev_private;
8068 struct drm_i915_gem_object *obj;
8069 struct drm_framebuffer *fb;
8070
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008071 if (!dev_priv->fbdev)
8072 return NULL;
8073
8074 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008075 return NULL;
8076
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008077 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008078 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008079
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008080 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008081 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8082 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008083 return NULL;
8084
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008085 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008086 return NULL;
8087
8088 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008089#else
8090 return NULL;
8091#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008092}
8093
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008094bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008095 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01008096 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008097{
8098 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008099 struct intel_encoder *intel_encoder =
8100 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008101 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008102 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008103 struct drm_crtc *crtc = NULL;
8104 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008105 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08008106 int i = -1;
8107
Chris Wilsond2dff872011-04-19 08:36:26 +01008108 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8109 connector->base.id, drm_get_connector_name(connector),
8110 encoder->base.id, drm_get_encoder_name(encoder));
8111
Jesse Barnes79e53942008-11-07 14:24:08 -08008112 /*
8113 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008114 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008115 * - if the connector already has an assigned crtc, use it (but make
8116 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008117 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008118 * - try to find the first unused crtc that can drive this connector,
8119 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008120 */
8121
8122 /* See if we already have a CRTC for this connector */
8123 if (encoder->crtc) {
8124 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008125
Daniel Vetter7b240562012-12-12 00:35:33 +01008126 mutex_lock(&crtc->mutex);
8127
Daniel Vetter24218aa2012-08-12 19:27:11 +02008128 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008129 old->load_detect_temp = false;
8130
8131 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008132 if (connector->dpms != DRM_MODE_DPMS_ON)
8133 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008134
Chris Wilson71731882011-04-19 23:10:58 +01008135 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008136 }
8137
8138 /* Find an unused one (if possible) */
8139 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
8140 i++;
8141 if (!(encoder->possible_crtcs & (1 << i)))
8142 continue;
8143 if (!possible_crtc->enabled) {
8144 crtc = possible_crtc;
8145 break;
8146 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008147 }
8148
8149 /*
8150 * If we didn't find an unused CRTC, don't use any.
8151 */
8152 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008153 DRM_DEBUG_KMS("no pipe available for load-detect\n");
8154 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008155 }
8156
Daniel Vetter7b240562012-12-12 00:35:33 +01008157 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02008158 intel_encoder->new_crtc = to_intel_crtc(crtc);
8159 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008160
8161 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008162 intel_crtc->new_enabled = true;
8163 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008164 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008165 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008166 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008167
Chris Wilson64927112011-04-20 07:25:26 +01008168 if (!mode)
8169 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008170
Chris Wilsond2dff872011-04-19 08:36:26 +01008171 /* We need a framebuffer large enough to accommodate all accesses
8172 * that the plane may generate whilst we perform load detection.
8173 * We can not rely on the fbcon either being present (we get called
8174 * during its initialisation to detect all boot displays, or it may
8175 * not even exist) or that it is large enough to satisfy the
8176 * requested mode.
8177 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008178 fb = mode_fits_in_fbdev(dev, mode);
8179 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008180 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008181 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8182 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008183 } else
8184 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008185 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008186 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008187 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008188 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008189
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008190 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008191 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008192 if (old->release_fb)
8193 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008194 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008195 }
Chris Wilson71731882011-04-19 23:10:58 +01008196
Jesse Barnes79e53942008-11-07 14:24:08 -08008197 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008198 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008199 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008200
8201 fail:
8202 intel_crtc->new_enabled = crtc->enabled;
8203 if (intel_crtc->new_enabled)
8204 intel_crtc->new_config = &intel_crtc->config;
8205 else
8206 intel_crtc->new_config = NULL;
8207 mutex_unlock(&crtc->mutex);
8208 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008209}
8210
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008211void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01008212 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008213{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008214 struct intel_encoder *intel_encoder =
8215 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008216 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008217 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008219
Chris Wilsond2dff872011-04-19 08:36:26 +01008220 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8221 connector->base.id, drm_get_connector_name(connector),
8222 encoder->base.id, drm_get_encoder_name(encoder));
8223
Chris Wilson8261b192011-04-19 23:18:09 +01008224 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008225 to_intel_connector(connector)->new_encoder = NULL;
8226 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008227 intel_crtc->new_enabled = false;
8228 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008229 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008230
Daniel Vetter36206362012-12-10 20:42:17 +01008231 if (old->release_fb) {
8232 drm_framebuffer_unregister_private(old->release_fb);
8233 drm_framebuffer_unreference(old->release_fb);
8234 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008235
Daniel Vetter67c96402013-01-23 16:25:09 +00008236 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01008237 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008238 }
8239
Eric Anholtc751ce42010-03-25 11:48:48 -07008240 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008241 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8242 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01008243
8244 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08008245}
8246
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008247static int i9xx_pll_refclk(struct drm_device *dev,
8248 const struct intel_crtc_config *pipe_config)
8249{
8250 struct drm_i915_private *dev_priv = dev->dev_private;
8251 u32 dpll = pipe_config->dpll_hw_state.dpll;
8252
8253 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008254 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008255 else if (HAS_PCH_SPLIT(dev))
8256 return 120000;
8257 else if (!IS_GEN2(dev))
8258 return 96000;
8259 else
8260 return 48000;
8261}
8262
Jesse Barnes79e53942008-11-07 14:24:08 -08008263/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008264static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8265 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008266{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008267 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008268 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008269 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008270 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008271 u32 fp;
8272 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008273 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008274
8275 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008276 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008277 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008278 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008279
8280 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008281 if (IS_PINEVIEW(dev)) {
8282 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8283 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008284 } else {
8285 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8286 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8287 }
8288
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008289 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008290 if (IS_PINEVIEW(dev))
8291 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8292 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008293 else
8294 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008295 DPLL_FPA01_P1_POST_DIV_SHIFT);
8296
8297 switch (dpll & DPLL_MODE_MASK) {
8298 case DPLLB_MODE_DAC_SERIAL:
8299 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8300 5 : 10;
8301 break;
8302 case DPLLB_MODE_LVDS:
8303 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8304 7 : 14;
8305 break;
8306 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008307 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008308 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008309 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008310 }
8311
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008312 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008313 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008314 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008315 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008316 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008317 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008318 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008319
8320 if (is_lvds) {
8321 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8322 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008323
8324 if (lvds & LVDS_CLKB_POWER_UP)
8325 clock.p2 = 7;
8326 else
8327 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008328 } else {
8329 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8330 clock.p1 = 2;
8331 else {
8332 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8333 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8334 }
8335 if (dpll & PLL_P2_DIVIDE_BY_4)
8336 clock.p2 = 4;
8337 else
8338 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008339 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008340
8341 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008342 }
8343
Ville Syrjälä18442d02013-09-13 16:00:08 +03008344 /*
8345 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008346 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008347 * encoder's get_config() function.
8348 */
8349 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008350}
8351
Ville Syrjälä6878da02013-09-13 15:59:11 +03008352int intel_dotclock_calculate(int link_freq,
8353 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008354{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008355 /*
8356 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008357 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008358 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008359 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008360 *
8361 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008362 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008363 */
8364
Ville Syrjälä6878da02013-09-13 15:59:11 +03008365 if (!m_n->link_n)
8366 return 0;
8367
8368 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8369}
8370
Ville Syrjälä18442d02013-09-13 16:00:08 +03008371static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8372 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008373{
8374 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008375
8376 /* read out port_clock from the DPLL */
8377 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008378
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008379 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008380 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008381 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008382 * agree once we know their relationship in the encoder's
8383 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008384 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008385 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008386 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8387 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008388}
8389
8390/** Returns the currently programmed mode of the given pipe. */
8391struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8392 struct drm_crtc *crtc)
8393{
Jesse Barnes548f2452011-02-17 10:40:53 -08008394 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008396 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008397 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008398 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008399 int htot = I915_READ(HTOTAL(cpu_transcoder));
8400 int hsync = I915_READ(HSYNC(cpu_transcoder));
8401 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8402 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008403 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008404
8405 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8406 if (!mode)
8407 return NULL;
8408
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008409 /*
8410 * Construct a pipe_config sufficient for getting the clock info
8411 * back out of crtc_clock_get.
8412 *
8413 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8414 * to use a real value here instead.
8415 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008416 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008417 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008418 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8419 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8420 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008421 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8422
Ville Syrjälä773ae032013-09-23 17:48:20 +03008423 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008424 mode->hdisplay = (htot & 0xffff) + 1;
8425 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8426 mode->hsync_start = (hsync & 0xffff) + 1;
8427 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8428 mode->vdisplay = (vtot & 0xffff) + 1;
8429 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8430 mode->vsync_start = (vsync & 0xffff) + 1;
8431 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8432
8433 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008434
8435 return mode;
8436}
8437
Daniel Vetter3dec0092010-08-20 21:40:52 +02008438static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008439{
8440 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008441 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008442 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8443 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008444 int dpll_reg = DPLL(pipe);
8445 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008446
Eric Anholtbad720f2009-10-22 16:11:14 -07008447 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008448 return;
8449
8450 if (!dev_priv->lvds_downclock_avail)
8451 return;
8452
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008453 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008454 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008455 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008456
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008457 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008458
8459 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8460 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008461 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008462
Jesse Barnes652c3932009-08-17 13:31:43 -07008463 dpll = I915_READ(dpll_reg);
8464 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008465 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008466 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008467}
8468
8469static void intel_decrease_pllclock(struct drm_crtc *crtc)
8470{
8471 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008472 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008474
Eric Anholtbad720f2009-10-22 16:11:14 -07008475 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008476 return;
8477
8478 if (!dev_priv->lvds_downclock_avail)
8479 return;
8480
8481 /*
8482 * Since this is called by a timer, we should never get here in
8483 * the manual case.
8484 */
8485 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008486 int pipe = intel_crtc->pipe;
8487 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008488 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008489
Zhao Yakui44d98a62009-10-09 11:39:40 +08008490 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008491
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008492 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008493
Chris Wilson074b5e12012-05-02 12:07:06 +01008494 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008495 dpll |= DISPLAY_RATE_SELECT_FPA1;
8496 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008497 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008498 dpll = I915_READ(dpll_reg);
8499 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008500 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008501 }
8502
8503}
8504
Chris Wilsonf047e392012-07-21 12:31:41 +01008505void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008506{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008507 struct drm_i915_private *dev_priv = dev->dev_private;
8508
Chris Wilsonf62a0072014-02-21 17:55:39 +00008509 if (dev_priv->mm.busy)
8510 return;
8511
Paulo Zanoni43694d62014-03-07 20:08:08 -03008512 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008513 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008514 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008515}
8516
8517void intel_mark_idle(struct drm_device *dev)
8518{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008519 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008520 struct drm_crtc *crtc;
8521
Chris Wilsonf62a0072014-02-21 17:55:39 +00008522 if (!dev_priv->mm.busy)
8523 return;
8524
8525 dev_priv->mm.busy = false;
8526
Jani Nikulad330a952014-01-21 11:24:25 +02008527 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008528 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008529
8530 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07008531 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008532 continue;
8533
8534 intel_decrease_pllclock(crtc);
8535 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008536
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008537 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008538 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008539
8540out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008541 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008542}
8543
Chris Wilsonc65355b2013-06-06 16:53:41 -03008544void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8545 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008546{
8547 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008548 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008549
Jani Nikulad330a952014-01-21 11:24:25 +02008550 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07008551 return;
8552
Jesse Barnes652c3932009-08-17 13:31:43 -07008553 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07008554 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -07008555 continue;
8556
Matt Roperf4510a22014-04-01 15:22:40 -07008557 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
Chris Wilsonc65355b2013-06-06 16:53:41 -03008558 continue;
8559
8560 intel_increase_pllclock(crtc);
8561 if (ring && intel_fbc_enabled(dev))
8562 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008563 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008564}
8565
Jesse Barnes79e53942008-11-07 14:24:08 -08008566static void intel_crtc_destroy(struct drm_crtc *crtc)
8567{
8568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008569 struct drm_device *dev = crtc->dev;
8570 struct intel_unpin_work *work;
8571 unsigned long flags;
8572
8573 spin_lock_irqsave(&dev->event_lock, flags);
8574 work = intel_crtc->unpin_work;
8575 intel_crtc->unpin_work = NULL;
8576 spin_unlock_irqrestore(&dev->event_lock, flags);
8577
8578 if (work) {
8579 cancel_work_sync(&work->work);
8580 kfree(work);
8581 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008582
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008583 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8584
Jesse Barnes79e53942008-11-07 14:24:08 -08008585 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008586
Jesse Barnes79e53942008-11-07 14:24:08 -08008587 kfree(intel_crtc);
8588}
8589
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008590static void intel_unpin_work_fn(struct work_struct *__work)
8591{
8592 struct intel_unpin_work *work =
8593 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008594 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008595
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008596 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008597 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008598 drm_gem_object_unreference(&work->pending_flip_obj->base);
8599 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008600
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008601 intel_update_fbc(dev);
8602 mutex_unlock(&dev->struct_mutex);
8603
8604 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8605 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8606
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008607 kfree(work);
8608}
8609
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008610static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008611 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008612{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008613 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8615 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008616 unsigned long flags;
8617
8618 /* Ignore early vblank irqs */
8619 if (intel_crtc == NULL)
8620 return;
8621
8622 spin_lock_irqsave(&dev->event_lock, flags);
8623 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008624
8625 /* Ensure we don't miss a work->pending update ... */
8626 smp_rmb();
8627
8628 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008629 spin_unlock_irqrestore(&dev->event_lock, flags);
8630 return;
8631 }
8632
Chris Wilsone7d841c2012-12-03 11:36:30 +00008633 /* and that the unpin work is consistent wrt ->pending. */
8634 smp_rmb();
8635
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008636 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008637
Rob Clark45a066e2012-10-08 14:50:40 -05008638 if (work->event)
8639 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008640
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008641 drm_vblank_put(dev, intel_crtc->pipe);
8642
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008643 spin_unlock_irqrestore(&dev->event_lock, flags);
8644
Daniel Vetter2c10d572012-12-20 21:24:07 +01008645 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008646
8647 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008648
8649 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008650}
8651
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008652void intel_finish_page_flip(struct drm_device *dev, int pipe)
8653{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008654 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008655 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8656
Mario Kleiner49b14a52010-12-09 07:00:07 +01008657 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008658}
8659
8660void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8661{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008662 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008663 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8664
Mario Kleiner49b14a52010-12-09 07:00:07 +01008665 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008666}
8667
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008668void intel_prepare_page_flip(struct drm_device *dev, int plane)
8669{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008670 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008671 struct intel_crtc *intel_crtc =
8672 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8673 unsigned long flags;
8674
Chris Wilsone7d841c2012-12-03 11:36:30 +00008675 /* NB: An MMIO update of the plane base pointer will also
8676 * generate a page-flip completion irq, i.e. every modeset
8677 * is also accompanied by a spurious intel_prepare_page_flip().
8678 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008679 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008680 if (intel_crtc->unpin_work)
8681 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008682 spin_unlock_irqrestore(&dev->event_lock, flags);
8683}
8684
Chris Wilsone7d841c2012-12-03 11:36:30 +00008685inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8686{
8687 /* Ensure that the work item is consistent when activating it ... */
8688 smp_wmb();
8689 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8690 /* and that it is marked active as soon as the irq could fire. */
8691 smp_wmb();
8692}
8693
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008694static int intel_gen2_queue_flip(struct drm_device *dev,
8695 struct drm_crtc *crtc,
8696 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008697 struct drm_i915_gem_object *obj,
8698 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008699{
8700 struct drm_i915_private *dev_priv = dev->dev_private;
8701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008702 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008703 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008704 int ret;
8705
Daniel Vetter6d90c952012-04-26 23:28:05 +02008706 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008707 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008708 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008709
Daniel Vetter6d90c952012-04-26 23:28:05 +02008710 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008711 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008712 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008713
8714 /* Can't queue multiple flips, so wait for the previous
8715 * one to finish before executing the next.
8716 */
8717 if (intel_crtc->plane)
8718 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8719 else
8720 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008721 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8722 intel_ring_emit(ring, MI_NOOP);
8723 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8724 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8725 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008726 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008727 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00008728
8729 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008730 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008731 return 0;
8732
8733err_unpin:
8734 intel_unpin_fb_obj(obj);
8735err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008736 return ret;
8737}
8738
8739static int intel_gen3_queue_flip(struct drm_device *dev,
8740 struct drm_crtc *crtc,
8741 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008742 struct drm_i915_gem_object *obj,
8743 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008744{
8745 struct drm_i915_private *dev_priv = dev->dev_private;
8746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008747 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008748 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008749 int ret;
8750
Daniel Vetter6d90c952012-04-26 23:28:05 +02008751 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008752 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008753 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008754
Daniel Vetter6d90c952012-04-26 23:28:05 +02008755 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008756 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008757 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008758
8759 if (intel_crtc->plane)
8760 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8761 else
8762 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008763 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8764 intel_ring_emit(ring, MI_NOOP);
8765 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8766 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8767 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008768 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008769 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008770
Chris Wilsone7d841c2012-12-03 11:36:30 +00008771 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008772 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008773 return 0;
8774
8775err_unpin:
8776 intel_unpin_fb_obj(obj);
8777err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008778 return ret;
8779}
8780
8781static int intel_gen4_queue_flip(struct drm_device *dev,
8782 struct drm_crtc *crtc,
8783 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008784 struct drm_i915_gem_object *obj,
8785 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008786{
8787 struct drm_i915_private *dev_priv = dev->dev_private;
8788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8789 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008790 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008791 int ret;
8792
Daniel Vetter6d90c952012-04-26 23:28:05 +02008793 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008794 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008795 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008796
Daniel Vetter6d90c952012-04-26 23:28:05 +02008797 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008798 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008799 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008800
8801 /* i965+ uses the linear or tiled offsets from the
8802 * Display Registers (which do not change across a page-flip)
8803 * so we need only reprogram the base address.
8804 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02008805 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8806 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8807 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02008808 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008809 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02008810 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008811
8812 /* XXX Enabling the panel-fitter across page-flip is so far
8813 * untested on non-native modes, so ignore it for now.
8814 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8815 */
8816 pf = 0;
8817 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008818 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008819
8820 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008821 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008822 return 0;
8823
8824err_unpin:
8825 intel_unpin_fb_obj(obj);
8826err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008827 return ret;
8828}
8829
8830static int intel_gen6_queue_flip(struct drm_device *dev,
8831 struct drm_crtc *crtc,
8832 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008833 struct drm_i915_gem_object *obj,
8834 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008835{
8836 struct drm_i915_private *dev_priv = dev->dev_private;
8837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02008838 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008839 uint32_t pf, pipesrc;
8840 int ret;
8841
Daniel Vetter6d90c952012-04-26 23:28:05 +02008842 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008843 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008844 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008845
Daniel Vetter6d90c952012-04-26 23:28:05 +02008846 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008847 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008848 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008849
Daniel Vetter6d90c952012-04-26 23:28:05 +02008850 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8851 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8852 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008853 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008854
Chris Wilson99d9acd2012-04-17 20:37:00 +01008855 /* Contrary to the suggestions in the documentation,
8856 * "Enable Panel Fitter" does not seem to be required when page
8857 * flipping with a non-native mode, and worse causes a normal
8858 * modeset to fail.
8859 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8860 */
8861 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008862 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02008863 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00008864
8865 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008866 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008867 return 0;
8868
8869err_unpin:
8870 intel_unpin_fb_obj(obj);
8871err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008872 return ret;
8873}
8874
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008875static int intel_gen7_queue_flip(struct drm_device *dev,
8876 struct drm_crtc *crtc,
8877 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008878 struct drm_i915_gem_object *obj,
8879 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008880{
8881 struct drm_i915_private *dev_priv = dev->dev_private;
8882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008883 struct intel_ring_buffer *ring;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008884 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01008885 int len, ret;
8886
8887 ring = obj->ring;
Chris Wilson1c5fd082013-09-04 10:54:30 +01008888 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
Chris Wilsonffe74d72013-08-26 20:58:12 +01008889 ring = &dev_priv->ring[BCS];
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008890
8891 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8892 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008893 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008894
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008895 switch(intel_crtc->plane) {
8896 case PLANE_A:
8897 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8898 break;
8899 case PLANE_B:
8900 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8901 break;
8902 case PLANE_C:
8903 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8904 break;
8905 default:
8906 WARN_ONCE(1, "unknown plane in flip command\n");
8907 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03008908 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008909 }
8910
Chris Wilsonffe74d72013-08-26 20:58:12 +01008911 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01008912 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01008913 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01008914 /*
8915 * On Gen 8, SRM is now taking an extra dword to accommodate
8916 * 48bits addresses, and we need a NOOP for the batch size to
8917 * stay even.
8918 */
8919 if (IS_GEN8(dev))
8920 len += 2;
8921 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01008922
Ville Syrjäläf66fab82014-02-11 19:52:06 +02008923 /*
8924 * BSpec MI_DISPLAY_FLIP for IVB:
8925 * "The full packet must be contained within the same cache line."
8926 *
8927 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
8928 * cacheline, if we ever start emitting more commands before
8929 * the MI_DISPLAY_FLIP we may need to first emit everything else,
8930 * then do the cacheline alignment, and finally emit the
8931 * MI_DISPLAY_FLIP.
8932 */
8933 ret = intel_ring_cacheline_align(ring);
8934 if (ret)
8935 goto err_unpin;
8936
Chris Wilsonffe74d72013-08-26 20:58:12 +01008937 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008938 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01008939 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008940
Chris Wilsonffe74d72013-08-26 20:58:12 +01008941 /* Unmask the flip-done completion message. Note that the bspec says that
8942 * we should do this for both the BCS and RCS, and that we must not unmask
8943 * more than one flip event at any time (or ensure that one flip message
8944 * can be sent by waiting for flip-done prior to queueing new flips).
8945 * Experimentation says that BCS works despite DERRMR masking all
8946 * flip-done completion events and that unmasking all planes at once
8947 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8948 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8949 */
8950 if (ring->id == RCS) {
8951 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8952 intel_ring_emit(ring, DERRMR);
8953 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8954 DERRMR_PIPEB_PRI_FLIP_DONE |
8955 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01008956 if (IS_GEN8(dev))
8957 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
8958 MI_SRM_LRM_GLOBAL_GTT);
8959 else
8960 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
8961 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01008962 intel_ring_emit(ring, DERRMR);
8963 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01008964 if (IS_GEN8(dev)) {
8965 intel_ring_emit(ring, 0);
8966 intel_ring_emit(ring, MI_NOOP);
8967 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01008968 }
8969
Daniel Vettercb05d8d2012-05-23 14:02:00 +02008970 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008971 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008972 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008973 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00008974
8975 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01008976 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01008977 return 0;
8978
8979err_unpin:
8980 intel_unpin_fb_obj(obj);
8981err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008982 return ret;
8983}
8984
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008985static int intel_default_queue_flip(struct drm_device *dev,
8986 struct drm_crtc *crtc,
8987 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008988 struct drm_i915_gem_object *obj,
8989 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008990{
8991 return -ENODEV;
8992}
8993
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008994static int intel_crtc_page_flip(struct drm_crtc *crtc,
8995 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008996 struct drm_pending_vblank_event *event,
8997 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008998{
8999 struct drm_device *dev = crtc->dev;
9000 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009001 struct drm_framebuffer *old_fb = crtc->primary->fb;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009002 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9004 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009005 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01009006 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009007
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009008 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009009 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009010 return -EINVAL;
9011
9012 /*
9013 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9014 * Note that pitch changes could also affect these register.
9015 */
9016 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009017 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9018 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009019 return -EINVAL;
9020
Chris Wilsonf900db42014-02-20 09:26:13 +00009021 if (i915_terminally_wedged(&dev_priv->gpu_error))
9022 goto out_hang;
9023
Daniel Vetterb14c5672013-09-19 12:18:32 +02009024 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009025 if (work == NULL)
9026 return -ENOMEM;
9027
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009028 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009029 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009030 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009031 INIT_WORK(&work->work, intel_unpin_work_fn);
9032
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009033 ret = drm_vblank_get(dev, intel_crtc->pipe);
9034 if (ret)
9035 goto free_work;
9036
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009037 /* We borrow the event spin lock for protecting unpin_work */
9038 spin_lock_irqsave(&dev->event_lock, flags);
9039 if (intel_crtc->unpin_work) {
9040 spin_unlock_irqrestore(&dev->event_lock, flags);
9041 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009042 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01009043
9044 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009045 return -EBUSY;
9046 }
9047 intel_crtc->unpin_work = work;
9048 spin_unlock_irqrestore(&dev->event_lock, flags);
9049
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009050 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9051 flush_workqueue(dev_priv->wq);
9052
Chris Wilson79158102012-05-23 11:13:58 +01009053 ret = i915_mutex_lock_interruptible(dev);
9054 if (ret)
9055 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009056
Jesse Barnes75dfca82010-02-10 15:09:44 -08009057 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009058 drm_gem_object_reference(&work->old_fb_obj->base);
9059 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009060
Matt Roperf4510a22014-04-01 15:22:40 -07009061 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009062
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009063 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009064
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01009065 work->enable_stall_check = true;
9066
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009067 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009068 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009069
Keith Packarded8d1972013-07-22 18:49:58 -07009070 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009071 if (ret)
9072 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009073
Chris Wilson7782de32011-07-08 12:22:41 +01009074 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009075 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009076 mutex_unlock(&dev->struct_mutex);
9077
Jesse Barnese5510fa2010-07-01 16:48:37 -07009078 trace_i915_flip_request(intel_crtc->plane, obj);
9079
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009080 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009081
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009082cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009083 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009084 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009085 drm_gem_object_unreference(&work->old_fb_obj->base);
9086 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009087 mutex_unlock(&dev->struct_mutex);
9088
Chris Wilson79158102012-05-23 11:13:58 +01009089cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01009090 spin_lock_irqsave(&dev->event_lock, flags);
9091 intel_crtc->unpin_work = NULL;
9092 spin_unlock_irqrestore(&dev->event_lock, flags);
9093
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009094 drm_vblank_put(dev, intel_crtc->pipe);
9095free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009096 kfree(work);
9097
Chris Wilsonf900db42014-02-20 09:26:13 +00009098 if (ret == -EIO) {
9099out_hang:
9100 intel_crtc_wait_for_pending_flips(crtc);
9101 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9102 if (ret == 0 && event)
9103 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9104 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009105 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009106}
9107
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009108static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009109 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9110 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009111};
9112
Daniel Vetter9a935852012-07-05 22:34:27 +02009113/**
9114 * intel_modeset_update_staged_output_state
9115 *
9116 * Updates the staged output configuration state, e.g. after we've read out the
9117 * current hw state.
9118 */
9119static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9120{
Ville Syrjälä76688512014-01-10 11:28:06 +02009121 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009122 struct intel_encoder *encoder;
9123 struct intel_connector *connector;
9124
9125 list_for_each_entry(connector, &dev->mode_config.connector_list,
9126 base.head) {
9127 connector->new_encoder =
9128 to_intel_encoder(connector->base.encoder);
9129 }
9130
9131 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9132 base.head) {
9133 encoder->new_crtc =
9134 to_intel_crtc(encoder->base.crtc);
9135 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009136
9137 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9138 base.head) {
9139 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009140
9141 if (crtc->new_enabled)
9142 crtc->new_config = &crtc->config;
9143 else
9144 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009145 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009146}
9147
9148/**
9149 * intel_modeset_commit_output_state
9150 *
9151 * This function copies the stage display pipe configuration to the real one.
9152 */
9153static void intel_modeset_commit_output_state(struct drm_device *dev)
9154{
Ville Syrjälä76688512014-01-10 11:28:06 +02009155 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009156 struct intel_encoder *encoder;
9157 struct intel_connector *connector;
9158
9159 list_for_each_entry(connector, &dev->mode_config.connector_list,
9160 base.head) {
9161 connector->base.encoder = &connector->new_encoder->base;
9162 }
9163
9164 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9165 base.head) {
9166 encoder->base.crtc = &encoder->new_crtc->base;
9167 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009168
9169 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9170 base.head) {
9171 crtc->base.enabled = crtc->new_enabled;
9172 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009173}
9174
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009175static void
9176connected_sink_compute_bpp(struct intel_connector * connector,
9177 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009178{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009179 int bpp = pipe_config->pipe_bpp;
9180
9181 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9182 connector->base.base.id,
9183 drm_get_connector_name(&connector->base));
9184
9185 /* Don't use an invalid EDID bpc value */
9186 if (connector->base.display_info.bpc &&
9187 connector->base.display_info.bpc * 3 < bpp) {
9188 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9189 bpp, connector->base.display_info.bpc*3);
9190 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9191 }
9192
9193 /* Clamp bpp to 8 on screens without EDID 1.4 */
9194 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9195 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9196 bpp);
9197 pipe_config->pipe_bpp = 24;
9198 }
9199}
9200
9201static int
9202compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9203 struct drm_framebuffer *fb,
9204 struct intel_crtc_config *pipe_config)
9205{
9206 struct drm_device *dev = crtc->base.dev;
9207 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009208 int bpp;
9209
Daniel Vetterd42264b2013-03-28 16:38:08 +01009210 switch (fb->pixel_format) {
9211 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009212 bpp = 8*3; /* since we go through a colormap */
9213 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009214 case DRM_FORMAT_XRGB1555:
9215 case DRM_FORMAT_ARGB1555:
9216 /* checked in intel_framebuffer_init already */
9217 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9218 return -EINVAL;
9219 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009220 bpp = 6*3; /* min is 18bpp */
9221 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009222 case DRM_FORMAT_XBGR8888:
9223 case DRM_FORMAT_ABGR8888:
9224 /* checked in intel_framebuffer_init already */
9225 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9226 return -EINVAL;
9227 case DRM_FORMAT_XRGB8888:
9228 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009229 bpp = 8*3;
9230 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009231 case DRM_FORMAT_XRGB2101010:
9232 case DRM_FORMAT_ARGB2101010:
9233 case DRM_FORMAT_XBGR2101010:
9234 case DRM_FORMAT_ABGR2101010:
9235 /* checked in intel_framebuffer_init already */
9236 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009237 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009238 bpp = 10*3;
9239 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009240 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009241 default:
9242 DRM_DEBUG_KMS("unsupported depth\n");
9243 return -EINVAL;
9244 }
9245
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009246 pipe_config->pipe_bpp = bpp;
9247
9248 /* Clamp display bpp to EDID value */
9249 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009250 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009251 if (!connector->new_encoder ||
9252 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009253 continue;
9254
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009255 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009256 }
9257
9258 return bpp;
9259}
9260
Daniel Vetter644db712013-09-19 14:53:58 +02009261static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9262{
9263 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9264 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009265 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009266 mode->crtc_hdisplay, mode->crtc_hsync_start,
9267 mode->crtc_hsync_end, mode->crtc_htotal,
9268 mode->crtc_vdisplay, mode->crtc_vsync_start,
9269 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9270}
9271
Daniel Vetterc0b03412013-05-28 12:05:54 +02009272static void intel_dump_pipe_config(struct intel_crtc *crtc,
9273 struct intel_crtc_config *pipe_config,
9274 const char *context)
9275{
9276 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9277 context, pipe_name(crtc->pipe));
9278
9279 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9280 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9281 pipe_config->pipe_bpp, pipe_config->dither);
9282 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9283 pipe_config->has_pch_encoder,
9284 pipe_config->fdi_lanes,
9285 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9286 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9287 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009288 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9289 pipe_config->has_dp_encoder,
9290 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9291 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9292 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009293 DRM_DEBUG_KMS("requested mode:\n");
9294 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9295 DRM_DEBUG_KMS("adjusted mode:\n");
9296 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009297 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009298 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009299 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9300 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009301 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9302 pipe_config->gmch_pfit.control,
9303 pipe_config->gmch_pfit.pgm_ratios,
9304 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009305 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009306 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009307 pipe_config->pch_pfit.size,
9308 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009309 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009310 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009311}
9312
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009313static bool encoders_cloneable(const struct intel_encoder *a,
9314 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009315{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009316 /* masks could be asymmetric, so check both ways */
9317 return a == b || (a->cloneable & (1 << b->type) &&
9318 b->cloneable & (1 << a->type));
9319}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009320
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009321static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9322 struct intel_encoder *encoder)
9323{
9324 struct drm_device *dev = crtc->base.dev;
9325 struct intel_encoder *source_encoder;
9326
9327 list_for_each_entry(source_encoder,
9328 &dev->mode_config.encoder_list, base.head) {
9329 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009330 continue;
9331
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009332 if (!encoders_cloneable(encoder, source_encoder))
9333 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009334 }
9335
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009336 return true;
9337}
9338
9339static bool check_encoder_cloning(struct intel_crtc *crtc)
9340{
9341 struct drm_device *dev = crtc->base.dev;
9342 struct intel_encoder *encoder;
9343
9344 list_for_each_entry(encoder,
9345 &dev->mode_config.encoder_list, base.head) {
9346 if (encoder->new_crtc != crtc)
9347 continue;
9348
9349 if (!check_single_encoder_cloning(crtc, encoder))
9350 return false;
9351 }
9352
9353 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009354}
9355
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009356static struct intel_crtc_config *
9357intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009358 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009359 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009360{
9361 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009362 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009363 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009364 int plane_bpp, ret = -EINVAL;
9365 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009366
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009367 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009368 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9369 return ERR_PTR(-EINVAL);
9370 }
9371
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009372 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9373 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009374 return ERR_PTR(-ENOMEM);
9375
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009376 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9377 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009378
Daniel Vettere143a212013-07-04 12:01:15 +02009379 pipe_config->cpu_transcoder =
9380 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009381 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009382
Imre Deak2960bc92013-07-30 13:36:32 +03009383 /*
9384 * Sanitize sync polarity flags based on requested ones. If neither
9385 * positive or negative polarity is requested, treat this as meaning
9386 * negative polarity.
9387 */
9388 if (!(pipe_config->adjusted_mode.flags &
9389 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9390 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9391
9392 if (!(pipe_config->adjusted_mode.flags &
9393 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9394 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9395
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009396 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9397 * plane pixel format and any sink constraints into account. Returns the
9398 * source plane bpp so that dithering can be selected on mismatches
9399 * after encoders and crtc also have had their say. */
9400 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9401 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009402 if (plane_bpp < 0)
9403 goto fail;
9404
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009405 /*
9406 * Determine the real pipe dimensions. Note that stereo modes can
9407 * increase the actual pipe size due to the frame doubling and
9408 * insertion of additional space for blanks between the frame. This
9409 * is stored in the crtc timings. We use the requested mode to do this
9410 * computation to clearly distinguish it from the adjusted mode, which
9411 * can be changed by the connectors in the below retry loop.
9412 */
9413 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9414 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9415 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9416
Daniel Vettere29c22c2013-02-21 00:00:16 +01009417encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009418 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009419 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009420 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009421
Daniel Vetter135c81b2013-07-21 21:37:09 +02009422 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009423 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009424
Daniel Vetter7758a112012-07-08 19:40:39 +02009425 /* Pass our mode to the connectors and the CRTC to give them a chance to
9426 * adjust it according to limitations or connector properties, and also
9427 * a chance to reject the mode entirely.
9428 */
9429 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9430 base.head) {
9431
9432 if (&encoder->new_crtc->base != crtc)
9433 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009434
Daniel Vetterefea6e82013-07-21 21:36:59 +02009435 if (!(encoder->compute_config(encoder, pipe_config))) {
9436 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009437 goto fail;
9438 }
9439 }
9440
Daniel Vetterff9a6752013-06-01 17:16:21 +02009441 /* Set default port clock if not overwritten by the encoder. Needs to be
9442 * done afterwards in case the encoder adjusts the mode. */
9443 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009444 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9445 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009446
Daniel Vettera43f6e02013-06-07 23:10:32 +02009447 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009448 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009449 DRM_DEBUG_KMS("CRTC fixup failed\n");
9450 goto fail;
9451 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009452
9453 if (ret == RETRY) {
9454 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9455 ret = -EINVAL;
9456 goto fail;
9457 }
9458
9459 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9460 retry = false;
9461 goto encoder_retry;
9462 }
9463
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009464 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9465 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9466 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9467
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009468 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009469fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009470 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009471 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009472}
9473
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009474/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9475 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9476static void
9477intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9478 unsigned *prepare_pipes, unsigned *disable_pipes)
9479{
9480 struct intel_crtc *intel_crtc;
9481 struct drm_device *dev = crtc->dev;
9482 struct intel_encoder *encoder;
9483 struct intel_connector *connector;
9484 struct drm_crtc *tmp_crtc;
9485
9486 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9487
9488 /* Check which crtcs have changed outputs connected to them, these need
9489 * to be part of the prepare_pipes mask. We don't (yet) support global
9490 * modeset across multiple crtcs, so modeset_pipes will only have one
9491 * bit set at most. */
9492 list_for_each_entry(connector, &dev->mode_config.connector_list,
9493 base.head) {
9494 if (connector->base.encoder == &connector->new_encoder->base)
9495 continue;
9496
9497 if (connector->base.encoder) {
9498 tmp_crtc = connector->base.encoder->crtc;
9499
9500 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9501 }
9502
9503 if (connector->new_encoder)
9504 *prepare_pipes |=
9505 1 << connector->new_encoder->new_crtc->pipe;
9506 }
9507
9508 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9509 base.head) {
9510 if (encoder->base.crtc == &encoder->new_crtc->base)
9511 continue;
9512
9513 if (encoder->base.crtc) {
9514 tmp_crtc = encoder->base.crtc;
9515
9516 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9517 }
9518
9519 if (encoder->new_crtc)
9520 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9521 }
9522
Ville Syrjälä76688512014-01-10 11:28:06 +02009523 /* Check for pipes that will be enabled/disabled ... */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009524 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9525 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009526 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009527 continue;
9528
Ville Syrjälä76688512014-01-10 11:28:06 +02009529 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009530 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +02009531 else
9532 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009533 }
9534
9535
9536 /* set_mode is also used to update properties on life display pipes. */
9537 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +02009538 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009539 *prepare_pipes |= 1 << intel_crtc->pipe;
9540
Daniel Vetterb6c51642013-04-12 18:48:43 +02009541 /*
9542 * For simplicity do a full modeset on any pipe where the output routing
9543 * changed. We could be more clever, but that would require us to be
9544 * more careful with calling the relevant encoder->mode_set functions.
9545 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009546 if (*prepare_pipes)
9547 *modeset_pipes = *prepare_pipes;
9548
9549 /* ... and mask these out. */
9550 *modeset_pipes &= ~(*disable_pipes);
9551 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009552
9553 /*
9554 * HACK: We don't (yet) fully support global modesets. intel_set_config
9555 * obies this rule, but the modeset restore mode of
9556 * intel_modeset_setup_hw_state does not.
9557 */
9558 *modeset_pipes &= 1 << intel_crtc->pipe;
9559 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009560
9561 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9562 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009563}
9564
Daniel Vetterea9d7582012-07-10 10:42:52 +02009565static bool intel_crtc_in_use(struct drm_crtc *crtc)
9566{
9567 struct drm_encoder *encoder;
9568 struct drm_device *dev = crtc->dev;
9569
9570 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9571 if (encoder->crtc == crtc)
9572 return true;
9573
9574 return false;
9575}
9576
9577static void
9578intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9579{
9580 struct intel_encoder *intel_encoder;
9581 struct intel_crtc *intel_crtc;
9582 struct drm_connector *connector;
9583
9584 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9585 base.head) {
9586 if (!intel_encoder->base.crtc)
9587 continue;
9588
9589 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9590
9591 if (prepare_pipes & (1 << intel_crtc->pipe))
9592 intel_encoder->connectors_active = false;
9593 }
9594
9595 intel_modeset_commit_output_state(dev);
9596
Ville Syrjälä76688512014-01-10 11:28:06 +02009597 /* Double check state. */
Daniel Vetterea9d7582012-07-10 10:42:52 +02009598 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9599 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009600 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009601 WARN_ON(intel_crtc->new_config &&
9602 intel_crtc->new_config != &intel_crtc->config);
9603 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009604 }
9605
9606 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9607 if (!connector->encoder || !connector->encoder->crtc)
9608 continue;
9609
9610 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9611
9612 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009613 struct drm_property *dpms_property =
9614 dev->mode_config.dpms_property;
9615
Daniel Vetterea9d7582012-07-10 10:42:52 +02009616 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009617 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009618 dpms_property,
9619 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009620
9621 intel_encoder = to_intel_encoder(connector->encoder);
9622 intel_encoder->connectors_active = true;
9623 }
9624 }
9625
9626}
9627
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009628static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009629{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009630 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009631
9632 if (clock1 == clock2)
9633 return true;
9634
9635 if (!clock1 || !clock2)
9636 return false;
9637
9638 diff = abs(clock1 - clock2);
9639
9640 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9641 return true;
9642
9643 return false;
9644}
9645
Daniel Vetter25c5b262012-07-08 22:08:04 +02009646#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9647 list_for_each_entry((intel_crtc), \
9648 &(dev)->mode_config.crtc_list, \
9649 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009650 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009651
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009652static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009653intel_pipe_config_compare(struct drm_device *dev,
9654 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009655 struct intel_crtc_config *pipe_config)
9656{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009657#define PIPE_CONF_CHECK_X(name) \
9658 if (current_config->name != pipe_config->name) { \
9659 DRM_ERROR("mismatch in " #name " " \
9660 "(expected 0x%08x, found 0x%08x)\n", \
9661 current_config->name, \
9662 pipe_config->name); \
9663 return false; \
9664 }
9665
Daniel Vetter08a24032013-04-19 11:25:34 +02009666#define PIPE_CONF_CHECK_I(name) \
9667 if (current_config->name != pipe_config->name) { \
9668 DRM_ERROR("mismatch in " #name " " \
9669 "(expected %i, found %i)\n", \
9670 current_config->name, \
9671 pipe_config->name); \
9672 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009673 }
9674
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009675#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9676 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009677 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009678 "(expected %i, found %i)\n", \
9679 current_config->name & (mask), \
9680 pipe_config->name & (mask)); \
9681 return false; \
9682 }
9683
Ville Syrjälä5e550652013-09-06 23:29:07 +03009684#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9685 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9686 DRM_ERROR("mismatch in " #name " " \
9687 "(expected %i, found %i)\n", \
9688 current_config->name, \
9689 pipe_config->name); \
9690 return false; \
9691 }
9692
Daniel Vetterbb760062013-06-06 14:55:52 +02009693#define PIPE_CONF_QUIRK(quirk) \
9694 ((current_config->quirks | pipe_config->quirks) & (quirk))
9695
Daniel Vettereccb1402013-05-22 00:50:22 +02009696 PIPE_CONF_CHECK_I(cpu_transcoder);
9697
Daniel Vetter08a24032013-04-19 11:25:34 +02009698 PIPE_CONF_CHECK_I(has_pch_encoder);
9699 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009700 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9701 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9702 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9703 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9704 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009705
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009706 PIPE_CONF_CHECK_I(has_dp_encoder);
9707 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9708 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9709 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9710 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9711 PIPE_CONF_CHECK_I(dp_m_n.tu);
9712
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009713 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9714 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9715 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9716 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9717 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9718 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9719
9720 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9721 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9722 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9723 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9724 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9725 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9726
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009727 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009728
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009729 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9730 DRM_MODE_FLAG_INTERLACE);
9731
Daniel Vetterbb760062013-06-06 14:55:52 +02009732 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9733 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9734 DRM_MODE_FLAG_PHSYNC);
9735 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9736 DRM_MODE_FLAG_NHSYNC);
9737 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9738 DRM_MODE_FLAG_PVSYNC);
9739 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9740 DRM_MODE_FLAG_NVSYNC);
9741 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009742
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009743 PIPE_CONF_CHECK_I(pipe_src_w);
9744 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009745
Daniel Vetter99535992014-04-13 12:00:33 +02009746 /*
9747 * FIXME: BIOS likes to set up a cloned config with lvds+external
9748 * screen. Since we don't yet re-compute the pipe config when moving
9749 * just the lvds port away to another pipe the sw tracking won't match.
9750 *
9751 * Proper atomic modesets with recomputed global state will fix this.
9752 * Until then just don't check gmch state for inherited modes.
9753 */
9754 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
9755 PIPE_CONF_CHECK_I(gmch_pfit.control);
9756 /* pfit ratios are autocomputed by the hw on gen4+ */
9757 if (INTEL_INFO(dev)->gen < 4)
9758 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9759 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
9760 }
9761
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009762 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9763 if (current_config->pch_pfit.enabled) {
9764 PIPE_CONF_CHECK_I(pch_pfit.pos);
9765 PIPE_CONF_CHECK_I(pch_pfit.size);
9766 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009767
Jesse Barnese59150d2014-01-07 13:30:45 -08009768 /* BDW+ don't expose a synchronous way to read the state */
9769 if (IS_HASWELL(dev))
9770 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009771
Ville Syrjälä282740f2013-09-04 18:30:03 +03009772 PIPE_CONF_CHECK_I(double_wide);
9773
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009774 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009775 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02009776 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009777 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9778 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009779
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009780 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9781 PIPE_CONF_CHECK_I(pipe_bpp);
9782
Jesse Barnesa9a7e982014-01-20 14:18:04 -08009783 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
9784 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +03009785
Daniel Vetter66e985c2013-06-05 13:34:20 +02009786#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02009787#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009788#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +03009789#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +02009790#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009791
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009792 return true;
9793}
9794
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009795static void
9796check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009797{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009798 struct intel_connector *connector;
9799
9800 list_for_each_entry(connector, &dev->mode_config.connector_list,
9801 base.head) {
9802 /* This also checks the encoder/connector hw state with the
9803 * ->get_hw_state callbacks. */
9804 intel_connector_check_state(connector);
9805
9806 WARN(&connector->new_encoder->base != connector->base.encoder,
9807 "connector's staged encoder doesn't match current encoder\n");
9808 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009809}
9810
9811static void
9812check_encoder_state(struct drm_device *dev)
9813{
9814 struct intel_encoder *encoder;
9815 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009816
9817 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9818 base.head) {
9819 bool enabled = false;
9820 bool active = false;
9821 enum pipe pipe, tracked_pipe;
9822
9823 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9824 encoder->base.base.id,
9825 drm_get_encoder_name(&encoder->base));
9826
9827 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9828 "encoder's stage crtc doesn't match current crtc\n");
9829 WARN(encoder->connectors_active && !encoder->base.crtc,
9830 "encoder's active_connectors set, but no crtc\n");
9831
9832 list_for_each_entry(connector, &dev->mode_config.connector_list,
9833 base.head) {
9834 if (connector->base.encoder != &encoder->base)
9835 continue;
9836 enabled = true;
9837 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9838 active = true;
9839 }
9840 WARN(!!encoder->base.crtc != enabled,
9841 "encoder's enabled state mismatch "
9842 "(expected %i, found %i)\n",
9843 !!encoder->base.crtc, enabled);
9844 WARN(active && !encoder->base.crtc,
9845 "active encoder with no crtc\n");
9846
9847 WARN(encoder->connectors_active != active,
9848 "encoder's computed active state doesn't match tracked active state "
9849 "(expected %i, found %i)\n", active, encoder->connectors_active);
9850
9851 active = encoder->get_hw_state(encoder, &pipe);
9852 WARN(active != encoder->connectors_active,
9853 "encoder's hw state doesn't match sw tracking "
9854 "(expected %i, found %i)\n",
9855 encoder->connectors_active, active);
9856
9857 if (!encoder->base.crtc)
9858 continue;
9859
9860 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9861 WARN(active && pipe != tracked_pipe,
9862 "active encoder's pipe doesn't match"
9863 "(expected %i, found %i)\n",
9864 tracked_pipe, pipe);
9865
9866 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009867}
9868
9869static void
9870check_crtc_state(struct drm_device *dev)
9871{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009872 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009873 struct intel_crtc *crtc;
9874 struct intel_encoder *encoder;
9875 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009876
9877 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9878 base.head) {
9879 bool enabled = false;
9880 bool active = false;
9881
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009882 memset(&pipe_config, 0, sizeof(pipe_config));
9883
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009884 DRM_DEBUG_KMS("[CRTC:%d]\n",
9885 crtc->base.base.id);
9886
9887 WARN(crtc->active && !crtc->base.enabled,
9888 "active crtc, but not enabled in sw tracking\n");
9889
9890 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9891 base.head) {
9892 if (encoder->base.crtc != &crtc->base)
9893 continue;
9894 enabled = true;
9895 if (encoder->connectors_active)
9896 active = true;
9897 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009898
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009899 WARN(active != crtc->active,
9900 "crtc's computed active state doesn't match tracked active state "
9901 "(expected %i, found %i)\n", active, crtc->active);
9902 WARN(enabled != crtc->base.enabled,
9903 "crtc's computed enabled state doesn't match tracked enabled state "
9904 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9905
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009906 active = dev_priv->display.get_pipe_config(crtc,
9907 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02009908
9909 /* hw state is inconsistent with the pipe A quirk */
9910 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9911 active = crtc->active;
9912
Daniel Vetter6c49f242013-06-06 12:45:25 +02009913 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9914 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +03009915 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +02009916 if (encoder->base.crtc != &crtc->base)
9917 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +01009918 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +02009919 encoder->get_config(encoder, &pipe_config);
9920 }
9921
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009922 WARN(crtc->active != active,
9923 "crtc active state doesn't match with hw state "
9924 "(expected %i, found %i)\n", crtc->active, active);
9925
Daniel Vetterc0b03412013-05-28 12:05:54 +02009926 if (active &&
9927 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9928 WARN(1, "pipe state doesn't match!\n");
9929 intel_dump_pipe_config(crtc, &pipe_config,
9930 "[hw state]");
9931 intel_dump_pipe_config(crtc, &crtc->config,
9932 "[sw state]");
9933 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009934 }
9935}
9936
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009937static void
9938check_shared_dpll_state(struct drm_device *dev)
9939{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009940 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009941 struct intel_crtc *crtc;
9942 struct intel_dpll_hw_state dpll_hw_state;
9943 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02009944
9945 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9946 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9947 int enabled_crtcs = 0, active_crtcs = 0;
9948 bool active;
9949
9950 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9951
9952 DRM_DEBUG_KMS("%s\n", pll->name);
9953
9954 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9955
9956 WARN(pll->active > pll->refcount,
9957 "more active pll users than references: %i vs %i\n",
9958 pll->active, pll->refcount);
9959 WARN(pll->active && !pll->on,
9960 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02009961 WARN(pll->on && !pll->active,
9962 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009963 WARN(pll->on != active,
9964 "pll on state mismatch (expected %i, found %i)\n",
9965 pll->on, active);
9966
9967 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9968 base.head) {
9969 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9970 enabled_crtcs++;
9971 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9972 active_crtcs++;
9973 }
9974 WARN(pll->active != active_crtcs,
9975 "pll active crtcs mismatch (expected %i, found %i)\n",
9976 pll->active, active_crtcs);
9977 WARN(pll->refcount != enabled_crtcs,
9978 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9979 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009980
9981 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9982 sizeof(dpll_hw_state)),
9983 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02009984 }
Daniel Vettera6778b32012-07-02 09:56:42 +02009985}
9986
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02009987void
9988intel_modeset_check_state(struct drm_device *dev)
9989{
9990 check_connector_state(dev);
9991 check_encoder_state(dev);
9992 check_crtc_state(dev);
9993 check_shared_dpll_state(dev);
9994}
9995
Ville Syrjälä18442d02013-09-13 16:00:08 +03009996void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9997 int dotclock)
9998{
9999 /*
10000 * FDI already provided one idea for the dotclock.
10001 * Yell if the encoder disagrees.
10002 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010003 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010004 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010005 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010006}
10007
Daniel Vetterf30da182013-04-11 20:22:50 +020010008static int __intel_set_mode(struct drm_crtc *crtc,
10009 struct drm_display_mode *mode,
10010 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010011{
10012 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010013 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010014 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010015 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010016 struct intel_crtc *intel_crtc;
10017 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010018 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010019
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010020 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010021 if (!saved_mode)
10022 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010023
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010024 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010025 &prepare_pipes, &disable_pipes);
10026
Tim Gardner3ac18232012-12-07 07:54:26 -070010027 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010028
Daniel Vetter25c5b262012-07-08 22:08:04 +020010029 /* Hack: Because we don't (yet) support global modeset on multiple
10030 * crtcs, we don't keep track of the new mode for more than one crtc.
10031 * Hence simply check whether any bit is set in modeset_pipes in all the
10032 * pieces of code that are not yet converted to deal with mutliple crtcs
10033 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010034 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010035 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010036 if (IS_ERR(pipe_config)) {
10037 ret = PTR_ERR(pipe_config);
10038 pipe_config = NULL;
10039
Tim Gardner3ac18232012-12-07 07:54:26 -070010040 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010041 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010042 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10043 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010044 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010045 }
10046
Jesse Barnes30a970c2013-11-04 13:48:12 -080010047 /*
10048 * See if the config requires any additional preparation, e.g.
10049 * to adjust global state with pipes off. We need to do this
10050 * here so we can get the modeset_pipe updated config for the new
10051 * mode set on this crtc. For other crtcs we need to use the
10052 * adjusted_mode bits in the crtc directly.
10053 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010054 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010055 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010056
Ville Syrjäläc164f832013-11-05 22:34:12 +020010057 /* may have added more to prepare_pipes than we should */
10058 prepare_pipes &= ~disable_pipes;
10059 }
10060
Daniel Vetter460da9162013-03-27 00:44:51 +010010061 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10062 intel_crtc_disable(&intel_crtc->base);
10063
Daniel Vetterea9d7582012-07-10 10:42:52 +020010064 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10065 if (intel_crtc->base.enabled)
10066 dev_priv->display.crtc_disable(&intel_crtc->base);
10067 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010068
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010069 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10070 * to set it here already despite that we pass it down the callchain.
10071 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010072 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010073 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010074 /* mode_set/enable/disable functions rely on a correct pipe
10075 * config. */
10076 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010077 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010078
10079 /*
10080 * Calculate and store various constants which
10081 * are later needed by vblank and swap-completion
10082 * timestamping. They are derived from true hwmode.
10083 */
10084 drm_calc_timestamping_constants(crtc,
10085 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010086 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010087
Daniel Vetterea9d7582012-07-10 10:42:52 +020010088 /* Only after disabling all output pipelines that will be changed can we
10089 * update the the output configuration. */
10090 intel_modeset_update_state(dev, prepare_pipes);
10091
Daniel Vetter47fab732012-10-26 10:58:18 +020010092 if (dev_priv->display.modeset_global_resources)
10093 dev_priv->display.modeset_global_resources(dev);
10094
Daniel Vettera6778b32012-07-02 09:56:42 +020010095 /* Set up the DPLL and any encoders state that needs to adjust or depend
10096 * on the DPLL.
10097 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010098 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010099 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010100 x, y, fb);
10101 if (ret)
10102 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010103 }
10104
10105 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010106 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
10107 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +020010108
Daniel Vettera6778b32012-07-02 09:56:42 +020010109 /* FIXME: add subpixel order */
10110done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010111 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010112 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010113
Tim Gardner3ac18232012-12-07 07:54:26 -070010114out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010115 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010116 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010117 return ret;
10118}
10119
Damien Lespiaue7457a92013-08-08 22:28:59 +010010120static int intel_set_mode(struct drm_crtc *crtc,
10121 struct drm_display_mode *mode,
10122 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010123{
10124 int ret;
10125
10126 ret = __intel_set_mode(crtc, mode, x, y, fb);
10127
10128 if (ret == 0)
10129 intel_modeset_check_state(crtc->dev);
10130
10131 return ret;
10132}
10133
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010134void intel_crtc_restore_mode(struct drm_crtc *crtc)
10135{
Matt Roperf4510a22014-04-01 15:22:40 -070010136 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010137}
10138
Daniel Vetter25c5b262012-07-08 22:08:04 +020010139#undef for_each_intel_crtc_masked
10140
Daniel Vetterd9e55602012-07-04 22:16:09 +020010141static void intel_set_config_free(struct intel_set_config *config)
10142{
10143 if (!config)
10144 return;
10145
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010146 kfree(config->save_connector_encoders);
10147 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010148 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010149 kfree(config);
10150}
10151
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010152static int intel_set_config_save_state(struct drm_device *dev,
10153 struct intel_set_config *config)
10154{
Ville Syrjälä76688512014-01-10 11:28:06 +020010155 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010156 struct drm_encoder *encoder;
10157 struct drm_connector *connector;
10158 int count;
10159
Ville Syrjälä76688512014-01-10 11:28:06 +020010160 config->save_crtc_enabled =
10161 kcalloc(dev->mode_config.num_crtc,
10162 sizeof(bool), GFP_KERNEL);
10163 if (!config->save_crtc_enabled)
10164 return -ENOMEM;
10165
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010166 config->save_encoder_crtcs =
10167 kcalloc(dev->mode_config.num_encoder,
10168 sizeof(struct drm_crtc *), GFP_KERNEL);
10169 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010170 return -ENOMEM;
10171
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010172 config->save_connector_encoders =
10173 kcalloc(dev->mode_config.num_connector,
10174 sizeof(struct drm_encoder *), GFP_KERNEL);
10175 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010176 return -ENOMEM;
10177
10178 /* Copy data. Note that driver private data is not affected.
10179 * Should anything bad happen only the expected state is
10180 * restored, not the drivers personal bookkeeping.
10181 */
10182 count = 0;
Ville Syrjälä76688512014-01-10 11:28:06 +020010183 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10184 config->save_crtc_enabled[count++] = crtc->enabled;
10185 }
10186
10187 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010188 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010189 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010190 }
10191
10192 count = 0;
10193 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010194 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010195 }
10196
10197 return 0;
10198}
10199
10200static void intel_set_config_restore_state(struct drm_device *dev,
10201 struct intel_set_config *config)
10202{
Ville Syrjälä76688512014-01-10 11:28:06 +020010203 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010204 struct intel_encoder *encoder;
10205 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010206 int count;
10207
10208 count = 0;
Ville Syrjälä76688512014-01-10 11:28:06 +020010209 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10210 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010211
10212 if (crtc->new_enabled)
10213 crtc->new_config = &crtc->config;
10214 else
10215 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010216 }
10217
10218 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010219 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10220 encoder->new_crtc =
10221 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010222 }
10223
10224 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010225 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10226 connector->new_encoder =
10227 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010228 }
10229}
10230
Imre Deake3de42b2013-05-03 19:44:07 +020010231static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010232is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010233{
10234 int i;
10235
Chris Wilson2e57f472013-07-17 12:14:40 +010010236 if (set->num_connectors == 0)
10237 return false;
10238
10239 if (WARN_ON(set->connectors == NULL))
10240 return false;
10241
10242 for (i = 0; i < set->num_connectors; i++)
10243 if (set->connectors[i]->encoder &&
10244 set->connectors[i]->encoder->crtc == set->crtc &&
10245 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010246 return true;
10247
10248 return false;
10249}
10250
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010251static void
10252intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10253 struct intel_set_config *config)
10254{
10255
10256 /* We should be able to check here if the fb has the same properties
10257 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010258 if (is_crtc_connector_off(set)) {
10259 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010260 } else if (set->crtc->primary->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010261 /* If we have no fb then treat it as a full mode set */
Matt Roperf4510a22014-04-01 15:22:40 -070010262 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010263 struct intel_crtc *intel_crtc =
10264 to_intel_crtc(set->crtc);
10265
Jani Nikulad330a952014-01-21 11:24:25 +020010266 if (intel_crtc->active && i915.fastboot) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010267 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10268 config->fb_changed = true;
10269 } else {
10270 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10271 config->mode_changed = true;
10272 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010273 } else if (set->fb == NULL) {
10274 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010275 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010276 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010277 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010278 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010279 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010280 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010281 }
10282
Daniel Vetter835c5872012-07-10 18:11:08 +020010283 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010284 config->fb_changed = true;
10285
10286 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10287 DRM_DEBUG_KMS("modes are different, full mode set\n");
10288 drm_mode_debug_printmodeline(&set->crtc->mode);
10289 drm_mode_debug_printmodeline(set->mode);
10290 config->mode_changed = true;
10291 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010292
10293 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10294 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010295}
10296
Daniel Vetter2e431052012-07-04 22:42:15 +020010297static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010298intel_modeset_stage_output_state(struct drm_device *dev,
10299 struct drm_mode_set *set,
10300 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010301{
Daniel Vetter9a935852012-07-05 22:34:27 +020010302 struct intel_connector *connector;
10303 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010304 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010305 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020010306
Damien Lespiau9abdda72013-02-13 13:29:23 +000010307 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020010308 * of connectors. For paranoia, double-check this. */
10309 WARN_ON(!set->fb && (set->num_connectors != 0));
10310 WARN_ON(set->fb && (set->num_connectors == 0));
10311
Daniel Vetter9a935852012-07-05 22:34:27 +020010312 list_for_each_entry(connector, &dev->mode_config.connector_list,
10313 base.head) {
10314 /* Otherwise traverse passed in connector list and get encoders
10315 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010316 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010317 if (set->connectors[ro] == &connector->base) {
10318 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +020010319 break;
10320 }
10321 }
10322
Daniel Vetter9a935852012-07-05 22:34:27 +020010323 /* If we disable the crtc, disable all its connectors. Also, if
10324 * the connector is on the changing crtc but not on the new
10325 * connector list, disable it. */
10326 if ((!set->fb || ro == set->num_connectors) &&
10327 connector->base.encoder &&
10328 connector->base.encoder->crtc == set->crtc) {
10329 connector->new_encoder = NULL;
10330
10331 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10332 connector->base.base.id,
10333 drm_get_connector_name(&connector->base));
10334 }
10335
10336
10337 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010338 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010339 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010340 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010341 }
10342 /* connector->new_encoder is now updated for all connectors. */
10343
10344 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020010345 list_for_each_entry(connector, &dev->mode_config.connector_list,
10346 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010347 struct drm_crtc *new_crtc;
10348
Daniel Vetter9a935852012-07-05 22:34:27 +020010349 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020010350 continue;
10351
Daniel Vetter9a935852012-07-05 22:34:27 +020010352 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020010353
10354 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010355 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020010356 new_crtc = set->crtc;
10357 }
10358
10359 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010010360 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10361 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010362 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020010363 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010364 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10365
10366 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10367 connector->base.base.id,
10368 drm_get_connector_name(&connector->base),
10369 new_crtc->base.id);
10370 }
10371
10372 /* Check for any encoders that needs to be disabled. */
10373 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10374 base.head) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010375 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010376 list_for_each_entry(connector,
10377 &dev->mode_config.connector_list,
10378 base.head) {
10379 if (connector->new_encoder == encoder) {
10380 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010381 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020010382 }
10383 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010384
10385 if (num_connectors == 0)
10386 encoder->new_crtc = NULL;
10387 else if (num_connectors > 1)
10388 return -EINVAL;
10389
Daniel Vetter9a935852012-07-05 22:34:27 +020010390 /* Only now check for crtc changes so we don't miss encoders
10391 * that will be disabled. */
10392 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010393 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010394 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010395 }
10396 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010397 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010398
Ville Syrjälä76688512014-01-10 11:28:06 +020010399 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10400 base.head) {
10401 crtc->new_enabled = false;
10402
10403 list_for_each_entry(encoder,
10404 &dev->mode_config.encoder_list,
10405 base.head) {
10406 if (encoder->new_crtc == crtc) {
10407 crtc->new_enabled = true;
10408 break;
10409 }
10410 }
10411
10412 if (crtc->new_enabled != crtc->base.enabled) {
10413 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10414 crtc->new_enabled ? "en" : "dis");
10415 config->mode_changed = true;
10416 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010417
10418 if (crtc->new_enabled)
10419 crtc->new_config = &crtc->config;
10420 else
10421 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010422 }
10423
Daniel Vetter2e431052012-07-04 22:42:15 +020010424 return 0;
10425}
10426
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010427static void disable_crtc_nofb(struct intel_crtc *crtc)
10428{
10429 struct drm_device *dev = crtc->base.dev;
10430 struct intel_encoder *encoder;
10431 struct intel_connector *connector;
10432
10433 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10434 pipe_name(crtc->pipe));
10435
10436 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10437 if (connector->new_encoder &&
10438 connector->new_encoder->new_crtc == crtc)
10439 connector->new_encoder = NULL;
10440 }
10441
10442 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10443 if (encoder->new_crtc == crtc)
10444 encoder->new_crtc = NULL;
10445 }
10446
10447 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010448 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010449}
10450
Daniel Vetter2e431052012-07-04 22:42:15 +020010451static int intel_crtc_set_config(struct drm_mode_set *set)
10452{
10453 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020010454 struct drm_mode_set save_set;
10455 struct intel_set_config *config;
10456 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020010457
Daniel Vetter8d3e3752012-07-05 16:09:09 +020010458 BUG_ON(!set);
10459 BUG_ON(!set->crtc);
10460 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020010461
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010010462 /* Enforce sane interface api - has been abused by the fb helper. */
10463 BUG_ON(!set->mode && set->fb);
10464 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020010465
Daniel Vetter2e431052012-07-04 22:42:15 +020010466 if (set->fb) {
10467 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10468 set->crtc->base.id, set->fb->base.id,
10469 (int)set->num_connectors, set->x, set->y);
10470 } else {
10471 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020010472 }
10473
10474 dev = set->crtc->dev;
10475
10476 ret = -ENOMEM;
10477 config = kzalloc(sizeof(*config), GFP_KERNEL);
10478 if (!config)
10479 goto out_config;
10480
10481 ret = intel_set_config_save_state(dev, config);
10482 if (ret)
10483 goto out_config;
10484
10485 save_set.crtc = set->crtc;
10486 save_set.mode = &set->crtc->mode;
10487 save_set.x = set->crtc->x;
10488 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070010489 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020010490
10491 /* Compute whether we need a full modeset, only an fb base update or no
10492 * change at all. In the future we might also check whether only the
10493 * mode changed, e.g. for LVDS where we only change the panel fitter in
10494 * such cases. */
10495 intel_set_config_compute_mode_changes(set, config);
10496
Daniel Vetter9a935852012-07-05 22:34:27 +020010497 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020010498 if (ret)
10499 goto fail;
10500
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010501 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010502 ret = intel_set_mode(set->crtc, set->mode,
10503 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010504 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010505 intel_crtc_wait_for_pending_flips(set->crtc);
10506
Daniel Vetter4f660f42012-07-02 09:47:37 +020010507 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010508 set->x, set->y, set->fb);
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010509 /*
10510 * In the fastboot case this may be our only check of the
10511 * state after boot. It would be better to only do it on
10512 * the first update, but we don't have a nice way of doing that
10513 * (and really, set_config isn't used much for high freq page
10514 * flipping, so increasing its cost here shouldn't be a big
10515 * deal).
10516 */
Jani Nikulad330a952014-01-21 11:24:25 +020010517 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010518 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010519 }
10520
Chris Wilson2d05eae2013-05-03 17:36:25 +010010521 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010522 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10523 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010524fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010525 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010526
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010527 /*
10528 * HACK: if the pipe was on, but we didn't have a framebuffer,
10529 * force the pipe off to avoid oopsing in the modeset code
10530 * due to fb==NULL. This should only happen during boot since
10531 * we don't yet reconstruct the FB from the hardware state.
10532 */
10533 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10534 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10535
Chris Wilson2d05eae2013-05-03 17:36:25 +010010536 /* Try to restore the config */
10537 if (config->mode_changed &&
10538 intel_set_mode(save_set.crtc, save_set.mode,
10539 save_set.x, save_set.y, save_set.fb))
10540 DRM_ERROR("failed to restore config after modeset failure\n");
10541 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010542
Daniel Vetterd9e55602012-07-04 22:16:09 +020010543out_config:
10544 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010545 return ret;
10546}
10547
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010548static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010549 .cursor_set = intel_crtc_cursor_set,
10550 .cursor_move = intel_crtc_cursor_move,
10551 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010552 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010553 .destroy = intel_crtc_destroy,
10554 .page_flip = intel_crtc_page_flip,
10555};
10556
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010557static void intel_cpu_pll_init(struct drm_device *dev)
10558{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010559 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010560 intel_ddi_pll_init(dev);
10561}
10562
Daniel Vetter53589012013-06-05 13:34:16 +020010563static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10564 struct intel_shared_dpll *pll,
10565 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010566{
Daniel Vetter53589012013-06-05 13:34:16 +020010567 uint32_t val;
10568
10569 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010570 hw_state->dpll = val;
10571 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10572 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010573
10574 return val & DPLL_VCO_ENABLE;
10575}
10576
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010577static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10578 struct intel_shared_dpll *pll)
10579{
10580 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10581 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10582}
10583
Daniel Vettere7b903d2013-06-05 13:34:14 +020010584static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10585 struct intel_shared_dpll *pll)
10586{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010587 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020010588 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020010589
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010590 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10591
10592 /* Wait for the clocks to stabilize. */
10593 POSTING_READ(PCH_DPLL(pll->id));
10594 udelay(150);
10595
10596 /* The pixel multiplier can only be updated once the
10597 * DPLL is enabled and the clocks are stable.
10598 *
10599 * So write it again.
10600 */
10601 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10602 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010603 udelay(200);
10604}
10605
10606static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10607 struct intel_shared_dpll *pll)
10608{
10609 struct drm_device *dev = dev_priv->dev;
10610 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010611
10612 /* Make sure no transcoder isn't still depending on us. */
10613 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10614 if (intel_crtc_to_shared_dpll(crtc) == pll)
10615 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10616 }
10617
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010618 I915_WRITE(PCH_DPLL(pll->id), 0);
10619 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010620 udelay(200);
10621}
10622
Daniel Vetter46edb022013-06-05 13:34:12 +020010623static char *ibx_pch_dpll_names[] = {
10624 "PCH DPLL A",
10625 "PCH DPLL B",
10626};
10627
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010628static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010629{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010630 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010631 int i;
10632
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010633 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010634
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010635 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010636 dev_priv->shared_dplls[i].id = i;
10637 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010638 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010639 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10640 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010641 dev_priv->shared_dplls[i].get_hw_state =
10642 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010643 }
10644}
10645
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010646static void intel_shared_dpll_init(struct drm_device *dev)
10647{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010648 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010649
10650 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10651 ibx_pch_dpll_init(dev);
10652 else
10653 dev_priv->num_shared_dpll = 0;
10654
10655 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010656}
10657
Hannes Ederb358d0a2008-12-18 21:18:47 +010010658static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010659{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010660 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010661 struct intel_crtc *intel_crtc;
10662 int i;
10663
Daniel Vetter955382f2013-09-19 14:05:45 +020010664 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010665 if (intel_crtc == NULL)
10666 return;
10667
10668 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10669
10670 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080010671 for (i = 0; i < 256; i++) {
10672 intel_crtc->lut_r[i] = i;
10673 intel_crtc->lut_g[i] = i;
10674 intel_crtc->lut_b[i] = i;
10675 }
10676
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020010677 /*
10678 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10679 * is hooked to plane B. Hence we want plane A feeding pipe B.
10680 */
Jesse Barnes80824002009-09-10 15:28:06 -070010681 intel_crtc->pipe = pipe;
10682 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010010683 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080010684 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010010685 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070010686 }
10687
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030010688 init_waitqueue_head(&intel_crtc->vbl_wait);
10689
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080010690 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10691 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10692 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10693 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10694
Jesse Barnes79e53942008-11-07 14:24:08 -080010695 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080010696}
10697
Jesse Barnes752aa882013-10-31 18:55:49 +020010698enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10699{
10700 struct drm_encoder *encoder = connector->base.encoder;
10701
10702 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10703
10704 if (!encoder)
10705 return INVALID_PIPE;
10706
10707 return to_intel_crtc(encoder->crtc)->pipe;
10708}
10709
Carl Worth08d7b3d2009-04-29 14:43:54 -070010710int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000010711 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070010712{
Carl Worth08d7b3d2009-04-29 14:43:54 -070010713 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020010714 struct drm_mode_object *drmmode_obj;
10715 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010716
Daniel Vetter1cff8f62012-04-24 09:55:08 +020010717 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10718 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010719
Daniel Vetterc05422d2009-08-11 16:05:30 +020010720 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10721 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070010722
Daniel Vetterc05422d2009-08-11 16:05:30 +020010723 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070010724 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030010725 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010726 }
10727
Daniel Vetterc05422d2009-08-11 16:05:30 +020010728 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10729 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010730
Daniel Vetterc05422d2009-08-11 16:05:30 +020010731 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070010732}
10733
Daniel Vetter66a92782012-07-12 20:08:18 +020010734static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010735{
Daniel Vetter66a92782012-07-12 20:08:18 +020010736 struct drm_device *dev = encoder->base.dev;
10737 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010738 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010739 int entry = 0;
10740
Daniel Vetter66a92782012-07-12 20:08:18 +020010741 list_for_each_entry(source_encoder,
10742 &dev->mode_config.encoder_list, base.head) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010743 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020010744 index_mask |= (1 << entry);
10745
Jesse Barnes79e53942008-11-07 14:24:08 -080010746 entry++;
10747 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010010748
Jesse Barnes79e53942008-11-07 14:24:08 -080010749 return index_mask;
10750}
10751
Chris Wilson4d302442010-12-14 19:21:29 +000010752static bool has_edp_a(struct drm_device *dev)
10753{
10754 struct drm_i915_private *dev_priv = dev->dev_private;
10755
10756 if (!IS_MOBILE(dev))
10757 return false;
10758
10759 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10760 return false;
10761
Damien Lespiaue3589902014-02-07 19:12:50 +000010762 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000010763 return false;
10764
10765 return true;
10766}
10767
Damien Lespiauba0fbca2014-01-08 14:18:23 +000010768const char *intel_output_name(int output)
10769{
10770 static const char *names[] = {
10771 [INTEL_OUTPUT_UNUSED] = "Unused",
10772 [INTEL_OUTPUT_ANALOG] = "Analog",
10773 [INTEL_OUTPUT_DVO] = "DVO",
10774 [INTEL_OUTPUT_SDVO] = "SDVO",
10775 [INTEL_OUTPUT_LVDS] = "LVDS",
10776 [INTEL_OUTPUT_TVOUT] = "TV",
10777 [INTEL_OUTPUT_HDMI] = "HDMI",
10778 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
10779 [INTEL_OUTPUT_EDP] = "eDP",
10780 [INTEL_OUTPUT_DSI] = "DSI",
10781 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
10782 };
10783
10784 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
10785 return "Invalid";
10786
10787 return names[output];
10788}
10789
Jesse Barnes79e53942008-11-07 14:24:08 -080010790static void intel_setup_outputs(struct drm_device *dev)
10791{
Eric Anholt725e30a2009-01-22 13:01:02 -080010792 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010793 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010794 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010795
Daniel Vetterc9093352013-06-06 22:22:47 +020010796 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010797
Paulo Zanonic40c0f52013-04-12 18:16:53 -030010798 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020010799 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010800
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010801 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030010802 int found;
10803
10804 /* Haswell uses DDI functions to detect digital outputs */
10805 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10806 /* DDI A only supports eDP */
10807 if (found)
10808 intel_ddi_init(dev, PORT_A);
10809
10810 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10811 * register */
10812 found = I915_READ(SFUSE_STRAP);
10813
10814 if (found & SFUSE_STRAP_DDIB_DETECTED)
10815 intel_ddi_init(dev, PORT_B);
10816 if (found & SFUSE_STRAP_DDIC_DETECTED)
10817 intel_ddi_init(dev, PORT_C);
10818 if (found & SFUSE_STRAP_DDID_DETECTED)
10819 intel_ddi_init(dev, PORT_D);
10820 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010821 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020010822 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020010823
10824 if (has_edp_a(dev))
10825 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040010826
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010827 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080010828 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010010829 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010830 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010831 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010832 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010833 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010834 }
10835
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010836 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010837 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010838
Paulo Zanonidc0fa712013-02-19 16:21:46 -030010839 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030010840 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080010841
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010842 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010843 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080010844
Daniel Vetter270b3042012-10-27 15:52:05 +020010845 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010846 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070010847 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030010848 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10849 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10850 PORT_B);
10851 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10852 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10853 }
10854
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010855 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10856 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10857 PORT_C);
10858 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020010859 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070010860 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053010861
Jani Nikula3cfca972013-08-27 15:12:26 +030010862 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080010863 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010864 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080010865
Paulo Zanonie2debe92013-02-18 19:00:27 -030010866 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010867 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010868 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010869 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10870 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010871 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010872 }
Ma Ling27185ae2009-08-24 13:50:23 +080010873
Imre Deake7281ea2013-05-08 13:14:08 +030010874 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010875 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080010876 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010877
10878 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040010879
Paulo Zanonie2debe92013-02-18 19:00:27 -030010880 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010881 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010882 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010883 }
Ma Ling27185ae2009-08-24 13:50:23 +080010884
Paulo Zanonie2debe92013-02-18 19:00:27 -030010885 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080010886
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010887 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10888 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030010889 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010890 }
Imre Deake7281ea2013-05-08 13:14:08 +030010891 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010892 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080010893 }
Ma Ling27185ae2009-08-24 13:50:23 +080010894
Jesse Barnesb01f2c32009-12-11 11:07:17 -080010895 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030010896 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030010897 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070010898 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010899 intel_dvo_init(dev);
10900
Zhenyu Wang103a1962009-11-27 11:44:36 +080010901 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080010902 intel_tv_init(dev);
10903
Chris Wilson4ef69c72010-09-09 15:14:28 +010010904 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10905 encoder->base.possible_crtcs = encoder->crtc_mask;
10906 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020010907 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080010908 }
Chris Wilson47356eb2011-01-11 17:06:04 +000010909
Paulo Zanonidde86e22012-12-01 12:04:25 -020010910 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020010911
10912 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010913}
10914
10915static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10916{
10917 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080010918
Daniel Vetteref2d6332014-02-10 18:00:38 +010010919 drm_framebuffer_cleanup(fb);
10920 WARN_ON(!intel_fb->obj->framebuffer_references--);
10921 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010922 kfree(intel_fb);
10923}
10924
10925static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000010926 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080010927 unsigned int *handle)
10928{
10929 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010930 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080010931
Chris Wilson05394f32010-11-08 19:18:58 +000010932 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080010933}
10934
10935static const struct drm_framebuffer_funcs intel_fb_funcs = {
10936 .destroy = intel_user_framebuffer_destroy,
10937 .create_handle = intel_user_framebuffer_create_handle,
10938};
10939
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010940static int intel_framebuffer_init(struct drm_device *dev,
10941 struct intel_framebuffer *intel_fb,
10942 struct drm_mode_fb_cmd2 *mode_cmd,
10943 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080010944{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080010945 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010946 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080010947 int ret;
10948
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010949 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10950
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010951 if (obj->tiling_mode == I915_TILING_Y) {
10952 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010010953 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010954 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010955
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010956 if (mode_cmd->pitches[0] & 63) {
10957 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10958 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010010959 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010960 }
Chris Wilson57cd6502010-08-08 12:34:44 +010010961
Chris Wilsona35cdaa2013-06-25 17:26:45 +010010962 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10963 pitch_limit = 32*1024;
10964 } else if (INTEL_INFO(dev)->gen >= 4) {
10965 if (obj->tiling_mode)
10966 pitch_limit = 16*1024;
10967 else
10968 pitch_limit = 32*1024;
10969 } else if (INTEL_INFO(dev)->gen >= 3) {
10970 if (obj->tiling_mode)
10971 pitch_limit = 8*1024;
10972 else
10973 pitch_limit = 16*1024;
10974 } else
10975 /* XXX DSPC is limited to 4k tiled */
10976 pitch_limit = 8*1024;
10977
10978 if (mode_cmd->pitches[0] > pitch_limit) {
10979 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10980 obj->tiling_mode ? "tiled" : "linear",
10981 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010982 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010983 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010984
10985 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010986 mode_cmd->pitches[0] != obj->stride) {
10987 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10988 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010989 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000010990 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020010991
Ville Syrjälä57779d02012-10-31 17:50:14 +020010992 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010993 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020010994 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020010995 case DRM_FORMAT_RGB565:
10996 case DRM_FORMAT_XRGB8888:
10997 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020010998 break;
10999 case DRM_FORMAT_XRGB1555:
11000 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011001 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011002 DRM_DEBUG("unsupported pixel format: %s\n",
11003 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011004 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011005 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020011006 break;
11007 case DRM_FORMAT_XBGR8888:
11008 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011009 case DRM_FORMAT_XRGB2101010:
11010 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011011 case DRM_FORMAT_XBGR2101010:
11012 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011013 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011014 DRM_DEBUG("unsupported pixel format: %s\n",
11015 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011016 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011017 }
Jesse Barnesb5626742011-06-24 12:19:27 -070011018 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020011019 case DRM_FORMAT_YUYV:
11020 case DRM_FORMAT_UYVY:
11021 case DRM_FORMAT_YVYU:
11022 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011023 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011024 DRM_DEBUG("unsupported pixel format: %s\n",
11025 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011026 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011027 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011028 break;
11029 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011030 DRM_DEBUG("unsupported pixel format: %s\n",
11031 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010011032 return -EINVAL;
11033 }
11034
Ville Syrjälä90f9a332012-10-31 17:50:19 +020011035 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11036 if (mode_cmd->offsets[0] != 0)
11037 return -EINVAL;
11038
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011039 aligned_height = intel_align_height(dev, mode_cmd->height,
11040 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020011041 /* FIXME drm helper for size checks (especially planar formats)? */
11042 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11043 return -EINVAL;
11044
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011045 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11046 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020011047 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011048
Jesse Barnes79e53942008-11-07 14:24:08 -080011049 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11050 if (ret) {
11051 DRM_ERROR("framebuffer init failed %d\n", ret);
11052 return ret;
11053 }
11054
Jesse Barnes79e53942008-11-07 14:24:08 -080011055 return 0;
11056}
11057
Jesse Barnes79e53942008-11-07 14:24:08 -080011058static struct drm_framebuffer *
11059intel_user_framebuffer_create(struct drm_device *dev,
11060 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011061 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080011062{
Chris Wilson05394f32010-11-08 19:18:58 +000011063 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011064
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011065 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11066 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000011067 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010011068 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080011069
Chris Wilsond2dff872011-04-19 08:36:26 +010011070 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080011071}
11072
Daniel Vetter4520f532013-10-09 09:18:51 +020011073#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020011074static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020011075{
11076}
11077#endif
11078
Jesse Barnes79e53942008-11-07 14:24:08 -080011079static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080011080 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020011081 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080011082};
11083
Jesse Barnese70236a2009-09-21 10:42:27 -070011084/* Set up chip specific display functions */
11085static void intel_init_display(struct drm_device *dev)
11086{
11087 struct drm_i915_private *dev_priv = dev->dev_private;
11088
Daniel Vetteree9300b2013-06-03 22:40:22 +020011089 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11090 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030011091 else if (IS_CHERRYVIEW(dev))
11092 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020011093 else if (IS_VALLEYVIEW(dev))
11094 dev_priv->display.find_dpll = vlv_find_best_dpll;
11095 else if (IS_PINEVIEW(dev))
11096 dev_priv->display.find_dpll = pnv_find_best_dpll;
11097 else
11098 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11099
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011100 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011101 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011102 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011103 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020011104 dev_priv->display.crtc_enable = haswell_crtc_enable;
11105 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011106 dev_priv->display.off = haswell_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011107 dev_priv->display.update_primary_plane =
11108 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011109 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011110 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011111 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011112 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011113 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11114 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011115 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011116 dev_priv->display.update_primary_plane =
11117 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011118 } else if (IS_VALLEYVIEW(dev)) {
11119 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011120 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011121 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11122 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11123 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11124 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011125 dev_priv->display.update_primary_plane =
11126 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011127 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011128 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011129 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011130 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011131 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11132 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011133 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011134 dev_priv->display.update_primary_plane =
11135 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011136 }
Jesse Barnese70236a2009-09-21 10:42:27 -070011137
Jesse Barnese70236a2009-09-21 10:42:27 -070011138 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070011139 if (IS_VALLEYVIEW(dev))
11140 dev_priv->display.get_display_clock_speed =
11141 valleyview_get_display_clock_speed;
11142 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070011143 dev_priv->display.get_display_clock_speed =
11144 i945_get_display_clock_speed;
11145 else if (IS_I915G(dev))
11146 dev_priv->display.get_display_clock_speed =
11147 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011148 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011149 dev_priv->display.get_display_clock_speed =
11150 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011151 else if (IS_PINEVIEW(dev))
11152 dev_priv->display.get_display_clock_speed =
11153 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070011154 else if (IS_I915GM(dev))
11155 dev_priv->display.get_display_clock_speed =
11156 i915gm_get_display_clock_speed;
11157 else if (IS_I865G(dev))
11158 dev_priv->display.get_display_clock_speed =
11159 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020011160 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011161 dev_priv->display.get_display_clock_speed =
11162 i855_get_display_clock_speed;
11163 else /* 852, 830 */
11164 dev_priv->display.get_display_clock_speed =
11165 i830_get_display_clock_speed;
11166
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080011167 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010011168 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011169 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011170 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080011171 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011172 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011173 dev_priv->display.write_eld = ironlake_write_eld;
Paulo Zanoni9a952a02014-03-07 20:12:34 -030011174 dev_priv->display.modeset_global_resources =
11175 snb_modeset_global_resources;
Jesse Barnes357555c2011-04-28 15:09:55 -070011176 } else if (IS_IVYBRIDGE(dev)) {
11177 /* FIXME: detect B0+ stepping and use auto training */
11178 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011179 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020011180 dev_priv->display.modeset_global_resources =
11181 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011182 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030011183 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080011184 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020011185 dev_priv->display.modeset_global_resources =
11186 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020011187 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070011188 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080011189 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011190 } else if (IS_VALLEYVIEW(dev)) {
11191 dev_priv->display.modeset_global_resources =
11192 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040011193 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070011194 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011195
11196 /* Default just returns -ENODEV to indicate unsupported */
11197 dev_priv->display.queue_flip = intel_default_queue_flip;
11198
11199 switch (INTEL_INFO(dev)->gen) {
11200 case 2:
11201 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11202 break;
11203
11204 case 3:
11205 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11206 break;
11207
11208 case 4:
11209 case 5:
11210 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11211 break;
11212
11213 case 6:
11214 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11215 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011216 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011217 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011218 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11219 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011220 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020011221
11222 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011223}
11224
Jesse Barnesb690e962010-07-19 13:53:12 -070011225/*
11226 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11227 * resume, or other times. This quirk makes sure that's the case for
11228 * affected systems.
11229 */
Akshay Joshi0206e352011-08-16 15:34:10 -040011230static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070011231{
11232 struct drm_i915_private *dev_priv = dev->dev_private;
11233
11234 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011235 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011236}
11237
Keith Packard435793d2011-07-12 14:56:22 -070011238/*
11239 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11240 */
11241static void quirk_ssc_force_disable(struct drm_device *dev)
11242{
11243 struct drm_i915_private *dev_priv = dev->dev_private;
11244 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011245 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070011246}
11247
Carsten Emde4dca20e2012-03-15 15:56:26 +010011248/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010011249 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11250 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010011251 */
11252static void quirk_invert_brightness(struct drm_device *dev)
11253{
11254 struct drm_i915_private *dev_priv = dev->dev_private;
11255 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011256 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011257}
11258
11259struct intel_quirk {
11260 int device;
11261 int subsystem_vendor;
11262 int subsystem_device;
11263 void (*hook)(struct drm_device *dev);
11264};
11265
Egbert Eich5f85f1762012-10-14 15:46:38 +020011266/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11267struct intel_dmi_quirk {
11268 void (*hook)(struct drm_device *dev);
11269 const struct dmi_system_id (*dmi_id_list)[];
11270};
11271
11272static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11273{
11274 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11275 return 1;
11276}
11277
11278static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11279 {
11280 .dmi_id_list = &(const struct dmi_system_id[]) {
11281 {
11282 .callback = intel_dmi_reverse_brightness,
11283 .ident = "NCR Corporation",
11284 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11285 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11286 },
11287 },
11288 { } /* terminating entry */
11289 },
11290 .hook = quirk_invert_brightness,
11291 },
11292};
11293
Ben Widawskyc43b5632012-04-16 14:07:40 -070011294static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070011295 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040011296 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070011297
Jesse Barnesb690e962010-07-19 13:53:12 -070011298 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11299 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11300
Jesse Barnesb690e962010-07-19 13:53:12 -070011301 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11302 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11303
Chris Wilsona4945f92013-10-08 11:16:59 +010011304 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020011305 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070011306
11307 /* Lenovo U160 cannot use SSC on LVDS */
11308 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020011309
11310 /* Sony Vaio Y cannot use SSC on LVDS */
11311 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010011312
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010011313 /* Acer Aspire 5734Z must invert backlight brightness */
11314 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11315
11316 /* Acer/eMachines G725 */
11317 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11318
11319 /* Acer/eMachines e725 */
11320 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11321
11322 /* Acer/Packard Bell NCL20 */
11323 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11324
11325 /* Acer Aspire 4736Z */
11326 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020011327
11328 /* Acer Aspire 5336 */
11329 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070011330};
11331
11332static void intel_init_quirks(struct drm_device *dev)
11333{
11334 struct pci_dev *d = dev->pdev;
11335 int i;
11336
11337 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11338 struct intel_quirk *q = &intel_quirks[i];
11339
11340 if (d->device == q->device &&
11341 (d->subsystem_vendor == q->subsystem_vendor ||
11342 q->subsystem_vendor == PCI_ANY_ID) &&
11343 (d->subsystem_device == q->subsystem_device ||
11344 q->subsystem_device == PCI_ANY_ID))
11345 q->hook(dev);
11346 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020011347 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11348 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11349 intel_dmi_quirks[i].hook(dev);
11350 }
Jesse Barnesb690e962010-07-19 13:53:12 -070011351}
11352
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011353/* Disable the VGA plane that we never use */
11354static void i915_disable_vga(struct drm_device *dev)
11355{
11356 struct drm_i915_private *dev_priv = dev->dev_private;
11357 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011358 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011359
Ville Syrjälä2b37c612014-01-22 21:32:38 +020011360 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011361 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070011362 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011363 sr1 = inb(VGA_SR_DATA);
11364 outb(sr1 | 1<<5, VGA_SR_DATA);
11365 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11366 udelay(300);
11367
11368 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11369 POSTING_READ(vga_reg);
11370}
11371
Daniel Vetterf8175862012-04-10 15:50:11 +020011372void intel_modeset_init_hw(struct drm_device *dev)
11373{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030011374 intel_prepare_ddi(dev);
11375
Daniel Vetterf8175862012-04-10 15:50:11 +020011376 intel_init_clock_gating(dev);
11377
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011378 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070011379
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011380 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020011381}
11382
Imre Deak7d708ee2013-04-17 14:04:50 +030011383void intel_modeset_suspend_hw(struct drm_device *dev)
11384{
11385 intel_suspend_hw(dev);
11386}
11387
Jesse Barnes79e53942008-11-07 14:24:08 -080011388void intel_modeset_init(struct drm_device *dev)
11389{
Jesse Barnes652c3932009-08-17 13:31:43 -070011390 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000011391 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000011392 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080011393 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080011394
11395 drm_mode_config_init(dev);
11396
11397 dev->mode_config.min_width = 0;
11398 dev->mode_config.min_height = 0;
11399
Dave Airlie019d96c2011-09-29 16:20:42 +010011400 dev->mode_config.preferred_depth = 24;
11401 dev->mode_config.prefer_shadow = 1;
11402
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020011403 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080011404
Jesse Barnesb690e962010-07-19 13:53:12 -070011405 intel_init_quirks(dev);
11406
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030011407 intel_init_pm(dev);
11408
Ben Widawskye3c74752013-04-05 13:12:39 -070011409 if (INTEL_INFO(dev)->num_pipes == 0)
11410 return;
11411
Jesse Barnese70236a2009-09-21 10:42:27 -070011412 intel_init_display(dev);
11413
Chris Wilsona6c45cf2010-09-17 00:32:17 +010011414 if (IS_GEN2(dev)) {
11415 dev->mode_config.max_width = 2048;
11416 dev->mode_config.max_height = 2048;
11417 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070011418 dev->mode_config.max_width = 4096;
11419 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080011420 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010011421 dev->mode_config.max_width = 8192;
11422 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080011423 }
Damien Lespiau068be562014-03-28 14:17:49 +000011424
11425 if (IS_GEN2(dev)) {
11426 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
11427 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
11428 } else {
11429 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
11430 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
11431 }
11432
Ben Widawsky5d4545a2013-01-17 12:45:15 -080011433 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011434
Zhao Yakui28c97732009-10-09 11:39:41 +080011435 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011436 INTEL_INFO(dev)->num_pipes,
11437 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080011438
Damien Lespiau8cc87b72014-03-03 17:31:44 +000011439 for_each_pipe(pipe) {
11440 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000011441 for_each_sprite(pipe, sprite) {
11442 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011443 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030011444 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000011445 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011446 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011447 }
11448
Jesse Barnesf42bb702013-12-16 16:34:23 -080011449 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011450 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080011451
Paulo Zanoni79f689a2012-10-05 12:05:52 -030011452 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011453 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011454
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011455 /* Just disable it once at startup */
11456 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011457 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000011458
11459 /* Just in case the BIOS is doing something questionable. */
11460 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080011461
Jesse Barnes8b687df2014-02-21 13:13:39 -080011462 mutex_lock(&dev->mode_config.mutex);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080011463 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes8b687df2014-02-21 13:13:39 -080011464 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes46f297f2014-03-07 08:57:48 -080011465
11466 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11467 base.head) {
11468 if (!crtc->active)
11469 continue;
11470
Jesse Barnes46f297f2014-03-07 08:57:48 -080011471 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080011472 * Note that reserving the BIOS fb up front prevents us
11473 * from stuffing other stolen allocations like the ring
11474 * on top. This prevents some ugliness at boot time, and
11475 * can even allow for smooth boot transitions if the BIOS
11476 * fb is large enough for the active pipe configuration.
11477 */
11478 if (dev_priv->display.get_plane_config) {
11479 dev_priv->display.get_plane_config(crtc,
11480 &crtc->plane_config);
11481 /*
11482 * If the fb is shared between multiple heads, we'll
11483 * just get the first one.
11484 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080011485 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080011486 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080011487 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010011488}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080011489
Daniel Vetter24929352012-07-02 20:28:59 +020011490static void
11491intel_connector_break_all_links(struct intel_connector *connector)
11492{
11493 connector->base.dpms = DRM_MODE_DPMS_OFF;
11494 connector->base.encoder = NULL;
11495 connector->encoder->connectors_active = false;
11496 connector->encoder->base.crtc = NULL;
11497}
11498
Daniel Vetter7fad7982012-07-04 17:51:47 +020011499static void intel_enable_pipe_a(struct drm_device *dev)
11500{
11501 struct intel_connector *connector;
11502 struct drm_connector *crt = NULL;
11503 struct intel_load_detect_pipe load_detect_temp;
11504
11505 /* We can't just switch on the pipe A, we need to set things up with a
11506 * proper mode and output configuration. As a gross hack, enable pipe A
11507 * by enabling the load detect pipe once. */
11508 list_for_each_entry(connector,
11509 &dev->mode_config.connector_list,
11510 base.head) {
11511 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
11512 crt = &connector->base;
11513 break;
11514 }
11515 }
11516
11517 if (!crt)
11518 return;
11519
11520 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
11521 intel_release_load_detect_pipe(crt, &load_detect_temp);
11522
11523
11524}
11525
Daniel Vetterfa555832012-10-10 23:14:00 +020011526static bool
11527intel_check_plane_mapping(struct intel_crtc *crtc)
11528{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011529 struct drm_device *dev = crtc->base.dev;
11530 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020011531 u32 reg, val;
11532
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011533 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020011534 return true;
11535
11536 reg = DSPCNTR(!crtc->plane);
11537 val = I915_READ(reg);
11538
11539 if ((val & DISPLAY_PLANE_ENABLE) &&
11540 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
11541 return false;
11542
11543 return true;
11544}
11545
Daniel Vetter24929352012-07-02 20:28:59 +020011546static void intel_sanitize_crtc(struct intel_crtc *crtc)
11547{
11548 struct drm_device *dev = crtc->base.dev;
11549 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020011550 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020011551
Daniel Vetter24929352012-07-02 20:28:59 +020011552 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020011553 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020011554 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
11555
11556 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020011557 * disable the crtc (and hence change the state) if it is wrong. Note
11558 * that gen4+ has a fixed plane -> pipe mapping. */
11559 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020011560 struct intel_connector *connector;
11561 bool plane;
11562
Daniel Vetter24929352012-07-02 20:28:59 +020011563 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
11564 crtc->base.base.id);
11565
11566 /* Pipe has the wrong plane attached and the plane is active.
11567 * Temporarily change the plane mapping and disable everything
11568 * ... */
11569 plane = crtc->plane;
11570 crtc->plane = !plane;
11571 dev_priv->display.crtc_disable(&crtc->base);
11572 crtc->plane = plane;
11573
11574 /* ... and break all links. */
11575 list_for_each_entry(connector, &dev->mode_config.connector_list,
11576 base.head) {
11577 if (connector->encoder->base.crtc != &crtc->base)
11578 continue;
11579
11580 intel_connector_break_all_links(connector);
11581 }
11582
11583 WARN_ON(crtc->active);
11584 crtc->base.enabled = false;
11585 }
Daniel Vetter24929352012-07-02 20:28:59 +020011586
Daniel Vetter7fad7982012-07-04 17:51:47 +020011587 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11588 crtc->pipe == PIPE_A && !crtc->active) {
11589 /* BIOS forgot to enable pipe A, this mostly happens after
11590 * resume. Force-enable the pipe to fix this, the update_dpms
11591 * call below we restore the pipe to the right state, but leave
11592 * the required bits on. */
11593 intel_enable_pipe_a(dev);
11594 }
11595
Daniel Vetter24929352012-07-02 20:28:59 +020011596 /* Adjust the state of the output pipe according to whether we
11597 * have active connectors/encoders. */
11598 intel_crtc_update_dpms(&crtc->base);
11599
11600 if (crtc->active != crtc->base.enabled) {
11601 struct intel_encoder *encoder;
11602
11603 /* This can happen either due to bugs in the get_hw_state
11604 * functions or because the pipe is force-enabled due to the
11605 * pipe A quirk. */
11606 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11607 crtc->base.base.id,
11608 crtc->base.enabled ? "enabled" : "disabled",
11609 crtc->active ? "enabled" : "disabled");
11610
11611 crtc->base.enabled = crtc->active;
11612
11613 /* Because we only establish the connector -> encoder ->
11614 * crtc links if something is active, this means the
11615 * crtc is now deactivated. Break the links. connector
11616 * -> encoder links are only establish when things are
11617 * actually up, hence no need to break them. */
11618 WARN_ON(crtc->active);
11619
11620 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11621 WARN_ON(encoder->connectors_active);
11622 encoder->base.crtc = NULL;
11623 }
11624 }
Daniel Vetter4cc31482014-03-24 00:01:41 +010011625 if (crtc->active) {
11626 /*
11627 * We start out with underrun reporting disabled to avoid races.
11628 * For correct bookkeeping mark this on active crtcs.
11629 *
11630 * No protection against concurrent access is required - at
11631 * worst a fifo underrun happens which also sets this to false.
11632 */
11633 crtc->cpu_fifo_underrun_disabled = true;
11634 crtc->pch_fifo_underrun_disabled = true;
11635 }
Daniel Vetter24929352012-07-02 20:28:59 +020011636}
11637
11638static void intel_sanitize_encoder(struct intel_encoder *encoder)
11639{
11640 struct intel_connector *connector;
11641 struct drm_device *dev = encoder->base.dev;
11642
11643 /* We need to check both for a crtc link (meaning that the
11644 * encoder is active and trying to read from a pipe) and the
11645 * pipe itself being active. */
11646 bool has_active_crtc = encoder->base.crtc &&
11647 to_intel_crtc(encoder->base.crtc)->active;
11648
11649 if (encoder->connectors_active && !has_active_crtc) {
11650 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11651 encoder->base.base.id,
11652 drm_get_encoder_name(&encoder->base));
11653
11654 /* Connector is active, but has no active pipe. This is
11655 * fallout from our resume register restoring. Disable
11656 * the encoder manually again. */
11657 if (encoder->base.crtc) {
11658 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11659 encoder->base.base.id,
11660 drm_get_encoder_name(&encoder->base));
11661 encoder->disable(encoder);
11662 }
11663
11664 /* Inconsistent output/port/pipe state happens presumably due to
11665 * a bug in one of the get_hw_state functions. Or someplace else
11666 * in our code, like the register restore mess on resume. Clamp
11667 * things to off as a safer default. */
11668 list_for_each_entry(connector,
11669 &dev->mode_config.connector_list,
11670 base.head) {
11671 if (connector->encoder != encoder)
11672 continue;
11673
11674 intel_connector_break_all_links(connector);
11675 }
11676 }
11677 /* Enabled encoders without active connectors will be fixed in
11678 * the crtc fixup. */
11679}
11680
Imre Deak04098752014-02-18 00:02:16 +020011681void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011682{
11683 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011684 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011685
Imre Deak04098752014-02-18 00:02:16 +020011686 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
11687 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
11688 i915_disable_vga(dev);
11689 }
11690}
11691
11692void i915_redisable_vga(struct drm_device *dev)
11693{
11694 struct drm_i915_private *dev_priv = dev->dev_private;
11695
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011696 /* This function can be called both from intel_modeset_setup_hw_state or
11697 * at a very early point in our resume sequence, where the power well
11698 * structures are not yet restored. Since this function is at a very
11699 * paranoid "someone might have enabled VGA while we were not looking"
11700 * level, just check if the power well is enabled instead of trying to
11701 * follow the "don't touch the power well if we don't need it" policy
11702 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020011703 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030011704 return;
11705
Imre Deak04098752014-02-18 00:02:16 +020011706 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010011707}
11708
Ville Syrjälä98ec7732014-04-30 17:43:01 +030011709static bool primary_get_hw_state(struct intel_crtc *crtc)
11710{
11711 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
11712
11713 if (!crtc->active)
11714 return false;
11715
11716 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
11717}
11718
Daniel Vetter30e984d2013-06-05 13:34:17 +020011719static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020011720{
11721 struct drm_i915_private *dev_priv = dev->dev_private;
11722 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020011723 struct intel_crtc *crtc;
11724 struct intel_encoder *encoder;
11725 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020011726 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020011727
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011728 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11729 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010011730 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020011731
Daniel Vetter99535992014-04-13 12:00:33 +020011732 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
11733
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011734 crtc->active = dev_priv->display.get_pipe_config(crtc,
11735 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011736
11737 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030011738 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020011739
11740 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11741 crtc->base.base.id,
11742 crtc->active ? "enabled" : "disabled");
11743 }
11744
Daniel Vetter53589012013-06-05 13:34:16 +020011745 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011746 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011747 intel_ddi_setup_hw_pll_state(dev);
11748
Daniel Vetter53589012013-06-05 13:34:16 +020011749 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11750 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11751
11752 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11753 pll->active = 0;
11754 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11755 base.head) {
11756 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11757 pll->active++;
11758 }
11759 pll->refcount = pll->active;
11760
Daniel Vetter35c95372013-07-17 06:55:04 +020011761 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11762 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020011763 }
11764
Daniel Vetter24929352012-07-02 20:28:59 +020011765 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11766 base.head) {
11767 pipe = 0;
11768
11769 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011770 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11771 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011772 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020011773 } else {
11774 encoder->base.crtc = NULL;
11775 }
11776
11777 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011778 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020011779 encoder->base.base.id,
11780 drm_get_encoder_name(&encoder->base),
11781 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010011782 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020011783 }
11784
11785 list_for_each_entry(connector, &dev->mode_config.connector_list,
11786 base.head) {
11787 if (connector->get_hw_state(connector)) {
11788 connector->base.dpms = DRM_MODE_DPMS_ON;
11789 connector->encoder->connectors_active = true;
11790 connector->base.encoder = &connector->encoder->base;
11791 } else {
11792 connector->base.dpms = DRM_MODE_DPMS_OFF;
11793 connector->base.encoder = NULL;
11794 }
11795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11796 connector->base.base.id,
11797 drm_get_connector_name(&connector->base),
11798 connector->base.encoder ? "enabled" : "disabled");
11799 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020011800}
11801
11802/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11803 * and i915 state tracking structures. */
11804void intel_modeset_setup_hw_state(struct drm_device *dev,
11805 bool force_restore)
11806{
11807 struct drm_i915_private *dev_priv = dev->dev_private;
11808 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011809 struct intel_crtc *crtc;
11810 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020011811 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020011812
11813 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020011814
Jesse Barnesbabea612013-06-26 18:57:38 +030011815 /*
11816 * Now that we have the config, copy it to each CRTC struct
11817 * Note that this could go away if we move to using crtc_config
11818 * checking everywhere.
11819 */
11820 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11821 base.head) {
Jani Nikulad330a952014-01-21 11:24:25 +020011822 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080011823 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030011824 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11825 crtc->base.base.id);
11826 drm_mode_debug_printmodeline(&crtc->base.mode);
11827 }
11828 }
11829
Daniel Vetter24929352012-07-02 20:28:59 +020011830 /* HW state is read out, now we need to sanitize this mess. */
11831 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11832 base.head) {
11833 intel_sanitize_encoder(encoder);
11834 }
11835
11836 for_each_pipe(pipe) {
11837 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11838 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011839 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020011840 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011841
Daniel Vetter35c95372013-07-17 06:55:04 +020011842 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11843 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11844
11845 if (!pll->on || pll->active)
11846 continue;
11847
11848 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11849
11850 pll->disable(dev_priv, pll);
11851 pll->on = false;
11852 }
11853
Ville Syrjälä96f90c52013-12-05 15:51:38 +020011854 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030011855 ilk_wm_get_hw_state(dev);
11856
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011857 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030011858 i915_redisable_vga(dev);
11859
Daniel Vetterf30da182013-04-11 20:22:50 +020011860 /*
11861 * We need to use raw interfaces for restoring state to avoid
11862 * checking (bogus) intermediate states.
11863 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011864 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070011865 struct drm_crtc *crtc =
11866 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020011867
11868 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070011869 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010011870 }
11871 } else {
11872 intel_modeset_update_staged_output_state(dev);
11873 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011874
11875 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010011876}
11877
11878void intel_modeset_gem_init(struct drm_device *dev)
11879{
Jesse Barnes484b41d2014-03-07 08:57:55 -080011880 struct drm_crtc *c;
11881 struct intel_framebuffer *fb;
11882
Imre Deakae484342014-03-31 15:10:44 +030011883 mutex_lock(&dev->struct_mutex);
11884 intel_init_gt_powersave(dev);
11885 mutex_unlock(&dev->struct_mutex);
11886
Chris Wilson1833b132012-05-09 11:56:28 +010011887 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020011888
11889 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080011890
11891 /*
11892 * Make sure any fbs we allocated at startup are properly
11893 * pinned & fenced. When we do the allocation it's too early
11894 * for this.
11895 */
11896 mutex_lock(&dev->struct_mutex);
11897 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
Dave Airlie66e514c2014-04-03 07:51:54 +100011898 if (!c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -080011899 continue;
11900
Dave Airlie66e514c2014-04-03 07:51:54 +100011901 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -080011902 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
11903 DRM_ERROR("failed to pin boot fb on pipe %d\n",
11904 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100011905 drm_framebuffer_unreference(c->primary->fb);
11906 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080011907 }
11908 }
11909 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080011910}
11911
Imre Deak4932e2c2014-02-11 17:12:48 +020011912void intel_connector_unregister(struct intel_connector *intel_connector)
11913{
11914 struct drm_connector *connector = &intel_connector->base;
11915
11916 intel_panel_destroy_backlight(connector);
11917 drm_sysfs_connector_remove(connector);
11918}
11919
Jesse Barnes79e53942008-11-07 14:24:08 -080011920void intel_modeset_cleanup(struct drm_device *dev)
11921{
Jesse Barnes652c3932009-08-17 13:31:43 -070011922 struct drm_i915_private *dev_priv = dev->dev_private;
11923 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030011924 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070011925
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011926 /*
11927 * Interrupts and polling as the first thing to avoid creating havoc.
11928 * Too much stuff here (turning of rps, connectors, ...) would
11929 * experience fancy races otherwise.
11930 */
11931 drm_irq_uninstall(dev);
11932 cancel_work_sync(&dev_priv->hotplug_work);
11933 /*
11934 * Due to the hpd irq storm handling the hotplug work can re-arm the
11935 * poll handlers. Hence disable polling after hpd handling is shut down.
11936 */
Keith Packardf87ea762010-10-03 19:36:26 -070011937 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020011938
Jesse Barnes652c3932009-08-17 13:31:43 -070011939 mutex_lock(&dev->struct_mutex);
11940
Jesse Barnes723bfd72010-10-07 16:01:13 -070011941 intel_unregister_dsm_handler();
11942
Jesse Barnes652c3932009-08-17 13:31:43 -070011943 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11944 /* Skip inactive CRTCs */
Matt Roperf4510a22014-04-01 15:22:40 -070011945 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -070011946 continue;
11947
Daniel Vetter3dec0092010-08-20 21:40:52 +020011948 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070011949 }
11950
Chris Wilson973d04f2011-07-08 12:22:37 +010011951 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011952
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011953 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000011954
Daniel Vetter930ebb42012-06-29 23:32:16 +020011955 ironlake_teardown_rc6(dev);
11956
Kristian Høgsberg69341a52009-11-11 12:19:17 -050011957 mutex_unlock(&dev->struct_mutex);
11958
Chris Wilson1630fe72011-07-08 12:22:42 +010011959 /* flush any delayed tasks or pending work */
11960 flush_scheduled_work();
11961
Jani Nikuladb31af12013-11-08 16:48:53 +020011962 /* destroy the backlight and sysfs files before encoders/connectors */
11963 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020011964 struct intel_connector *intel_connector;
11965
11966 intel_connector = to_intel_connector(connector);
11967 intel_connector->unregister(intel_connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020011968 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030011969
Jesse Barnes79e53942008-11-07 14:24:08 -080011970 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010011971
11972 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030011973
11974 mutex_lock(&dev->struct_mutex);
11975 intel_cleanup_gt_powersave(dev);
11976 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080011977}
11978
Dave Airlie28d52042009-09-21 14:33:58 +100011979/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080011980 * Return which encoder is currently attached for connector.
11981 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010011982struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080011983{
Chris Wilsondf0e9242010-09-09 16:20:55 +010011984 return &intel_attached_encoder(connector)->base;
11985}
Jesse Barnes79e53942008-11-07 14:24:08 -080011986
Chris Wilsondf0e9242010-09-09 16:20:55 +010011987void intel_connector_attach_encoder(struct intel_connector *connector,
11988 struct intel_encoder *encoder)
11989{
11990 connector->encoder = encoder;
11991 drm_mode_connector_attach_encoder(&connector->base,
11992 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011993}
Dave Airlie28d52042009-09-21 14:33:58 +100011994
11995/*
11996 * set vga decode state - true == enable VGA decode
11997 */
11998int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11999{
12000 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000012001 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100012002 u16 gmch_ctrl;
12003
Chris Wilson75fa0412014-02-07 18:37:02 -020012004 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12005 DRM_ERROR("failed to read control word\n");
12006 return -EIO;
12007 }
12008
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020012009 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12010 return 0;
12011
Dave Airlie28d52042009-09-21 14:33:58 +100012012 if (state)
12013 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12014 else
12015 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020012016
12017 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12018 DRM_ERROR("failed to write control word\n");
12019 return -EIO;
12020 }
12021
Dave Airlie28d52042009-09-21 14:33:58 +100012022 return 0;
12023}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012024
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012025struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012026
12027 u32 power_well_driver;
12028
Chris Wilson63b66e52013-08-08 15:12:06 +020012029 int num_transcoders;
12030
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012031 struct intel_cursor_error_state {
12032 u32 control;
12033 u32 position;
12034 u32 base;
12035 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010012036 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012037
12038 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012039 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012040 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030012041 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010012042 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012043
12044 struct intel_plane_error_state {
12045 u32 control;
12046 u32 stride;
12047 u32 size;
12048 u32 pos;
12049 u32 addr;
12050 u32 surface;
12051 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010012052 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020012053
12054 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012055 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020012056 enum transcoder cpu_transcoder;
12057
12058 u32 conf;
12059
12060 u32 htotal;
12061 u32 hblank;
12062 u32 hsync;
12063 u32 vtotal;
12064 u32 vblank;
12065 u32 vsync;
12066 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012067};
12068
12069struct intel_display_error_state *
12070intel_display_capture_error_state(struct drm_device *dev)
12071{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012072 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012073 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020012074 int transcoders[] = {
12075 TRANSCODER_A,
12076 TRANSCODER_B,
12077 TRANSCODER_C,
12078 TRANSCODER_EDP,
12079 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012080 int i;
12081
Chris Wilson63b66e52013-08-08 15:12:06 +020012082 if (INTEL_INFO(dev)->num_pipes == 0)
12083 return NULL;
12084
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012085 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012086 if (error == NULL)
12087 return NULL;
12088
Imre Deak190be112013-11-25 17:15:31 +020012089 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012090 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12091
Damien Lespiau52331302012-08-15 19:23:25 +010012092 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020012093 error->pipe[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012094 intel_display_power_enabled_sw(dev_priv,
12095 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020012096 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012097 continue;
12098
Paulo Zanonia18c4c32013-03-06 20:03:12 -030012099 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
12100 error->cursor[i].control = I915_READ(CURCNTR(i));
12101 error->cursor[i].position = I915_READ(CURPOS(i));
12102 error->cursor[i].base = I915_READ(CURBASE(i));
12103 } else {
12104 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
12105 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
12106 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
12107 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012108
12109 error->plane[i].control = I915_READ(DSPCNTR(i));
12110 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012111 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030012112 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012113 error->plane[i].pos = I915_READ(DSPPOS(i));
12114 }
Paulo Zanonica291362013-03-06 20:03:14 -030012115 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12116 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012117 if (INTEL_INFO(dev)->gen >= 4) {
12118 error->plane[i].surface = I915_READ(DSPSURF(i));
12119 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12120 }
12121
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012122 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030012123
12124 if (!HAS_PCH_SPLIT(dev))
12125 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020012126 }
12127
12128 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12129 if (HAS_DDI(dev_priv->dev))
12130 error->num_transcoders++; /* Account for eDP. */
12131
12132 for (i = 0; i < error->num_transcoders; i++) {
12133 enum transcoder cpu_transcoder = transcoders[i];
12134
Imre Deakddf9c532013-11-27 22:02:02 +020012135 error->transcoder[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012136 intel_display_power_enabled_sw(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020012137 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012138 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012139 continue;
12140
Chris Wilson63b66e52013-08-08 15:12:06 +020012141 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12142
12143 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12144 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12145 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12146 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12147 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12148 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12149 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012150 }
12151
12152 return error;
12153}
12154
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012155#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12156
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012157void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012158intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012159 struct drm_device *dev,
12160 struct intel_display_error_state *error)
12161{
12162 int i;
12163
Chris Wilson63b66e52013-08-08 15:12:06 +020012164 if (!error)
12165 return;
12166
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012167 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020012168 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012169 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012170 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010012171 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012172 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020012173 err_printf(m, " Power: %s\n",
12174 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012175 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030012176 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012177
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012178 err_printf(m, "Plane [%d]:\n", i);
12179 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12180 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012181 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012182 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12183 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012184 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030012185 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012186 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012187 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012188 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12189 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012190 }
12191
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012192 err_printf(m, "Cursor [%d]:\n", i);
12193 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12194 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12195 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012196 }
Chris Wilson63b66e52013-08-08 15:12:06 +020012197
12198 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010012199 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020012200 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012201 err_printf(m, " Power: %s\n",
12202 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020012203 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12204 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12205 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12206 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12207 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12208 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12209 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12210 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012211}