blob: b5cbb2830420d55784705df656f4ecb044a862bb [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>
Matt Roper465c1202014-05-29 08:06:54 -070042#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080044#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045
Matt Roper465c1202014-05-29 08:06:54 -070046/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
Chon Ming Leeef9348c2014-04-09 13:28:18 +030071#define DIV_ROUND_CLOSEST_ULL(ll, d) \
Matt Roper465c1202014-05-29 08:06:54 -070072({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
Chon Ming Leeef9348c2014-04-09 13:28:18 +030073
Daniel Vetter3dec0092010-08-20 21:40:52 +020074static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010075static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080076
Jesse Barnesf1f644d2013-06-27 00:39:25 +030077static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
78 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030079static void ironlake_pch_clock_get(struct intel_crtc *crtc,
80 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030081
Damien Lespiaue7457a92013-08-08 22:28:59 +010082static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
83 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080084static int intel_framebuffer_init(struct drm_device *dev,
85 struct intel_framebuffer *ifb,
86 struct drm_mode_fb_cmd2 *mode_cmd,
87 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020088static void intel_dp_set_m_n(struct intel_crtc *crtc);
89static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
90static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020091static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
92 struct intel_link_m_n *m_n);
93static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020094static void haswell_set_pipeconf(struct drm_crtc *crtc);
95static void intel_set_pipe_csc(struct drm_crtc *crtc);
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +020096static void vlv_prepare_pll(struct intel_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +010097
Jesse Barnes79e53942008-11-07 14:24:08 -080098typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040099 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800100} intel_range_t;
101
102typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400103 int dot_limit;
104 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800105} intel_p2_t;
106
Ma Lingd4906092009-03-18 20:13:27 +0800107typedef struct intel_limit intel_limit_t;
108struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400109 intel_range_t dot, vco, n, m, m1, m2, p, p1;
110 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800111};
Jesse Barnes79e53942008-11-07 14:24:08 -0800112
Daniel Vetterd2acd212012-10-20 20:57:43 +0200113int
114intel_pch_rawclk(struct drm_device *dev)
115{
116 struct drm_i915_private *dev_priv = dev->dev_private;
117
118 WARN_ON(!HAS_PCH_SPLIT(dev));
119
120 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
121}
122
Chris Wilson021357a2010-09-07 20:54:59 +0100123static inline u32 /* units of 100MHz */
124intel_fdi_link_freq(struct drm_device *dev)
125{
Chris Wilson8b99e682010-10-13 09:59:17 +0100126 if (IS_GEN5(dev)) {
127 struct drm_i915_private *dev_priv = dev->dev_private;
128 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
129 } else
130 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100131}
132
Daniel Vetter5d536e22013-07-06 12:52:06 +0200133static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400134 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200135 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200136 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .m = { .min = 96, .max = 140 },
138 .m1 = { .min = 18, .max = 26 },
139 .m2 = { .min = 6, .max = 16 },
140 .p = { .min = 4, .max = 128 },
141 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700142 .p2 = { .dot_limit = 165000,
143 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700144};
145
Daniel Vetter5d536e22013-07-06 12:52:06 +0200146static const intel_limit_t intel_limits_i8xx_dvo = {
147 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200148 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200149 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200150 .m = { .min = 96, .max = 140 },
151 .m1 = { .min = 18, .max = 26 },
152 .m2 = { .min = 6, .max = 16 },
153 .p = { .min = 4, .max = 128 },
154 .p1 = { .min = 2, .max = 33 },
155 .p2 = { .dot_limit = 165000,
156 .p2_slow = 4, .p2_fast = 4 },
157};
158
Keith Packarde4b36692009-06-05 19:22:17 -0700159static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200161 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200162 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
Eric Anholt273e27c2011-03-30 13:01:10 -0700171
Keith Packarde4b36692009-06-05 19:22:17 -0700172static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400173 .dot = { .min = 20000, .max = 400000 },
174 .vco = { .min = 1400000, .max = 2800000 },
175 .n = { .min = 1, .max = 6 },
176 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100177 .m1 = { .min = 8, .max = 18 },
178 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700181 .p2 = { .dot_limit = 200000,
182 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .dot = { .min = 20000, .max = 400000 },
187 .vco = { .min = 1400000, .max = 2800000 },
188 .n = { .min = 1, .max = 6 },
189 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100190 .m1 = { .min = 8, .max = 18 },
191 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400192 .p = { .min = 7, .max = 98 },
193 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700194 .p2 = { .dot_limit = 112000,
195 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700196};
197
Eric Anholt273e27c2011-03-30 13:01:10 -0700198
Keith Packarde4b36692009-06-05 19:22:17 -0700199static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 25000, .max = 270000 },
201 .vco = { .min = 1750000, .max = 3500000},
202 .n = { .min = 1, .max = 4 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 10, .max = 30 },
207 .p1 = { .min = 1, .max = 3},
208 .p2 = { .dot_limit = 270000,
209 .p2_slow = 10,
210 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800211 },
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
214static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .dot = { .min = 22000, .max = 400000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 16, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 5, .max = 80 },
222 .p1 = { .min = 1, .max = 8},
223 .p2 = { .dot_limit = 165000,
224 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700225};
226
227static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700228 .dot = { .min = 20000, .max = 115000 },
229 .vco = { .min = 1750000, .max = 3500000 },
230 .n = { .min = 1, .max = 3 },
231 .m = { .min = 104, .max = 138 },
232 .m1 = { .min = 17, .max = 23 },
233 .m2 = { .min = 5, .max = 11 },
234 .p = { .min = 28, .max = 112 },
235 .p1 = { .min = 2, .max = 8 },
236 .p2 = { .dot_limit = 0,
237 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800238 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
241static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700242 .dot = { .min = 80000, .max = 224000 },
243 .vco = { .min = 1750000, .max = 3500000 },
244 .n = { .min = 1, .max = 3 },
245 .m = { .min = 104, .max = 138 },
246 .m1 = { .min = 17, .max = 23 },
247 .m2 = { .min = 5, .max = 11 },
248 .p = { .min = 14, .max = 42 },
249 .p1 = { .min = 2, .max = 6 },
250 .p2 = { .dot_limit = 0,
251 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800252 },
Keith Packarde4b36692009-06-05 19:22:17 -0700253};
254
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500255static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .dot = { .min = 20000, .max = 400000},
257 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700258 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .n = { .min = 3, .max = 6 },
260 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .m1 = { .min = 0, .max = 0 },
263 .m2 = { .min = 0, .max = 254 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .p2 = { .dot_limit = 200000,
267 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700268};
269
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500270static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400271 .dot = { .min = 20000, .max = 400000 },
272 .vco = { .min = 1700000, .max = 3500000 },
273 .n = { .min = 3, .max = 6 },
274 .m = { .min = 2, .max = 256 },
275 .m1 = { .min = 0, .max = 0 },
276 .m2 = { .min = 0, .max = 254 },
277 .p = { .min = 7, .max = 112 },
278 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700279 .p2 = { .dot_limit = 112000,
280 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700281};
282
Eric Anholt273e27c2011-03-30 13:01:10 -0700283/* Ironlake / Sandybridge
284 *
285 * We calculate clock using (register_value + 2) for N/M1/M2, so here
286 * the range value for them is (actual_value - 2).
287 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800288static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .dot = { .min = 25000, .max = 350000 },
290 .vco = { .min = 1760000, .max = 3510000 },
291 .n = { .min = 1, .max = 5 },
292 .m = { .min = 79, .max = 127 },
293 .m1 = { .min = 12, .max = 22 },
294 .m2 = { .min = 5, .max = 9 },
295 .p = { .min = 5, .max = 80 },
296 .p1 = { .min = 1, .max = 8 },
297 .p2 = { .dot_limit = 225000,
298 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
300
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800301static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .dot = { .min = 25000, .max = 350000 },
303 .vco = { .min = 1760000, .max = 3510000 },
304 .n = { .min = 1, .max = 3 },
305 .m = { .min = 79, .max = 118 },
306 .m1 = { .min = 12, .max = 22 },
307 .m2 = { .min = 5, .max = 9 },
308 .p = { .min = 28, .max = 112 },
309 .p1 = { .min = 2, .max = 8 },
310 .p2 = { .dot_limit = 225000,
311 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800312};
313
314static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 3 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 14, .max = 56 },
322 .p1 = { .min = 2, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800328static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 350000 },
330 .vco = { .min = 1760000, .max = 3510000 },
331 .n = { .min = 1, .max = 2 },
332 .m = { .min = 79, .max = 126 },
333 .m1 = { .min = 12, .max = 22 },
334 .m2 = { .min = 5, .max = 9 },
335 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400336 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700337 .p2 = { .dot_limit = 225000,
338 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800339};
340
341static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700342 .dot = { .min = 25000, .max = 350000 },
343 .vco = { .min = 1760000, .max = 3510000 },
344 .n = { .min = 1, .max = 3 },
345 .m = { .min = 79, .max = 126 },
346 .m1 = { .min = 12, .max = 22 },
347 .m2 = { .min = 5, .max = 9 },
348 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400349 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .p2 = { .dot_limit = 225000,
351 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800352};
353
Ville Syrjälädc730512013-09-24 21:26:30 +0300354static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300355 /*
356 * These are the data rate limits (measured in fast clocks)
357 * since those are the strictest limits we have. The fast
358 * clock and actual rate limits are more relaxed, so checking
359 * them would make no difference.
360 */
361 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200362 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700363 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700364 .m1 = { .min = 2, .max = 3 },
365 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300366 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300367 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700368};
369
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300370static const intel_limit_t intel_limits_chv = {
371 /*
372 * These are the data rate limits (measured in fast clocks)
373 * since those are the strictest limits we have. The fast
374 * clock and actual rate limits are more relaxed, so checking
375 * them would make no difference.
376 */
377 .dot = { .min = 25000 * 5, .max = 540000 * 5},
378 .vco = { .min = 4860000, .max = 6700000 },
379 .n = { .min = 1, .max = 1 },
380 .m1 = { .min = 2, .max = 2 },
381 .m2 = { .min = 24 << 22, .max = 175 << 22 },
382 .p1 = { .min = 2, .max = 4 },
383 .p2 = { .p2_slow = 1, .p2_fast = 14 },
384};
385
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300386static void vlv_clock(int refclk, intel_clock_t *clock)
387{
388 clock->m = clock->m1 * clock->m2;
389 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200390 if (WARN_ON(clock->n == 0 || clock->p == 0))
391 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300392 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
393 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300394}
395
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300396/**
397 * Returns whether any output on the specified pipe is of the specified type
398 */
399static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
400{
401 struct drm_device *dev = crtc->dev;
402 struct intel_encoder *encoder;
403
404 for_each_encoder_on_crtc(dev, crtc, encoder)
405 if (encoder->type == type)
406 return true;
407
408 return false;
409}
410
Chris Wilson1b894b52010-12-14 20:04:54 +0000411static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
412 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800413{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800414 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800415 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800416
417 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100418 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000419 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800420 limit = &intel_limits_ironlake_dual_lvds_100m;
421 else
422 limit = &intel_limits_ironlake_dual_lvds;
423 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000424 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800425 limit = &intel_limits_ironlake_single_lvds_100m;
426 else
427 limit = &intel_limits_ironlake_single_lvds;
428 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200429 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800431
432 return limit;
433}
434
Ma Ling044c7c42009-03-18 20:13:23 +0800435static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
436{
437 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800438 const intel_limit_t *limit;
439
440 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100441 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700442 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800443 else
Keith Packarde4b36692009-06-05 19:22:17 -0700444 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800445 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
446 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700447 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800448 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700449 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800450 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700451 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800452
453 return limit;
454}
455
Chris Wilson1b894b52010-12-14 20:04:54 +0000456static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800457{
458 struct drm_device *dev = crtc->dev;
459 const intel_limit_t *limit;
460
Eric Anholtbad720f2009-10-22 16:11:14 -0700461 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000462 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800463 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800464 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500465 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800466 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500467 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800468 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500469 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300470 } else if (IS_CHERRYVIEW(dev)) {
471 limit = &intel_limits_chv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700472 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300473 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100474 } else if (!IS_GEN2(dev)) {
475 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
476 limit = &intel_limits_i9xx_lvds;
477 else
478 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800479 } else {
480 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700481 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200482 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700483 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200484 else
485 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800486 }
487 return limit;
488}
489
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500490/* m1 is reserved as 0 in Pineview, n is a ring counter */
491static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800492{
Shaohua Li21778322009-02-23 15:19:16 +0800493 clock->m = clock->m2 + 2;
494 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200495 if (WARN_ON(clock->n == 0 || clock->p == 0))
496 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300497 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
498 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800499}
500
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200501static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
502{
503 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
504}
505
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200506static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800507{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200508 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800509 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200510 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
511 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800514}
515
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300516static void chv_clock(int refclk, intel_clock_t *clock)
517{
518 clock->m = clock->m1 * clock->m2;
519 clock->p = clock->p1 * clock->p2;
520 if (WARN_ON(clock->n == 0 || clock->p == 0))
521 return;
522 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
523 clock->n << 22);
524 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
525}
526
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800527#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800528/**
529 * Returns whether the given set of divisors are valid for a given refclk with
530 * the given connectors.
531 */
532
Chris Wilson1b894b52010-12-14 20:04:54 +0000533static bool intel_PLL_is_valid(struct drm_device *dev,
534 const intel_limit_t *limit,
535 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800536{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300537 if (clock->n < limit->n.min || limit->n.max < clock->n)
538 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800539 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400540 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800541 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400542 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800543 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400544 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300545
546 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
547 if (clock->m1 <= clock->m2)
548 INTELPllInvalid("m1 <= m2\n");
549
550 if (!IS_VALLEYVIEW(dev)) {
551 if (clock->p < limit->p.min || limit->p.max < clock->p)
552 INTELPllInvalid("p out of range\n");
553 if (clock->m < limit->m.min || limit->m.max < clock->m)
554 INTELPllInvalid("m out of range\n");
555 }
556
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400558 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800559 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
560 * connector, etc., rather than just a single range.
561 */
562 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400563 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800564
565 return true;
566}
567
Ma Lingd4906092009-03-18 20:13:27 +0800568static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200569i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800570 int target, int refclk, intel_clock_t *match_clock,
571 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800572{
573 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800575 int err = target;
576
Daniel Vettera210b022012-11-26 17:22:08 +0100577 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800578 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100579 * For LVDS just rely on its current settings for dual-channel.
580 * We haven't figured out how to reliably set up different
581 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100583 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800584 clock.p2 = limit->p2.p2_fast;
585 else
586 clock.p2 = limit->p2.p2_slow;
587 } else {
588 if (target < limit->p2.dot_limit)
589 clock.p2 = limit->p2.p2_slow;
590 else
591 clock.p2 = limit->p2.p2_fast;
592 }
593
Akshay Joshi0206e352011-08-16 15:34:10 -0400594 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800595
Zhao Yakui42158662009-11-20 11:24:18 +0800596 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
597 clock.m1++) {
598 for (clock.m2 = limit->m2.min;
599 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200600 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800601 break;
602 for (clock.n = limit->n.min;
603 clock.n <= limit->n.max; clock.n++) {
604 for (clock.p1 = limit->p1.min;
605 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 int this_err;
607
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200608 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000609 if (!intel_PLL_is_valid(dev, limit,
610 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800612 if (match_clock &&
613 clock.p != match_clock->p)
614 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800615
616 this_err = abs(clock.dot - target);
617 if (this_err < err) {
618 *best_clock = clock;
619 err = this_err;
620 }
621 }
622 }
623 }
624 }
625
626 return (err != target);
627}
628
Ma Lingd4906092009-03-18 20:13:27 +0800629static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200630pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
631 int target, int refclk, intel_clock_t *match_clock,
632 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200633{
634 struct drm_device *dev = crtc->dev;
635 intel_clock_t clock;
636 int err = target;
637
638 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
639 /*
640 * For LVDS just rely on its current settings for dual-channel.
641 * We haven't figured out how to reliably set up different
642 * single/dual channel state, if we even can.
643 */
644 if (intel_is_dual_link_lvds(dev))
645 clock.p2 = limit->p2.p2_fast;
646 else
647 clock.p2 = limit->p2.p2_slow;
648 } else {
649 if (target < limit->p2.dot_limit)
650 clock.p2 = limit->p2.p2_slow;
651 else
652 clock.p2 = limit->p2.p2_fast;
653 }
654
655 memset(best_clock, 0, sizeof(*best_clock));
656
657 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
658 clock.m1++) {
659 for (clock.m2 = limit->m2.min;
660 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
665 int this_err;
666
667 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800668 if (!intel_PLL_is_valid(dev, limit,
669 &clock))
670 continue;
671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
Ma Lingd4906092009-03-18 20:13:27 +0800688static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200689g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800692{
693 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800694 intel_clock_t clock;
695 int max_n;
696 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400697 /* approximately equals target * 0.00585 */
698 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800699 found = false;
700
701 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100702 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800703 clock.p2 = limit->p2.p2_fast;
704 else
705 clock.p2 = limit->p2.p2_slow;
706 } else {
707 if (target < limit->p2.dot_limit)
708 clock.p2 = limit->p2.p2_slow;
709 else
710 clock.p2 = limit->p2.p2_fast;
711 }
712
713 memset(best_clock, 0, sizeof(*best_clock));
714 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200715 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800716 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200717 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800718 for (clock.m1 = limit->m1.max;
719 clock.m1 >= limit->m1.min; clock.m1--) {
720 for (clock.m2 = limit->m2.max;
721 clock.m2 >= limit->m2.min; clock.m2--) {
722 for (clock.p1 = limit->p1.max;
723 clock.p1 >= limit->p1.min; clock.p1--) {
724 int this_err;
725
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200726 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000727 if (!intel_PLL_is_valid(dev, limit,
728 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800729 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000730
731 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800732 if (this_err < err_most) {
733 *best_clock = clock;
734 err_most = this_err;
735 max_n = clock.n;
736 found = true;
737 }
738 }
739 }
740 }
741 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800742 return found;
743}
Ma Lingd4906092009-03-18 20:13:27 +0800744
Zhenyu Wang2c072452009-06-05 15:38:42 +0800745static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200746vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
747 int target, int refclk, intel_clock_t *match_clock,
748 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700749{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300750 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300751 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300752 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300753 /* min update 19.2 MHz */
754 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300755 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700756
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300757 target *= 5; /* fast clock */
758
759 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700760
761 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300762 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300763 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300764 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300765 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300766 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700767 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300768 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300769 unsigned int ppm, diff;
770
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300771 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
772 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300773
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300774 vlv_clock(refclk, &clock);
775
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300776 if (!intel_PLL_is_valid(dev, limit,
777 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300778 continue;
779
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300780 diff = abs(clock.dot - target);
781 ppm = div_u64(1000000ULL * diff, target);
782
783 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300784 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300785 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300786 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300787 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300788
Ville Syrjäläc6861222013-09-24 21:26:21 +0300789 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300790 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300791 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300792 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700793 }
794 }
795 }
796 }
797 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700798
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300799 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700800}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700801
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300802static bool
803chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
804 int target, int refclk, intel_clock_t *match_clock,
805 intel_clock_t *best_clock)
806{
807 struct drm_device *dev = crtc->dev;
808 intel_clock_t clock;
809 uint64_t m2;
810 int found = false;
811
812 memset(best_clock, 0, sizeof(*best_clock));
813
814 /*
815 * Based on hardware doc, the n always set to 1, and m1 always
816 * set to 2. If requires to support 200Mhz refclk, we need to
817 * revisit this because n may not 1 anymore.
818 */
819 clock.n = 1, clock.m1 = 2;
820 target *= 5; /* fast clock */
821
822 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
823 for (clock.p2 = limit->p2.p2_fast;
824 clock.p2 >= limit->p2.p2_slow;
825 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
826
827 clock.p = clock.p1 * clock.p2;
828
829 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
830 clock.n) << 22, refclk * clock.m1);
831
832 if (m2 > INT_MAX/clock.m1)
833 continue;
834
835 clock.m2 = m2;
836
837 chv_clock(refclk, &clock);
838
839 if (!intel_PLL_is_valid(dev, limit, &clock))
840 continue;
841
842 /* based on hardware requirement, prefer bigger p
843 */
844 if (clock.p > best_clock->p) {
845 *best_clock = clock;
846 found = true;
847 }
848 }
849 }
850
851 return found;
852}
853
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300854bool intel_crtc_active(struct drm_crtc *crtc)
855{
856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
857
858 /* Be paranoid as we can arrive here with only partial
859 * state retrieved from the hardware during setup.
860 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100861 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300862 * as Haswell has gained clock readout/fastboot support.
863 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000864 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300865 * properly reconstruct framebuffers.
866 */
Matt Roperf4510a22014-04-01 15:22:40 -0700867 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100868 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300869}
870
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200871enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
872 enum pipe pipe)
873{
874 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
876
Daniel Vetter3b117c82013-04-17 20:15:07 +0200877 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200878}
879
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200880static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300881{
882 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200883 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300884
885 frame = I915_READ(frame_reg);
886
887 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
Jesse Barnes93937072014-04-04 16:12:09 -0700888 WARN(1, "vblank wait timed out\n");
Paulo Zanonia928d532012-05-04 17:18:15 -0300889}
890
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700891/**
892 * intel_wait_for_vblank - wait for vblank on a given pipe
893 * @dev: drm device
894 * @pipe: pipe to wait for
895 *
896 * Wait for vblank to occur on a given pipe. Needed for various bits of
897 * mode setting code.
898 */
899void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800900{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700901 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800902 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700903
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200904 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
905 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300906 return;
907 }
908
Chris Wilson300387c2010-09-05 20:25:43 +0100909 /* Clear existing vblank status. Note this will clear any other
910 * sticky status fields as well.
911 *
912 * This races with i915_driver_irq_handler() with the result
913 * that either function could miss a vblank event. Here it is not
914 * fatal, as we will either wait upon the next vblank interrupt or
915 * timeout. Generally speaking intel_wait_for_vblank() is only
916 * called during modeset at which time the GPU should be idle and
917 * should *not* be performing page flips and thus not waiting on
918 * vblanks...
919 * Currently, the result of us stealing a vblank from the irq
920 * handler is that a single frame will be skipped during swapbuffers.
921 */
922 I915_WRITE(pipestat_reg,
923 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
924
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700925 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100926 if (wait_for(I915_READ(pipestat_reg) &
927 PIPE_VBLANK_INTERRUPT_STATUS,
928 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700929 DRM_DEBUG_KMS("vblank wait timed out\n");
930}
931
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300932static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
933{
934 struct drm_i915_private *dev_priv = dev->dev_private;
935 u32 reg = PIPEDSL(pipe);
936 u32 line1, line2;
937 u32 line_mask;
938
939 if (IS_GEN2(dev))
940 line_mask = DSL_LINEMASK_GEN2;
941 else
942 line_mask = DSL_LINEMASK_GEN3;
943
944 line1 = I915_READ(reg) & line_mask;
945 mdelay(5);
946 line2 = I915_READ(reg) & line_mask;
947
948 return line1 == line2;
949}
950
Keith Packardab7ad7f2010-10-03 00:33:06 -0700951/*
952 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700953 * @dev: drm device
954 * @pipe: pipe to wait for
955 *
956 * After disabling a pipe, we can't wait for vblank in the usual way,
957 * spinning on the vblank interrupt status bit, since we won't actually
958 * see an interrupt when the pipe is disabled.
959 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700960 * On Gen4 and above:
961 * wait for the pipe register state bit to turn off
962 *
963 * Otherwise:
964 * wait for the display line value to settle (it usually
965 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100966 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700967 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100968void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700969{
970 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200971 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
972 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700973
Keith Packardab7ad7f2010-10-03 00:33:06 -0700974 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200975 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700976
Keith Packardab7ad7f2010-10-03 00:33:06 -0700977 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100978 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
979 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200980 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700981 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700982 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300983 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200984 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700985 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800986}
987
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000988/*
989 * ibx_digital_port_connected - is the specified port connected?
990 * @dev_priv: i915 private structure
991 * @port: the port to test
992 *
993 * Returns true if @port is connected, false otherwise.
994 */
995bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
996 struct intel_digital_port *port)
997{
998 u32 bit;
999
Damien Lespiauc36346e2012-12-13 16:09:03 +00001000 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001001 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001002 case PORT_B:
1003 bit = SDE_PORTB_HOTPLUG;
1004 break;
1005 case PORT_C:
1006 bit = SDE_PORTC_HOTPLUG;
1007 break;
1008 case PORT_D:
1009 bit = SDE_PORTD_HOTPLUG;
1010 break;
1011 default:
1012 return true;
1013 }
1014 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001015 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001016 case PORT_B:
1017 bit = SDE_PORTB_HOTPLUG_CPT;
1018 break;
1019 case PORT_C:
1020 bit = SDE_PORTC_HOTPLUG_CPT;
1021 break;
1022 case PORT_D:
1023 bit = SDE_PORTD_HOTPLUG_CPT;
1024 break;
1025 default:
1026 return true;
1027 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001028 }
1029
1030 return I915_READ(SDEISR) & bit;
1031}
1032
Jesse Barnesb24e7172011-01-04 15:09:30 -08001033static const char *state_string(bool enabled)
1034{
1035 return enabled ? "on" : "off";
1036}
1037
1038/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001039void assert_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001041{
1042 int reg;
1043 u32 val;
1044 bool cur_state;
1045
1046 reg = DPLL(pipe);
1047 val = I915_READ(reg);
1048 cur_state = !!(val & DPLL_VCO_ENABLE);
1049 WARN(cur_state != state,
1050 "PLL state assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
1052}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001053
Jani Nikula23538ef2013-08-27 15:12:22 +03001054/* XXX: the dsi pll is shared between MIPI DSI ports */
1055static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1056{
1057 u32 val;
1058 bool cur_state;
1059
1060 mutex_lock(&dev_priv->dpio_lock);
1061 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1062 mutex_unlock(&dev_priv->dpio_lock);
1063
1064 cur_state = val & DSI_PLL_VCO_EN;
1065 WARN(cur_state != state,
1066 "DSI PLL state assertion failure (expected %s, current %s)\n",
1067 state_string(state), state_string(cur_state));
1068}
1069#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1070#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1071
Daniel Vetter55607e82013-06-16 21:42:39 +02001072struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001073intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001074{
Daniel Vettere2b78262013-06-07 23:10:03 +02001075 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1076
Daniel Vettera43f6e02013-06-07 23:10:32 +02001077 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001078 return NULL;
1079
Daniel Vettera43f6e02013-06-07 23:10:32 +02001080 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001081}
1082
Jesse Barnesb24e7172011-01-04 15:09:30 -08001083/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001084void assert_shared_dpll(struct drm_i915_private *dev_priv,
1085 struct intel_shared_dpll *pll,
1086 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001087{
Jesse Barnes040484a2011-01-03 12:14:26 -08001088 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001089 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001090
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001091 if (HAS_PCH_LPT(dev_priv->dev)) {
1092 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1093 return;
1094 }
1095
Chris Wilson92b27b02012-05-20 18:10:50 +01001096 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001097 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001098 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001099
Daniel Vetter53589012013-06-05 13:34:16 +02001100 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001101 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001102 "%s assertion failure (expected %s, current %s)\n",
1103 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001104}
Jesse Barnes040484a2011-01-03 12:14:26 -08001105
1106static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1107 enum pipe pipe, bool state)
1108{
1109 int reg;
1110 u32 val;
1111 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001112 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1113 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001114
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001115 if (HAS_DDI(dev_priv->dev)) {
1116 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001117 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001118 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001119 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001120 } else {
1121 reg = FDI_TX_CTL(pipe);
1122 val = I915_READ(reg);
1123 cur_state = !!(val & FDI_TX_ENABLE);
1124 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001125 WARN(cur_state != state,
1126 "FDI TX state assertion failure (expected %s, current %s)\n",
1127 state_string(state), state_string(cur_state));
1128}
1129#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1130#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1131
1132static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1133 enum pipe pipe, bool state)
1134{
1135 int reg;
1136 u32 val;
1137 bool cur_state;
1138
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001139 reg = FDI_RX_CTL(pipe);
1140 val = I915_READ(reg);
1141 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001142 WARN(cur_state != state,
1143 "FDI RX state assertion failure (expected %s, current %s)\n",
1144 state_string(state), state_string(cur_state));
1145}
1146#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1147#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1148
1149static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1150 enum pipe pipe)
1151{
1152 int reg;
1153 u32 val;
1154
1155 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001156 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001157 return;
1158
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001159 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001160 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001161 return;
1162
Jesse Barnes040484a2011-01-03 12:14:26 -08001163 reg = FDI_TX_CTL(pipe);
1164 val = I915_READ(reg);
1165 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1166}
1167
Daniel Vetter55607e82013-06-16 21:42:39 +02001168void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1169 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001170{
1171 int reg;
1172 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001173 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001174
1175 reg = FDI_RX_CTL(pipe);
1176 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001177 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1178 WARN(cur_state != state,
1179 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1180 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001181}
1182
Jesse Barnesea0760c2011-01-04 15:09:32 -08001183static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1184 enum pipe pipe)
1185{
1186 int pp_reg, lvds_reg;
1187 u32 val;
1188 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001189 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001190
1191 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1192 pp_reg = PCH_PP_CONTROL;
1193 lvds_reg = PCH_LVDS;
1194 } else {
1195 pp_reg = PP_CONTROL;
1196 lvds_reg = LVDS;
1197 }
1198
1199 val = I915_READ(pp_reg);
1200 if (!(val & PANEL_POWER_ON) ||
1201 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1202 locked = false;
1203
1204 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1205 panel_pipe = PIPE_B;
1206
1207 WARN(panel_pipe == pipe && locked,
1208 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001209 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001210}
1211
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001212static void assert_cursor(struct drm_i915_private *dev_priv,
1213 enum pipe pipe, bool state)
1214{
1215 struct drm_device *dev = dev_priv->dev;
1216 bool cur_state;
1217
Paulo Zanonid9d82082014-02-27 16:30:56 -03001218 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001219 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001220 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001221 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001222
1223 WARN(cur_state != state,
1224 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1225 pipe_name(pipe), state_string(state), state_string(cur_state));
1226}
1227#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1228#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1229
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001230void assert_pipe(struct drm_i915_private *dev_priv,
1231 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001232{
1233 int reg;
1234 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001235 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001236 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1237 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001238
Daniel Vetter8e636782012-01-22 01:36:48 +01001239 /* if we need the pipe A quirk it must be always on */
1240 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1241 state = true;
1242
Imre Deakda7e29b2014-02-18 00:02:02 +02001243 if (!intel_display_power_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001244 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001245 cur_state = false;
1246 } else {
1247 reg = PIPECONF(cpu_transcoder);
1248 val = I915_READ(reg);
1249 cur_state = !!(val & PIPECONF_ENABLE);
1250 }
1251
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001252 WARN(cur_state != state,
1253 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001254 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001255}
1256
Chris Wilson931872f2012-01-16 23:01:13 +00001257static void assert_plane(struct drm_i915_private *dev_priv,
1258 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001259{
1260 int reg;
1261 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001262 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001263
1264 reg = DSPCNTR(plane);
1265 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001266 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1267 WARN(cur_state != state,
1268 "plane %c assertion failure (expected %s, current %s)\n",
1269 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001270}
1271
Chris Wilson931872f2012-01-16 23:01:13 +00001272#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1273#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1274
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1276 enum pipe pipe)
1277{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001278 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279 int reg, i;
1280 u32 val;
1281 int cur_pipe;
1282
Ville Syrjälä653e1022013-06-04 13:49:05 +03001283 /* Primary planes are fixed to pipes on gen4+ */
1284 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001285 reg = DSPCNTR(pipe);
1286 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001287 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001288 "plane %c assertion failure, should be disabled but not\n",
1289 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001290 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001291 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001292
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001294 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001295 reg = DSPCNTR(i);
1296 val = I915_READ(reg);
1297 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1298 DISPPLANE_SEL_PIPE_SHIFT;
1299 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001300 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1301 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001302 }
1303}
1304
Jesse Barnes19332d72013-03-28 09:55:38 -07001305static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1306 enum pipe pipe)
1307{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001308 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001309 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001310 u32 val;
1311
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001312 if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001313 for_each_sprite(pipe, sprite) {
1314 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001315 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001316 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001317 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001318 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001319 }
1320 } else if (INTEL_INFO(dev)->gen >= 7) {
1321 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001322 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001323 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001324 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001325 plane_name(pipe), pipe_name(pipe));
1326 } else if (INTEL_INFO(dev)->gen >= 5) {
1327 reg = DVSCNTR(pipe);
1328 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001329 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001330 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1331 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001332 }
1333}
1334
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001335static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001336{
1337 u32 val;
1338 bool enabled;
1339
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001340 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001341
Jesse Barnes92f25842011-01-04 15:09:34 -08001342 val = I915_READ(PCH_DREF_CONTROL);
1343 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1344 DREF_SUPERSPREAD_SOURCE_MASK));
1345 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1346}
1347
Daniel Vetterab9412b2013-05-03 11:49:46 +02001348static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1349 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001350{
1351 int reg;
1352 u32 val;
1353 bool enabled;
1354
Daniel Vetterab9412b2013-05-03 11:49:46 +02001355 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001356 val = I915_READ(reg);
1357 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001358 WARN(enabled,
1359 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1360 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001361}
1362
Keith Packard4e634382011-08-06 10:39:45 -07001363static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001365{
1366 if ((val & DP_PORT_EN) == 0)
1367 return false;
1368
1369 if (HAS_PCH_CPT(dev_priv->dev)) {
1370 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1371 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1372 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1373 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001374 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1375 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1376 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001377 } else {
1378 if ((val & DP_PIPE_MASK) != (pipe << 30))
1379 return false;
1380 }
1381 return true;
1382}
1383
Keith Packard1519b992011-08-06 10:35:34 -07001384static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1385 enum pipe pipe, u32 val)
1386{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001387 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001388 return false;
1389
1390 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001391 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001392 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001393 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1394 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1395 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001396 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001397 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001398 return false;
1399 }
1400 return true;
1401}
1402
1403static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe, u32 val)
1405{
1406 if ((val & LVDS_PORT_EN) == 0)
1407 return false;
1408
1409 if (HAS_PCH_CPT(dev_priv->dev)) {
1410 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1411 return false;
1412 } else {
1413 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1414 return false;
1415 }
1416 return true;
1417}
1418
1419static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1420 enum pipe pipe, u32 val)
1421{
1422 if ((val & ADPA_DAC_ENABLE) == 0)
1423 return false;
1424 if (HAS_PCH_CPT(dev_priv->dev)) {
1425 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1426 return false;
1427 } else {
1428 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1429 return false;
1430 }
1431 return true;
1432}
1433
Jesse Barnes291906f2011-02-02 12:28:03 -08001434static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001435 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001436{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001437 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001438 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001439 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001440 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001441
Daniel Vetter75c5da22012-09-10 21:58:29 +02001442 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1443 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001444 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001445}
1446
1447static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1448 enum pipe pipe, int reg)
1449{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001450 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001451 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001452 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001453 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001454
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001455 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001456 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001457 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001458}
1459
1460static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe)
1462{
1463 int reg;
1464 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001465
Keith Packardf0575e92011-07-25 22:12:43 -07001466 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1467 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1468 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001469
1470 reg = PCH_ADPA;
1471 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001472 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001473 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001474 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001475
1476 reg = PCH_LVDS;
1477 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001478 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001479 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001480 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001481
Paulo Zanonie2debe92013-02-18 19:00:27 -03001482 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1483 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001485}
1486
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001487static void intel_init_dpio(struct drm_device *dev)
1488{
1489 struct drm_i915_private *dev_priv = dev->dev_private;
1490
1491 if (!IS_VALLEYVIEW(dev))
1492 return;
1493
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001494 /*
1495 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1496 * CHV x1 PHY (DP/HDMI D)
1497 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1498 */
1499 if (IS_CHERRYVIEW(dev)) {
1500 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1501 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1502 } else {
1503 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1504 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001505}
1506
1507static void intel_reset_dpio(struct drm_device *dev)
1508{
1509 struct drm_i915_private *dev_priv = dev->dev_private;
1510
1511 if (!IS_VALLEYVIEW(dev))
1512 return;
1513
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001514 if (IS_CHERRYVIEW(dev)) {
1515 enum dpio_phy phy;
1516 u32 val;
1517
1518 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1519 /* Poll for phypwrgood signal */
1520 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1521 PHY_POWERGOOD(phy), 1))
1522 DRM_ERROR("Display PHY %d is not power up\n", phy);
1523
1524 /*
1525 * Deassert common lane reset for PHY.
1526 *
1527 * This should only be done on init and resume from S3
1528 * with both PLLs disabled, or we risk losing DPIO and
1529 * PLL synchronization.
1530 */
1531 val = I915_READ(DISPLAY_PHY_CONTROL);
1532 I915_WRITE(DISPLAY_PHY_CONTROL,
1533 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1534 }
1535
1536 } else {
1537 /*
Jesse Barnes57021052014-05-23 13:16:40 -07001538 * If DPIO has already been reset, e.g. by BIOS, just skip all
1539 * this.
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001540 */
Jesse Barnes57021052014-05-23 13:16:40 -07001541 if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
1542 return;
1543
1544 /*
1545 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1546 * Need to assert and de-assert PHY SB reset by gating the
1547 * common lane power, then un-gating it.
1548 * Simply ungating isn't enough to reset the PHY enough to get
1549 * ports and lanes running.
1550 */
1551 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1552 false);
1553 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1554 true);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001555 }
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001556}
1557
Daniel Vetter426115c2013-07-11 22:13:42 +02001558static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001559{
Daniel Vetter426115c2013-07-11 22:13:42 +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;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001564
Daniel Vetter426115c2013-07-11 22:13:42 +02001565 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001566
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001567 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001568 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001572 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001573
Daniel Vetter426115c2013-07-11 22:13:42 +02001574 I915_WRITE(reg, dpll);
1575 POSTING_READ(reg);
1576 udelay(150);
1577
1578 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1579 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1580
1581 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1582 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001583
1584 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001585 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001586 POSTING_READ(reg);
1587 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001588 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001589 POSTING_READ(reg);
1590 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001591 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001592 POSTING_READ(reg);
1593 udelay(150); /* wait for warmup */
1594}
1595
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596static void chv_enable_pll(struct intel_crtc *crtc)
1597{
1598 struct drm_device *dev = crtc->base.dev;
1599 struct drm_i915_private *dev_priv = dev->dev_private;
1600 int pipe = crtc->pipe;
1601 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001602 u32 tmp;
1603
1604 assert_pipe_disabled(dev_priv, crtc->pipe);
1605
1606 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1607
1608 mutex_lock(&dev_priv->dpio_lock);
1609
1610 /* Enable back the 10bit clock to display controller */
1611 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1612 tmp |= DPIO_DCLKP_EN;
1613 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1614
1615 /*
1616 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1617 */
1618 udelay(1);
1619
1620 /* Enable PLL */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001621 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001622
1623 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001624 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001625 DRM_ERROR("PLL %d failed to lock\n", pipe);
1626
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001627 /* not sure when this should be written */
1628 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1629 POSTING_READ(DPLL_MD(pipe));
1630
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001631 mutex_unlock(&dev_priv->dpio_lock);
1632}
1633
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001634static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001635{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636 struct drm_device *dev = crtc->base.dev;
1637 struct drm_i915_private *dev_priv = dev->dev_private;
1638 int reg = DPLL(crtc->pipe);
1639 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001640
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001641 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001642
1643 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001644 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001645
1646 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 if (IS_MOBILE(dev) && !IS_I830(dev))
1648 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001649
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001650 I915_WRITE(reg, dpll);
1651
1652 /* Wait for the clocks to stabilize. */
1653 POSTING_READ(reg);
1654 udelay(150);
1655
1656 if (INTEL_INFO(dev)->gen >= 4) {
1657 I915_WRITE(DPLL_MD(crtc->pipe),
1658 crtc->config.dpll_hw_state.dpll_md);
1659 } else {
1660 /* The pixel multiplier can only be updated once the
1661 * DPLL is enabled and the clocks are stable.
1662 *
1663 * So write it again.
1664 */
1665 I915_WRITE(reg, dpll);
1666 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667
1668 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001672 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001673 POSTING_READ(reg);
1674 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001675 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001676 POSTING_READ(reg);
1677 udelay(150); /* wait for warmup */
1678}
1679
1680/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001681 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001682 * @dev_priv: i915 private structure
1683 * @pipe: pipe PLL to disable
1684 *
1685 * Disable the PLL for @pipe, making sure the pipe is off first.
1686 *
1687 * Note! This is for pre-ILK only.
1688 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001689static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001690{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001691 /* Don't disable pipe A or pipe A PLLs if needed */
1692 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1693 return;
1694
1695 /* Make sure the pipe isn't still relying on us */
1696 assert_pipe_disabled(dev_priv, pipe);
1697
Daniel Vetter50b44a42013-06-05 13:34:33 +02001698 I915_WRITE(DPLL(pipe), 0);
1699 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001700}
1701
Jesse Barnesf6071162013-10-01 10:41:38 -07001702static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1703{
1704 u32 val = 0;
1705
1706 /* Make sure the pipe isn't still relying on us */
1707 assert_pipe_disabled(dev_priv, pipe);
1708
Imre Deake5cbfbf2014-01-09 17:08:16 +02001709 /*
1710 * Leave integrated clock source and reference clock enabled for pipe B.
1711 * The latter is needed for VGA hotplug / manual detection.
1712 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001713 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001714 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001715 I915_WRITE(DPLL(pipe), val);
1716 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001717
1718}
1719
1720static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1721{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001722 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001723 u32 val;
1724
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001725 /* Make sure the pipe isn't still relying on us */
1726 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001727
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001728 /* Set PLL en = 0 */
1729 val = DPLL_SSC_REF_CLOCK_CHV;
1730 if (pipe != PIPE_A)
1731 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1732 I915_WRITE(DPLL(pipe), val);
1733 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001734
1735 mutex_lock(&dev_priv->dpio_lock);
1736
1737 /* Disable 10bit clock to display controller */
1738 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1739 val &= ~DPIO_DCLKP_EN;
1740 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1741
Ville Syrjälä61407f62014-05-27 16:32:55 +03001742 /* disable left/right clock distribution */
1743 if (pipe != PIPE_B) {
1744 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1745 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1746 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1747 } else {
1748 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1749 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1750 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1751 }
1752
Ville Syrjäläd7520482014-04-09 13:28:59 +03001753 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001754}
1755
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001756void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1757 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001758{
1759 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001760 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001761
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001762 switch (dport->port) {
1763 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001764 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001765 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001766 break;
1767 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001768 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 dpll_reg = DPLL(0);
1770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001774 break;
1775 default:
1776 BUG();
1777 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001778
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001779 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001780 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001781 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001782}
1783
Daniel Vetterb14b1052014-04-24 23:55:13 +02001784static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1785{
1786 struct drm_device *dev = crtc->base.dev;
1787 struct drm_i915_private *dev_priv = dev->dev_private;
1788 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1789
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001790 if (WARN_ON(pll == NULL))
1791 return;
1792
Daniel Vetterb14b1052014-04-24 23:55:13 +02001793 WARN_ON(!pll->refcount);
1794 if (pll->active == 0) {
1795 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1796 WARN_ON(pll->on);
1797 assert_shared_dpll_disabled(dev_priv, pll);
1798
1799 pll->mode_set(dev_priv, pll);
1800 }
1801}
1802
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001803/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001804 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001805 * @dev_priv: i915 private structure
1806 * @pipe: pipe PLL to enable
1807 *
1808 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1809 * drives the transcoder clock.
1810 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001811static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001812{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001813 struct drm_device *dev = crtc->base.dev;
1814 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001815 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001816
Daniel Vetter87a875b2013-06-05 13:34:19 +02001817 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001818 return;
1819
1820 if (WARN_ON(pll->refcount == 0))
1821 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001822
Daniel Vetter46edb022013-06-05 13:34:12 +02001823 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1824 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001825 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001826
Daniel Vettercdbd2312013-06-05 13:34:03 +02001827 if (pll->active++) {
1828 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001829 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001830 return;
1831 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001832 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001833
Daniel Vetter46edb022013-06-05 13:34:12 +02001834 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001835 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001836 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001837}
1838
Daniel Vettere2b78262013-06-07 23:10:03 +02001839static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001840{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001841 struct drm_device *dev = crtc->base.dev;
1842 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001843 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001844
Jesse Barnes92f25842011-01-04 15:09:34 -08001845 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001846 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001847 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001848 return;
1849
Chris Wilson48da64a2012-05-13 20:16:12 +01001850 if (WARN_ON(pll->refcount == 0))
1851 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001852
Daniel Vetter46edb022013-06-05 13:34:12 +02001853 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1854 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001855 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001856
Chris Wilson48da64a2012-05-13 20:16:12 +01001857 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001858 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001859 return;
1860 }
1861
Daniel Vettere9d69442013-06-05 13:34:15 +02001862 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001863 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001864 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001865 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001866
Daniel Vetter46edb022013-06-05 13:34:12 +02001867 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001868 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001869 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001870}
1871
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001872static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1873 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001874{
Daniel Vetter23670b322012-11-01 09:15:30 +01001875 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001876 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001878 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001879
1880 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001881 BUG_ON(INTEL_INFO(dev)->gen < 5);
Jesse Barnes040484a2011-01-03 12:14:26 -08001882
1883 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001884 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001885 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001886
1887 /* FDI must be feeding us bits for PCH ports */
1888 assert_fdi_tx_enabled(dev_priv, pipe);
1889 assert_fdi_rx_enabled(dev_priv, pipe);
1890
Daniel Vetter23670b322012-11-01 09:15:30 +01001891 if (HAS_PCH_CPT(dev)) {
1892 /* Workaround: Set the timing override bit before enabling the
1893 * pch transcoder. */
1894 reg = TRANS_CHICKEN2(pipe);
1895 val = I915_READ(reg);
1896 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1897 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001898 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001899
Daniel Vetterab9412b2013-05-03 11:49:46 +02001900 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001901 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001902 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001903
1904 if (HAS_PCH_IBX(dev_priv->dev)) {
1905 /*
1906 * make the BPC in transcoder be consistent with
1907 * that in pipeconf reg.
1908 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001909 val &= ~PIPECONF_BPC_MASK;
1910 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001911 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001912
1913 val &= ~TRANS_INTERLACE_MASK;
1914 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001915 if (HAS_PCH_IBX(dev_priv->dev) &&
1916 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1917 val |= TRANS_LEGACY_INTERLACED_ILK;
1918 else
1919 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001920 else
1921 val |= TRANS_PROGRESSIVE;
1922
Jesse Barnes040484a2011-01-03 12:14:26 -08001923 I915_WRITE(reg, val | TRANS_ENABLE);
1924 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001925 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001926}
1927
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001928static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001929 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001930{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001931 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001932
1933 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001934 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001935
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001936 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001937 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001938 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001939
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001940 /* Workaround: set timing override bit. */
1941 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001942 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001943 I915_WRITE(_TRANSA_CHICKEN2, val);
1944
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001945 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001946 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001947
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001948 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1949 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001950 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001951 else
1952 val |= TRANS_PROGRESSIVE;
1953
Daniel Vetterab9412b2013-05-03 11:49:46 +02001954 I915_WRITE(LPT_TRANSCONF, val);
1955 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001956 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001957}
1958
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001959static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1960 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001961{
Daniel Vetter23670b322012-11-01 09:15:30 +01001962 struct drm_device *dev = dev_priv->dev;
1963 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001964
1965 /* FDI relies on the transcoder */
1966 assert_fdi_tx_disabled(dev_priv, pipe);
1967 assert_fdi_rx_disabled(dev_priv, pipe);
1968
Jesse Barnes291906f2011-02-02 12:28:03 -08001969 /* Ports must be off as well */
1970 assert_pch_ports_disabled(dev_priv, pipe);
1971
Daniel Vetterab9412b2013-05-03 11:49:46 +02001972 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001973 val = I915_READ(reg);
1974 val &= ~TRANS_ENABLE;
1975 I915_WRITE(reg, val);
1976 /* wait for PCH transcoder off, transcoder state */
1977 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001978 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001979
1980 if (!HAS_PCH_IBX(dev)) {
1981 /* Workaround: Clear the timing override chicken bit again. */
1982 reg = TRANS_CHICKEN2(pipe);
1983 val = I915_READ(reg);
1984 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1985 I915_WRITE(reg, val);
1986 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001987}
1988
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001989static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001990{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001991 u32 val;
1992
Daniel Vetterab9412b2013-05-03 11:49:46 +02001993 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001994 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001995 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001996 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001997 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001998 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001999
2000 /* Workaround: clear timing override bit. */
2001 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002002 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002003 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002004}
2005
2006/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002007 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002008 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002009 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002010 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002011 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002012 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002013static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002014{
Paulo Zanoni03722642014-01-17 13:51:09 -02002015 struct drm_device *dev = crtc->base.dev;
2016 struct drm_i915_private *dev_priv = dev->dev_private;
2017 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002018 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2019 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002020 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002021 int reg;
2022 u32 val;
2023
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002024 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002025 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002026 assert_sprites_disabled(dev_priv, pipe);
2027
Paulo Zanoni681e5812012-12-06 11:12:38 -02002028 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002029 pch_transcoder = TRANSCODER_A;
2030 else
2031 pch_transcoder = pipe;
2032
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033 /*
2034 * A pipe without a PLL won't actually be able to drive bits from
2035 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2036 * need the check.
2037 */
2038 if (!HAS_PCH_SPLIT(dev_priv->dev))
Paulo Zanonifbf32182014-01-17 13:51:11 -02002039 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002040 assert_dsi_pll_enabled(dev_priv);
2041 else
2042 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002043 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002044 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002045 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002046 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002047 assert_fdi_tx_pll_enabled(dev_priv,
2048 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002049 }
2050 /* FIXME: assert CPU port conditions for SNB+ */
2051 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002053 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002055 if (val & PIPECONF_ENABLE) {
2056 WARN_ON(!(pipe == PIPE_A &&
2057 dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson00d70b12011-03-17 07:18:29 +00002058 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002059 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002060
2061 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002062 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002063}
2064
2065/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002066 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002067 * @dev_priv: i915 private structure
2068 * @pipe: pipe to disable
2069 *
2070 * Disable @pipe, making sure that various hardware specific requirements
2071 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2072 *
2073 * @pipe should be %PIPE_A or %PIPE_B.
2074 *
2075 * Will wait until the pipe has shut down before returning.
2076 */
2077static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2078 enum pipe pipe)
2079{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002080 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2081 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002082 int reg;
2083 u32 val;
2084
2085 /*
2086 * Make sure planes won't keep trying to pump pixels to us,
2087 * or we might hang the display.
2088 */
2089 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002090 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002091 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002092
2093 /* Don't disable pipe A or pipe A PLLs if needed */
2094 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2095 return;
2096
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002097 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002098 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002099 if ((val & PIPECONF_ENABLE) == 0)
2100 return;
2101
2102 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2104}
2105
Keith Packardd74362c2011-07-28 14:47:14 -07002106/*
2107 * Plane regs are double buffered, going from enabled->disabled needs a
2108 * trigger in order to latch. The display address reg provides this.
2109 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002110void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2111 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002112{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002113 struct drm_device *dev = dev_priv->dev;
2114 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002115
2116 I915_WRITE(reg, I915_READ(reg));
2117 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002118}
2119
Jesse Barnesb24e7172011-01-04 15:09:30 -08002120/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002121 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002122 * @dev_priv: i915 private structure
2123 * @plane: plane to enable
2124 * @pipe: pipe being fed
2125 *
2126 * Enable @plane on @pipe, making sure that @pipe is running first.
2127 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002128static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2129 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002130{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002131 struct intel_crtc *intel_crtc =
2132 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002133 int reg;
2134 u32 val;
2135
2136 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2137 assert_pipe_enabled(dev_priv, pipe);
2138
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002139 if (intel_crtc->primary_enabled)
2140 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002141
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002142 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002143
Jesse Barnesb24e7172011-01-04 15:09:30 -08002144 reg = DSPCNTR(plane);
2145 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002146 WARN_ON(val & DISPLAY_PLANE_ENABLE);
Chris Wilson00d70b12011-03-17 07:18:29 +00002147
2148 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002149 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002150}
2151
Jesse Barnesb24e7172011-01-04 15:09:30 -08002152/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002153 * intel_disable_primary_hw_plane - disable the primary hardware plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002154 * @dev_priv: i915 private structure
2155 * @plane: plane to disable
2156 * @pipe: pipe consuming the data
2157 *
2158 * Disable @plane; should be an independent operation.
2159 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002160static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2161 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002162{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002163 struct intel_crtc *intel_crtc =
2164 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002165 int reg;
2166 u32 val;
2167
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002168 if (!intel_crtc->primary_enabled)
2169 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002170
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002171 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002172
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 reg = DSPCNTR(plane);
2174 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002175 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
Chris Wilson00d70b12011-03-17 07:18:29 +00002176
2177 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002178 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002179}
2180
Chris Wilson693db182013-03-05 14:52:39 +00002181static bool need_vtd_wa(struct drm_device *dev)
2182{
2183#ifdef CONFIG_INTEL_IOMMU
2184 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2185 return true;
2186#endif
2187 return false;
2188}
2189
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002190static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2191{
2192 int tile_height;
2193
2194 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2195 return ALIGN(height, tile_height);
2196}
2197
Chris Wilson127bd2a2010-07-23 23:32:05 +01002198int
Chris Wilson48b956c2010-09-14 12:50:34 +01002199intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002200 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002201 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002202{
Chris Wilsonce453d82011-02-21 14:43:56 +00002203 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002204 u32 alignment;
2205 int ret;
2206
Chris Wilson05394f32010-11-08 19:18:58 +00002207 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002208 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002209 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2210 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002211 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002212 alignment = 4 * 1024;
2213 else
2214 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002215 break;
2216 case I915_TILING_X:
2217 /* pin() will align the object as required by fence */
2218 alignment = 0;
2219 break;
2220 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002221 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002222 return -EINVAL;
2223 default:
2224 BUG();
2225 }
2226
Chris Wilson693db182013-03-05 14:52:39 +00002227 /* Note that the w/a also requires 64 PTE of padding following the
2228 * bo. We currently fill all unused PTE with the shadow page and so
2229 * we should always have valid PTE following the scanout preventing
2230 * the VT-d warning.
2231 */
2232 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2233 alignment = 256 * 1024;
2234
Chris Wilsonce453d82011-02-21 14:43:56 +00002235 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002236 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002237 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002238 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002239
2240 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2241 * fence, whereas 965+ only requires a fence if using
2242 * framebuffer compression. For simplicity, we always install
2243 * a fence as the cost is not that onerous.
2244 */
Chris Wilson06d98132012-04-17 15:31:24 +01002245 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002246 if (ret)
2247 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002248
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002249 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002250
Chris Wilsonce453d82011-02-21 14:43:56 +00002251 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002252 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002253
2254err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002255 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002256err_interruptible:
2257 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002258 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002259}
2260
Chris Wilson1690e1e2011-12-14 13:57:08 +01002261void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2262{
2263 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002264 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002265}
2266
Daniel Vetterc2c75132012-07-05 12:17:30 +02002267/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2268 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002269unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2270 unsigned int tiling_mode,
2271 unsigned int cpp,
2272 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002273{
Chris Wilsonbc752862013-02-21 20:04:31 +00002274 if (tiling_mode != I915_TILING_NONE) {
2275 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002276
Chris Wilsonbc752862013-02-21 20:04:31 +00002277 tile_rows = *y / 8;
2278 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002279
Chris Wilsonbc752862013-02-21 20:04:31 +00002280 tiles = *x / (512/cpp);
2281 *x %= 512/cpp;
2282
2283 return tile_rows * pitch * 8 + tiles * 4096;
2284 } else {
2285 unsigned int offset;
2286
2287 offset = *y * pitch + *x * cpp;
2288 *y = 0;
2289 *x = (offset & 4095) / cpp;
2290 return offset & -4096;
2291 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002292}
2293
Jesse Barnes46f297f2014-03-07 08:57:48 -08002294int intel_format_to_fourcc(int format)
2295{
2296 switch (format) {
2297 case DISPPLANE_8BPP:
2298 return DRM_FORMAT_C8;
2299 case DISPPLANE_BGRX555:
2300 return DRM_FORMAT_XRGB1555;
2301 case DISPPLANE_BGRX565:
2302 return DRM_FORMAT_RGB565;
2303 default:
2304 case DISPPLANE_BGRX888:
2305 return DRM_FORMAT_XRGB8888;
2306 case DISPPLANE_RGBX888:
2307 return DRM_FORMAT_XBGR8888;
2308 case DISPPLANE_BGRX101010:
2309 return DRM_FORMAT_XRGB2101010;
2310 case DISPPLANE_RGBX101010:
2311 return DRM_FORMAT_XBGR2101010;
2312 }
2313}
2314
Jesse Barnes484b41d2014-03-07 08:57:55 -08002315static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002316 struct intel_plane_config *plane_config)
2317{
2318 struct drm_device *dev = crtc->base.dev;
2319 struct drm_i915_gem_object *obj = NULL;
2320 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2321 u32 base = plane_config->base;
2322
Chris Wilsonff2652e2014-03-10 08:07:02 +00002323 if (plane_config->size == 0)
2324 return false;
2325
Jesse Barnes46f297f2014-03-07 08:57:48 -08002326 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2327 plane_config->size);
2328 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002329 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002330
2331 if (plane_config->tiled) {
2332 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002333 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002334 }
2335
Dave Airlie66e514c2014-04-03 07:51:54 +10002336 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2337 mode_cmd.width = crtc->base.primary->fb->width;
2338 mode_cmd.height = crtc->base.primary->fb->height;
2339 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002340
2341 mutex_lock(&dev->struct_mutex);
2342
Dave Airlie66e514c2014-04-03 07:51:54 +10002343 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002344 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002345 DRM_DEBUG_KMS("intel fb init failed\n");
2346 goto out_unref_obj;
2347 }
2348
2349 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002350
2351 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2352 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002353
2354out_unref_obj:
2355 drm_gem_object_unreference(&obj->base);
2356 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002357 return false;
2358}
2359
2360static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2361 struct intel_plane_config *plane_config)
2362{
2363 struct drm_device *dev = intel_crtc->base.dev;
2364 struct drm_crtc *c;
2365 struct intel_crtc *i;
2366 struct intel_framebuffer *fb;
2367
Dave Airlie66e514c2014-04-03 07:51:54 +10002368 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002369 return;
2370
2371 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2372 return;
2373
Dave Airlie66e514c2014-04-03 07:51:54 +10002374 kfree(intel_crtc->base.primary->fb);
2375 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002376
2377 /*
2378 * Failed to alloc the obj, check to see if we should share
2379 * an fb with another CRTC instead
2380 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002381 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002382 i = to_intel_crtc(c);
2383
2384 if (c == &intel_crtc->base)
2385 continue;
2386
Dave Airlie66e514c2014-04-03 07:51:54 +10002387 if (!i->active || !c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002388 continue;
2389
Dave Airlie66e514c2014-04-03 07:51:54 +10002390 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002391 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002392 drm_framebuffer_reference(c->primary->fb);
2393 intel_crtc->base.primary->fb = c->primary->fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002394 break;
2395 }
2396 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002397}
2398
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002399static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2400 struct drm_framebuffer *fb,
2401 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002402{
2403 struct drm_device *dev = crtc->dev;
2404 struct drm_i915_private *dev_priv = dev->dev_private;
2405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2406 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002407 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002408 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002409 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002410 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002411 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002412
Jesse Barnes81255562010-08-02 12:07:50 -07002413 intel_fb = to_intel_framebuffer(fb);
2414 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002415
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 reg = DSPCNTR(plane);
2417 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002418 /* Mask out pixel format bits in case we change it */
2419 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002420 switch (fb->pixel_format) {
2421 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002422 dspcntr |= DISPPLANE_8BPP;
2423 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002424 case DRM_FORMAT_XRGB1555:
2425 case DRM_FORMAT_ARGB1555:
2426 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002427 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002428 case DRM_FORMAT_RGB565:
2429 dspcntr |= DISPPLANE_BGRX565;
2430 break;
2431 case DRM_FORMAT_XRGB8888:
2432 case DRM_FORMAT_ARGB8888:
2433 dspcntr |= DISPPLANE_BGRX888;
2434 break;
2435 case DRM_FORMAT_XBGR8888:
2436 case DRM_FORMAT_ABGR8888:
2437 dspcntr |= DISPPLANE_RGBX888;
2438 break;
2439 case DRM_FORMAT_XRGB2101010:
2440 case DRM_FORMAT_ARGB2101010:
2441 dspcntr |= DISPPLANE_BGRX101010;
2442 break;
2443 case DRM_FORMAT_XBGR2101010:
2444 case DRM_FORMAT_ABGR2101010:
2445 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002446 break;
2447 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002448 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002449 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002450
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002451 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002452 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002453 dspcntr |= DISPPLANE_TILED;
2454 else
2455 dspcntr &= ~DISPPLANE_TILED;
2456 }
2457
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002458 if (IS_G4X(dev))
2459 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2460
Chris Wilson5eddb702010-09-11 13:48:45 +01002461 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002462
Daniel Vettere506a0c2012-07-05 12:17:29 +02002463 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002464
Daniel Vetterc2c75132012-07-05 12:17:30 +02002465 if (INTEL_INFO(dev)->gen >= 4) {
2466 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002467 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2468 fb->bits_per_pixel / 8,
2469 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002470 linear_offset -= intel_crtc->dspaddr_offset;
2471 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002472 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002473 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002474
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002475 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2476 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2477 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002478 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002479 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002480 I915_WRITE(DSPSURF(plane),
2481 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002482 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002483 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002484 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002485 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002486 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002487}
2488
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002489static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2490 struct drm_framebuffer *fb,
2491 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002492{
2493 struct drm_device *dev = crtc->dev;
2494 struct drm_i915_private *dev_priv = dev->dev_private;
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2496 struct intel_framebuffer *intel_fb;
2497 struct drm_i915_gem_object *obj;
2498 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002499 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002500 u32 dspcntr;
2501 u32 reg;
2502
Jesse Barnes17638cd2011-06-24 12:19:23 -07002503 intel_fb = to_intel_framebuffer(fb);
2504 obj = intel_fb->obj;
2505
2506 reg = DSPCNTR(plane);
2507 dspcntr = I915_READ(reg);
2508 /* Mask out pixel format bits in case we change it */
2509 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002510 switch (fb->pixel_format) {
2511 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002512 dspcntr |= DISPPLANE_8BPP;
2513 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002514 case DRM_FORMAT_RGB565:
2515 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002516 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002517 case DRM_FORMAT_XRGB8888:
2518 case DRM_FORMAT_ARGB8888:
2519 dspcntr |= DISPPLANE_BGRX888;
2520 break;
2521 case DRM_FORMAT_XBGR8888:
2522 case DRM_FORMAT_ABGR8888:
2523 dspcntr |= DISPPLANE_RGBX888;
2524 break;
2525 case DRM_FORMAT_XRGB2101010:
2526 case DRM_FORMAT_ARGB2101010:
2527 dspcntr |= DISPPLANE_BGRX101010;
2528 break;
2529 case DRM_FORMAT_XBGR2101010:
2530 case DRM_FORMAT_ABGR2101010:
2531 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002532 break;
2533 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002534 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002535 }
2536
2537 if (obj->tiling_mode != I915_TILING_NONE)
2538 dspcntr |= DISPPLANE_TILED;
2539 else
2540 dspcntr &= ~DISPPLANE_TILED;
2541
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002542 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002543 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2544 else
2545 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002546
2547 I915_WRITE(reg, dspcntr);
2548
Daniel Vettere506a0c2012-07-05 12:17:29 +02002549 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002550 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002551 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2552 fb->bits_per_pixel / 8,
2553 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002554 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002555
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002556 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2557 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2558 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002559 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002560 I915_WRITE(DSPSURF(plane),
2561 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002562 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002563 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2564 } else {
2565 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2566 I915_WRITE(DSPLINOFF(plane), linear_offset);
2567 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002568 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002569}
2570
2571/* Assume fb object is pinned & idle & fenced and just update base pointers */
2572static int
2573intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2574 int x, int y, enum mode_set_atomic state)
2575{
2576 struct drm_device *dev = crtc->dev;
2577 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002578
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002579 if (dev_priv->display.disable_fbc)
2580 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002581 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002582
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002583 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2584
2585 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002586}
2587
Ville Syrjälä96a02912013-02-18 19:08:49 +02002588void intel_display_handle_reset(struct drm_device *dev)
2589{
2590 struct drm_i915_private *dev_priv = dev->dev_private;
2591 struct drm_crtc *crtc;
2592
2593 /*
2594 * Flips in the rings have been nuked by the reset,
2595 * so complete all pending flips so that user space
2596 * will get its events and not get stuck.
2597 *
2598 * Also update the base address of all primary
2599 * planes to the the last fb to make sure we're
2600 * showing the correct fb after a reset.
2601 *
2602 * Need to make two loops over the crtcs so that we
2603 * don't try to grab a crtc mutex before the
2604 * pending_flip_queue really got woken up.
2605 */
2606
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002607 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2609 enum plane plane = intel_crtc->plane;
2610
2611 intel_prepare_page_flip(dev, plane);
2612 intel_finish_page_flip_plane(dev, plane);
2613 }
2614
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002615 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2617
Rob Clark51fd3712013-11-19 12:10:12 -05002618 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002619 /*
2620 * FIXME: Once we have proper support for primary planes (and
2621 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002622 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002623 */
Matt Roperf4510a22014-04-01 15:22:40 -07002624 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002625 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002626 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002627 crtc->x,
2628 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002629 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002630 }
2631}
2632
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002633static int
Chris Wilson14667a42012-04-03 17:58:35 +01002634intel_finish_fb(struct drm_framebuffer *old_fb)
2635{
2636 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2637 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2638 bool was_interruptible = dev_priv->mm.interruptible;
2639 int ret;
2640
Chris Wilson14667a42012-04-03 17:58:35 +01002641 /* Big Hammer, we also need to ensure that any pending
2642 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2643 * current scanout is retired before unpinning the old
2644 * framebuffer.
2645 *
2646 * This should only fail upon a hung GPU, in which case we
2647 * can safely continue.
2648 */
2649 dev_priv->mm.interruptible = false;
2650 ret = i915_gem_object_finish_gpu(obj);
2651 dev_priv->mm.interruptible = was_interruptible;
2652
2653 return ret;
2654}
2655
Chris Wilson7d5e3792014-03-04 13:15:08 +00002656static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2657{
2658 struct drm_device *dev = crtc->dev;
2659 struct drm_i915_private *dev_priv = dev->dev_private;
2660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2661 unsigned long flags;
2662 bool pending;
2663
2664 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2665 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2666 return false;
2667
2668 spin_lock_irqsave(&dev->event_lock, flags);
2669 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2670 spin_unlock_irqrestore(&dev->event_lock, flags);
2671
2672 return pending;
2673}
2674
Chris Wilson14667a42012-04-03 17:58:35 +01002675static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002676intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002677 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002678{
2679 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002680 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002682 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002683 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002684
Chris Wilson7d5e3792014-03-04 13:15:08 +00002685 if (intel_crtc_has_pending_flip(crtc)) {
2686 DRM_ERROR("pipe is still busy with an old pageflip\n");
2687 return -EBUSY;
2688 }
2689
Jesse Barnes79e53942008-11-07 14:24:08 -08002690 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002691 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002692 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002693 return 0;
2694 }
2695
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002696 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002697 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2698 plane_name(intel_crtc->plane),
2699 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002700 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002701 }
2702
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002703 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002704 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002705 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002706 NULL);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002707 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002708 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002709 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002710 return ret;
2711 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002712
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002713 /*
2714 * Update pipe size and adjust fitter if needed: the reason for this is
2715 * that in compute_mode_changes we check the native mode (not the pfit
2716 * mode) to see if we can flip rather than do a full mode set. In the
2717 * fastboot case, we'll flip, but if we don't update the pipesrc and
2718 * pfit state, we'll end up with a big fb scanned out into the wrong
2719 * sized surface.
2720 *
2721 * To fix this properly, we need to hoist the checks up into
2722 * compute_mode_changes (or above), check the actual pfit state and
2723 * whether the platform allows pfit disable with pipe active, and only
2724 * then update the pipesrc and pfit state, even on the flip path.
2725 */
Jani Nikulad330a952014-01-21 11:24:25 +02002726 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002727 const struct drm_display_mode *adjusted_mode =
2728 &intel_crtc->config.adjusted_mode;
2729
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002730 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002731 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2732 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002733 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002734 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2735 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2736 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2737 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2738 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2739 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002740 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2741 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002742 }
2743
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002744 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002745
Matt Roperf4510a22014-04-01 15:22:40 -07002746 old_fb = crtc->primary->fb;
2747 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002748 crtc->x = x;
2749 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002750
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002751 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002752 if (intel_crtc->active && old_fb != fb)
2753 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002754 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002755 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002756 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002757 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002758
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002759 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002760 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002761 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002762 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002763
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002764 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002765}
2766
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002767static void intel_fdi_normal_train(struct drm_crtc *crtc)
2768{
2769 struct drm_device *dev = crtc->dev;
2770 struct drm_i915_private *dev_priv = dev->dev_private;
2771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2772 int pipe = intel_crtc->pipe;
2773 u32 reg, temp;
2774
2775 /* enable normal train */
2776 reg = FDI_TX_CTL(pipe);
2777 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002778 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002779 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2780 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002781 } else {
2782 temp &= ~FDI_LINK_TRAIN_NONE;
2783 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002784 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002785 I915_WRITE(reg, temp);
2786
2787 reg = FDI_RX_CTL(pipe);
2788 temp = I915_READ(reg);
2789 if (HAS_PCH_CPT(dev)) {
2790 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2791 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2792 } else {
2793 temp &= ~FDI_LINK_TRAIN_NONE;
2794 temp |= FDI_LINK_TRAIN_NONE;
2795 }
2796 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2797
2798 /* wait one idle pattern time */
2799 POSTING_READ(reg);
2800 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002801
2802 /* IVB wants error correction enabled */
2803 if (IS_IVYBRIDGE(dev))
2804 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2805 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002806}
2807
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002808static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002809{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002810 return crtc->base.enabled && crtc->active &&
2811 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002812}
2813
Daniel Vetter01a415f2012-10-27 15:58:40 +02002814static void ivb_modeset_global_resources(struct drm_device *dev)
2815{
2816 struct drm_i915_private *dev_priv = dev->dev_private;
2817 struct intel_crtc *pipe_B_crtc =
2818 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2819 struct intel_crtc *pipe_C_crtc =
2820 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2821 uint32_t temp;
2822
Daniel Vetter1e833f42013-02-19 22:31:57 +01002823 /*
2824 * When everything is off disable fdi C so that we could enable fdi B
2825 * with all lanes. Note that we don't care about enabled pipes without
2826 * an enabled pch encoder.
2827 */
2828 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2829 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002830 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2831 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2832
2833 temp = I915_READ(SOUTH_CHICKEN1);
2834 temp &= ~FDI_BC_BIFURCATION_SELECT;
2835 DRM_DEBUG_KMS("disabling fdi C rx\n");
2836 I915_WRITE(SOUTH_CHICKEN1, temp);
2837 }
2838}
2839
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002840/* The FDI link training functions for ILK/Ibexpeak. */
2841static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2842{
2843 struct drm_device *dev = crtc->dev;
2844 struct drm_i915_private *dev_priv = dev->dev_private;
2845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2846 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002847 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002848
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002849 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002850 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002851
Adam Jacksone1a44742010-06-25 15:32:14 -04002852 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2853 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002854 reg = FDI_RX_IMR(pipe);
2855 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002856 temp &= ~FDI_RX_SYMBOL_LOCK;
2857 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002858 I915_WRITE(reg, temp);
2859 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002860 udelay(150);
2861
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002862 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002863 reg = FDI_TX_CTL(pipe);
2864 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002865 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2866 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002867 temp &= ~FDI_LINK_TRAIN_NONE;
2868 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002869 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002870
Chris Wilson5eddb702010-09-11 13:48:45 +01002871 reg = FDI_RX_CTL(pipe);
2872 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002873 temp &= ~FDI_LINK_TRAIN_NONE;
2874 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002875 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2876
2877 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002878 udelay(150);
2879
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002880 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002881 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2882 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2883 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002884
Chris Wilson5eddb702010-09-11 13:48:45 +01002885 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002886 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002887 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002888 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2889
2890 if ((temp & FDI_RX_BIT_LOCK)) {
2891 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002892 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002893 break;
2894 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002895 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002896 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002897 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002898
2899 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002900 reg = FDI_TX_CTL(pipe);
2901 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002902 temp &= ~FDI_LINK_TRAIN_NONE;
2903 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002904 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002905
Chris Wilson5eddb702010-09-11 13:48:45 +01002906 reg = FDI_RX_CTL(pipe);
2907 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002908 temp &= ~FDI_LINK_TRAIN_NONE;
2909 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002910 I915_WRITE(reg, temp);
2911
2912 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002913 udelay(150);
2914
Chris Wilson5eddb702010-09-11 13:48:45 +01002915 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002916 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002917 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002918 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2919
2920 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002921 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002922 DRM_DEBUG_KMS("FDI train 2 done.\n");
2923 break;
2924 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002925 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002926 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002927 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002928
2929 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002930
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002931}
2932
Akshay Joshi0206e352011-08-16 15:34:10 -04002933static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002934 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2935 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2936 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2937 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2938};
2939
2940/* The FDI link training functions for SNB/Cougarpoint. */
2941static void gen6_fdi_link_train(struct drm_crtc *crtc)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_i915_private *dev_priv = dev->dev_private;
2945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2946 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002947 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002948
Adam Jacksone1a44742010-06-25 15:32:14 -04002949 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2950 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002951 reg = FDI_RX_IMR(pipe);
2952 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002953 temp &= ~FDI_RX_SYMBOL_LOCK;
2954 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002955 I915_WRITE(reg, temp);
2956
2957 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002958 udelay(150);
2959
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002960 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002961 reg = FDI_TX_CTL(pipe);
2962 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002963 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2964 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002965 temp &= ~FDI_LINK_TRAIN_NONE;
2966 temp |= FDI_LINK_TRAIN_PATTERN_1;
2967 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2968 /* SNB-B */
2969 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002970 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002971
Daniel Vetterd74cf322012-10-26 10:58:13 +02002972 I915_WRITE(FDI_RX_MISC(pipe),
2973 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2974
Chris Wilson5eddb702010-09-11 13:48:45 +01002975 reg = FDI_RX_CTL(pipe);
2976 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002977 if (HAS_PCH_CPT(dev)) {
2978 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2979 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2980 } else {
2981 temp &= ~FDI_LINK_TRAIN_NONE;
2982 temp |= FDI_LINK_TRAIN_PATTERN_1;
2983 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002984 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2985
2986 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002987 udelay(150);
2988
Akshay Joshi0206e352011-08-16 15:34:10 -04002989 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002990 reg = FDI_TX_CTL(pipe);
2991 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002992 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2993 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002994 I915_WRITE(reg, temp);
2995
2996 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002997 udelay(500);
2998
Sean Paulfa37d392012-03-02 12:53:39 -05002999 for (retry = 0; retry < 5; retry++) {
3000 reg = FDI_RX_IIR(pipe);
3001 temp = I915_READ(reg);
3002 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3003 if (temp & FDI_RX_BIT_LOCK) {
3004 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3005 DRM_DEBUG_KMS("FDI train 1 done.\n");
3006 break;
3007 }
3008 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003009 }
Sean Paulfa37d392012-03-02 12:53:39 -05003010 if (retry < 5)
3011 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003012 }
3013 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003014 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003015
3016 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003017 reg = FDI_TX_CTL(pipe);
3018 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003019 temp &= ~FDI_LINK_TRAIN_NONE;
3020 temp |= FDI_LINK_TRAIN_PATTERN_2;
3021 if (IS_GEN6(dev)) {
3022 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3023 /* SNB-B */
3024 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3025 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003026 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003027
Chris Wilson5eddb702010-09-11 13:48:45 +01003028 reg = FDI_RX_CTL(pipe);
3029 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003030 if (HAS_PCH_CPT(dev)) {
3031 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3032 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3033 } else {
3034 temp &= ~FDI_LINK_TRAIN_NONE;
3035 temp |= FDI_LINK_TRAIN_PATTERN_2;
3036 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003037 I915_WRITE(reg, temp);
3038
3039 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003040 udelay(150);
3041
Akshay Joshi0206e352011-08-16 15:34:10 -04003042 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003043 reg = FDI_TX_CTL(pipe);
3044 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003045 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3046 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003047 I915_WRITE(reg, temp);
3048
3049 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003050 udelay(500);
3051
Sean Paulfa37d392012-03-02 12:53:39 -05003052 for (retry = 0; retry < 5; retry++) {
3053 reg = FDI_RX_IIR(pipe);
3054 temp = I915_READ(reg);
3055 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3056 if (temp & FDI_RX_SYMBOL_LOCK) {
3057 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3058 DRM_DEBUG_KMS("FDI train 2 done.\n");
3059 break;
3060 }
3061 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003062 }
Sean Paulfa37d392012-03-02 12:53:39 -05003063 if (retry < 5)
3064 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003065 }
3066 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003067 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003068
3069 DRM_DEBUG_KMS("FDI train done.\n");
3070}
3071
Jesse Barnes357555c2011-04-28 15:09:55 -07003072/* Manual link training for Ivy Bridge A0 parts */
3073static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3074{
3075 struct drm_device *dev = crtc->dev;
3076 struct drm_i915_private *dev_priv = dev->dev_private;
3077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3078 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003079 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003080
3081 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3082 for train result */
3083 reg = FDI_RX_IMR(pipe);
3084 temp = I915_READ(reg);
3085 temp &= ~FDI_RX_SYMBOL_LOCK;
3086 temp &= ~FDI_RX_BIT_LOCK;
3087 I915_WRITE(reg, temp);
3088
3089 POSTING_READ(reg);
3090 udelay(150);
3091
Daniel Vetter01a415f2012-10-27 15:58:40 +02003092 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3093 I915_READ(FDI_RX_IIR(pipe)));
3094
Jesse Barnes139ccd32013-08-19 11:04:55 -07003095 /* Try each vswing and preemphasis setting twice before moving on */
3096 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3097 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003098 reg = FDI_TX_CTL(pipe);
3099 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003100 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3101 temp &= ~FDI_TX_ENABLE;
3102 I915_WRITE(reg, temp);
3103
3104 reg = FDI_RX_CTL(pipe);
3105 temp = I915_READ(reg);
3106 temp &= ~FDI_LINK_TRAIN_AUTO;
3107 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3108 temp &= ~FDI_RX_ENABLE;
3109 I915_WRITE(reg, temp);
3110
3111 /* enable CPU FDI TX and PCH FDI RX */
3112 reg = FDI_TX_CTL(pipe);
3113 temp = I915_READ(reg);
3114 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3115 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3116 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003117 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003118 temp |= snb_b_fdi_train_param[j/2];
3119 temp |= FDI_COMPOSITE_SYNC;
3120 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3121
3122 I915_WRITE(FDI_RX_MISC(pipe),
3123 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3124
3125 reg = FDI_RX_CTL(pipe);
3126 temp = I915_READ(reg);
3127 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3128 temp |= FDI_COMPOSITE_SYNC;
3129 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3130
3131 POSTING_READ(reg);
3132 udelay(1); /* should be 0.5us */
3133
3134 for (i = 0; i < 4; i++) {
3135 reg = FDI_RX_IIR(pipe);
3136 temp = I915_READ(reg);
3137 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3138
3139 if (temp & FDI_RX_BIT_LOCK ||
3140 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3141 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3142 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3143 i);
3144 break;
3145 }
3146 udelay(1); /* should be 0.5us */
3147 }
3148 if (i == 4) {
3149 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3150 continue;
3151 }
3152
3153 /* Train 2 */
3154 reg = FDI_TX_CTL(pipe);
3155 temp = I915_READ(reg);
3156 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3157 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3158 I915_WRITE(reg, temp);
3159
3160 reg = FDI_RX_CTL(pipe);
3161 temp = I915_READ(reg);
3162 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3163 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003164 I915_WRITE(reg, temp);
3165
3166 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003167 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003168
Jesse Barnes139ccd32013-08-19 11:04:55 -07003169 for (i = 0; i < 4; i++) {
3170 reg = FDI_RX_IIR(pipe);
3171 temp = I915_READ(reg);
3172 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003173
Jesse Barnes139ccd32013-08-19 11:04:55 -07003174 if (temp & FDI_RX_SYMBOL_LOCK ||
3175 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3176 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3177 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3178 i);
3179 goto train_done;
3180 }
3181 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003182 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003183 if (i == 4)
3184 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003185 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003186
Jesse Barnes139ccd32013-08-19 11:04:55 -07003187train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003188 DRM_DEBUG_KMS("FDI train done.\n");
3189}
3190
Daniel Vetter88cefb62012-08-12 19:27:14 +02003191static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003192{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003193 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003194 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003195 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003196 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003197
Jesse Barnesc64e3112010-09-10 11:27:03 -07003198
Jesse Barnes0e23b992010-09-10 11:10:00 -07003199 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003200 reg = FDI_RX_CTL(pipe);
3201 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003202 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3203 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003204 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003205 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3206
3207 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003208 udelay(200);
3209
3210 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003211 temp = I915_READ(reg);
3212 I915_WRITE(reg, temp | FDI_PCDCLK);
3213
3214 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003215 udelay(200);
3216
Paulo Zanoni20749732012-11-23 15:30:38 -02003217 /* Enable CPU FDI TX PLL, always on for Ironlake */
3218 reg = FDI_TX_CTL(pipe);
3219 temp = I915_READ(reg);
3220 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3221 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003222
Paulo Zanoni20749732012-11-23 15:30:38 -02003223 POSTING_READ(reg);
3224 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003225 }
3226}
3227
Daniel Vetter88cefb62012-08-12 19:27:14 +02003228static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3229{
3230 struct drm_device *dev = intel_crtc->base.dev;
3231 struct drm_i915_private *dev_priv = dev->dev_private;
3232 int pipe = intel_crtc->pipe;
3233 u32 reg, temp;
3234
3235 /* Switch from PCDclk to Rawclk */
3236 reg = FDI_RX_CTL(pipe);
3237 temp = I915_READ(reg);
3238 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3239
3240 /* Disable CPU FDI TX PLL */
3241 reg = FDI_TX_CTL(pipe);
3242 temp = I915_READ(reg);
3243 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3244
3245 POSTING_READ(reg);
3246 udelay(100);
3247
3248 reg = FDI_RX_CTL(pipe);
3249 temp = I915_READ(reg);
3250 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3251
3252 /* Wait for the clocks to turn off. */
3253 POSTING_READ(reg);
3254 udelay(100);
3255}
3256
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003257static void ironlake_fdi_disable(struct drm_crtc *crtc)
3258{
3259 struct drm_device *dev = crtc->dev;
3260 struct drm_i915_private *dev_priv = dev->dev_private;
3261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3262 int pipe = intel_crtc->pipe;
3263 u32 reg, temp;
3264
3265 /* disable CPU FDI tx and PCH FDI rx */
3266 reg = FDI_TX_CTL(pipe);
3267 temp = I915_READ(reg);
3268 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3269 POSTING_READ(reg);
3270
3271 reg = FDI_RX_CTL(pipe);
3272 temp = I915_READ(reg);
3273 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003274 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003275 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3276
3277 POSTING_READ(reg);
3278 udelay(100);
3279
3280 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003281 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003282 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003283
3284 /* still set train pattern 1 */
3285 reg = FDI_TX_CTL(pipe);
3286 temp = I915_READ(reg);
3287 temp &= ~FDI_LINK_TRAIN_NONE;
3288 temp |= FDI_LINK_TRAIN_PATTERN_1;
3289 I915_WRITE(reg, temp);
3290
3291 reg = FDI_RX_CTL(pipe);
3292 temp = I915_READ(reg);
3293 if (HAS_PCH_CPT(dev)) {
3294 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3295 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3296 } else {
3297 temp &= ~FDI_LINK_TRAIN_NONE;
3298 temp |= FDI_LINK_TRAIN_PATTERN_1;
3299 }
3300 /* BPC in FDI rx is consistent with that in PIPECONF */
3301 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003302 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003303 I915_WRITE(reg, temp);
3304
3305 POSTING_READ(reg);
3306 udelay(100);
3307}
3308
Chris Wilson5dce5b932014-01-20 10:17:36 +00003309bool intel_has_pending_fb_unpin(struct drm_device *dev)
3310{
3311 struct intel_crtc *crtc;
3312
3313 /* Note that we don't need to be called with mode_config.lock here
3314 * as our list of CRTC objects is static for the lifetime of the
3315 * device and so cannot disappear as we iterate. Similarly, we can
3316 * happily treat the predicates as racy, atomic checks as userspace
3317 * cannot claim and pin a new fb without at least acquring the
3318 * struct_mutex and so serialising with us.
3319 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003320 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003321 if (atomic_read(&crtc->unpin_work_count) == 0)
3322 continue;
3323
3324 if (crtc->unpin_work)
3325 intel_wait_for_vblank(dev, crtc->pipe);
3326
3327 return true;
3328 }
3329
3330 return false;
3331}
3332
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003333void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003334{
Chris Wilson0f911282012-04-17 10:05:38 +01003335 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003336 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003337
Matt Roperf4510a22014-04-01 15:22:40 -07003338 if (crtc->primary->fb == NULL)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003339 return;
3340
Daniel Vetter2c10d572012-12-20 21:24:07 +01003341 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3342
Daniel Vettereed6d672014-05-19 16:09:35 +02003343 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3344 !intel_crtc_has_pending_flip(crtc),
3345 60*HZ) == 0);
Chris Wilson5bb61642012-09-27 21:25:58 +01003346
Chris Wilson0f911282012-04-17 10:05:38 +01003347 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07003348 intel_finish_fb(crtc->primary->fb);
Chris Wilson0f911282012-04-17 10:05:38 +01003349 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003350}
3351
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003352/* Program iCLKIP clock to the desired frequency */
3353static void lpt_program_iclkip(struct drm_crtc *crtc)
3354{
3355 struct drm_device *dev = crtc->dev;
3356 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003357 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003358 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3359 u32 temp;
3360
Daniel Vetter09153002012-12-12 14:06:44 +01003361 mutex_lock(&dev_priv->dpio_lock);
3362
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003363 /* It is necessary to ungate the pixclk gate prior to programming
3364 * the divisors, and gate it back when it is done.
3365 */
3366 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3367
3368 /* Disable SSCCTL */
3369 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003370 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3371 SBI_SSCCTL_DISABLE,
3372 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003373
3374 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003375 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003376 auxdiv = 1;
3377 divsel = 0x41;
3378 phaseinc = 0x20;
3379 } else {
3380 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003381 * but the adjusted_mode->crtc_clock in in KHz. To get the
3382 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003383 * convert the virtual clock precision to KHz here for higher
3384 * precision.
3385 */
3386 u32 iclk_virtual_root_freq = 172800 * 1000;
3387 u32 iclk_pi_range = 64;
3388 u32 desired_divisor, msb_divisor_value, pi_value;
3389
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003390 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003391 msb_divisor_value = desired_divisor / iclk_pi_range;
3392 pi_value = desired_divisor % iclk_pi_range;
3393
3394 auxdiv = 0;
3395 divsel = msb_divisor_value - 2;
3396 phaseinc = pi_value;
3397 }
3398
3399 /* This should not happen with any sane values */
3400 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3401 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3402 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3403 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3404
3405 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 +03003406 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003407 auxdiv,
3408 divsel,
3409 phasedir,
3410 phaseinc);
3411
3412 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003413 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003414 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3415 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3416 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3417 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3418 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3419 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003420 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003421
3422 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003423 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003424 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3425 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003426 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003427
3428 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003429 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003430 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003431 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003432
3433 /* Wait for initialization time */
3434 udelay(24);
3435
3436 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003437
3438 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003439}
3440
Daniel Vetter275f01b22013-05-03 11:49:47 +02003441static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3442 enum pipe pch_transcoder)
3443{
3444 struct drm_device *dev = crtc->base.dev;
3445 struct drm_i915_private *dev_priv = dev->dev_private;
3446 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3447
3448 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3449 I915_READ(HTOTAL(cpu_transcoder)));
3450 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3451 I915_READ(HBLANK(cpu_transcoder)));
3452 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3453 I915_READ(HSYNC(cpu_transcoder)));
3454
3455 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3456 I915_READ(VTOTAL(cpu_transcoder)));
3457 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3458 I915_READ(VBLANK(cpu_transcoder)));
3459 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3460 I915_READ(VSYNC(cpu_transcoder)));
3461 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3462 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3463}
3464
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003465static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3466{
3467 struct drm_i915_private *dev_priv = dev->dev_private;
3468 uint32_t temp;
3469
3470 temp = I915_READ(SOUTH_CHICKEN1);
3471 if (temp & FDI_BC_BIFURCATION_SELECT)
3472 return;
3473
3474 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3475 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3476
3477 temp |= FDI_BC_BIFURCATION_SELECT;
3478 DRM_DEBUG_KMS("enabling fdi C rx\n");
3479 I915_WRITE(SOUTH_CHICKEN1, temp);
3480 POSTING_READ(SOUTH_CHICKEN1);
3481}
3482
3483static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3484{
3485 struct drm_device *dev = intel_crtc->base.dev;
3486 struct drm_i915_private *dev_priv = dev->dev_private;
3487
3488 switch (intel_crtc->pipe) {
3489 case PIPE_A:
3490 break;
3491 case PIPE_B:
3492 if (intel_crtc->config.fdi_lanes > 2)
3493 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3494 else
3495 cpt_enable_fdi_bc_bifurcation(dev);
3496
3497 break;
3498 case PIPE_C:
3499 cpt_enable_fdi_bc_bifurcation(dev);
3500
3501 break;
3502 default:
3503 BUG();
3504 }
3505}
3506
Jesse Barnesf67a5592011-01-05 10:31:48 -08003507/*
3508 * Enable PCH resources required for PCH ports:
3509 * - PCH PLLs
3510 * - FDI training & RX/TX
3511 * - update transcoder timings
3512 * - DP transcoding bits
3513 * - transcoder
3514 */
3515static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003516{
3517 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003518 struct drm_i915_private *dev_priv = dev->dev_private;
3519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3520 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003521 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003522
Daniel Vetterab9412b2013-05-03 11:49:46 +02003523 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003524
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003525 if (IS_IVYBRIDGE(dev))
3526 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3527
Daniel Vettercd986ab2012-10-26 10:58:12 +02003528 /* Write the TU size bits before fdi link training, so that error
3529 * detection works. */
3530 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3531 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3532
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003533 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003534 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003535
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003536 /* We need to program the right clock selection before writing the pixel
3537 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003538 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003539 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003540
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003541 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003542 temp |= TRANS_DPLL_ENABLE(pipe);
3543 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003544 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003545 temp |= sel;
3546 else
3547 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003548 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003549 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003550
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003551 /* XXX: pch pll's can be enabled any time before we enable the PCH
3552 * transcoder, and we actually should do this to not upset any PCH
3553 * transcoder that already use the clock when we share it.
3554 *
3555 * Note that enable_shared_dpll tries to do the right thing, but
3556 * get_shared_dpll unconditionally resets the pll - we need that to have
3557 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003558 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003559
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003560 /* set transcoder timing, panel must allow it */
3561 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003562 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003563
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003564 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003565
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003566 /* For PCH DP, enable TRANS_DP_CTL */
3567 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003568 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3569 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003570 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003571 reg = TRANS_DP_CTL(pipe);
3572 temp = I915_READ(reg);
3573 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003574 TRANS_DP_SYNC_MASK |
3575 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 temp |= (TRANS_DP_OUTPUT_ENABLE |
3577 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003578 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003579
3580 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003582 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003583 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003584
3585 switch (intel_trans_dp_port_sel(crtc)) {
3586 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003588 break;
3589 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003590 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003591 break;
3592 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003593 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003594 break;
3595 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003596 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003597 }
3598
Chris Wilson5eddb702010-09-11 13:48:45 +01003599 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003600 }
3601
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003602 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003603}
3604
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003605static void lpt_pch_enable(struct drm_crtc *crtc)
3606{
3607 struct drm_device *dev = crtc->dev;
3608 struct drm_i915_private *dev_priv = dev->dev_private;
3609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003610 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003611
Daniel Vetterab9412b2013-05-03 11:49:46 +02003612 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003613
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003614 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003615
Paulo Zanoni0540e482012-10-31 18:12:40 -02003616 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003617 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003618
Paulo Zanoni937bb612012-10-31 18:12:47 -02003619 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003620}
3621
Daniel Vettere2b78262013-06-07 23:10:03 +02003622static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003623{
Daniel Vettere2b78262013-06-07 23:10:03 +02003624 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003625
3626 if (pll == NULL)
3627 return;
3628
3629 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003630 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003631 return;
3632 }
3633
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003634 if (--pll->refcount == 0) {
3635 WARN_ON(pll->on);
3636 WARN_ON(pll->active);
3637 }
3638
Daniel Vettera43f6e02013-06-07 23:10:32 +02003639 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003640}
3641
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003642static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003643{
Daniel Vettere2b78262013-06-07 23:10:03 +02003644 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3645 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3646 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003647
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003648 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003649 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3650 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003651 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003652 }
3653
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003654 if (HAS_PCH_IBX(dev_priv->dev)) {
3655 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003656 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003657 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003658
Daniel Vetter46edb022013-06-05 13:34:12 +02003659 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3660 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003661
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003662 WARN_ON(pll->refcount);
3663
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003664 goto found;
3665 }
3666
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003667 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3668 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003669
3670 /* Only want to check enabled timings first */
3671 if (pll->refcount == 0)
3672 continue;
3673
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003674 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3675 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003676 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003677 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003678 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003679
3680 goto found;
3681 }
3682 }
3683
3684 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003685 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3686 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003687 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003688 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3689 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003690 goto found;
3691 }
3692 }
3693
3694 return NULL;
3695
3696found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003697 if (pll->refcount == 0)
3698 pll->hw_state = crtc->config.dpll_hw_state;
3699
Daniel Vettera43f6e02013-06-07 23:10:32 +02003700 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003701 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3702 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003703
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003704 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003705
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003706 return pll;
3707}
3708
Daniel Vettera1520312013-05-03 11:49:50 +02003709static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003710{
3711 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003712 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003713 u32 temp;
3714
3715 temp = I915_READ(dslreg);
3716 udelay(500);
3717 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003718 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003719 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003720 }
3721}
3722
Jesse Barnesb074cec2013-04-25 12:55:02 -07003723static void ironlake_pfit_enable(struct intel_crtc *crtc)
3724{
3725 struct drm_device *dev = crtc->base.dev;
3726 struct drm_i915_private *dev_priv = dev->dev_private;
3727 int pipe = crtc->pipe;
3728
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003729 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003730 /* Force use of hard-coded filter coefficients
3731 * as some pre-programmed values are broken,
3732 * e.g. x201.
3733 */
3734 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3735 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3736 PF_PIPE_SEL_IVB(pipe));
3737 else
3738 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3739 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3740 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003741 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003742}
3743
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003744static void intel_enable_planes(struct drm_crtc *crtc)
3745{
3746 struct drm_device *dev = crtc->dev;
3747 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003748 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003749 struct intel_plane *intel_plane;
3750
Matt Roperaf2b6532014-04-01 15:22:32 -07003751 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3752 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003753 if (intel_plane->pipe == pipe)
3754 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003755 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003756}
3757
3758static void intel_disable_planes(struct drm_crtc *crtc)
3759{
3760 struct drm_device *dev = crtc->dev;
3761 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003762 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003763 struct intel_plane *intel_plane;
3764
Matt Roperaf2b6532014-04-01 15:22:32 -07003765 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3766 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003767 if (intel_plane->pipe == pipe)
3768 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003769 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003770}
3771
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003772void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003773{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003774 struct drm_device *dev = crtc->base.dev;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003776
3777 if (!crtc->config.ips_enabled)
3778 return;
3779
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003780 /* We can only enable IPS after we enable a plane and wait for a vblank */
3781 intel_wait_for_vblank(dev, crtc->pipe);
3782
Paulo Zanonid77e4532013-09-24 13:52:55 -03003783 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003784 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003785 mutex_lock(&dev_priv->rps.hw_lock);
3786 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3787 mutex_unlock(&dev_priv->rps.hw_lock);
3788 /* Quoting Art Runyan: "its not safe to expect any particular
3789 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003790 * mailbox." Moreover, the mailbox may return a bogus state,
3791 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003792 */
3793 } else {
3794 I915_WRITE(IPS_CTL, IPS_ENABLE);
3795 /* The bit only becomes 1 in the next vblank, so this wait here
3796 * is essentially intel_wait_for_vblank. If we don't have this
3797 * and don't wait for vblanks until the end of crtc_enable, then
3798 * the HW state readout code will complain that the expected
3799 * IPS_CTL value is not the one we read. */
3800 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3801 DRM_ERROR("Timed out waiting for IPS enable\n");
3802 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003803}
3804
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003805void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003806{
3807 struct drm_device *dev = crtc->base.dev;
3808 struct drm_i915_private *dev_priv = dev->dev_private;
3809
3810 if (!crtc->config.ips_enabled)
3811 return;
3812
3813 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003814 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003815 mutex_lock(&dev_priv->rps.hw_lock);
3816 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3817 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003818 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3819 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3820 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003821 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003822 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003823 POSTING_READ(IPS_CTL);
3824 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003825
3826 /* We need to wait for a vblank before we can disable the plane. */
3827 intel_wait_for_vblank(dev, crtc->pipe);
3828}
3829
3830/** Loads the palette/gamma unit for the CRTC with the prepared values */
3831static void intel_crtc_load_lut(struct drm_crtc *crtc)
3832{
3833 struct drm_device *dev = crtc->dev;
3834 struct drm_i915_private *dev_priv = dev->dev_private;
3835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3836 enum pipe pipe = intel_crtc->pipe;
3837 int palreg = PALETTE(pipe);
3838 int i;
3839 bool reenable_ips = false;
3840
3841 /* The clocks have to be on to load the palette. */
3842 if (!crtc->enabled || !intel_crtc->active)
3843 return;
3844
3845 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3846 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3847 assert_dsi_pll_enabled(dev_priv);
3848 else
3849 assert_pll_enabled(dev_priv, pipe);
3850 }
3851
3852 /* use legacy palette for Ironlake */
3853 if (HAS_PCH_SPLIT(dev))
3854 palreg = LGC_PALETTE(pipe);
3855
3856 /* Workaround : Do not read or write the pipe palette/gamma data while
3857 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3858 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003859 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003860 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3861 GAMMA_MODE_MODE_SPLIT)) {
3862 hsw_disable_ips(intel_crtc);
3863 reenable_ips = true;
3864 }
3865
3866 for (i = 0; i < 256; i++) {
3867 I915_WRITE(palreg + 4 * i,
3868 (intel_crtc->lut_r[i] << 16) |
3869 (intel_crtc->lut_g[i] << 8) |
3870 intel_crtc->lut_b[i]);
3871 }
3872
3873 if (reenable_ips)
3874 hsw_enable_ips(intel_crtc);
3875}
3876
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003877static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3878{
3879 if (!enable && intel_crtc->overlay) {
3880 struct drm_device *dev = intel_crtc->base.dev;
3881 struct drm_i915_private *dev_priv = dev->dev_private;
3882
3883 mutex_lock(&dev->struct_mutex);
3884 dev_priv->mm.interruptible = false;
3885 (void) intel_overlay_switch_off(intel_crtc->overlay);
3886 dev_priv->mm.interruptible = true;
3887 mutex_unlock(&dev->struct_mutex);
3888 }
3889
3890 /* Let userspace switch the overlay on again. In most cases userspace
3891 * has to recompute where to put it anyway.
3892 */
3893}
3894
3895/**
3896 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3897 * cursor plane briefly if not already running after enabling the display
3898 * plane.
3899 * This workaround avoids occasional blank screens when self refresh is
3900 * enabled.
3901 */
3902static void
3903g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3904{
3905 u32 cntl = I915_READ(CURCNTR(pipe));
3906
3907 if ((cntl & CURSOR_MODE) == 0) {
3908 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3909
3910 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3911 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3912 intel_wait_for_vblank(dev_priv->dev, pipe);
3913 I915_WRITE(CURCNTR(pipe), cntl);
3914 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3915 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3916 }
3917}
3918
3919static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003920{
3921 struct drm_device *dev = crtc->dev;
3922 struct drm_i915_private *dev_priv = dev->dev_private;
3923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3924 int pipe = intel_crtc->pipe;
3925 int plane = intel_crtc->plane;
3926
Ville Syrjäläf98551a2014-05-22 17:48:06 +03003927 drm_vblank_on(dev, pipe);
3928
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003929 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3930 intel_enable_planes(crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003931 /* The fixup needs to happen before cursor is enabled */
3932 if (IS_G4X(dev))
3933 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003934 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003935 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003936
3937 hsw_enable_ips(intel_crtc);
3938
3939 mutex_lock(&dev->struct_mutex);
3940 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02003941 intel_edp_psr_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003942 mutex_unlock(&dev->struct_mutex);
3943}
3944
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003945static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003946{
3947 struct drm_device *dev = crtc->dev;
3948 struct drm_i915_private *dev_priv = dev->dev_private;
3949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3950 int pipe = intel_crtc->pipe;
3951 int plane = intel_crtc->plane;
3952
3953 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003954
3955 if (dev_priv->fbc.plane == plane)
3956 intel_disable_fbc(dev);
3957
3958 hsw_disable_ips(intel_crtc);
3959
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003960 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003961 intel_crtc_update_cursor(crtc, false);
3962 intel_disable_planes(crtc);
3963 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03003964
3965 drm_vblank_off(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003966}
3967
Jesse Barnesf67a5592011-01-05 10:31:48 -08003968static void ironlake_crtc_enable(struct drm_crtc *crtc)
3969{
3970 struct drm_device *dev = crtc->dev;
3971 struct drm_i915_private *dev_priv = dev->dev_private;
3972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003973 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003974 int pipe = intel_crtc->pipe;
Daniel Vetter29407aa2014-04-24 23:55:08 +02003975 enum plane plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003976
Daniel Vetter08a48462012-07-02 11:43:47 +02003977 WARN_ON(!crtc->enabled);
3978
Jesse Barnesf67a5592011-01-05 10:31:48 -08003979 if (intel_crtc->active)
3980 return;
3981
Daniel Vetterb14b1052014-04-24 23:55:13 +02003982 if (intel_crtc->config.has_pch_encoder)
3983 intel_prepare_shared_dpll(intel_crtc);
3984
Daniel Vetter29407aa2014-04-24 23:55:08 +02003985 if (intel_crtc->config.has_dp_encoder)
3986 intel_dp_set_m_n(intel_crtc);
3987
3988 intel_set_pipe_timings(intel_crtc);
3989
3990 if (intel_crtc->config.has_pch_encoder) {
3991 intel_cpu_transcoder_set_m_n(intel_crtc,
3992 &intel_crtc->config.fdi_m_n);
3993 }
3994
3995 ironlake_set_pipeconf(crtc);
3996
3997 /* Set up the display plane register */
3998 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
3999 POSTING_READ(DSPCNTR(plane));
4000
4001 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4002 crtc->x, crtc->y);
4003
Jesse Barnesf67a5592011-01-05 10:31:48 -08004004 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004005
4006 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4007 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4008
Daniel Vetterf6736a12013-06-05 13:34:30 +02004009 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004010 if (encoder->pre_enable)
4011 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004012
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004013 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004014 /* Note: FDI PLL enabling _must_ be done before we enable the
4015 * cpu pipes, hence this is separate from all the other fdi/pch
4016 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004017 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004018 } else {
4019 assert_fdi_tx_disabled(dev_priv, pipe);
4020 assert_fdi_rx_disabled(dev_priv, pipe);
4021 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004022
Jesse Barnesb074cec2013-04-25 12:55:02 -07004023 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004024
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004025 /*
4026 * On ILK+ LUT must be loaded before the pipe is running but with
4027 * clocks enabled
4028 */
4029 intel_crtc_load_lut(crtc);
4030
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004031 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004032 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004033
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004034 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004035 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004036
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004037 for_each_encoder_on_crtc(dev, crtc, encoder)
4038 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004039
4040 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004041 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004042
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004043 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004044}
4045
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004046/* IPS only exists on ULT machines and is tied to pipe A. */
4047static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4048{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004049 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004050}
4051
Paulo Zanonie4916942013-09-20 16:21:19 -03004052/*
4053 * This implements the workaround described in the "notes" section of the mode
4054 * set sequence documentation. When going from no pipes or single pipe to
4055 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4056 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4057 */
4058static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4059{
4060 struct drm_device *dev = crtc->base.dev;
4061 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4062
4063 /* We want to get the other_active_crtc only if there's only 1 other
4064 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004065 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004066 if (!crtc_it->active || crtc_it == crtc)
4067 continue;
4068
4069 if (other_active_crtc)
4070 return;
4071
4072 other_active_crtc = crtc_it;
4073 }
4074 if (!other_active_crtc)
4075 return;
4076
4077 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4078 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4079}
4080
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004081static void haswell_crtc_enable(struct drm_crtc *crtc)
4082{
4083 struct drm_device *dev = crtc->dev;
4084 struct drm_i915_private *dev_priv = dev->dev_private;
4085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4086 struct intel_encoder *encoder;
4087 int pipe = intel_crtc->pipe;
Daniel Vetter229fca92014-04-24 23:55:09 +02004088 enum plane plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004089
4090 WARN_ON(!crtc->enabled);
4091
4092 if (intel_crtc->active)
4093 return;
4094
Daniel Vetter229fca92014-04-24 23:55:09 +02004095 if (intel_crtc->config.has_dp_encoder)
4096 intel_dp_set_m_n(intel_crtc);
4097
4098 intel_set_pipe_timings(intel_crtc);
4099
4100 if (intel_crtc->config.has_pch_encoder) {
4101 intel_cpu_transcoder_set_m_n(intel_crtc,
4102 &intel_crtc->config.fdi_m_n);
4103 }
4104
4105 haswell_set_pipeconf(crtc);
4106
4107 intel_set_pipe_csc(crtc);
4108
4109 /* Set up the display plane register */
4110 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4111 POSTING_READ(DSPCNTR(plane));
4112
4113 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4114 crtc->x, crtc->y);
4115
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004116 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004117
4118 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4119 if (intel_crtc->config.has_pch_encoder)
4120 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4121
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004122 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02004123 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004124
4125 for_each_encoder_on_crtc(dev, crtc, encoder)
4126 if (encoder->pre_enable)
4127 encoder->pre_enable(encoder);
4128
Paulo Zanoni1f544382012-10-24 11:32:00 -02004129 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004130
Jesse Barnesb074cec2013-04-25 12:55:02 -07004131 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004132
4133 /*
4134 * On ILK+ LUT must be loaded before the pipe is running but with
4135 * clocks enabled
4136 */
4137 intel_crtc_load_lut(crtc);
4138
Paulo Zanoni1f544382012-10-24 11:32:00 -02004139 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004140 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004141
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004142 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004143 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004144
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004145 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004146 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004147
Jani Nikula8807e552013-08-30 19:40:32 +03004148 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004149 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004150 intel_opregion_notify_encoder(encoder, true);
4151 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004152
Paulo Zanonie4916942013-09-20 16:21:19 -03004153 /* If we change the relative order between pipe/planes enabling, we need
4154 * to change the workaround. */
4155 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004156 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004157}
4158
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004159static void ironlake_pfit_disable(struct intel_crtc *crtc)
4160{
4161 struct drm_device *dev = crtc->base.dev;
4162 struct drm_i915_private *dev_priv = dev->dev_private;
4163 int pipe = crtc->pipe;
4164
4165 /* To avoid upsetting the power well on haswell only disable the pfit if
4166 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004167 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004168 I915_WRITE(PF_CTL(pipe), 0);
4169 I915_WRITE(PF_WIN_POS(pipe), 0);
4170 I915_WRITE(PF_WIN_SZ(pipe), 0);
4171 }
4172}
4173
Jesse Barnes6be4a602010-09-10 10:26:01 -07004174static void ironlake_crtc_disable(struct drm_crtc *crtc)
4175{
4176 struct drm_device *dev = crtc->dev;
4177 struct drm_i915_private *dev_priv = dev->dev_private;
4178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004179 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004180 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004181 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004182
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004183 if (!intel_crtc->active)
4184 return;
4185
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004186 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004187
Daniel Vetterea9d7582012-07-10 10:42:52 +02004188 for_each_encoder_on_crtc(dev, crtc, encoder)
4189 encoder->disable(encoder);
4190
Daniel Vetterd925c592013-06-05 13:34:04 +02004191 if (intel_crtc->config.has_pch_encoder)
4192 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4193
Jesse Barnesb24e7172011-01-04 15:09:30 -08004194 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004195
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004196 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004197
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004198 for_each_encoder_on_crtc(dev, crtc, encoder)
4199 if (encoder->post_disable)
4200 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004201
Daniel Vetterd925c592013-06-05 13:34:04 +02004202 if (intel_crtc->config.has_pch_encoder) {
4203 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004204
Daniel Vetterd925c592013-06-05 13:34:04 +02004205 ironlake_disable_pch_transcoder(dev_priv, pipe);
4206 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004207
Daniel Vetterd925c592013-06-05 13:34:04 +02004208 if (HAS_PCH_CPT(dev)) {
4209 /* disable TRANS_DP_CTL */
4210 reg = TRANS_DP_CTL(pipe);
4211 temp = I915_READ(reg);
4212 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4213 TRANS_DP_PORT_SEL_MASK);
4214 temp |= TRANS_DP_PORT_SEL_NONE;
4215 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004216
Daniel Vetterd925c592013-06-05 13:34:04 +02004217 /* disable DPLL_SEL */
4218 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004219 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004220 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004221 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004222
4223 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004224 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004225
4226 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004227 }
4228
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004229 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004230 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004231
4232 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004233 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004234 intel_edp_psr_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004235 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004236}
4237
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004238static void haswell_crtc_disable(struct drm_crtc *crtc)
4239{
4240 struct drm_device *dev = crtc->dev;
4241 struct drm_i915_private *dev_priv = dev->dev_private;
4242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4243 struct intel_encoder *encoder;
4244 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004245 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004246
4247 if (!intel_crtc->active)
4248 return;
4249
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004250 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004251
Jani Nikula8807e552013-08-30 19:40:32 +03004252 for_each_encoder_on_crtc(dev, crtc, encoder) {
4253 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004254 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004255 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004256
Paulo Zanoni86642812013-04-12 17:57:57 -03004257 if (intel_crtc->config.has_pch_encoder)
4258 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004259 intel_disable_pipe(dev_priv, pipe);
4260
Paulo Zanoniad80a812012-10-24 16:06:19 -02004261 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004262
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004263 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004264
Paulo Zanoni1f544382012-10-24 11:32:00 -02004265 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004266
4267 for_each_encoder_on_crtc(dev, crtc, encoder)
4268 if (encoder->post_disable)
4269 encoder->post_disable(encoder);
4270
Daniel Vetter88adfff2013-03-28 10:42:01 +01004271 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004272 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004273 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004274 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004275 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004276
4277 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004278 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004279
4280 mutex_lock(&dev->struct_mutex);
4281 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004282 intel_edp_psr_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004283 mutex_unlock(&dev->struct_mutex);
4284}
4285
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004286static void ironlake_crtc_off(struct drm_crtc *crtc)
4287{
4288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004289 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004290}
4291
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004292static void haswell_crtc_off(struct drm_crtc *crtc)
4293{
4294 intel_ddi_put_crtc_pll(crtc);
4295}
4296
Jesse Barnes2dd24552013-04-25 12:55:01 -07004297static void i9xx_pfit_enable(struct intel_crtc *crtc)
4298{
4299 struct drm_device *dev = crtc->base.dev;
4300 struct drm_i915_private *dev_priv = dev->dev_private;
4301 struct intel_crtc_config *pipe_config = &crtc->config;
4302
Daniel Vetter328d8e82013-05-08 10:36:31 +02004303 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004304 return;
4305
Daniel Vetterc0b03412013-05-28 12:05:54 +02004306 /*
4307 * The panel fitter should only be adjusted whilst the pipe is disabled,
4308 * according to register description and PRM.
4309 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004310 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4311 assert_pipe_disabled(dev_priv, crtc->pipe);
4312
Jesse Barnesb074cec2013-04-25 12:55:02 -07004313 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4314 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004315
4316 /* Border color in case we don't scale up to the full screen. Black by
4317 * default, change to something else for debugging. */
4318 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004319}
4320
Imre Deak77d22dc2014-03-05 16:20:52 +02004321#define for_each_power_domain(domain, mask) \
4322 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4323 if ((1 << (domain)) & (mask))
4324
Imre Deak319be8a2014-03-04 19:22:57 +02004325enum intel_display_power_domain
4326intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004327{
Imre Deak319be8a2014-03-04 19:22:57 +02004328 struct drm_device *dev = intel_encoder->base.dev;
4329 struct intel_digital_port *intel_dig_port;
4330
4331 switch (intel_encoder->type) {
4332 case INTEL_OUTPUT_UNKNOWN:
4333 /* Only DDI platforms should ever use this output type */
4334 WARN_ON_ONCE(!HAS_DDI(dev));
4335 case INTEL_OUTPUT_DISPLAYPORT:
4336 case INTEL_OUTPUT_HDMI:
4337 case INTEL_OUTPUT_EDP:
4338 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4339 switch (intel_dig_port->port) {
4340 case PORT_A:
4341 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4342 case PORT_B:
4343 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4344 case PORT_C:
4345 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4346 case PORT_D:
4347 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4348 default:
4349 WARN_ON_ONCE(1);
4350 return POWER_DOMAIN_PORT_OTHER;
4351 }
4352 case INTEL_OUTPUT_ANALOG:
4353 return POWER_DOMAIN_PORT_CRT;
4354 case INTEL_OUTPUT_DSI:
4355 return POWER_DOMAIN_PORT_DSI;
4356 default:
4357 return POWER_DOMAIN_PORT_OTHER;
4358 }
4359}
4360
4361static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4362{
4363 struct drm_device *dev = crtc->dev;
4364 struct intel_encoder *intel_encoder;
4365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4366 enum pipe pipe = intel_crtc->pipe;
4367 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
Imre Deak77d22dc2014-03-05 16:20:52 +02004368 unsigned long mask;
4369 enum transcoder transcoder;
4370
4371 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4372
4373 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4374 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4375 if (pfit_enabled)
4376 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4377
Imre Deak319be8a2014-03-04 19:22:57 +02004378 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4379 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4380
Imre Deak77d22dc2014-03-05 16:20:52 +02004381 return mask;
4382}
4383
4384void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4385 bool enable)
4386{
4387 if (dev_priv->power_domains.init_power_on == enable)
4388 return;
4389
4390 if (enable)
4391 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4392 else
4393 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4394
4395 dev_priv->power_domains.init_power_on = enable;
4396}
4397
4398static void modeset_update_crtc_power_domains(struct drm_device *dev)
4399{
4400 struct drm_i915_private *dev_priv = dev->dev_private;
4401 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4402 struct intel_crtc *crtc;
4403
4404 /*
4405 * First get all needed power domains, then put all unneeded, to avoid
4406 * any unnecessary toggling of the power wells.
4407 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004408 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004409 enum intel_display_power_domain domain;
4410
4411 if (!crtc->base.enabled)
4412 continue;
4413
Imre Deak319be8a2014-03-04 19:22:57 +02004414 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004415
4416 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4417 intel_display_power_get(dev_priv, domain);
4418 }
4419
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004420 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004421 enum intel_display_power_domain domain;
4422
4423 for_each_power_domain(domain, crtc->enabled_power_domains)
4424 intel_display_power_put(dev_priv, domain);
4425
4426 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4427 }
4428
4429 intel_display_set_init_power(dev_priv, false);
4430}
4431
Jesse Barnes586f49d2013-11-04 16:06:59 -08004432int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004433{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004434 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004435
Jesse Barnes586f49d2013-11-04 16:06:59 -08004436 /* Obtain SKU information */
4437 mutex_lock(&dev_priv->dpio_lock);
4438 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4439 CCK_FUSE_HPLL_FREQ_MASK;
4440 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004441
Jesse Barnes586f49d2013-11-04 16:06:59 -08004442 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08004443}
4444
4445/* Adjust CDclk dividers to allow high res or save power if possible */
4446static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4447{
4448 struct drm_i915_private *dev_priv = dev->dev_private;
4449 u32 val, cmd;
4450
Imre Deakd60c4472014-03-27 17:45:10 +02004451 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4452 dev_priv->vlv_cdclk_freq = cdclk;
4453
Jesse Barnes30a970c2013-11-04 13:48:12 -08004454 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4455 cmd = 2;
4456 else if (cdclk == 266)
4457 cmd = 1;
4458 else
4459 cmd = 0;
4460
4461 mutex_lock(&dev_priv->rps.hw_lock);
4462 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4463 val &= ~DSPFREQGUAR_MASK;
4464 val |= (cmd << DSPFREQGUAR_SHIFT);
4465 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4466 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4467 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4468 50)) {
4469 DRM_ERROR("timed out waiting for CDclk change\n");
4470 }
4471 mutex_unlock(&dev_priv->rps.hw_lock);
4472
4473 if (cdclk == 400) {
4474 u32 divider, vco;
4475
4476 vco = valleyview_get_vco(dev_priv);
4477 divider = ((vco << 1) / cdclk) - 1;
4478
4479 mutex_lock(&dev_priv->dpio_lock);
4480 /* adjust cdclk divider */
4481 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4482 val &= ~0xf;
4483 val |= divider;
4484 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4485 mutex_unlock(&dev_priv->dpio_lock);
4486 }
4487
4488 mutex_lock(&dev_priv->dpio_lock);
4489 /* adjust self-refresh exit latency value */
4490 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4491 val &= ~0x7f;
4492
4493 /*
4494 * For high bandwidth configs, we set a higher latency in the bunit
4495 * so that the core display fetch happens in time to avoid underruns.
4496 */
4497 if (cdclk == 400)
4498 val |= 4500 / 250; /* 4.5 usec */
4499 else
4500 val |= 3000 / 250; /* 3.0 usec */
4501 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4502 mutex_unlock(&dev_priv->dpio_lock);
4503
4504 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4505 intel_i2c_reset(dev);
4506}
4507
Imre Deakd60c4472014-03-27 17:45:10 +02004508int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004509{
4510 int cur_cdclk, vco;
4511 int divider;
4512
4513 vco = valleyview_get_vco(dev_priv);
4514
4515 mutex_lock(&dev_priv->dpio_lock);
4516 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4517 mutex_unlock(&dev_priv->dpio_lock);
4518
4519 divider &= 0xf;
4520
4521 cur_cdclk = (vco << 1) / (divider + 1);
4522
4523 return cur_cdclk;
4524}
4525
4526static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4527 int max_pixclk)
4528{
Jesse Barnes30a970c2013-11-04 13:48:12 -08004529 /*
4530 * Really only a few cases to deal with, as only 4 CDclks are supported:
4531 * 200MHz
4532 * 267MHz
4533 * 320MHz
4534 * 400MHz
4535 * So we check to see whether we're above 90% of the lower bin and
4536 * adjust if needed.
4537 */
4538 if (max_pixclk > 288000) {
4539 return 400;
4540 } else if (max_pixclk > 240000) {
4541 return 320;
4542 } else
4543 return 266;
4544 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4545}
4546
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004547/* compute the max pixel clock for new configuration */
4548static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004549{
4550 struct drm_device *dev = dev_priv->dev;
4551 struct intel_crtc *intel_crtc;
4552 int max_pixclk = 0;
4553
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004554 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004555 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004556 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004557 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004558 }
4559
4560 return max_pixclk;
4561}
4562
4563static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004564 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004565{
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004568 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004569
Imre Deakd60c4472014-03-27 17:45:10 +02004570 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4571 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004572 return;
4573
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004574 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004575 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004576 if (intel_crtc->base.enabled)
4577 *prepare_pipes |= (1 << intel_crtc->pipe);
4578}
4579
4580static void valleyview_modeset_global_resources(struct drm_device *dev)
4581{
4582 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004583 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004584 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4585
Imre Deakd60c4472014-03-27 17:45:10 +02004586 if (req_cdclk != dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004587 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak77961eb2014-03-05 16:20:56 +02004588 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004589}
4590
Jesse Barnes89b667f2013-04-18 14:51:36 -07004591static void valleyview_crtc_enable(struct drm_crtc *crtc)
4592{
4593 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004594 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 struct intel_encoder *encoder;
4597 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004598 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004599 bool is_dsi;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004600 u32 dspcntr;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004601
4602 WARN_ON(!crtc->enabled);
4603
4604 if (intel_crtc->active)
4605 return;
4606
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02004607 vlv_prepare_pll(intel_crtc);
4608
Daniel Vetter5b18e572014-04-24 23:55:06 +02004609 /* Set up the display plane register */
4610 dspcntr = DISPPLANE_GAMMA_ENABLE;
4611
4612 if (intel_crtc->config.has_dp_encoder)
4613 intel_dp_set_m_n(intel_crtc);
4614
4615 intel_set_pipe_timings(intel_crtc);
4616
4617 /* pipesrc and dspsize control the size that is scaled from,
4618 * which should always be the user's requested size.
4619 */
4620 I915_WRITE(DSPSIZE(plane),
4621 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4622 (intel_crtc->config.pipe_src_w - 1));
4623 I915_WRITE(DSPPOS(plane), 0);
4624
4625 i9xx_set_pipeconf(intel_crtc);
4626
4627 I915_WRITE(DSPCNTR(plane), dspcntr);
4628 POSTING_READ(DSPCNTR(plane));
4629
4630 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4631 crtc->x, crtc->y);
4632
Jesse Barnes89b667f2013-04-18 14:51:36 -07004633 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004634
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004635 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4636
Jesse Barnes89b667f2013-04-18 14:51:36 -07004637 for_each_encoder_on_crtc(dev, crtc, encoder)
4638 if (encoder->pre_pll_enable)
4639 encoder->pre_pll_enable(encoder);
4640
Jani Nikula23538ef2013-08-27 15:12:22 +03004641 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4642
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004643 if (!is_dsi) {
4644 if (IS_CHERRYVIEW(dev))
4645 chv_enable_pll(intel_crtc);
4646 else
4647 vlv_enable_pll(intel_crtc);
4648 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004649
4650 for_each_encoder_on_crtc(dev, crtc, encoder)
4651 if (encoder->pre_enable)
4652 encoder->pre_enable(encoder);
4653
Jesse Barnes2dd24552013-04-25 12:55:01 -07004654 i9xx_pfit_enable(intel_crtc);
4655
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004656 intel_crtc_load_lut(crtc);
4657
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004658 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004659 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004660
Jani Nikula50049452013-07-30 12:20:32 +03004661 for_each_encoder_on_crtc(dev, crtc, encoder)
4662 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004663
4664 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004665
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004666 /* Underruns don't raise interrupts, so check manually. */
4667 i9xx_check_fifo_underruns(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004668}
4669
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004670static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4671{
4672 struct drm_device *dev = crtc->base.dev;
4673 struct drm_i915_private *dev_priv = dev->dev_private;
4674
4675 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4676 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4677}
4678
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004679static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004680{
4681 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004682 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08004683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004684 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004685 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004686 int plane = intel_crtc->plane;
4687 u32 dspcntr;
Jesse Barnes79e53942008-11-07 14:24:08 -08004688
Daniel Vetter08a48462012-07-02 11:43:47 +02004689 WARN_ON(!crtc->enabled);
4690
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004691 if (intel_crtc->active)
4692 return;
4693
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004694 i9xx_set_pll_dividers(intel_crtc);
4695
Daniel Vetter5b18e572014-04-24 23:55:06 +02004696 /* Set up the display plane register */
4697 dspcntr = DISPPLANE_GAMMA_ENABLE;
4698
4699 if (pipe == 0)
4700 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4701 else
4702 dspcntr |= DISPPLANE_SEL_PIPE_B;
4703
4704 if (intel_crtc->config.has_dp_encoder)
4705 intel_dp_set_m_n(intel_crtc);
4706
4707 intel_set_pipe_timings(intel_crtc);
4708
4709 /* pipesrc and dspsize control the size that is scaled from,
4710 * which should always be the user's requested size.
4711 */
4712 I915_WRITE(DSPSIZE(plane),
4713 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4714 (intel_crtc->config.pipe_src_w - 1));
4715 I915_WRITE(DSPPOS(plane), 0);
4716
4717 i9xx_set_pipeconf(intel_crtc);
4718
4719 I915_WRITE(DSPCNTR(plane), dspcntr);
4720 POSTING_READ(DSPCNTR(plane));
4721
4722 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4723 crtc->x, crtc->y);
4724
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004725 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004726
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004727 if (!IS_GEN2(dev))
4728 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4729
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004730 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004731 if (encoder->pre_enable)
4732 encoder->pre_enable(encoder);
4733
Daniel Vetterf6736a12013-06-05 13:34:30 +02004734 i9xx_enable_pll(intel_crtc);
4735
Jesse Barnes2dd24552013-04-25 12:55:01 -07004736 i9xx_pfit_enable(intel_crtc);
4737
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004738 intel_crtc_load_lut(crtc);
4739
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004740 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004741 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004742
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004743 for_each_encoder_on_crtc(dev, crtc, encoder)
4744 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004745
4746 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004747
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004748 /*
4749 * Gen2 reports pipe underruns whenever all planes are disabled.
4750 * So don't enable underrun reporting before at least some planes
4751 * are enabled.
4752 * FIXME: Need to fix the logic to work when we turn off all planes
4753 * but leave the pipe running.
4754 */
4755 if (IS_GEN2(dev))
4756 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4757
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004758 /* Underruns don't raise interrupts, so check manually. */
4759 i9xx_check_fifo_underruns(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004760}
4761
Daniel Vetter87476d62013-04-11 16:29:06 +02004762static void i9xx_pfit_disable(struct intel_crtc *crtc)
4763{
4764 struct drm_device *dev = crtc->base.dev;
4765 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004766
4767 if (!crtc->config.gmch_pfit.control)
4768 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004769
4770 assert_pipe_disabled(dev_priv, crtc->pipe);
4771
Daniel Vetter328d8e82013-05-08 10:36:31 +02004772 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4773 I915_READ(PFIT_CONTROL));
4774 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004775}
4776
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004777static void i9xx_crtc_disable(struct drm_crtc *crtc)
4778{
4779 struct drm_device *dev = crtc->dev;
4780 struct drm_i915_private *dev_priv = dev->dev_private;
4781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004782 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004783 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004784
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004785 if (!intel_crtc->active)
4786 return;
4787
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004788 /*
4789 * Gen2 reports pipe underruns whenever all planes are disabled.
4790 * So diasble underrun reporting before all the planes get disabled.
4791 * FIXME: Need to fix the logic to work when we turn off all planes
4792 * but leave the pipe running.
4793 */
4794 if (IS_GEN2(dev))
4795 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4796
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004797 intel_crtc_disable_planes(crtc);
4798
Daniel Vetterea9d7582012-07-10 10:42:52 +02004799 for_each_encoder_on_crtc(dev, crtc, encoder)
4800 encoder->disable(encoder);
4801
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004802 /*
4803 * On gen2 planes are double buffered but the pipe isn't, so we must
4804 * wait for planes to fully turn off before disabling the pipe.
4805 */
4806 if (IS_GEN2(dev))
4807 intel_wait_for_vblank(dev, pipe);
4808
Jesse Barnesb24e7172011-01-04 15:09:30 -08004809 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004810
Daniel Vetter87476d62013-04-11 16:29:06 +02004811 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004812
Jesse Barnes89b667f2013-04-18 14:51:36 -07004813 for_each_encoder_on_crtc(dev, crtc, encoder)
4814 if (encoder->post_disable)
4815 encoder->post_disable(encoder);
4816
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004817 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4818 if (IS_CHERRYVIEW(dev))
4819 chv_disable_pll(dev_priv, pipe);
4820 else if (IS_VALLEYVIEW(dev))
4821 vlv_disable_pll(dev_priv, pipe);
4822 else
4823 i9xx_disable_pll(dev_priv, pipe);
4824 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004825
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004826 if (!IS_GEN2(dev))
4827 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4828
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004829 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004830 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004831
Daniel Vetterefa96242014-04-24 23:55:02 +02004832 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004833 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004834 intel_edp_psr_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02004835 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004836}
4837
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004838static void i9xx_crtc_off(struct drm_crtc *crtc)
4839{
4840}
4841
Daniel Vetter976f8a22012-07-08 22:34:21 +02004842static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4843 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004844{
4845 struct drm_device *dev = crtc->dev;
4846 struct drm_i915_master_private *master_priv;
4847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4848 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004849
4850 if (!dev->primary->master)
4851 return;
4852
4853 master_priv = dev->primary->master->driver_priv;
4854 if (!master_priv->sarea_priv)
4855 return;
4856
Jesse Barnes79e53942008-11-07 14:24:08 -08004857 switch (pipe) {
4858 case 0:
4859 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4860 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4861 break;
4862 case 1:
4863 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4864 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4865 break;
4866 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004867 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004868 break;
4869 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004870}
4871
Daniel Vetter976f8a22012-07-08 22:34:21 +02004872/**
4873 * Sets the power management mode of the pipe and plane.
4874 */
4875void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004876{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004877 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004878 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004879 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004880 struct intel_encoder *intel_encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004881 enum intel_display_power_domain domain;
4882 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004883 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004884
Daniel Vetter976f8a22012-07-08 22:34:21 +02004885 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4886 enable |= intel_encoder->connectors_active;
4887
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004888 if (enable) {
4889 if (!intel_crtc->active) {
4890 /*
4891 * FIXME: DDI plls and relevant code isn't converted
4892 * yet, so do runtime PM for DPMS only for all other
4893 * platforms for now.
4894 */
4895 if (!HAS_DDI(dev)) {
4896 domains = get_crtc_power_domains(crtc);
4897 for_each_power_domain(domain, domains)
4898 intel_display_power_get(dev_priv, domain);
4899 intel_crtc->enabled_power_domains = domains;
4900 }
4901
4902 dev_priv->display.crtc_enable(crtc);
4903 }
4904 } else {
4905 if (intel_crtc->active) {
4906 dev_priv->display.crtc_disable(crtc);
4907
4908 if (!HAS_DDI(dev)) {
4909 domains = intel_crtc->enabled_power_domains;
4910 for_each_power_domain(domain, domains)
4911 intel_display_power_put(dev_priv, domain);
4912 intel_crtc->enabled_power_domains = 0;
4913 }
4914 }
4915 }
Daniel Vetter976f8a22012-07-08 22:34:21 +02004916
4917 intel_crtc_update_sarea(crtc, enable);
4918}
4919
Daniel Vetter976f8a22012-07-08 22:34:21 +02004920static void intel_crtc_disable(struct drm_crtc *crtc)
4921{
4922 struct drm_device *dev = crtc->dev;
4923 struct drm_connector *connector;
4924 struct drm_i915_private *dev_priv = dev->dev_private;
4925
4926 /* crtc should still be enabled when we disable it. */
4927 WARN_ON(!crtc->enabled);
4928
4929 dev_priv->display.crtc_disable(crtc);
4930 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004931 dev_priv->display.off(crtc);
4932
Chris Wilson931872f2012-01-16 23:01:13 +00004933 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004934 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004935 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004936
Matt Roperf4510a22014-04-01 15:22:40 -07004937 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01004938 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004939 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004940 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004941 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004942 }
4943
4944 /* Update computed state. */
4945 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4946 if (!connector->encoder || !connector->encoder->crtc)
4947 continue;
4948
4949 if (connector->encoder->crtc != crtc)
4950 continue;
4951
4952 connector->dpms = DRM_MODE_DPMS_OFF;
4953 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004954 }
4955}
4956
Chris Wilsonea5b2132010-08-04 13:50:23 +01004957void intel_encoder_destroy(struct drm_encoder *encoder)
4958{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004959 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004960
Chris Wilsonea5b2132010-08-04 13:50:23 +01004961 drm_encoder_cleanup(encoder);
4962 kfree(intel_encoder);
4963}
4964
Damien Lespiau92373292013-08-08 22:28:57 +01004965/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004966 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4967 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004968static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004969{
4970 if (mode == DRM_MODE_DPMS_ON) {
4971 encoder->connectors_active = true;
4972
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004973 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004974 } else {
4975 encoder->connectors_active = false;
4976
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004977 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004978 }
4979}
4980
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004981/* Cross check the actual hw state with our own modeset state tracking (and it's
4982 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004983static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004984{
4985 if (connector->get_hw_state(connector)) {
4986 struct intel_encoder *encoder = connector->encoder;
4987 struct drm_crtc *crtc;
4988 bool encoder_enabled;
4989 enum pipe pipe;
4990
4991 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4992 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03004993 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004994
4995 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4996 "wrong connector dpms state\n");
4997 WARN(connector->base.encoder != &encoder->base,
4998 "active connector not linked to encoder\n");
4999 WARN(!encoder->connectors_active,
5000 "encoder->connectors_active not set\n");
5001
5002 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5003 WARN(!encoder_enabled, "encoder not enabled\n");
5004 if (WARN_ON(!encoder->base.crtc))
5005 return;
5006
5007 crtc = encoder->base.crtc;
5008
5009 WARN(!crtc->enabled, "crtc not enabled\n");
5010 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5011 WARN(pipe != to_intel_crtc(crtc)->pipe,
5012 "encoder active on the wrong pipe\n");
5013 }
5014}
5015
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005016/* Even simpler default implementation, if there's really no special case to
5017 * consider. */
5018void intel_connector_dpms(struct drm_connector *connector, int mode)
5019{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005020 /* All the simple cases only support two dpms states. */
5021 if (mode != DRM_MODE_DPMS_ON)
5022 mode = DRM_MODE_DPMS_OFF;
5023
5024 if (mode == connector->dpms)
5025 return;
5026
5027 connector->dpms = mode;
5028
5029 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005030 if (connector->encoder)
5031 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005032
Daniel Vetterb9805142012-08-31 17:37:33 +02005033 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005034}
5035
Daniel Vetterf0947c32012-07-02 13:10:34 +02005036/* Simple connector->get_hw_state implementation for encoders that support only
5037 * one connector and no cloning and hence the encoder state determines the state
5038 * of the connector. */
5039bool intel_connector_get_hw_state(struct intel_connector *connector)
5040{
Daniel Vetter24929352012-07-02 20:28:59 +02005041 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005042 struct intel_encoder *encoder = connector->encoder;
5043
5044 return encoder->get_hw_state(encoder, &pipe);
5045}
5046
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005047static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5048 struct intel_crtc_config *pipe_config)
5049{
5050 struct drm_i915_private *dev_priv = dev->dev_private;
5051 struct intel_crtc *pipe_B_crtc =
5052 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5053
5054 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5055 pipe_name(pipe), pipe_config->fdi_lanes);
5056 if (pipe_config->fdi_lanes > 4) {
5057 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5058 pipe_name(pipe), pipe_config->fdi_lanes);
5059 return false;
5060 }
5061
Paulo Zanonibafb6552013-11-02 21:07:44 -07005062 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005063 if (pipe_config->fdi_lanes > 2) {
5064 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5065 pipe_config->fdi_lanes);
5066 return false;
5067 } else {
5068 return true;
5069 }
5070 }
5071
5072 if (INTEL_INFO(dev)->num_pipes == 2)
5073 return true;
5074
5075 /* Ivybridge 3 pipe is really complicated */
5076 switch (pipe) {
5077 case PIPE_A:
5078 return true;
5079 case PIPE_B:
5080 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5081 pipe_config->fdi_lanes > 2) {
5082 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5083 pipe_name(pipe), pipe_config->fdi_lanes);
5084 return false;
5085 }
5086 return true;
5087 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005088 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005089 pipe_B_crtc->config.fdi_lanes <= 2) {
5090 if (pipe_config->fdi_lanes > 2) {
5091 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5092 pipe_name(pipe), pipe_config->fdi_lanes);
5093 return false;
5094 }
5095 } else {
5096 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5097 return false;
5098 }
5099 return true;
5100 default:
5101 BUG();
5102 }
5103}
5104
Daniel Vettere29c22c2013-02-21 00:00:16 +01005105#define RETRY 1
5106static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5107 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005108{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005109 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005110 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005111 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005112 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005113
Daniel Vettere29c22c2013-02-21 00:00:16 +01005114retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005115 /* FDI is a binary signal running at ~2.7GHz, encoding
5116 * each output octet as 10 bits. The actual frequency
5117 * is stored as a divider into a 100MHz clock, and the
5118 * mode pixel clock is stored in units of 1KHz.
5119 * Hence the bw of each lane in terms of the mode signal
5120 * is:
5121 */
5122 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5123
Damien Lespiau241bfc32013-09-25 16:45:37 +01005124 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005125
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005126 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005127 pipe_config->pipe_bpp);
5128
5129 pipe_config->fdi_lanes = lane;
5130
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005131 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005132 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005133
Daniel Vettere29c22c2013-02-21 00:00:16 +01005134 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5135 intel_crtc->pipe, pipe_config);
5136 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5137 pipe_config->pipe_bpp -= 2*3;
5138 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5139 pipe_config->pipe_bpp);
5140 needs_recompute = true;
5141 pipe_config->bw_constrained = true;
5142
5143 goto retry;
5144 }
5145
5146 if (needs_recompute)
5147 return RETRY;
5148
5149 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005150}
5151
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005152static void hsw_compute_ips_config(struct intel_crtc *crtc,
5153 struct intel_crtc_config *pipe_config)
5154{
Jani Nikulad330a952014-01-21 11:24:25 +02005155 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005156 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005157 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005158}
5159
Daniel Vettera43f6e02013-06-07 23:10:32 +02005160static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005161 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005162{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005163 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005164 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005165
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005166 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005167 if (INTEL_INFO(dev)->gen < 4) {
5168 struct drm_i915_private *dev_priv = dev->dev_private;
5169 int clock_limit =
5170 dev_priv->display.get_display_clock_speed(dev);
5171
5172 /*
5173 * Enable pixel doubling when the dot clock
5174 * is > 90% of the (display) core speed.
5175 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005176 * GDG double wide on either pipe,
5177 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005178 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005179 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005180 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005181 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005182 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005183 }
5184
Damien Lespiau241bfc32013-09-25 16:45:37 +01005185 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005186 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005187 }
Chris Wilson89749352010-09-12 18:25:19 +01005188
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005189 /*
5190 * Pipe horizontal size must be even in:
5191 * - DVO ganged mode
5192 * - LVDS dual channel mode
5193 * - Double wide pipe
5194 */
5195 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5196 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5197 pipe_config->pipe_src_w &= ~1;
5198
Damien Lespiau8693a822013-05-03 18:48:11 +01005199 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5200 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005201 */
5202 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5203 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005204 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005205
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005206 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005207 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005208 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005209 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5210 * for lvds. */
5211 pipe_config->pipe_bpp = 8*3;
5212 }
5213
Damien Lespiauf5adf942013-06-24 18:29:34 +01005214 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005215 hsw_compute_ips_config(crtc, pipe_config);
5216
5217 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5218 * clock survives for now. */
5219 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5220 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005221
Daniel Vetter877d48d2013-04-19 11:24:43 +02005222 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005223 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005224
Daniel Vettere29c22c2013-02-21 00:00:16 +01005225 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005226}
5227
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005228static int valleyview_get_display_clock_speed(struct drm_device *dev)
5229{
5230 return 400000; /* FIXME */
5231}
5232
Jesse Barnese70236a2009-09-21 10:42:27 -07005233static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005234{
Jesse Barnese70236a2009-09-21 10:42:27 -07005235 return 400000;
5236}
Jesse Barnes79e53942008-11-07 14:24:08 -08005237
Jesse Barnese70236a2009-09-21 10:42:27 -07005238static int i915_get_display_clock_speed(struct drm_device *dev)
5239{
5240 return 333000;
5241}
Jesse Barnes79e53942008-11-07 14:24:08 -08005242
Jesse Barnese70236a2009-09-21 10:42:27 -07005243static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5244{
5245 return 200000;
5246}
Jesse Barnes79e53942008-11-07 14:24:08 -08005247
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005248static int pnv_get_display_clock_speed(struct drm_device *dev)
5249{
5250 u16 gcfgc = 0;
5251
5252 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5253
5254 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5255 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5256 return 267000;
5257 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5258 return 333000;
5259 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5260 return 444000;
5261 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5262 return 200000;
5263 default:
5264 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5265 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5266 return 133000;
5267 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5268 return 167000;
5269 }
5270}
5271
Jesse Barnese70236a2009-09-21 10:42:27 -07005272static int i915gm_get_display_clock_speed(struct drm_device *dev)
5273{
5274 u16 gcfgc = 0;
5275
5276 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5277
5278 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005279 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005280 else {
5281 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5282 case GC_DISPLAY_CLOCK_333_MHZ:
5283 return 333000;
5284 default:
5285 case GC_DISPLAY_CLOCK_190_200_MHZ:
5286 return 190000;
5287 }
5288 }
5289}
Jesse Barnes79e53942008-11-07 14:24:08 -08005290
Jesse Barnese70236a2009-09-21 10:42:27 -07005291static int i865_get_display_clock_speed(struct drm_device *dev)
5292{
5293 return 266000;
5294}
5295
5296static int i855_get_display_clock_speed(struct drm_device *dev)
5297{
5298 u16 hpllcc = 0;
5299 /* Assume that the hardware is in the high speed state. This
5300 * should be the default.
5301 */
5302 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5303 case GC_CLOCK_133_200:
5304 case GC_CLOCK_100_200:
5305 return 200000;
5306 case GC_CLOCK_166_250:
5307 return 250000;
5308 case GC_CLOCK_100_133:
5309 return 133000;
5310 }
5311
5312 /* Shouldn't happen */
5313 return 0;
5314}
5315
5316static int i830_get_display_clock_speed(struct drm_device *dev)
5317{
5318 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005319}
5320
Zhenyu Wang2c072452009-06-05 15:38:42 +08005321static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005322intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005323{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005324 while (*num > DATA_LINK_M_N_MASK ||
5325 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005326 *num >>= 1;
5327 *den >>= 1;
5328 }
5329}
5330
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005331static void compute_m_n(unsigned int m, unsigned int n,
5332 uint32_t *ret_m, uint32_t *ret_n)
5333{
5334 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5335 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5336 intel_reduce_m_n_ratio(ret_m, ret_n);
5337}
5338
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005339void
5340intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5341 int pixel_clock, int link_clock,
5342 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005343{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005344 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005345
5346 compute_m_n(bits_per_pixel * pixel_clock,
5347 link_clock * nlanes * 8,
5348 &m_n->gmch_m, &m_n->gmch_n);
5349
5350 compute_m_n(pixel_clock, link_clock,
5351 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005352}
5353
Chris Wilsona7615032011-01-12 17:04:08 +00005354static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5355{
Jani Nikulad330a952014-01-21 11:24:25 +02005356 if (i915.panel_use_ssc >= 0)
5357 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005358 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005359 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005360}
5361
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005362static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5363{
5364 struct drm_device *dev = crtc->dev;
5365 struct drm_i915_private *dev_priv = dev->dev_private;
5366 int refclk;
5367
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005368 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005369 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005370 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005371 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005372 refclk = dev_priv->vbt.lvds_ssc_freq;
5373 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005374 } else if (!IS_GEN2(dev)) {
5375 refclk = 96000;
5376 } else {
5377 refclk = 48000;
5378 }
5379
5380 return refclk;
5381}
5382
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005383static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005384{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005385 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005386}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005387
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005388static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5389{
5390 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005391}
5392
Daniel Vetterf47709a2013-03-28 10:42:02 +01005393static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005394 intel_clock_t *reduced_clock)
5395{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005396 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005397 u32 fp, fp2 = 0;
5398
5399 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005400 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005401 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005402 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005403 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005404 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005405 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005406 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005407 }
5408
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005409 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005410
Daniel Vetterf47709a2013-03-28 10:42:02 +01005411 crtc->lowfreq_avail = false;
5412 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005413 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005414 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005415 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005416 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005417 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005418 }
5419}
5420
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005421static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5422 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005423{
5424 u32 reg_val;
5425
5426 /*
5427 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5428 * and set it to a reasonable value instead.
5429 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005430 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005431 reg_val &= 0xffffff00;
5432 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005433 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005434
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005435 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005436 reg_val &= 0x8cffffff;
5437 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005438 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005439
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005440 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005441 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005442 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005443
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005444 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005445 reg_val &= 0x00ffffff;
5446 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005447 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005448}
5449
Daniel Vetterb5518422013-05-03 11:49:48 +02005450static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5451 struct intel_link_m_n *m_n)
5452{
5453 struct drm_device *dev = crtc->base.dev;
5454 struct drm_i915_private *dev_priv = dev->dev_private;
5455 int pipe = crtc->pipe;
5456
Daniel Vettere3b95f12013-05-03 11:49:49 +02005457 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5458 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5459 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5460 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005461}
5462
5463static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5464 struct intel_link_m_n *m_n)
5465{
5466 struct drm_device *dev = crtc->base.dev;
5467 struct drm_i915_private *dev_priv = dev->dev_private;
5468 int pipe = crtc->pipe;
5469 enum transcoder transcoder = crtc->config.cpu_transcoder;
5470
5471 if (INTEL_INFO(dev)->gen >= 5) {
5472 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5473 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5474 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5475 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5476 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005477 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5478 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5479 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5480 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005481 }
5482}
5483
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005484static void intel_dp_set_m_n(struct intel_crtc *crtc)
5485{
5486 if (crtc->config.has_pch_encoder)
5487 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5488 else
5489 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5490}
5491
Daniel Vetterf47709a2013-03-28 10:42:02 +01005492static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005493{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005494 u32 dpll, dpll_md;
5495
5496 /*
5497 * Enable DPIO clock input. We should never disable the reference
5498 * clock for pipe B, since VGA hotplug / manual detection depends
5499 * on it.
5500 */
5501 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5502 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5503 /* We should never disable this, set it here for state tracking */
5504 if (crtc->pipe == PIPE_B)
5505 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5506 dpll |= DPLL_VCO_ENABLE;
5507 crtc->config.dpll_hw_state.dpll = dpll;
5508
5509 dpll_md = (crtc->config.pixel_multiplier - 1)
5510 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5511 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5512}
5513
5514static void vlv_prepare_pll(struct intel_crtc *crtc)
5515{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005516 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005517 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005518 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005519 u32 mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005520 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005521 u32 coreclk, reg_val;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005522
Daniel Vetter09153002012-12-12 14:06:44 +01005523 mutex_lock(&dev_priv->dpio_lock);
5524
Daniel Vetterf47709a2013-03-28 10:42:02 +01005525 bestn = crtc->config.dpll.n;
5526 bestm1 = crtc->config.dpll.m1;
5527 bestm2 = crtc->config.dpll.m2;
5528 bestp1 = crtc->config.dpll.p1;
5529 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005530
Jesse Barnes89b667f2013-04-18 14:51:36 -07005531 /* See eDP HDMI DPIO driver vbios notes doc */
5532
5533 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005534 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005535 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005536
5537 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005538 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005539
5540 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005541 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005542 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005543 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005544
5545 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005546 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005547
5548 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005549 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5550 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5551 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005552 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005553
5554 /*
5555 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5556 * but we don't support that).
5557 * Note: don't use the DAC post divider as it seems unstable.
5558 */
5559 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005560 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005561
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005562 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005563 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005564
Jesse Barnes89b667f2013-04-18 14:51:36 -07005565 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005566 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005567 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005568 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005569 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005570 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005571 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005572 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005573 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005574
Jesse Barnes89b667f2013-04-18 14:51:36 -07005575 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5576 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5577 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005578 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005579 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005580 0x0df40000);
5581 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005582 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005583 0x0df70000);
5584 } else { /* HDMI or VGA */
5585 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005586 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005587 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005588 0x0df70000);
5589 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005590 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005591 0x0df40000);
5592 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005593
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005594 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005595 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5596 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5597 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5598 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005599 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005600
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005601 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005602 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005603}
5604
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005605static void chv_update_pll(struct intel_crtc *crtc)
5606{
5607 struct drm_device *dev = crtc->base.dev;
5608 struct drm_i915_private *dev_priv = dev->dev_private;
5609 int pipe = crtc->pipe;
5610 int dpll_reg = DPLL(crtc->pipe);
5611 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005612 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005613 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5614 int refclk;
5615
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005616 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5617 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5618 DPLL_VCO_ENABLE;
5619 if (pipe != PIPE_A)
5620 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5621
5622 crtc->config.dpll_hw_state.dpll_md =
5623 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005624
5625 bestn = crtc->config.dpll.n;
5626 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5627 bestm1 = crtc->config.dpll.m1;
5628 bestm2 = crtc->config.dpll.m2 >> 22;
5629 bestp1 = crtc->config.dpll.p1;
5630 bestp2 = crtc->config.dpll.p2;
5631
5632 /*
5633 * Enable Refclk and SSC
5634 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005635 I915_WRITE(dpll_reg,
5636 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5637
5638 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005639
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005640 /* p1 and p2 divider */
5641 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5642 5 << DPIO_CHV_S1_DIV_SHIFT |
5643 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5644 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5645 1 << DPIO_CHV_K_DIV_SHIFT);
5646
5647 /* Feedback post-divider - m2 */
5648 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5649
5650 /* Feedback refclk divider - n and m1 */
5651 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5652 DPIO_CHV_M1_DIV_BY_2 |
5653 1 << DPIO_CHV_N_DIV_SHIFT);
5654
5655 /* M2 fraction division */
5656 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5657
5658 /* M2 fraction division enable */
5659 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5660 DPIO_CHV_FRAC_DIV_EN |
5661 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5662
5663 /* Loop filter */
5664 refclk = i9xx_get_refclk(&crtc->base, 0);
5665 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5666 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5667 if (refclk == 100000)
5668 intcoeff = 11;
5669 else if (refclk == 38400)
5670 intcoeff = 10;
5671 else
5672 intcoeff = 9;
5673 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5674 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5675
5676 /* AFC Recal */
5677 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5678 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5679 DPIO_AFC_RECAL);
5680
5681 mutex_unlock(&dev_priv->dpio_lock);
5682}
5683
Daniel Vetterf47709a2013-03-28 10:42:02 +01005684static void i9xx_update_pll(struct intel_crtc *crtc,
5685 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005686 int num_connectors)
5687{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005688 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005689 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005690 u32 dpll;
5691 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005692 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005693
Daniel Vetterf47709a2013-03-28 10:42:02 +01005694 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305695
Daniel Vetterf47709a2013-03-28 10:42:02 +01005696 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5697 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005698
5699 dpll = DPLL_VGA_MODE_DIS;
5700
Daniel Vetterf47709a2013-03-28 10:42:02 +01005701 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005702 dpll |= DPLLB_MODE_LVDS;
5703 else
5704 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005705
Daniel Vetteref1b4602013-06-01 17:17:04 +02005706 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005707 dpll |= (crtc->config.pixel_multiplier - 1)
5708 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005709 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005710
5711 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005712 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005713
Daniel Vetterf47709a2013-03-28 10:42:02 +01005714 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005715 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005716
5717 /* compute bitmask from p1 value */
5718 if (IS_PINEVIEW(dev))
5719 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5720 else {
5721 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5722 if (IS_G4X(dev) && reduced_clock)
5723 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5724 }
5725 switch (clock->p2) {
5726 case 5:
5727 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5728 break;
5729 case 7:
5730 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5731 break;
5732 case 10:
5733 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5734 break;
5735 case 14:
5736 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5737 break;
5738 }
5739 if (INTEL_INFO(dev)->gen >= 4)
5740 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5741
Daniel Vetter09ede542013-04-30 14:01:45 +02005742 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005743 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005744 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005745 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5746 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5747 else
5748 dpll |= PLL_REF_INPUT_DREFCLK;
5749
5750 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005751 crtc->config.dpll_hw_state.dpll = dpll;
5752
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005753 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005754 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5755 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005756 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005757 }
5758}
5759
Daniel Vetterf47709a2013-03-28 10:42:02 +01005760static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005761 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005762 int num_connectors)
5763{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005764 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005765 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005766 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005767 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005768
Daniel Vetterf47709a2013-03-28 10:42:02 +01005769 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305770
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005771 dpll = DPLL_VGA_MODE_DIS;
5772
Daniel Vetterf47709a2013-03-28 10:42:02 +01005773 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005774 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5775 } else {
5776 if (clock->p1 == 2)
5777 dpll |= PLL_P1_DIVIDE_BY_TWO;
5778 else
5779 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5780 if (clock->p2 == 4)
5781 dpll |= PLL_P2_DIVIDE_BY_4;
5782 }
5783
Daniel Vetter4a33e482013-07-06 12:52:05 +02005784 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5785 dpll |= DPLL_DVO_2X_MODE;
5786
Daniel Vetterf47709a2013-03-28 10:42:02 +01005787 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005788 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5789 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5790 else
5791 dpll |= PLL_REF_INPUT_DREFCLK;
5792
5793 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005794 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005795}
5796
Daniel Vetter8a654f32013-06-01 17:16:22 +02005797static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005798{
5799 struct drm_device *dev = intel_crtc->base.dev;
5800 struct drm_i915_private *dev_priv = dev->dev_private;
5801 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005802 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005803 struct drm_display_mode *adjusted_mode =
5804 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005805 uint32_t crtc_vtotal, crtc_vblank_end;
5806 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005807
5808 /* We need to be careful not to changed the adjusted mode, for otherwise
5809 * the hw state checker will get angry at the mismatch. */
5810 crtc_vtotal = adjusted_mode->crtc_vtotal;
5811 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005812
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005813 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005814 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005815 crtc_vtotal -= 1;
5816 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005817
5818 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5819 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5820 else
5821 vsyncshift = adjusted_mode->crtc_hsync_start -
5822 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005823 if (vsyncshift < 0)
5824 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005825 }
5826
5827 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005828 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005829
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005830 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005831 (adjusted_mode->crtc_hdisplay - 1) |
5832 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005833 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005834 (adjusted_mode->crtc_hblank_start - 1) |
5835 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005836 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005837 (adjusted_mode->crtc_hsync_start - 1) |
5838 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5839
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005840 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005841 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005842 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005843 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005844 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005845 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005846 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005847 (adjusted_mode->crtc_vsync_start - 1) |
5848 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5849
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005850 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5851 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5852 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5853 * bits. */
5854 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5855 (pipe == PIPE_B || pipe == PIPE_C))
5856 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5857
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005858 /* pipesrc controls the size that is scaled from, which should
5859 * always be the user's requested size.
5860 */
5861 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005862 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5863 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005864}
5865
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005866static void intel_get_pipe_timings(struct intel_crtc *crtc,
5867 struct intel_crtc_config *pipe_config)
5868{
5869 struct drm_device *dev = crtc->base.dev;
5870 struct drm_i915_private *dev_priv = dev->dev_private;
5871 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5872 uint32_t tmp;
5873
5874 tmp = I915_READ(HTOTAL(cpu_transcoder));
5875 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5876 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5877 tmp = I915_READ(HBLANK(cpu_transcoder));
5878 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5879 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5880 tmp = I915_READ(HSYNC(cpu_transcoder));
5881 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5882 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5883
5884 tmp = I915_READ(VTOTAL(cpu_transcoder));
5885 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5886 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5887 tmp = I915_READ(VBLANK(cpu_transcoder));
5888 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5889 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5890 tmp = I915_READ(VSYNC(cpu_transcoder));
5891 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5892 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5893
5894 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5895 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5896 pipe_config->adjusted_mode.crtc_vtotal += 1;
5897 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5898 }
5899
5900 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005901 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5902 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5903
5904 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5905 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005906}
5907
Daniel Vetterf6a83282014-02-11 15:28:57 -08005908void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5909 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03005910{
Daniel Vetterf6a83282014-02-11 15:28:57 -08005911 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5912 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5913 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5914 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005915
Daniel Vetterf6a83282014-02-11 15:28:57 -08005916 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5917 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5918 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5919 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005920
Daniel Vetterf6a83282014-02-11 15:28:57 -08005921 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005922
Daniel Vetterf6a83282014-02-11 15:28:57 -08005923 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5924 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005925}
5926
Daniel Vetter84b046f2013-02-19 18:48:54 +01005927static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5928{
5929 struct drm_device *dev = intel_crtc->base.dev;
5930 struct drm_i915_private *dev_priv = dev->dev_private;
5931 uint32_t pipeconf;
5932
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005933 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005934
Daniel Vetter67c72a12013-09-24 11:46:14 +02005935 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5936 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5937 pipeconf |= PIPECONF_ENABLE;
5938
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005939 if (intel_crtc->config.double_wide)
5940 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005941
Daniel Vetterff9ce462013-04-24 14:57:17 +02005942 /* only g4x and later have fancy bpc/dither controls */
5943 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005944 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5945 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5946 pipeconf |= PIPECONF_DITHER_EN |
5947 PIPECONF_DITHER_TYPE_SP;
5948
5949 switch (intel_crtc->config.pipe_bpp) {
5950 case 18:
5951 pipeconf |= PIPECONF_6BPC;
5952 break;
5953 case 24:
5954 pipeconf |= PIPECONF_8BPC;
5955 break;
5956 case 30:
5957 pipeconf |= PIPECONF_10BPC;
5958 break;
5959 default:
5960 /* Case prevented by intel_choose_pipe_bpp_dither. */
5961 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005962 }
5963 }
5964
5965 if (HAS_PIPE_CXSR(dev)) {
5966 if (intel_crtc->lowfreq_avail) {
5967 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5968 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5969 } else {
5970 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005971 }
5972 }
5973
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02005974 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5975 if (INTEL_INFO(dev)->gen < 4 ||
5976 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5977 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5978 else
5979 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5980 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01005981 pipeconf |= PIPECONF_PROGRESSIVE;
5982
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005983 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5984 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005985
Daniel Vetter84b046f2013-02-19 18:48:54 +01005986 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5987 POSTING_READ(PIPECONF(intel_crtc->pipe));
5988}
5989
Eric Anholtf564048e2011-03-30 13:01:02 -07005990static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005991 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005992 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005993{
5994 struct drm_device *dev = crtc->dev;
5995 struct drm_i915_private *dev_priv = dev->dev_private;
5996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07005997 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005998 intel_clock_t clock, reduced_clock;
Daniel Vettera16af7212013-04-30 14:01:44 +02005999 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006000 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006001 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006002 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006003
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02006004 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006005 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006006 case INTEL_OUTPUT_LVDS:
6007 is_lvds = true;
6008 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006009 case INTEL_OUTPUT_DSI:
6010 is_dsi = true;
6011 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006012 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006013
Eric Anholtc751ce42010-03-25 11:48:48 -07006014 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006015 }
6016
Jani Nikulaf2335332013-09-13 11:03:09 +03006017 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006018 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006019
Jani Nikulaf2335332013-09-13 11:03:09 +03006020 if (!intel_crtc->config.clock_set) {
6021 refclk = i9xx_get_refclk(crtc, num_connectors);
6022
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006023 /*
6024 * Returns a set of divisors for the desired target clock with
6025 * the given refclk, or FALSE. The returned values represent
6026 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6027 * 2) / p1 / p2.
6028 */
6029 limit = intel_limit(crtc, refclk);
6030 ok = dev_priv->display.find_dpll(limit, crtc,
6031 intel_crtc->config.port_clock,
6032 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006033 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006034 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6035 return -EINVAL;
6036 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006037
Jani Nikulaf2335332013-09-13 11:03:09 +03006038 if (is_lvds && dev_priv->lvds_downclock_avail) {
6039 /*
6040 * Ensure we match the reduced clock's P to the target
6041 * clock. If the clocks don't match, we can't switch
6042 * the display clock by using the FP0/FP1. In such case
6043 * we will disable the LVDS downclock feature.
6044 */
6045 has_reduced_clock =
6046 dev_priv->display.find_dpll(limit, crtc,
6047 dev_priv->lvds_downclock,
6048 refclk, &clock,
6049 &reduced_clock);
6050 }
6051 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01006052 intel_crtc->config.dpll.n = clock.n;
6053 intel_crtc->config.dpll.m1 = clock.m1;
6054 intel_crtc->config.dpll.m2 = clock.m2;
6055 intel_crtc->config.dpll.p1 = clock.p1;
6056 intel_crtc->config.dpll.p2 = clock.p2;
6057 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006058
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006059 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02006060 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306061 has_reduced_clock ? &reduced_clock : NULL,
6062 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006063 } else if (IS_CHERRYVIEW(dev)) {
6064 chv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006065 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03006066 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006067 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01006068 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006069 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006070 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006071 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006072
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006073 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006074}
6075
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006076static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6077 struct intel_crtc_config *pipe_config)
6078{
6079 struct drm_device *dev = crtc->base.dev;
6080 struct drm_i915_private *dev_priv = dev->dev_private;
6081 uint32_t tmp;
6082
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006083 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6084 return;
6085
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006086 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006087 if (!(tmp & PFIT_ENABLE))
6088 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006089
Daniel Vetter06922822013-07-11 13:35:40 +02006090 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006091 if (INTEL_INFO(dev)->gen < 4) {
6092 if (crtc->pipe != PIPE_B)
6093 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006094 } else {
6095 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6096 return;
6097 }
6098
Daniel Vetter06922822013-07-11 13:35:40 +02006099 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006100 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6101 if (INTEL_INFO(dev)->gen < 5)
6102 pipe_config->gmch_pfit.lvds_border_bits =
6103 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6104}
6105
Jesse Barnesacbec812013-09-20 11:29:32 -07006106static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6107 struct intel_crtc_config *pipe_config)
6108{
6109 struct drm_device *dev = crtc->base.dev;
6110 struct drm_i915_private *dev_priv = dev->dev_private;
6111 int pipe = pipe_config->cpu_transcoder;
6112 intel_clock_t clock;
6113 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006114 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006115
6116 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006117 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006118 mutex_unlock(&dev_priv->dpio_lock);
6119
6120 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6121 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6122 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6123 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6124 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6125
Ville Syrjäläf6466282013-10-14 14:50:31 +03006126 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006127
Ville Syrjäläf6466282013-10-14 14:50:31 +03006128 /* clock.dot is the fast clock */
6129 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006130}
6131
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006132static void i9xx_get_plane_config(struct intel_crtc *crtc,
6133 struct intel_plane_config *plane_config)
6134{
6135 struct drm_device *dev = crtc->base.dev;
6136 struct drm_i915_private *dev_priv = dev->dev_private;
6137 u32 val, base, offset;
6138 int pipe = crtc->pipe, plane = crtc->plane;
6139 int fourcc, pixel_format;
6140 int aligned_height;
6141
Dave Airlie66e514c2014-04-03 07:51:54 +10006142 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6143 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006144 DRM_DEBUG_KMS("failed to alloc fb\n");
6145 return;
6146 }
6147
6148 val = I915_READ(DSPCNTR(plane));
6149
6150 if (INTEL_INFO(dev)->gen >= 4)
6151 if (val & DISPPLANE_TILED)
6152 plane_config->tiled = true;
6153
6154 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6155 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006156 crtc->base.primary->fb->pixel_format = fourcc;
6157 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006158 drm_format_plane_cpp(fourcc, 0) * 8;
6159
6160 if (INTEL_INFO(dev)->gen >= 4) {
6161 if (plane_config->tiled)
6162 offset = I915_READ(DSPTILEOFF(plane));
6163 else
6164 offset = I915_READ(DSPLINOFF(plane));
6165 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6166 } else {
6167 base = I915_READ(DSPADDR(plane));
6168 }
6169 plane_config->base = base;
6170
6171 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006172 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6173 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006174
6175 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006176 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006177
Dave Airlie66e514c2014-04-03 07:51:54 +10006178 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006179 plane_config->tiled);
6180
Dave Airlie66e514c2014-04-03 07:51:54 +10006181 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006182 aligned_height, PAGE_SIZE);
6183
6184 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 +10006185 pipe, plane, crtc->base.primary->fb->width,
6186 crtc->base.primary->fb->height,
6187 crtc->base.primary->fb->bits_per_pixel, base,
6188 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006189 plane_config->size);
6190
6191}
6192
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006193static void chv_crtc_clock_get(struct intel_crtc *crtc,
6194 struct intel_crtc_config *pipe_config)
6195{
6196 struct drm_device *dev = crtc->base.dev;
6197 struct drm_i915_private *dev_priv = dev->dev_private;
6198 int pipe = pipe_config->cpu_transcoder;
6199 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6200 intel_clock_t clock;
6201 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6202 int refclk = 100000;
6203
6204 mutex_lock(&dev_priv->dpio_lock);
6205 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6206 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6207 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6208 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6209 mutex_unlock(&dev_priv->dpio_lock);
6210
6211 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6212 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6213 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6214 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6215 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6216
6217 chv_clock(refclk, &clock);
6218
6219 /* clock.dot is the fast clock */
6220 pipe_config->port_clock = clock.dot / 5;
6221}
6222
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006223static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6224 struct intel_crtc_config *pipe_config)
6225{
6226 struct drm_device *dev = crtc->base.dev;
6227 struct drm_i915_private *dev_priv = dev->dev_private;
6228 uint32_t tmp;
6229
Imre Deakb5482bd2014-03-05 16:20:55 +02006230 if (!intel_display_power_enabled(dev_priv,
6231 POWER_DOMAIN_PIPE(crtc->pipe)))
6232 return false;
6233
Daniel Vettere143a212013-07-04 12:01:15 +02006234 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006235 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006236
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006237 tmp = I915_READ(PIPECONF(crtc->pipe));
6238 if (!(tmp & PIPECONF_ENABLE))
6239 return false;
6240
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006241 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6242 switch (tmp & PIPECONF_BPC_MASK) {
6243 case PIPECONF_6BPC:
6244 pipe_config->pipe_bpp = 18;
6245 break;
6246 case PIPECONF_8BPC:
6247 pipe_config->pipe_bpp = 24;
6248 break;
6249 case PIPECONF_10BPC:
6250 pipe_config->pipe_bpp = 30;
6251 break;
6252 default:
6253 break;
6254 }
6255 }
6256
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006257 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6258 pipe_config->limited_color_range = true;
6259
Ville Syrjälä282740f2013-09-04 18:30:03 +03006260 if (INTEL_INFO(dev)->gen < 4)
6261 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6262
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006263 intel_get_pipe_timings(crtc, pipe_config);
6264
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006265 i9xx_get_pfit_config(crtc, pipe_config);
6266
Daniel Vetter6c49f242013-06-06 12:45:25 +02006267 if (INTEL_INFO(dev)->gen >= 4) {
6268 tmp = I915_READ(DPLL_MD(crtc->pipe));
6269 pipe_config->pixel_multiplier =
6270 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6271 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006272 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006273 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6274 tmp = I915_READ(DPLL(crtc->pipe));
6275 pipe_config->pixel_multiplier =
6276 ((tmp & SDVO_MULTIPLIER_MASK)
6277 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6278 } else {
6279 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6280 * port and will be fixed up in the encoder->get_config
6281 * function. */
6282 pipe_config->pixel_multiplier = 1;
6283 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006284 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6285 if (!IS_VALLEYVIEW(dev)) {
6286 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6287 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006288 } else {
6289 /* Mask out read-only status bits. */
6290 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6291 DPLL_PORTC_READY_MASK |
6292 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006293 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006294
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006295 if (IS_CHERRYVIEW(dev))
6296 chv_crtc_clock_get(crtc, pipe_config);
6297 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006298 vlv_crtc_clock_get(crtc, pipe_config);
6299 else
6300 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006301
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006302 return true;
6303}
6304
Paulo Zanonidde86e22012-12-01 12:04:25 -02006305static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006306{
6307 struct drm_i915_private *dev_priv = dev->dev_private;
6308 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006309 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006310 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006311 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006312 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006313 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006314 bool has_ck505 = false;
6315 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006316
6317 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07006318 list_for_each_entry(encoder, &mode_config->encoder_list,
6319 base.head) {
6320 switch (encoder->type) {
6321 case INTEL_OUTPUT_LVDS:
6322 has_panel = true;
6323 has_lvds = true;
6324 break;
6325 case INTEL_OUTPUT_EDP:
6326 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006327 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006328 has_cpu_edp = true;
6329 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006330 }
6331 }
6332
Keith Packard99eb6a02011-09-26 14:29:12 -07006333 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006334 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006335 can_ssc = has_ck505;
6336 } else {
6337 has_ck505 = false;
6338 can_ssc = true;
6339 }
6340
Imre Deak2de69052013-05-08 13:14:04 +03006341 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6342 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006343
6344 /* Ironlake: try to setup display ref clock before DPLL
6345 * enabling. This is only under driver's control after
6346 * PCH B stepping, previous chipset stepping should be
6347 * ignoring this setting.
6348 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006349 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006350
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006351 /* As we must carefully and slowly disable/enable each source in turn,
6352 * compute the final state we want first and check if we need to
6353 * make any changes at all.
6354 */
6355 final = val;
6356 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006357 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006358 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006359 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006360 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6361
6362 final &= ~DREF_SSC_SOURCE_MASK;
6363 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6364 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006365
Keith Packard199e5d72011-09-22 12:01:57 -07006366 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006367 final |= DREF_SSC_SOURCE_ENABLE;
6368
6369 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6370 final |= DREF_SSC1_ENABLE;
6371
6372 if (has_cpu_edp) {
6373 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6374 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6375 else
6376 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6377 } else
6378 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6379 } else {
6380 final |= DREF_SSC_SOURCE_DISABLE;
6381 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6382 }
6383
6384 if (final == val)
6385 return;
6386
6387 /* Always enable nonspread source */
6388 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6389
6390 if (has_ck505)
6391 val |= DREF_NONSPREAD_CK505_ENABLE;
6392 else
6393 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6394
6395 if (has_panel) {
6396 val &= ~DREF_SSC_SOURCE_MASK;
6397 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006398
Keith Packard199e5d72011-09-22 12:01:57 -07006399 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006400 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006401 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006402 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006403 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006404 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006405
6406 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006407 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006408 POSTING_READ(PCH_DREF_CONTROL);
6409 udelay(200);
6410
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006411 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006412
6413 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006414 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006415 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006416 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006417 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006418 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006419 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006420 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006421 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006422
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006423 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006424 POSTING_READ(PCH_DREF_CONTROL);
6425 udelay(200);
6426 } else {
6427 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6428
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006429 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006430
6431 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006432 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006433
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006434 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006435 POSTING_READ(PCH_DREF_CONTROL);
6436 udelay(200);
6437
6438 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006439 val &= ~DREF_SSC_SOURCE_MASK;
6440 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006441
6442 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006443 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006444
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006445 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006446 POSTING_READ(PCH_DREF_CONTROL);
6447 udelay(200);
6448 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006449
6450 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006451}
6452
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006453static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006454{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006455 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006456
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006457 tmp = I915_READ(SOUTH_CHICKEN2);
6458 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6459 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006460
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006461 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6462 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6463 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006464
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006465 tmp = I915_READ(SOUTH_CHICKEN2);
6466 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6467 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006468
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006469 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6470 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6471 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006472}
6473
6474/* WaMPhyProgramming:hsw */
6475static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6476{
6477 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006478
6479 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6480 tmp &= ~(0xFF << 24);
6481 tmp |= (0x12 << 24);
6482 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6483
Paulo Zanonidde86e22012-12-01 12:04:25 -02006484 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6485 tmp |= (1 << 11);
6486 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6487
6488 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6489 tmp |= (1 << 11);
6490 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6491
Paulo Zanonidde86e22012-12-01 12:04:25 -02006492 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6493 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6494 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6495
6496 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6497 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6498 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6499
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006500 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6501 tmp &= ~(7 << 13);
6502 tmp |= (5 << 13);
6503 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006504
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006505 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6506 tmp &= ~(7 << 13);
6507 tmp |= (5 << 13);
6508 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006509
6510 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6511 tmp &= ~0xFF;
6512 tmp |= 0x1C;
6513 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6514
6515 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6516 tmp &= ~0xFF;
6517 tmp |= 0x1C;
6518 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6519
6520 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6521 tmp &= ~(0xFF << 16);
6522 tmp |= (0x1C << 16);
6523 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6524
6525 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6526 tmp &= ~(0xFF << 16);
6527 tmp |= (0x1C << 16);
6528 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6529
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006530 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6531 tmp |= (1 << 27);
6532 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006533
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006534 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6535 tmp |= (1 << 27);
6536 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006537
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006538 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6539 tmp &= ~(0xF << 28);
6540 tmp |= (4 << 28);
6541 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006542
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006543 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6544 tmp &= ~(0xF << 28);
6545 tmp |= (4 << 28);
6546 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006547}
6548
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006549/* Implements 3 different sequences from BSpec chapter "Display iCLK
6550 * Programming" based on the parameters passed:
6551 * - Sequence to enable CLKOUT_DP
6552 * - Sequence to enable CLKOUT_DP without spread
6553 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6554 */
6555static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6556 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006557{
6558 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006559 uint32_t reg, tmp;
6560
6561 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6562 with_spread = true;
6563 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6564 with_fdi, "LP PCH doesn't have FDI\n"))
6565 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006566
6567 mutex_lock(&dev_priv->dpio_lock);
6568
6569 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6570 tmp &= ~SBI_SSCCTL_DISABLE;
6571 tmp |= SBI_SSCCTL_PATHALT;
6572 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6573
6574 udelay(24);
6575
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006576 if (with_spread) {
6577 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6578 tmp &= ~SBI_SSCCTL_PATHALT;
6579 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006580
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006581 if (with_fdi) {
6582 lpt_reset_fdi_mphy(dev_priv);
6583 lpt_program_fdi_mphy(dev_priv);
6584 }
6585 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006586
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006587 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6588 SBI_GEN0 : SBI_DBUFF0;
6589 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6590 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6591 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006592
6593 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006594}
6595
Paulo Zanoni47701c32013-07-23 11:19:25 -03006596/* Sequence to disable CLKOUT_DP */
6597static void lpt_disable_clkout_dp(struct drm_device *dev)
6598{
6599 struct drm_i915_private *dev_priv = dev->dev_private;
6600 uint32_t reg, tmp;
6601
6602 mutex_lock(&dev_priv->dpio_lock);
6603
6604 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6605 SBI_GEN0 : SBI_DBUFF0;
6606 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6607 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6608 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6609
6610 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6611 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6612 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6613 tmp |= SBI_SSCCTL_PATHALT;
6614 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6615 udelay(32);
6616 }
6617 tmp |= SBI_SSCCTL_DISABLE;
6618 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6619 }
6620
6621 mutex_unlock(&dev_priv->dpio_lock);
6622}
6623
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006624static void lpt_init_pch_refclk(struct drm_device *dev)
6625{
6626 struct drm_mode_config *mode_config = &dev->mode_config;
6627 struct intel_encoder *encoder;
6628 bool has_vga = false;
6629
6630 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6631 switch (encoder->type) {
6632 case INTEL_OUTPUT_ANALOG:
6633 has_vga = true;
6634 break;
6635 }
6636 }
6637
Paulo Zanoni47701c32013-07-23 11:19:25 -03006638 if (has_vga)
6639 lpt_enable_clkout_dp(dev, true, true);
6640 else
6641 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006642}
6643
Paulo Zanonidde86e22012-12-01 12:04:25 -02006644/*
6645 * Initialize reference clocks when the driver loads
6646 */
6647void intel_init_pch_refclk(struct drm_device *dev)
6648{
6649 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6650 ironlake_init_pch_refclk(dev);
6651 else if (HAS_PCH_LPT(dev))
6652 lpt_init_pch_refclk(dev);
6653}
6654
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006655static int ironlake_get_refclk(struct drm_crtc *crtc)
6656{
6657 struct drm_device *dev = crtc->dev;
6658 struct drm_i915_private *dev_priv = dev->dev_private;
6659 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006660 int num_connectors = 0;
6661 bool is_lvds = false;
6662
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02006663 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006664 switch (encoder->type) {
6665 case INTEL_OUTPUT_LVDS:
6666 is_lvds = true;
6667 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006668 }
6669 num_connectors++;
6670 }
6671
6672 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006673 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006674 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006675 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006676 }
6677
6678 return 120000;
6679}
6680
Daniel Vetter6ff93602013-04-19 11:24:36 +02006681static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006682{
6683 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6685 int pipe = intel_crtc->pipe;
6686 uint32_t val;
6687
Daniel Vetter78114072013-06-13 00:54:57 +02006688 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006689
Daniel Vetter965e0c42013-03-27 00:44:57 +01006690 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006691 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006692 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006693 break;
6694 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006695 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006696 break;
6697 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006698 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006699 break;
6700 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006701 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006702 break;
6703 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006704 /* Case prevented by intel_choose_pipe_bpp_dither. */
6705 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006706 }
6707
Daniel Vetterd8b32242013-04-25 17:54:44 +02006708 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006709 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6710
Daniel Vetter6ff93602013-04-19 11:24:36 +02006711 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006712 val |= PIPECONF_INTERLACED_ILK;
6713 else
6714 val |= PIPECONF_PROGRESSIVE;
6715
Daniel Vetter50f3b012013-03-27 00:44:56 +01006716 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006717 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006718
Paulo Zanonic8203562012-09-12 10:06:29 -03006719 I915_WRITE(PIPECONF(pipe), val);
6720 POSTING_READ(PIPECONF(pipe));
6721}
6722
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006723/*
6724 * Set up the pipe CSC unit.
6725 *
6726 * Currently only full range RGB to limited range RGB conversion
6727 * is supported, but eventually this should handle various
6728 * RGB<->YCbCr scenarios as well.
6729 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006730static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006731{
6732 struct drm_device *dev = crtc->dev;
6733 struct drm_i915_private *dev_priv = dev->dev_private;
6734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6735 int pipe = intel_crtc->pipe;
6736 uint16_t coeff = 0x7800; /* 1.0 */
6737
6738 /*
6739 * TODO: Check what kind of values actually come out of the pipe
6740 * with these coeff/postoff values and adjust to get the best
6741 * accuracy. Perhaps we even need to take the bpc value into
6742 * consideration.
6743 */
6744
Daniel Vetter50f3b012013-03-27 00:44:56 +01006745 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006746 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6747
6748 /*
6749 * GY/GU and RY/RU should be the other way around according
6750 * to BSpec, but reality doesn't agree. Just set them up in
6751 * a way that results in the correct picture.
6752 */
6753 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6754 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6755
6756 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6757 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6758
6759 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6760 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6761
6762 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6763 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6764 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6765
6766 if (INTEL_INFO(dev)->gen > 6) {
6767 uint16_t postoff = 0;
6768
Daniel Vetter50f3b012013-03-27 00:44:56 +01006769 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006770 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006771
6772 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6773 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6774 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6775
6776 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6777 } else {
6778 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6779
Daniel Vetter50f3b012013-03-27 00:44:56 +01006780 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006781 mode |= CSC_BLACK_SCREEN_OFFSET;
6782
6783 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6784 }
6785}
6786
Daniel Vetter6ff93602013-04-19 11:24:36 +02006787static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006788{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006789 struct drm_device *dev = crtc->dev;
6790 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006792 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006793 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006794 uint32_t val;
6795
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006796 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006797
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006798 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006799 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6800
Daniel Vetter6ff93602013-04-19 11:24:36 +02006801 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006802 val |= PIPECONF_INTERLACED_ILK;
6803 else
6804 val |= PIPECONF_PROGRESSIVE;
6805
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006806 I915_WRITE(PIPECONF(cpu_transcoder), val);
6807 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006808
6809 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6810 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006811
6812 if (IS_BROADWELL(dev)) {
6813 val = 0;
6814
6815 switch (intel_crtc->config.pipe_bpp) {
6816 case 18:
6817 val |= PIPEMISC_DITHER_6_BPC;
6818 break;
6819 case 24:
6820 val |= PIPEMISC_DITHER_8_BPC;
6821 break;
6822 case 30:
6823 val |= PIPEMISC_DITHER_10_BPC;
6824 break;
6825 case 36:
6826 val |= PIPEMISC_DITHER_12_BPC;
6827 break;
6828 default:
6829 /* Case prevented by pipe_config_set_bpp. */
6830 BUG();
6831 }
6832
6833 if (intel_crtc->config.dither)
6834 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6835
6836 I915_WRITE(PIPEMISC(pipe), val);
6837 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006838}
6839
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006840static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006841 intel_clock_t *clock,
6842 bool *has_reduced_clock,
6843 intel_clock_t *reduced_clock)
6844{
6845 struct drm_device *dev = crtc->dev;
6846 struct drm_i915_private *dev_priv = dev->dev_private;
6847 struct intel_encoder *intel_encoder;
6848 int refclk;
6849 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006850 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006851
6852 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6853 switch (intel_encoder->type) {
6854 case INTEL_OUTPUT_LVDS:
6855 is_lvds = true;
6856 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006857 }
6858 }
6859
6860 refclk = ironlake_get_refclk(crtc);
6861
6862 /*
6863 * Returns a set of divisors for the desired target clock with the given
6864 * refclk, or FALSE. The returned values represent the clock equation:
6865 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6866 */
6867 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006868 ret = dev_priv->display.find_dpll(limit, crtc,
6869 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006870 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006871 if (!ret)
6872 return false;
6873
6874 if (is_lvds && dev_priv->lvds_downclock_avail) {
6875 /*
6876 * Ensure we match the reduced clock's P to the target clock.
6877 * If the clocks don't match, we can't switch the display clock
6878 * by using the FP0/FP1. In such case we will disable the LVDS
6879 * downclock feature.
6880 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006881 *has_reduced_clock =
6882 dev_priv->display.find_dpll(limit, crtc,
6883 dev_priv->lvds_downclock,
6884 refclk, clock,
6885 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006886 }
6887
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006888 return true;
6889}
6890
Paulo Zanonid4b19312012-11-29 11:29:32 -02006891int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6892{
6893 /*
6894 * Account for spread spectrum to avoid
6895 * oversubscribing the link. Max center spread
6896 * is 2.5%; use 5% for safety's sake.
6897 */
6898 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02006899 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02006900}
6901
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006902static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006903{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006904 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006905}
6906
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006907static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006908 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006909 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006910{
6911 struct drm_crtc *crtc = &intel_crtc->base;
6912 struct drm_device *dev = crtc->dev;
6913 struct drm_i915_private *dev_priv = dev->dev_private;
6914 struct intel_encoder *intel_encoder;
6915 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006916 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006917 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006918
6919 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6920 switch (intel_encoder->type) {
6921 case INTEL_OUTPUT_LVDS:
6922 is_lvds = true;
6923 break;
6924 case INTEL_OUTPUT_SDVO:
6925 case INTEL_OUTPUT_HDMI:
6926 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006927 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006928 }
6929
6930 num_connectors++;
6931 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006932
Chris Wilsonc1858122010-12-03 21:35:48 +00006933 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006934 factor = 21;
6935 if (is_lvds) {
6936 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006937 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006938 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006939 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006940 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006941 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006942
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006943 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006944 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006945
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006946 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6947 *fp2 |= FP_CB_TUNE;
6948
Chris Wilson5eddb702010-09-11 13:48:45 +01006949 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006950
Eric Anholta07d6782011-03-30 13:01:08 -07006951 if (is_lvds)
6952 dpll |= DPLLB_MODE_LVDS;
6953 else
6954 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006955
Daniel Vetteref1b4602013-06-01 17:17:04 +02006956 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6957 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006958
6959 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006960 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006961 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006962 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006963
Eric Anholta07d6782011-03-30 13:01:08 -07006964 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006965 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006966 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006967 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006968
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006969 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006970 case 5:
6971 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6972 break;
6973 case 7:
6974 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6975 break;
6976 case 10:
6977 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6978 break;
6979 case 14:
6980 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6981 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006982 }
6983
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006984 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006985 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006986 else
6987 dpll |= PLL_REF_INPUT_DREFCLK;
6988
Daniel Vetter959e16d2013-06-05 13:34:21 +02006989 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006990}
6991
Jesse Barnes79e53942008-11-07 14:24:08 -08006992static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006993 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006994 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006995{
6996 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006998 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006999 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007000 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007001 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007002 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007003 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02007004 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007005
7006 for_each_encoder_on_crtc(dev, crtc, encoder) {
7007 switch (encoder->type) {
7008 case INTEL_OUTPUT_LVDS:
7009 is_lvds = true;
7010 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007011 }
7012
7013 num_connectors++;
7014 }
7015
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007016 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7017 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7018
Daniel Vetterff9a6752013-06-01 17:16:21 +02007019 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007020 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02007021 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007022 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7023 return -EINVAL;
7024 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007025 /* Compat-code for transition, will disappear. */
7026 if (!intel_crtc->config.clock_set) {
7027 intel_crtc->config.dpll.n = clock.n;
7028 intel_crtc->config.dpll.m1 = clock.m1;
7029 intel_crtc->config.dpll.m2 = clock.m2;
7030 intel_crtc->config.dpll.p1 = clock.p1;
7031 intel_crtc->config.dpll.p2 = clock.p2;
7032 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007033
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007034 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01007035 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007036 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007037 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007038 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007039
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007040 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007041 &fp, &reduced_clock,
7042 has_reduced_clock ? &fp2 : NULL);
7043
Daniel Vetter959e16d2013-06-05 13:34:21 +02007044 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007045 intel_crtc->config.dpll_hw_state.fp0 = fp;
7046 if (has_reduced_clock)
7047 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7048 else
7049 intel_crtc->config.dpll_hw_state.fp1 = fp;
7050
Daniel Vetterb89a1d32013-06-05 13:34:24 +02007051 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007052 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007053 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Daniel Vetter29407aa2014-04-24 23:55:08 +02007054 pipe_name(intel_crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007055 return -EINVAL;
7056 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007057 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02007058 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007059
Jani Nikulad330a952014-01-21 11:24:25 +02007060 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007061 intel_crtc->lowfreq_avail = true;
7062 else
7063 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007064
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007065 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007066}
7067
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007068static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7069 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007070{
7071 struct drm_device *dev = crtc->base.dev;
7072 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007073 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007074
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007075 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7076 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7077 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7078 & ~TU_SIZE_MASK;
7079 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7080 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7081 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7082}
7083
7084static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7085 enum transcoder transcoder,
7086 struct intel_link_m_n *m_n)
7087{
7088 struct drm_device *dev = crtc->base.dev;
7089 struct drm_i915_private *dev_priv = dev->dev_private;
7090 enum pipe pipe = crtc->pipe;
7091
7092 if (INTEL_INFO(dev)->gen >= 5) {
7093 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7094 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7095 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7096 & ~TU_SIZE_MASK;
7097 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7098 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7099 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7100 } else {
7101 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7102 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7103 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7104 & ~TU_SIZE_MASK;
7105 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7106 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7107 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7108 }
7109}
7110
7111void intel_dp_get_m_n(struct intel_crtc *crtc,
7112 struct intel_crtc_config *pipe_config)
7113{
7114 if (crtc->config.has_pch_encoder)
7115 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7116 else
7117 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7118 &pipe_config->dp_m_n);
7119}
7120
Daniel Vetter72419202013-04-04 13:28:53 +02007121static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7122 struct intel_crtc_config *pipe_config)
7123{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007124 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7125 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02007126}
7127
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007128static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7129 struct intel_crtc_config *pipe_config)
7130{
7131 struct drm_device *dev = crtc->base.dev;
7132 struct drm_i915_private *dev_priv = dev->dev_private;
7133 uint32_t tmp;
7134
7135 tmp = I915_READ(PF_CTL(crtc->pipe));
7136
7137 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007138 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007139 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7140 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007141
7142 /* We currently do not free assignements of panel fitters on
7143 * ivb/hsw (since we don't use the higher upscaling modes which
7144 * differentiates them) so just WARN about this case for now. */
7145 if (IS_GEN7(dev)) {
7146 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7147 PF_PIPE_SEL_IVB(crtc->pipe));
7148 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007149 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007150}
7151
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007152static void ironlake_get_plane_config(struct intel_crtc *crtc,
7153 struct intel_plane_config *plane_config)
7154{
7155 struct drm_device *dev = crtc->base.dev;
7156 struct drm_i915_private *dev_priv = dev->dev_private;
7157 u32 val, base, offset;
7158 int pipe = crtc->pipe, plane = crtc->plane;
7159 int fourcc, pixel_format;
7160 int aligned_height;
7161
Dave Airlie66e514c2014-04-03 07:51:54 +10007162 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7163 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007164 DRM_DEBUG_KMS("failed to alloc fb\n");
7165 return;
7166 }
7167
7168 val = I915_READ(DSPCNTR(plane));
7169
7170 if (INTEL_INFO(dev)->gen >= 4)
7171 if (val & DISPPLANE_TILED)
7172 plane_config->tiled = true;
7173
7174 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7175 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007176 crtc->base.primary->fb->pixel_format = fourcc;
7177 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007178 drm_format_plane_cpp(fourcc, 0) * 8;
7179
7180 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7181 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7182 offset = I915_READ(DSPOFFSET(plane));
7183 } else {
7184 if (plane_config->tiled)
7185 offset = I915_READ(DSPTILEOFF(plane));
7186 else
7187 offset = I915_READ(DSPLINOFF(plane));
7188 }
7189 plane_config->base = base;
7190
7191 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007192 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7193 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007194
7195 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007196 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007197
Dave Airlie66e514c2014-04-03 07:51:54 +10007198 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007199 plane_config->tiled);
7200
Dave Airlie66e514c2014-04-03 07:51:54 +10007201 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007202 aligned_height, PAGE_SIZE);
7203
7204 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 +10007205 pipe, plane, crtc->base.primary->fb->width,
7206 crtc->base.primary->fb->height,
7207 crtc->base.primary->fb->bits_per_pixel, base,
7208 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007209 plane_config->size);
7210}
7211
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007212static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7213 struct intel_crtc_config *pipe_config)
7214{
7215 struct drm_device *dev = crtc->base.dev;
7216 struct drm_i915_private *dev_priv = dev->dev_private;
7217 uint32_t tmp;
7218
Daniel Vettere143a212013-07-04 12:01:15 +02007219 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007220 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007221
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007222 tmp = I915_READ(PIPECONF(crtc->pipe));
7223 if (!(tmp & PIPECONF_ENABLE))
7224 return false;
7225
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007226 switch (tmp & PIPECONF_BPC_MASK) {
7227 case PIPECONF_6BPC:
7228 pipe_config->pipe_bpp = 18;
7229 break;
7230 case PIPECONF_8BPC:
7231 pipe_config->pipe_bpp = 24;
7232 break;
7233 case PIPECONF_10BPC:
7234 pipe_config->pipe_bpp = 30;
7235 break;
7236 case PIPECONF_12BPC:
7237 pipe_config->pipe_bpp = 36;
7238 break;
7239 default:
7240 break;
7241 }
7242
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007243 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7244 pipe_config->limited_color_range = true;
7245
Daniel Vetterab9412b2013-05-03 11:49:46 +02007246 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007247 struct intel_shared_dpll *pll;
7248
Daniel Vetter88adfff2013-03-28 10:42:01 +01007249 pipe_config->has_pch_encoder = true;
7250
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007251 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7252 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7253 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007254
7255 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007256
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007257 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007258 pipe_config->shared_dpll =
7259 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007260 } else {
7261 tmp = I915_READ(PCH_DPLL_SEL);
7262 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7263 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7264 else
7265 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7266 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007267
7268 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7269
7270 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7271 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007272
7273 tmp = pipe_config->dpll_hw_state.dpll;
7274 pipe_config->pixel_multiplier =
7275 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7276 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007277
7278 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007279 } else {
7280 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007281 }
7282
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007283 intel_get_pipe_timings(crtc, pipe_config);
7284
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007285 ironlake_get_pfit_config(crtc, pipe_config);
7286
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007287 return true;
7288}
7289
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007290static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7291{
7292 struct drm_device *dev = dev_priv->dev;
7293 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7294 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007295
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007296 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007297 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007298 pipe_name(crtc->pipe));
7299
7300 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7301 WARN(plls->spll_refcount, "SPLL enabled\n");
7302 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7303 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7304 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7305 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7306 "CPU PWM1 enabled\n");
7307 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7308 "CPU PWM2 enabled\n");
7309 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7310 "PCH PWM1 enabled\n");
7311 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7312 "Utility pin enabled\n");
7313 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7314
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007315 /*
7316 * In theory we can still leave IRQs enabled, as long as only the HPD
7317 * interrupts remain enabled. We used to check for that, but since it's
7318 * gen-specific and since we only disable LCPLL after we fully disable
7319 * the interrupts, the check below should be enough.
7320 */
7321 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007322}
7323
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007324static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7325{
7326 struct drm_device *dev = dev_priv->dev;
7327
7328 if (IS_HASWELL(dev)) {
7329 mutex_lock(&dev_priv->rps.hw_lock);
7330 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7331 val))
7332 DRM_ERROR("Failed to disable D_COMP\n");
7333 mutex_unlock(&dev_priv->rps.hw_lock);
7334 } else {
7335 I915_WRITE(D_COMP, val);
7336 }
7337 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007338}
7339
7340/*
7341 * This function implements pieces of two sequences from BSpec:
7342 * - Sequence for display software to disable LCPLL
7343 * - Sequence for display software to allow package C8+
7344 * The steps implemented here are just the steps that actually touch the LCPLL
7345 * register. Callers should take care of disabling all the display engine
7346 * functions, doing the mode unset, fixing interrupts, etc.
7347 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007348static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7349 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007350{
7351 uint32_t val;
7352
7353 assert_can_disable_lcpll(dev_priv);
7354
7355 val = I915_READ(LCPLL_CTL);
7356
7357 if (switch_to_fclk) {
7358 val |= LCPLL_CD_SOURCE_FCLK;
7359 I915_WRITE(LCPLL_CTL, val);
7360
7361 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7362 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7363 DRM_ERROR("Switching to FCLK failed\n");
7364
7365 val = I915_READ(LCPLL_CTL);
7366 }
7367
7368 val |= LCPLL_PLL_DISABLE;
7369 I915_WRITE(LCPLL_CTL, val);
7370 POSTING_READ(LCPLL_CTL);
7371
7372 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7373 DRM_ERROR("LCPLL still locked\n");
7374
7375 val = I915_READ(D_COMP);
7376 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007377 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007378 ndelay(100);
7379
7380 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7381 DRM_ERROR("D_COMP RCOMP still in progress\n");
7382
7383 if (allow_power_down) {
7384 val = I915_READ(LCPLL_CTL);
7385 val |= LCPLL_POWER_DOWN_ALLOW;
7386 I915_WRITE(LCPLL_CTL, val);
7387 POSTING_READ(LCPLL_CTL);
7388 }
7389}
7390
7391/*
7392 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7393 * source.
7394 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007395static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007396{
7397 uint32_t val;
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007398 unsigned long irqflags;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007399
7400 val = I915_READ(LCPLL_CTL);
7401
7402 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7403 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7404 return;
7405
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007406 /*
7407 * Make sure we're not on PC8 state before disabling PC8, otherwise
7408 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7409 *
7410 * The other problem is that hsw_restore_lcpll() is called as part of
7411 * the runtime PM resume sequence, so we can't just call
7412 * gen6_gt_force_wake_get() because that function calls
7413 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7414 * while we are on the resume sequence. So to solve this problem we have
7415 * to call special forcewake code that doesn't touch runtime PM and
7416 * doesn't enable the forcewake delayed work.
7417 */
7418 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7419 if (dev_priv->uncore.forcewake_count++ == 0)
7420 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7421 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007422
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007423 if (val & LCPLL_POWER_DOWN_ALLOW) {
7424 val &= ~LCPLL_POWER_DOWN_ALLOW;
7425 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007426 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007427 }
7428
7429 val = I915_READ(D_COMP);
7430 val |= D_COMP_COMP_FORCE;
7431 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007432 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007433
7434 val = I915_READ(LCPLL_CTL);
7435 val &= ~LCPLL_PLL_DISABLE;
7436 I915_WRITE(LCPLL_CTL, val);
7437
7438 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7439 DRM_ERROR("LCPLL not locked yet\n");
7440
7441 if (val & LCPLL_CD_SOURCE_FCLK) {
7442 val = I915_READ(LCPLL_CTL);
7443 val &= ~LCPLL_CD_SOURCE_FCLK;
7444 I915_WRITE(LCPLL_CTL, val);
7445
7446 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7447 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7448 DRM_ERROR("Switching back to LCPLL failed\n");
7449 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007450
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007451 /* See the big comment above. */
7452 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7453 if (--dev_priv->uncore.forcewake_count == 0)
7454 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7455 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007456}
7457
Paulo Zanoni765dab62014-03-07 20:08:18 -03007458/*
7459 * Package states C8 and deeper are really deep PC states that can only be
7460 * reached when all the devices on the system allow it, so even if the graphics
7461 * device allows PC8+, it doesn't mean the system will actually get to these
7462 * states. Our driver only allows PC8+ when going into runtime PM.
7463 *
7464 * The requirements for PC8+ are that all the outputs are disabled, the power
7465 * well is disabled and most interrupts are disabled, and these are also
7466 * requirements for runtime PM. When these conditions are met, we manually do
7467 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7468 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7469 * hang the machine.
7470 *
7471 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7472 * the state of some registers, so when we come back from PC8+ we need to
7473 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7474 * need to take care of the registers kept by RC6. Notice that this happens even
7475 * if we don't put the device in PCI D3 state (which is what currently happens
7476 * because of the runtime PM support).
7477 *
7478 * For more, read "Display Sequences for Package C8" on the hardware
7479 * documentation.
7480 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007481void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007482{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007483 struct drm_device *dev = dev_priv->dev;
7484 uint32_t val;
7485
Paulo Zanonic67a4702013-08-19 13:18:09 -03007486 DRM_DEBUG_KMS("Enabling package C8+\n");
7487
Paulo Zanonic67a4702013-08-19 13:18:09 -03007488 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7489 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7490 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7491 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7492 }
7493
7494 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007495 hsw_disable_lcpll(dev_priv, true, true);
7496}
7497
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007498void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007499{
7500 struct drm_device *dev = dev_priv->dev;
7501 uint32_t val;
7502
Paulo Zanonic67a4702013-08-19 13:18:09 -03007503 DRM_DEBUG_KMS("Disabling package C8+\n");
7504
7505 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007506 lpt_init_pch_refclk(dev);
7507
7508 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7509 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7510 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7511 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7512 }
7513
7514 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007515}
7516
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007517static void snb_modeset_global_resources(struct drm_device *dev)
7518{
7519 modeset_update_crtc_power_domains(dev);
7520}
7521
Imre Deak4f074122013-10-16 17:25:51 +03007522static void haswell_modeset_global_resources(struct drm_device *dev)
7523{
Paulo Zanonida723562013-12-19 11:54:51 -02007524 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007525}
7526
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007527static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007528 int x, int y,
7529 struct drm_framebuffer *fb)
7530{
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007532
Paulo Zanoni566b7342013-11-25 15:27:08 -02007533 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007534 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02007535 intel_ddi_pll_enable(intel_crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007536
Daniel Vetter644cef32014-04-24 23:55:07 +02007537 intel_crtc->lowfreq_avail = false;
7538
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007539 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007540}
7541
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007542static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7543 struct intel_crtc_config *pipe_config)
7544{
7545 struct drm_device *dev = crtc->base.dev;
7546 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007547 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007548 uint32_t tmp;
7549
Imre Deakb5482bd2014-03-05 16:20:55 +02007550 if (!intel_display_power_enabled(dev_priv,
7551 POWER_DOMAIN_PIPE(crtc->pipe)))
7552 return false;
7553
Daniel Vettere143a212013-07-04 12:01:15 +02007554 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007555 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7556
Daniel Vettereccb1402013-05-22 00:50:22 +02007557 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7558 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7559 enum pipe trans_edp_pipe;
7560 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7561 default:
7562 WARN(1, "unknown pipe linked to edp transcoder\n");
7563 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7564 case TRANS_DDI_EDP_INPUT_A_ON:
7565 trans_edp_pipe = PIPE_A;
7566 break;
7567 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7568 trans_edp_pipe = PIPE_B;
7569 break;
7570 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7571 trans_edp_pipe = PIPE_C;
7572 break;
7573 }
7574
7575 if (trans_edp_pipe == crtc->pipe)
7576 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7577 }
7578
Imre Deakda7e29b2014-02-18 00:02:02 +02007579 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007580 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007581 return false;
7582
Daniel Vettereccb1402013-05-22 00:50:22 +02007583 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007584 if (!(tmp & PIPECONF_ENABLE))
7585 return false;
7586
Daniel Vetter88adfff2013-03-28 10:42:01 +01007587 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03007588 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01007589 * DDI E. So just check whether this pipe is wired to DDI E and whether
7590 * the PCH transcoder is on.
7591 */
Daniel Vettereccb1402013-05-22 00:50:22 +02007592 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01007593 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02007594 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01007595 pipe_config->has_pch_encoder = true;
7596
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007597 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7598 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7599 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007600
7601 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007602 }
7603
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007604 intel_get_pipe_timings(crtc, pipe_config);
7605
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007606 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007607 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007608 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007609
Jesse Barnese59150d2014-01-07 13:30:45 -08007610 if (IS_HASWELL(dev))
7611 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7612 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007613
Daniel Vetter6c49f242013-06-06 12:45:25 +02007614 pipe_config->pixel_multiplier = 1;
7615
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007616 return true;
7617}
7618
Jani Nikula1a915102013-10-16 12:34:48 +03007619static struct {
7620 int clock;
7621 u32 config;
7622} hdmi_audio_clock[] = {
7623 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7624 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7625 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7626 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7627 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7628 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7629 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7630 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7631 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7632 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7633};
7634
7635/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7636static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7637{
7638 int i;
7639
7640 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7641 if (mode->clock == hdmi_audio_clock[i].clock)
7642 break;
7643 }
7644
7645 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7646 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7647 i = 1;
7648 }
7649
7650 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7651 hdmi_audio_clock[i].clock,
7652 hdmi_audio_clock[i].config);
7653
7654 return hdmi_audio_clock[i].config;
7655}
7656
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007657static bool intel_eld_uptodate(struct drm_connector *connector,
7658 int reg_eldv, uint32_t bits_eldv,
7659 int reg_elda, uint32_t bits_elda,
7660 int reg_edid)
7661{
7662 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7663 uint8_t *eld = connector->eld;
7664 uint32_t i;
7665
7666 i = I915_READ(reg_eldv);
7667 i &= bits_eldv;
7668
7669 if (!eld[0])
7670 return !i;
7671
7672 if (!i)
7673 return false;
7674
7675 i = I915_READ(reg_elda);
7676 i &= ~bits_elda;
7677 I915_WRITE(reg_elda, i);
7678
7679 for (i = 0; i < eld[2]; i++)
7680 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7681 return false;
7682
7683 return true;
7684}
7685
Wu Fengguange0dac652011-09-05 14:25:34 +08007686static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007687 struct drm_crtc *crtc,
7688 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007689{
7690 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7691 uint8_t *eld = connector->eld;
7692 uint32_t eldv;
7693 uint32_t len;
7694 uint32_t i;
7695
7696 i = I915_READ(G4X_AUD_VID_DID);
7697
7698 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7699 eldv = G4X_ELDV_DEVCL_DEVBLC;
7700 else
7701 eldv = G4X_ELDV_DEVCTG;
7702
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007703 if (intel_eld_uptodate(connector,
7704 G4X_AUD_CNTL_ST, eldv,
7705 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7706 G4X_HDMIW_HDMIEDID))
7707 return;
7708
Wu Fengguange0dac652011-09-05 14:25:34 +08007709 i = I915_READ(G4X_AUD_CNTL_ST);
7710 i &= ~(eldv | G4X_ELD_ADDR);
7711 len = (i >> 9) & 0x1f; /* ELD buffer size */
7712 I915_WRITE(G4X_AUD_CNTL_ST, i);
7713
7714 if (!eld[0])
7715 return;
7716
7717 len = min_t(uint8_t, eld[2], len);
7718 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7719 for (i = 0; i < len; i++)
7720 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7721
7722 i = I915_READ(G4X_AUD_CNTL_ST);
7723 i |= eldv;
7724 I915_WRITE(G4X_AUD_CNTL_ST, i);
7725}
7726
Wang Xingchao83358c852012-08-16 22:43:37 +08007727static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007728 struct drm_crtc *crtc,
7729 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007730{
7731 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7732 uint8_t *eld = connector->eld;
Wang Xingchao83358c852012-08-16 22:43:37 +08007733 uint32_t eldv;
7734 uint32_t i;
7735 int len;
7736 int pipe = to_intel_crtc(crtc)->pipe;
7737 int tmp;
7738
7739 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7740 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7741 int aud_config = HSW_AUD_CFG(pipe);
7742 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7743
Wang Xingchao83358c852012-08-16 22:43:37 +08007744 /* Audio output enable */
7745 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7746 tmp = I915_READ(aud_cntrl_st2);
7747 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7748 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007749 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007750
Daniel Vetterc7905792014-04-16 16:56:09 +02007751 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007752
7753 /* Set ELD valid state */
7754 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007755 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007756 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7757 I915_WRITE(aud_cntrl_st2, tmp);
7758 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007759 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007760
7761 /* Enable HDMI mode */
7762 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007763 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007764 /* clear N_programing_enable and N_value_index */
7765 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7766 I915_WRITE(aud_config, tmp);
7767
7768 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7769
7770 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7771
7772 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7773 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7774 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7775 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007776 } else {
7777 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7778 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007779
7780 if (intel_eld_uptodate(connector,
7781 aud_cntrl_st2, eldv,
7782 aud_cntl_st, IBX_ELD_ADDRESS,
7783 hdmiw_hdmiedid))
7784 return;
7785
7786 i = I915_READ(aud_cntrl_st2);
7787 i &= ~eldv;
7788 I915_WRITE(aud_cntrl_st2, i);
7789
7790 if (!eld[0])
7791 return;
7792
7793 i = I915_READ(aud_cntl_st);
7794 i &= ~IBX_ELD_ADDRESS;
7795 I915_WRITE(aud_cntl_st, i);
7796 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7797 DRM_DEBUG_DRIVER("port num:%d\n", i);
7798
7799 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7800 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7801 for (i = 0; i < len; i++)
7802 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7803
7804 i = I915_READ(aud_cntrl_st2);
7805 i |= eldv;
7806 I915_WRITE(aud_cntrl_st2, i);
7807
7808}
7809
Wu Fengguange0dac652011-09-05 14:25:34 +08007810static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007811 struct drm_crtc *crtc,
7812 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007813{
7814 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7815 uint8_t *eld = connector->eld;
7816 uint32_t eldv;
7817 uint32_t i;
7818 int len;
7819 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007820 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007821 int aud_cntl_st;
7822 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007823 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007824
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007825 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007826 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7827 aud_config = IBX_AUD_CFG(pipe);
7828 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007829 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007830 } else if (IS_VALLEYVIEW(connector->dev)) {
7831 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7832 aud_config = VLV_AUD_CFG(pipe);
7833 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7834 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007835 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007836 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7837 aud_config = CPT_AUD_CFG(pipe);
7838 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007839 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007840 }
7841
Wang Xingchao9b138a82012-08-09 16:52:18 +08007842 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007843
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007844 if (IS_VALLEYVIEW(connector->dev)) {
7845 struct intel_encoder *intel_encoder;
7846 struct intel_digital_port *intel_dig_port;
7847
7848 intel_encoder = intel_attached_encoder(connector);
7849 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7850 i = intel_dig_port->port;
7851 } else {
7852 i = I915_READ(aud_cntl_st);
7853 i = (i >> 29) & DIP_PORT_SEL_MASK;
7854 /* DIP_Port_Select, 0x1 = PortB */
7855 }
7856
Wu Fengguange0dac652011-09-05 14:25:34 +08007857 if (!i) {
7858 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7859 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007860 eldv = IBX_ELD_VALIDB;
7861 eldv |= IBX_ELD_VALIDB << 4;
7862 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007863 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007864 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007865 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007866 }
7867
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007868 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7869 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7870 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007871 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007872 } else {
7873 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7874 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007875
7876 if (intel_eld_uptodate(connector,
7877 aud_cntrl_st2, eldv,
7878 aud_cntl_st, IBX_ELD_ADDRESS,
7879 hdmiw_hdmiedid))
7880 return;
7881
Wu Fengguange0dac652011-09-05 14:25:34 +08007882 i = I915_READ(aud_cntrl_st2);
7883 i &= ~eldv;
7884 I915_WRITE(aud_cntrl_st2, i);
7885
7886 if (!eld[0])
7887 return;
7888
Wu Fengguange0dac652011-09-05 14:25:34 +08007889 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007890 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007891 I915_WRITE(aud_cntl_st, i);
7892
7893 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7894 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7895 for (i = 0; i < len; i++)
7896 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7897
7898 i = I915_READ(aud_cntrl_st2);
7899 i |= eldv;
7900 I915_WRITE(aud_cntrl_st2, i);
7901}
7902
7903void intel_write_eld(struct drm_encoder *encoder,
7904 struct drm_display_mode *mode)
7905{
7906 struct drm_crtc *crtc = encoder->crtc;
7907 struct drm_connector *connector;
7908 struct drm_device *dev = encoder->dev;
7909 struct drm_i915_private *dev_priv = dev->dev_private;
7910
7911 connector = drm_select_eld(encoder, mode);
7912 if (!connector)
7913 return;
7914
7915 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7916 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03007917 connector->name,
Wu Fengguange0dac652011-09-05 14:25:34 +08007918 connector->encoder->base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +03007919 connector->encoder->name);
Wu Fengguange0dac652011-09-05 14:25:34 +08007920
7921 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7922
7923 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007924 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007925}
7926
Chris Wilson560b85b2010-08-07 11:01:38 +01007927static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7928{
7929 struct drm_device *dev = crtc->dev;
7930 struct drm_i915_private *dev_priv = dev->dev_private;
7931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson4b0e3332014-05-30 16:35:26 +03007932 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01007933
Chris Wilson4b0e3332014-05-30 16:35:26 +03007934 if (base != intel_crtc->cursor_base) {
Chris Wilson560b85b2010-08-07 11:01:38 +01007935 /* On these chipsets we can only modify the base whilst
7936 * the cursor is disabled.
7937 */
Chris Wilson4b0e3332014-05-30 16:35:26 +03007938 if (intel_crtc->cursor_cntl) {
7939 I915_WRITE(_CURACNTR, 0);
7940 POSTING_READ(_CURACNTR);
7941 intel_crtc->cursor_cntl = 0;
7942 }
7943
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007944 I915_WRITE(_CURABASE, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03007945 POSTING_READ(_CURABASE);
7946 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007947
Chris Wilson4b0e3332014-05-30 16:35:26 +03007948 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7949 cntl = 0;
7950 if (base)
7951 cntl = (CURSOR_ENABLE |
Chris Wilson560b85b2010-08-07 11:01:38 +01007952 CURSOR_GAMMA_ENABLE |
Chris Wilson4b0e3332014-05-30 16:35:26 +03007953 CURSOR_FORMAT_ARGB);
7954 if (intel_crtc->cursor_cntl != cntl) {
7955 I915_WRITE(_CURACNTR, cntl);
7956 POSTING_READ(_CURACNTR);
7957 intel_crtc->cursor_cntl = cntl;
7958 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007959}
7960
7961static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7962{
7963 struct drm_device *dev = crtc->dev;
7964 struct drm_i915_private *dev_priv = dev->dev_private;
7965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7966 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03007967 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01007968
Chris Wilson4b0e3332014-05-30 16:35:26 +03007969 cntl = 0;
7970 if (base) {
7971 cntl = MCURSOR_GAMMA_ENABLE;
7972 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307973 case 64:
7974 cntl |= CURSOR_MODE_64_ARGB_AX;
7975 break;
7976 case 128:
7977 cntl |= CURSOR_MODE_128_ARGB_AX;
7978 break;
7979 case 256:
7980 cntl |= CURSOR_MODE_256_ARGB_AX;
7981 break;
7982 default:
7983 WARN_ON(1);
7984 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01007985 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03007986 cntl |= pipe << 28; /* Connect to correct pipe */
Chris Wilson560b85b2010-08-07 11:01:38 +01007987 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03007988 if (intel_crtc->cursor_cntl != cntl) {
7989 I915_WRITE(CURCNTR(pipe), cntl);
7990 POSTING_READ(CURCNTR(pipe));
7991 intel_crtc->cursor_cntl = cntl;
7992 }
7993
Chris Wilson560b85b2010-08-07 11:01:38 +01007994 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007995 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007996 POSTING_READ(CURBASE(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01007997}
7998
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007999static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
8000{
8001 struct drm_device *dev = crtc->dev;
8002 struct drm_i915_private *dev_priv = dev->dev_private;
8003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8004 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008005 uint32_t cntl;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008006
Chris Wilson4b0e3332014-05-30 16:35:26 +03008007 cntl = 0;
8008 if (base) {
8009 cntl = MCURSOR_GAMMA_ENABLE;
8010 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308011 case 64:
8012 cntl |= CURSOR_MODE_64_ARGB_AX;
8013 break;
8014 case 128:
8015 cntl |= CURSOR_MODE_128_ARGB_AX;
8016 break;
8017 case 256:
8018 cntl |= CURSOR_MODE_256_ARGB_AX;
8019 break;
8020 default:
8021 WARN_ON(1);
8022 return;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008023 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008024 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008025 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8026 cntl |= CURSOR_PIPE_CSC_ENABLE;
8027
8028 if (intel_crtc->cursor_cntl != cntl) {
8029 I915_WRITE(CURCNTR(pipe), cntl);
8030 POSTING_READ(CURCNTR(pipe));
8031 intel_crtc->cursor_cntl = cntl;
8032 }
8033
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008034 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008035 I915_WRITE(CURBASE(pipe), base);
8036 POSTING_READ(CURBASE(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008037}
8038
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008039/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008040static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8041 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008042{
8043 struct drm_device *dev = crtc->dev;
8044 struct drm_i915_private *dev_priv = dev->dev_private;
8045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8046 int pipe = intel_crtc->pipe;
8047 int x = intel_crtc->cursor_x;
8048 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008049 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008050
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008051 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008052 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008053
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008054 if (x >= intel_crtc->config.pipe_src_w)
8055 base = 0;
8056
8057 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008058 base = 0;
8059
8060 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008061 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008062 base = 0;
8063
8064 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8065 x = -x;
8066 }
8067 pos |= x << CURSOR_X_SHIFT;
8068
8069 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008070 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008071 base = 0;
8072
8073 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8074 y = -y;
8075 }
8076 pos |= y << CURSOR_Y_SHIFT;
8077
Chris Wilson4b0e3332014-05-30 16:35:26 +03008078 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008079 return;
8080
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008081 I915_WRITE(CURPOS(pipe), pos);
8082
8083 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008084 ivb_update_cursor(crtc, base);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008085 else if (IS_845G(dev) || IS_I865G(dev))
8086 i845_update_cursor(crtc, base);
8087 else
8088 i9xx_update_cursor(crtc, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03008089 intel_crtc->cursor_base = base;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008090}
8091
Jesse Barnes79e53942008-11-07 14:24:08 -08008092static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00008093 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008094 uint32_t handle,
8095 uint32_t width, uint32_t height)
8096{
8097 struct drm_device *dev = crtc->dev;
8098 struct drm_i915_private *dev_priv = dev->dev_private;
8099 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00008100 struct drm_i915_gem_object *obj;
Chris Wilson64f962e2014-03-26 12:38:15 +00008101 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008102 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008103 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008104
Jesse Barnes79e53942008-11-07 14:24:08 -08008105 /* if we want to turn off the cursor ignore width and height */
8106 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008107 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008108 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00008109 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008110 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008111 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008112 }
8113
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308114 /* Check for which cursor types we support */
8115 if (!((width == 64 && height == 64) ||
8116 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8117 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8118 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08008119 return -EINVAL;
8120 }
8121
Chris Wilson05394f32010-11-08 19:18:58 +00008122 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00008123 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08008124 return -ENOENT;
8125
Chris Wilson05394f32010-11-08 19:18:58 +00008126 if (obj->base.size < width * height * 4) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008127 DRM_DEBUG_KMS("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10008128 ret = -ENOMEM;
8129 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008130 }
8131
Dave Airlie71acb5e2008-12-30 20:31:46 +10008132 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008133 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008134 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008135 unsigned alignment;
8136
Chris Wilsond9e86c02010-11-10 16:40:20 +00008137 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008138 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008139 ret = -EINVAL;
8140 goto fail_locked;
8141 }
8142
Chris Wilson693db182013-03-05 14:52:39 +00008143 /* Note that the w/a also requires 2 PTE of padding following
8144 * the bo. We currently fill all unused PTE with the shadow
8145 * page and so we should always have valid PTE following the
8146 * cursor preventing the VT-d warning.
8147 */
8148 alignment = 0;
8149 if (need_vtd_wa(dev))
8150 alignment = 64*1024;
8151
8152 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008153 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008154 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008155 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008156 }
8157
Chris Wilsond9e86c02010-11-10 16:40:20 +00008158 ret = i915_gem_object_put_fence(obj);
8159 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008160 DRM_DEBUG_KMS("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008161 goto fail_unpin;
8162 }
8163
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008164 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008165 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008166 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008167 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008168 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008169 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008170 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008171 }
Chris Wilson00731152014-05-21 12:42:56 +01008172 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008173 }
8174
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008175 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04008176 I915_WRITE(CURSIZE, (height << 12) | width);
8177
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008178 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008179 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008180 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008181 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00008182 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008183 }
Jesse Barnes80824002009-09-10 15:28:06 -07008184
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008185 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008186
Chris Wilson64f962e2014-03-26 12:38:15 +00008187 old_width = intel_crtc->cursor_width;
8188
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008189 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008190 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008191 intel_crtc->cursor_width = width;
8192 intel_crtc->cursor_height = height;
8193
Chris Wilson64f962e2014-03-26 12:38:15 +00008194 if (intel_crtc->active) {
8195 if (old_width != width)
8196 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03008197 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00008198 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008199
Jesse Barnes79e53942008-11-07 14:24:08 -08008200 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008201fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008202 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008203fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008204 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00008205fail:
Chris Wilson05394f32010-11-08 19:18:58 +00008206 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10008207 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008208}
8209
8210static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
8211{
Jesse Barnes79e53942008-11-07 14:24:08 -08008212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008213
Ville Syrjälä92e76c82013-10-21 19:01:58 +03008214 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
8215 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07008216
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03008217 if (intel_crtc->active)
8218 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08008219
8220 return 0;
8221}
8222
Jesse Barnes79e53942008-11-07 14:24:08 -08008223static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008224 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008225{
James Simmons72034252010-08-03 01:33:19 +01008226 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008228
James Simmons72034252010-08-03 01:33:19 +01008229 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008230 intel_crtc->lut_r[i] = red[i] >> 8;
8231 intel_crtc->lut_g[i] = green[i] >> 8;
8232 intel_crtc->lut_b[i] = blue[i] >> 8;
8233 }
8234
8235 intel_crtc_load_lut(crtc);
8236}
8237
Jesse Barnes79e53942008-11-07 14:24:08 -08008238/* VESA 640x480x72Hz mode to set on the pipe */
8239static struct drm_display_mode load_detect_mode = {
8240 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8241 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8242};
8243
Daniel Vettera8bb6812014-02-10 18:00:39 +01008244struct drm_framebuffer *
8245__intel_framebuffer_create(struct drm_device *dev,
8246 struct drm_mode_fb_cmd2 *mode_cmd,
8247 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008248{
8249 struct intel_framebuffer *intel_fb;
8250 int ret;
8251
8252 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8253 if (!intel_fb) {
8254 drm_gem_object_unreference_unlocked(&obj->base);
8255 return ERR_PTR(-ENOMEM);
8256 }
8257
8258 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008259 if (ret)
8260 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008261
8262 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008263err:
8264 drm_gem_object_unreference_unlocked(&obj->base);
8265 kfree(intel_fb);
8266
8267 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008268}
8269
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008270static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008271intel_framebuffer_create(struct drm_device *dev,
8272 struct drm_mode_fb_cmd2 *mode_cmd,
8273 struct drm_i915_gem_object *obj)
8274{
8275 struct drm_framebuffer *fb;
8276 int ret;
8277
8278 ret = i915_mutex_lock_interruptible(dev);
8279 if (ret)
8280 return ERR_PTR(ret);
8281 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8282 mutex_unlock(&dev->struct_mutex);
8283
8284 return fb;
8285}
8286
Chris Wilsond2dff872011-04-19 08:36:26 +01008287static u32
8288intel_framebuffer_pitch_for_width(int width, int bpp)
8289{
8290 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8291 return ALIGN(pitch, 64);
8292}
8293
8294static u32
8295intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8296{
8297 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8298 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8299}
8300
8301static struct drm_framebuffer *
8302intel_framebuffer_create_for_mode(struct drm_device *dev,
8303 struct drm_display_mode *mode,
8304 int depth, int bpp)
8305{
8306 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008307 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008308
8309 obj = i915_gem_alloc_object(dev,
8310 intel_framebuffer_size_for_mode(mode, bpp));
8311 if (obj == NULL)
8312 return ERR_PTR(-ENOMEM);
8313
8314 mode_cmd.width = mode->hdisplay;
8315 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008316 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8317 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008318 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008319
8320 return intel_framebuffer_create(dev, &mode_cmd, obj);
8321}
8322
8323static struct drm_framebuffer *
8324mode_fits_in_fbdev(struct drm_device *dev,
8325 struct drm_display_mode *mode)
8326{
Daniel Vetter4520f532013-10-09 09:18:51 +02008327#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008328 struct drm_i915_private *dev_priv = dev->dev_private;
8329 struct drm_i915_gem_object *obj;
8330 struct drm_framebuffer *fb;
8331
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008332 if (!dev_priv->fbdev)
8333 return NULL;
8334
8335 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008336 return NULL;
8337
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008338 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008339 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008340
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008341 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008342 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8343 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008344 return NULL;
8345
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008346 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008347 return NULL;
8348
8349 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008350#else
8351 return NULL;
8352#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008353}
8354
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008355bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008356 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008357 struct intel_load_detect_pipe *old,
8358 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008359{
8360 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008361 struct intel_encoder *intel_encoder =
8362 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008363 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008364 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008365 struct drm_crtc *crtc = NULL;
8366 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008367 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008368 struct drm_mode_config *config = &dev->mode_config;
8369 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008370
Chris Wilsond2dff872011-04-19 08:36:26 +01008371 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008372 connector->base.id, connector->name,
Jani Nikula8e329a02014-06-03 14:56:21 +03008373 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008374
Rob Clark51fd3712013-11-19 12:10:12 -05008375 drm_modeset_acquire_init(ctx, 0);
8376
8377retry:
8378 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8379 if (ret)
8380 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008381
Jesse Barnes79e53942008-11-07 14:24:08 -08008382 /*
8383 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008384 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008385 * - if the connector already has an assigned crtc, use it (but make
8386 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008387 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008388 * - try to find the first unused crtc that can drive this connector,
8389 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008390 */
8391
8392 /* See if we already have a CRTC for this connector */
8393 if (encoder->crtc) {
8394 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008395
Rob Clark51fd3712013-11-19 12:10:12 -05008396 ret = drm_modeset_lock(&crtc->mutex, ctx);
8397 if (ret)
8398 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008399
Daniel Vetter24218aa2012-08-12 19:27:11 +02008400 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008401 old->load_detect_temp = false;
8402
8403 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008404 if (connector->dpms != DRM_MODE_DPMS_ON)
8405 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008406
Chris Wilson71731882011-04-19 23:10:58 +01008407 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008408 }
8409
8410 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008411 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008412 i++;
8413 if (!(encoder->possible_crtcs & (1 << i)))
8414 continue;
8415 if (!possible_crtc->enabled) {
8416 crtc = possible_crtc;
8417 break;
8418 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008419 }
8420
8421 /*
8422 * If we didn't find an unused CRTC, don't use any.
8423 */
8424 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008425 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008426 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008427 }
8428
Rob Clark51fd3712013-11-19 12:10:12 -05008429 ret = drm_modeset_lock(&crtc->mutex, ctx);
8430 if (ret)
8431 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008432 intel_encoder->new_crtc = to_intel_crtc(crtc);
8433 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008434
8435 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008436 intel_crtc->new_enabled = true;
8437 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008438 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008439 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008440 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008441
Chris Wilson64927112011-04-20 07:25:26 +01008442 if (!mode)
8443 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008444
Chris Wilsond2dff872011-04-19 08:36:26 +01008445 /* We need a framebuffer large enough to accommodate all accesses
8446 * that the plane may generate whilst we perform load detection.
8447 * We can not rely on the fbcon either being present (we get called
8448 * during its initialisation to detect all boot displays, or it may
8449 * not even exist) or that it is large enough to satisfy the
8450 * requested mode.
8451 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008452 fb = mode_fits_in_fbdev(dev, mode);
8453 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008454 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008455 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8456 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008457 } else
8458 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008459 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008460 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008461 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008462 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008463
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008464 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008465 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008466 if (old->release_fb)
8467 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008468 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008469 }
Chris Wilson71731882011-04-19 23:10:58 +01008470
Jesse Barnes79e53942008-11-07 14:24:08 -08008471 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008472 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008473 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008474
8475 fail:
8476 intel_crtc->new_enabled = crtc->enabled;
8477 if (intel_crtc->new_enabled)
8478 intel_crtc->new_config = &intel_crtc->config;
8479 else
8480 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008481fail_unlock:
8482 if (ret == -EDEADLK) {
8483 drm_modeset_backoff(ctx);
8484 goto retry;
8485 }
8486
8487 drm_modeset_drop_locks(ctx);
8488 drm_modeset_acquire_fini(ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008489
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008490 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008491}
8492
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008493void intel_release_load_detect_pipe(struct drm_connector *connector,
Rob Clark51fd3712013-11-19 12:10:12 -05008494 struct intel_load_detect_pipe *old,
8495 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008496{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008497 struct intel_encoder *intel_encoder =
8498 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008499 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008500 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008502
Chris Wilsond2dff872011-04-19 08:36:26 +01008503 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008504 connector->base.id, connector->name,
Jani Nikula8e329a02014-06-03 14:56:21 +03008505 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008506
Chris Wilson8261b192011-04-19 23:18:09 +01008507 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008508 to_intel_connector(connector)->new_encoder = NULL;
8509 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008510 intel_crtc->new_enabled = false;
8511 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008512 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008513
Daniel Vetter36206362012-12-10 20:42:17 +01008514 if (old->release_fb) {
8515 drm_framebuffer_unregister_private(old->release_fb);
8516 drm_framebuffer_unreference(old->release_fb);
8517 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008518
Rob Clark51fd3712013-11-19 12:10:12 -05008519 goto unlock;
Chris Wilson0622a532011-04-21 09:32:11 +01008520 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008521 }
8522
Eric Anholtc751ce42010-03-25 11:48:48 -07008523 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008524 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8525 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01008526
Rob Clark51fd3712013-11-19 12:10:12 -05008527unlock:
8528 drm_modeset_drop_locks(ctx);
8529 drm_modeset_acquire_fini(ctx);
Jesse Barnes79e53942008-11-07 14:24:08 -08008530}
8531
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008532static int i9xx_pll_refclk(struct drm_device *dev,
8533 const struct intel_crtc_config *pipe_config)
8534{
8535 struct drm_i915_private *dev_priv = dev->dev_private;
8536 u32 dpll = pipe_config->dpll_hw_state.dpll;
8537
8538 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008539 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008540 else if (HAS_PCH_SPLIT(dev))
8541 return 120000;
8542 else if (!IS_GEN2(dev))
8543 return 96000;
8544 else
8545 return 48000;
8546}
8547
Jesse Barnes79e53942008-11-07 14:24:08 -08008548/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008549static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8550 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008551{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008552 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008553 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008554 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008555 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008556 u32 fp;
8557 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008558 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008559
8560 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008561 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008562 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008563 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008564
8565 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008566 if (IS_PINEVIEW(dev)) {
8567 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8568 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008569 } else {
8570 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8571 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8572 }
8573
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008574 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008575 if (IS_PINEVIEW(dev))
8576 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8577 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008578 else
8579 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008580 DPLL_FPA01_P1_POST_DIV_SHIFT);
8581
8582 switch (dpll & DPLL_MODE_MASK) {
8583 case DPLLB_MODE_DAC_SERIAL:
8584 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8585 5 : 10;
8586 break;
8587 case DPLLB_MODE_LVDS:
8588 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8589 7 : 14;
8590 break;
8591 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008592 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008593 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008594 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008595 }
8596
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008597 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008598 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008599 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008600 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008601 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008602 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008603 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008604
8605 if (is_lvds) {
8606 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8607 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008608
8609 if (lvds & LVDS_CLKB_POWER_UP)
8610 clock.p2 = 7;
8611 else
8612 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008613 } else {
8614 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8615 clock.p1 = 2;
8616 else {
8617 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8618 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8619 }
8620 if (dpll & PLL_P2_DIVIDE_BY_4)
8621 clock.p2 = 4;
8622 else
8623 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008624 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008625
8626 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008627 }
8628
Ville Syrjälä18442d02013-09-13 16:00:08 +03008629 /*
8630 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008631 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008632 * encoder's get_config() function.
8633 */
8634 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008635}
8636
Ville Syrjälä6878da02013-09-13 15:59:11 +03008637int intel_dotclock_calculate(int link_freq,
8638 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008639{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008640 /*
8641 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008642 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008643 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008644 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008645 *
8646 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008647 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008648 */
8649
Ville Syrjälä6878da02013-09-13 15:59:11 +03008650 if (!m_n->link_n)
8651 return 0;
8652
8653 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8654}
8655
Ville Syrjälä18442d02013-09-13 16:00:08 +03008656static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8657 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008658{
8659 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008660
8661 /* read out port_clock from the DPLL */
8662 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008663
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008664 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008665 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008666 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008667 * agree once we know their relationship in the encoder's
8668 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008669 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008670 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008671 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8672 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008673}
8674
8675/** Returns the currently programmed mode of the given pipe. */
8676struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8677 struct drm_crtc *crtc)
8678{
Jesse Barnes548f2452011-02-17 10:40:53 -08008679 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008680 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008681 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008682 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008683 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008684 int htot = I915_READ(HTOTAL(cpu_transcoder));
8685 int hsync = I915_READ(HSYNC(cpu_transcoder));
8686 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8687 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008688 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008689
8690 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8691 if (!mode)
8692 return NULL;
8693
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008694 /*
8695 * Construct a pipe_config sufficient for getting the clock info
8696 * back out of crtc_clock_get.
8697 *
8698 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8699 * to use a real value here instead.
8700 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008701 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008702 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008703 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8704 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8705 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008706 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8707
Ville Syrjälä773ae032013-09-23 17:48:20 +03008708 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008709 mode->hdisplay = (htot & 0xffff) + 1;
8710 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8711 mode->hsync_start = (hsync & 0xffff) + 1;
8712 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8713 mode->vdisplay = (vtot & 0xffff) + 1;
8714 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8715 mode->vsync_start = (vsync & 0xffff) + 1;
8716 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8717
8718 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008719
8720 return mode;
8721}
8722
Daniel Vetter3dec0092010-08-20 21:40:52 +02008723static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008724{
8725 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008726 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8728 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008729 int dpll_reg = DPLL(pipe);
8730 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008731
Eric Anholtbad720f2009-10-22 16:11:14 -07008732 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008733 return;
8734
8735 if (!dev_priv->lvds_downclock_avail)
8736 return;
8737
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008738 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008739 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008740 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008741
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008742 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008743
8744 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8745 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008746 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008747
Jesse Barnes652c3932009-08-17 13:31:43 -07008748 dpll = I915_READ(dpll_reg);
8749 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008750 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008751 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008752}
8753
8754static void intel_decrease_pllclock(struct drm_crtc *crtc)
8755{
8756 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008757 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008759
Eric Anholtbad720f2009-10-22 16:11:14 -07008760 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008761 return;
8762
8763 if (!dev_priv->lvds_downclock_avail)
8764 return;
8765
8766 /*
8767 * Since this is called by a timer, we should never get here in
8768 * the manual case.
8769 */
8770 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008771 int pipe = intel_crtc->pipe;
8772 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008773 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008774
Zhao Yakui44d98a62009-10-09 11:39:40 +08008775 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008776
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008777 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008778
Chris Wilson074b5e12012-05-02 12:07:06 +01008779 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008780 dpll |= DISPLAY_RATE_SELECT_FPA1;
8781 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008782 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008783 dpll = I915_READ(dpll_reg);
8784 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008785 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008786 }
8787
8788}
8789
Chris Wilsonf047e392012-07-21 12:31:41 +01008790void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008791{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008792 struct drm_i915_private *dev_priv = dev->dev_private;
8793
Chris Wilsonf62a0072014-02-21 17:55:39 +00008794 if (dev_priv->mm.busy)
8795 return;
8796
Paulo Zanoni43694d62014-03-07 20:08:08 -03008797 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008798 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008799 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008800}
8801
8802void intel_mark_idle(struct drm_device *dev)
8803{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008804 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008805 struct drm_crtc *crtc;
8806
Chris Wilsonf62a0072014-02-21 17:55:39 +00008807 if (!dev_priv->mm.busy)
8808 return;
8809
8810 dev_priv->mm.busy = false;
8811
Jani Nikulad330a952014-01-21 11:24:25 +02008812 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008813 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008814
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008815 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008816 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008817 continue;
8818
8819 intel_decrease_pllclock(crtc);
8820 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008821
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008822 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008823 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008824
8825out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008826 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008827}
8828
Chris Wilsonc65355b2013-06-06 16:53:41 -03008829void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01008830 struct intel_engine_cs *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008831{
8832 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008833 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008834
Jani Nikulad330a952014-01-21 11:24:25 +02008835 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07008836 return;
8837
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008838 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008839 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -07008840 continue;
8841
Matt Roperf4510a22014-04-01 15:22:40 -07008842 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
Chris Wilsonc65355b2013-06-06 16:53:41 -03008843 continue;
8844
8845 intel_increase_pllclock(crtc);
8846 if (ring && intel_fbc_enabled(dev))
8847 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008848 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008849}
8850
Jesse Barnes79e53942008-11-07 14:24:08 -08008851static void intel_crtc_destroy(struct drm_crtc *crtc)
8852{
8853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008854 struct drm_device *dev = crtc->dev;
8855 struct intel_unpin_work *work;
8856 unsigned long flags;
8857
8858 spin_lock_irqsave(&dev->event_lock, flags);
8859 work = intel_crtc->unpin_work;
8860 intel_crtc->unpin_work = NULL;
8861 spin_unlock_irqrestore(&dev->event_lock, flags);
8862
8863 if (work) {
8864 cancel_work_sync(&work->work);
8865 kfree(work);
8866 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008867
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008868 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8869
Jesse Barnes79e53942008-11-07 14:24:08 -08008870 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008871
Jesse Barnes79e53942008-11-07 14:24:08 -08008872 kfree(intel_crtc);
8873}
8874
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008875static void intel_unpin_work_fn(struct work_struct *__work)
8876{
8877 struct intel_unpin_work *work =
8878 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008879 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008880
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008881 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008882 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008883 drm_gem_object_unreference(&work->pending_flip_obj->base);
8884 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008885
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008886 intel_update_fbc(dev);
8887 mutex_unlock(&dev->struct_mutex);
8888
8889 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8890 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8891
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008892 kfree(work);
8893}
8894
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008895static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008896 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008897{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008898 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8900 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008901 unsigned long flags;
8902
8903 /* Ignore early vblank irqs */
8904 if (intel_crtc == NULL)
8905 return;
8906
8907 spin_lock_irqsave(&dev->event_lock, flags);
8908 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008909
8910 /* Ensure we don't miss a work->pending update ... */
8911 smp_rmb();
8912
8913 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008914 spin_unlock_irqrestore(&dev->event_lock, flags);
8915 return;
8916 }
8917
Chris Wilsone7d841c2012-12-03 11:36:30 +00008918 /* and that the unpin work is consistent wrt ->pending. */
8919 smp_rmb();
8920
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008921 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008922
Rob Clark45a066e2012-10-08 14:50:40 -05008923 if (work->event)
8924 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008925
Daniel Vetter87b6b102014-05-15 15:33:46 +02008926 drm_crtc_vblank_put(crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008927
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008928 spin_unlock_irqrestore(&dev->event_lock, flags);
8929
Daniel Vetter2c10d572012-12-20 21:24:07 +01008930 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008931
8932 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008933
8934 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008935}
8936
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008937void intel_finish_page_flip(struct drm_device *dev, int pipe)
8938{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008939 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008940 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8941
Mario Kleiner49b14a52010-12-09 07:00:07 +01008942 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008943}
8944
8945void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8946{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008947 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008948 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8949
Mario Kleiner49b14a52010-12-09 07:00:07 +01008950 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008951}
8952
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008953/* Is 'a' after or equal to 'b'? */
8954static bool g4x_flip_count_after_eq(u32 a, u32 b)
8955{
8956 return !((a - b) & 0x80000000);
8957}
8958
8959static bool page_flip_finished(struct intel_crtc *crtc)
8960{
8961 struct drm_device *dev = crtc->base.dev;
8962 struct drm_i915_private *dev_priv = dev->dev_private;
8963
8964 /*
8965 * The relevant registers doen't exist on pre-ctg.
8966 * As the flip done interrupt doesn't trigger for mmio
8967 * flips on gmch platforms, a flip count check isn't
8968 * really needed there. But since ctg has the registers,
8969 * include it in the check anyway.
8970 */
8971 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8972 return true;
8973
8974 /*
8975 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8976 * used the same base address. In that case the mmio flip might
8977 * have completed, but the CS hasn't even executed the flip yet.
8978 *
8979 * A flip count check isn't enough as the CS might have updated
8980 * the base address just after start of vblank, but before we
8981 * managed to process the interrupt. This means we'd complete the
8982 * CS flip too soon.
8983 *
8984 * Combining both checks should get us a good enough result. It may
8985 * still happen that the CS flip has been executed, but has not
8986 * yet actually completed. But in case the base address is the same
8987 * anyway, we don't really care.
8988 */
8989 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8990 crtc->unpin_work->gtt_offset &&
8991 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8992 crtc->unpin_work->flip_count);
8993}
8994
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008995void intel_prepare_page_flip(struct drm_device *dev, int plane)
8996{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008997 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008998 struct intel_crtc *intel_crtc =
8999 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9000 unsigned long flags;
9001
Chris Wilsone7d841c2012-12-03 11:36:30 +00009002 /* NB: An MMIO update of the plane base pointer will also
9003 * generate a page-flip completion irq, i.e. every modeset
9004 * is also accompanied by a spurious intel_prepare_page_flip().
9005 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009006 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009007 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009008 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009009 spin_unlock_irqrestore(&dev->event_lock, flags);
9010}
9011
Robin Schroereba905b2014-05-18 02:24:50 +02009012static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009013{
9014 /* Ensure that the work item is consistent when activating it ... */
9015 smp_wmb();
9016 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9017 /* and that it is marked active as soon as the irq could fire. */
9018 smp_wmb();
9019}
9020
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009021static int intel_gen2_queue_flip(struct drm_device *dev,
9022 struct drm_crtc *crtc,
9023 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009024 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009025 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009026 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009027{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009029 u32 flip_mask;
9030 int ret;
9031
Daniel Vetter6d90c952012-04-26 23:28:05 +02009032 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009033 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009034 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009035
9036 /* Can't queue multiple flips, so wait for the previous
9037 * one to finish before executing the next.
9038 */
9039 if (intel_crtc->plane)
9040 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9041 else
9042 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009043 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9044 intel_ring_emit(ring, MI_NOOP);
9045 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9046 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9047 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009048 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009049 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009050
9051 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009052 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009053 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009054}
9055
9056static int intel_gen3_queue_flip(struct drm_device *dev,
9057 struct drm_crtc *crtc,
9058 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009059 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009060 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009061 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009062{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009064 u32 flip_mask;
9065 int ret;
9066
Daniel Vetter6d90c952012-04-26 23:28:05 +02009067 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009068 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009069 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009070
9071 if (intel_crtc->plane)
9072 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9073 else
9074 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009075 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9076 intel_ring_emit(ring, MI_NOOP);
9077 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9078 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9079 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009080 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009081 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009082
Chris Wilsone7d841c2012-12-03 11:36:30 +00009083 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009084 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009085 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009086}
9087
9088static int intel_gen4_queue_flip(struct drm_device *dev,
9089 struct drm_crtc *crtc,
9090 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009091 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009092 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009093 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009094{
9095 struct drm_i915_private *dev_priv = dev->dev_private;
9096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9097 uint32_t pf, pipesrc;
9098 int ret;
9099
Daniel Vetter6d90c952012-04-26 23:28:05 +02009100 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009101 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009102 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009103
9104 /* i965+ uses the linear or tiled offsets from the
9105 * Display Registers (which do not change across a page-flip)
9106 * so we need only reprogram the base address.
9107 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009108 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9109 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9110 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009111 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009112 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009113
9114 /* XXX Enabling the panel-fitter across page-flip is so far
9115 * untested on non-native modes, so ignore it for now.
9116 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9117 */
9118 pf = 0;
9119 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009120 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009121
9122 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009123 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009124 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009125}
9126
9127static int intel_gen6_queue_flip(struct drm_device *dev,
9128 struct drm_crtc *crtc,
9129 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009130 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009131 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009132 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009133{
9134 struct drm_i915_private *dev_priv = dev->dev_private;
9135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9136 uint32_t pf, pipesrc;
9137 int ret;
9138
Daniel Vetter6d90c952012-04-26 23:28:05 +02009139 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009140 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009141 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009142
Daniel Vetter6d90c952012-04-26 23:28:05 +02009143 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9144 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9145 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009146 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009147
Chris Wilson99d9acd2012-04-17 20:37:00 +01009148 /* Contrary to the suggestions in the documentation,
9149 * "Enable Panel Fitter" does not seem to be required when page
9150 * flipping with a non-native mode, and worse causes a normal
9151 * modeset to fail.
9152 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9153 */
9154 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009155 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009156 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009157
9158 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009159 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009160 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009161}
9162
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009163static int intel_gen7_queue_flip(struct drm_device *dev,
9164 struct drm_crtc *crtc,
9165 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009166 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009167 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009168 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009169{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009171 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009172 int len, ret;
9173
Robin Schroereba905b2014-05-18 02:24:50 +02009174 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009175 case PLANE_A:
9176 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9177 break;
9178 case PLANE_B:
9179 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9180 break;
9181 case PLANE_C:
9182 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9183 break;
9184 default:
9185 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009186 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009187 }
9188
Chris Wilsonffe74d72013-08-26 20:58:12 +01009189 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009190 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009191 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009192 /*
9193 * On Gen 8, SRM is now taking an extra dword to accommodate
9194 * 48bits addresses, and we need a NOOP for the batch size to
9195 * stay even.
9196 */
9197 if (IS_GEN8(dev))
9198 len += 2;
9199 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009200
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009201 /*
9202 * BSpec MI_DISPLAY_FLIP for IVB:
9203 * "The full packet must be contained within the same cache line."
9204 *
9205 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9206 * cacheline, if we ever start emitting more commands before
9207 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9208 * then do the cacheline alignment, and finally emit the
9209 * MI_DISPLAY_FLIP.
9210 */
9211 ret = intel_ring_cacheline_align(ring);
9212 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009213 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009214
Chris Wilsonffe74d72013-08-26 20:58:12 +01009215 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009216 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009217 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009218
Chris Wilsonffe74d72013-08-26 20:58:12 +01009219 /* Unmask the flip-done completion message. Note that the bspec says that
9220 * we should do this for both the BCS and RCS, and that we must not unmask
9221 * more than one flip event at any time (or ensure that one flip message
9222 * can be sent by waiting for flip-done prior to queueing new flips).
9223 * Experimentation says that BCS works despite DERRMR masking all
9224 * flip-done completion events and that unmasking all planes at once
9225 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9226 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9227 */
9228 if (ring->id == RCS) {
9229 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9230 intel_ring_emit(ring, DERRMR);
9231 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9232 DERRMR_PIPEB_PRI_FLIP_DONE |
9233 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009234 if (IS_GEN8(dev))
9235 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9236 MI_SRM_LRM_GLOBAL_GTT);
9237 else
9238 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9239 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009240 intel_ring_emit(ring, DERRMR);
9241 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009242 if (IS_GEN8(dev)) {
9243 intel_ring_emit(ring, 0);
9244 intel_ring_emit(ring, MI_NOOP);
9245 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009246 }
9247
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009248 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009249 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009250 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009251 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009252
9253 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009254 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009255 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009256}
9257
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009258static int intel_default_queue_flip(struct drm_device *dev,
9259 struct drm_crtc *crtc,
9260 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009261 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009262 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009263 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009264{
9265 return -ENODEV;
9266}
9267
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009268static int intel_crtc_page_flip(struct drm_crtc *crtc,
9269 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009270 struct drm_pending_vblank_event *event,
9271 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009272{
9273 struct drm_device *dev = crtc->dev;
9274 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009275 struct drm_framebuffer *old_fb = crtc->primary->fb;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009276 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9278 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009279 struct intel_engine_cs *ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009280 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01009281 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009282
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009283 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009284 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009285 return -EINVAL;
9286
9287 /*
9288 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9289 * Note that pitch changes could also affect these register.
9290 */
9291 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009292 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9293 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009294 return -EINVAL;
9295
Chris Wilsonf900db42014-02-20 09:26:13 +00009296 if (i915_terminally_wedged(&dev_priv->gpu_error))
9297 goto out_hang;
9298
Daniel Vetterb14c5672013-09-19 12:18:32 +02009299 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009300 if (work == NULL)
9301 return -ENOMEM;
9302
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009303 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009304 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009305 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009306 INIT_WORK(&work->work, intel_unpin_work_fn);
9307
Daniel Vetter87b6b102014-05-15 15:33:46 +02009308 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009309 if (ret)
9310 goto free_work;
9311
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009312 /* We borrow the event spin lock for protecting unpin_work */
9313 spin_lock_irqsave(&dev->event_lock, flags);
9314 if (intel_crtc->unpin_work) {
9315 spin_unlock_irqrestore(&dev->event_lock, flags);
9316 kfree(work);
Daniel Vetter87b6b102014-05-15 15:33:46 +02009317 drm_crtc_vblank_put(crtc);
Chris Wilson468f0b42010-05-27 13:18:13 +01009318
9319 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009320 return -EBUSY;
9321 }
9322 intel_crtc->unpin_work = work;
9323 spin_unlock_irqrestore(&dev->event_lock, flags);
9324
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009325 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9326 flush_workqueue(dev_priv->wq);
9327
Chris Wilson79158102012-05-23 11:13:58 +01009328 ret = i915_mutex_lock_interruptible(dev);
9329 if (ret)
9330 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009331
Jesse Barnes75dfca82010-02-10 15:09:44 -08009332 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009333 drm_gem_object_reference(&work->old_fb_obj->base);
9334 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009335
Matt Roperf4510a22014-04-01 15:22:40 -07009336 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009337
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009338 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009339
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01009340 work->enable_stall_check = true;
9341
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009342 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009343 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009344
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009345 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
9346 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
9347
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009348 if (IS_VALLEYVIEW(dev)) {
9349 ring = &dev_priv->ring[BCS];
9350 } else if (INTEL_INFO(dev)->gen >= 7) {
9351 ring = obj->ring;
9352 if (ring == NULL || ring->id != RCS)
9353 ring = &dev_priv->ring[BCS];
9354 } else {
9355 ring = &dev_priv->ring[RCS];
9356 }
9357
9358 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009359 if (ret)
9360 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009361
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009362 work->gtt_offset =
9363 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9364
9365 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
9366 if (ret)
9367 goto cleanup_unpin;
9368
Chris Wilson7782de32011-07-08 12:22:41 +01009369 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009370 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009371 mutex_unlock(&dev->struct_mutex);
9372
Jesse Barnese5510fa2010-07-01 16:48:37 -07009373 trace_i915_flip_request(intel_crtc->plane, obj);
9374
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009375 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009376
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009377cleanup_unpin:
9378 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009379cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009380 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009381 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009382 drm_gem_object_unreference(&work->old_fb_obj->base);
9383 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009384 mutex_unlock(&dev->struct_mutex);
9385
Chris Wilson79158102012-05-23 11:13:58 +01009386cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01009387 spin_lock_irqsave(&dev->event_lock, flags);
9388 intel_crtc->unpin_work = NULL;
9389 spin_unlock_irqrestore(&dev->event_lock, flags);
9390
Daniel Vetter87b6b102014-05-15 15:33:46 +02009391 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009392free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009393 kfree(work);
9394
Chris Wilsonf900db42014-02-20 09:26:13 +00009395 if (ret == -EIO) {
9396out_hang:
9397 intel_crtc_wait_for_pending_flips(crtc);
9398 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9399 if (ret == 0 && event)
9400 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9401 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009402 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009403}
9404
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009405static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009406 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9407 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009408};
9409
Daniel Vetter9a935852012-07-05 22:34:27 +02009410/**
9411 * intel_modeset_update_staged_output_state
9412 *
9413 * Updates the staged output configuration state, e.g. after we've read out the
9414 * current hw state.
9415 */
9416static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9417{
Ville Syrjälä76688512014-01-10 11:28:06 +02009418 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009419 struct intel_encoder *encoder;
9420 struct intel_connector *connector;
9421
9422 list_for_each_entry(connector, &dev->mode_config.connector_list,
9423 base.head) {
9424 connector->new_encoder =
9425 to_intel_encoder(connector->base.encoder);
9426 }
9427
9428 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9429 base.head) {
9430 encoder->new_crtc =
9431 to_intel_crtc(encoder->base.crtc);
9432 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009433
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009434 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009435 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009436
9437 if (crtc->new_enabled)
9438 crtc->new_config = &crtc->config;
9439 else
9440 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009441 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009442}
9443
9444/**
9445 * intel_modeset_commit_output_state
9446 *
9447 * This function copies the stage display pipe configuration to the real one.
9448 */
9449static void intel_modeset_commit_output_state(struct drm_device *dev)
9450{
Ville Syrjälä76688512014-01-10 11:28:06 +02009451 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009452 struct intel_encoder *encoder;
9453 struct intel_connector *connector;
9454
9455 list_for_each_entry(connector, &dev->mode_config.connector_list,
9456 base.head) {
9457 connector->base.encoder = &connector->new_encoder->base;
9458 }
9459
9460 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9461 base.head) {
9462 encoder->base.crtc = &encoder->new_crtc->base;
9463 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009464
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009465 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009466 crtc->base.enabled = crtc->new_enabled;
9467 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009468}
9469
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009470static void
Robin Schroereba905b2014-05-18 02:24:50 +02009471connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009472 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009473{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009474 int bpp = pipe_config->pipe_bpp;
9475
9476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9477 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03009478 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009479
9480 /* Don't use an invalid EDID bpc value */
9481 if (connector->base.display_info.bpc &&
9482 connector->base.display_info.bpc * 3 < bpp) {
9483 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9484 bpp, connector->base.display_info.bpc*3);
9485 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9486 }
9487
9488 /* Clamp bpp to 8 on screens without EDID 1.4 */
9489 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9490 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9491 bpp);
9492 pipe_config->pipe_bpp = 24;
9493 }
9494}
9495
9496static int
9497compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9498 struct drm_framebuffer *fb,
9499 struct intel_crtc_config *pipe_config)
9500{
9501 struct drm_device *dev = crtc->base.dev;
9502 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009503 int bpp;
9504
Daniel Vetterd42264b2013-03-28 16:38:08 +01009505 switch (fb->pixel_format) {
9506 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009507 bpp = 8*3; /* since we go through a colormap */
9508 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009509 case DRM_FORMAT_XRGB1555:
9510 case DRM_FORMAT_ARGB1555:
9511 /* checked in intel_framebuffer_init already */
9512 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9513 return -EINVAL;
9514 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009515 bpp = 6*3; /* min is 18bpp */
9516 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009517 case DRM_FORMAT_XBGR8888:
9518 case DRM_FORMAT_ABGR8888:
9519 /* checked in intel_framebuffer_init already */
9520 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9521 return -EINVAL;
9522 case DRM_FORMAT_XRGB8888:
9523 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009524 bpp = 8*3;
9525 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009526 case DRM_FORMAT_XRGB2101010:
9527 case DRM_FORMAT_ARGB2101010:
9528 case DRM_FORMAT_XBGR2101010:
9529 case DRM_FORMAT_ABGR2101010:
9530 /* checked in intel_framebuffer_init already */
9531 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009532 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009533 bpp = 10*3;
9534 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009535 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009536 default:
9537 DRM_DEBUG_KMS("unsupported depth\n");
9538 return -EINVAL;
9539 }
9540
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009541 pipe_config->pipe_bpp = bpp;
9542
9543 /* Clamp display bpp to EDID value */
9544 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009545 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009546 if (!connector->new_encoder ||
9547 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009548 continue;
9549
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009550 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009551 }
9552
9553 return bpp;
9554}
9555
Daniel Vetter644db712013-09-19 14:53:58 +02009556static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9557{
9558 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9559 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009560 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009561 mode->crtc_hdisplay, mode->crtc_hsync_start,
9562 mode->crtc_hsync_end, mode->crtc_htotal,
9563 mode->crtc_vdisplay, mode->crtc_vsync_start,
9564 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9565}
9566
Daniel Vetterc0b03412013-05-28 12:05:54 +02009567static void intel_dump_pipe_config(struct intel_crtc *crtc,
9568 struct intel_crtc_config *pipe_config,
9569 const char *context)
9570{
9571 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9572 context, pipe_name(crtc->pipe));
9573
9574 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9575 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9576 pipe_config->pipe_bpp, pipe_config->dither);
9577 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9578 pipe_config->has_pch_encoder,
9579 pipe_config->fdi_lanes,
9580 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9581 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9582 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009583 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9584 pipe_config->has_dp_encoder,
9585 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9586 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9587 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009588 DRM_DEBUG_KMS("requested mode:\n");
9589 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9590 DRM_DEBUG_KMS("adjusted mode:\n");
9591 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009592 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009593 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009594 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9595 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009596 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9597 pipe_config->gmch_pfit.control,
9598 pipe_config->gmch_pfit.pgm_ratios,
9599 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009600 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009601 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009602 pipe_config->pch_pfit.size,
9603 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009604 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009605 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009606}
9607
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009608static bool encoders_cloneable(const struct intel_encoder *a,
9609 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009610{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009611 /* masks could be asymmetric, so check both ways */
9612 return a == b || (a->cloneable & (1 << b->type) &&
9613 b->cloneable & (1 << a->type));
9614}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009615
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009616static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9617 struct intel_encoder *encoder)
9618{
9619 struct drm_device *dev = crtc->base.dev;
9620 struct intel_encoder *source_encoder;
9621
9622 list_for_each_entry(source_encoder,
9623 &dev->mode_config.encoder_list, base.head) {
9624 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009625 continue;
9626
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009627 if (!encoders_cloneable(encoder, source_encoder))
9628 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009629 }
9630
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009631 return true;
9632}
9633
9634static bool check_encoder_cloning(struct intel_crtc *crtc)
9635{
9636 struct drm_device *dev = crtc->base.dev;
9637 struct intel_encoder *encoder;
9638
9639 list_for_each_entry(encoder,
9640 &dev->mode_config.encoder_list, base.head) {
9641 if (encoder->new_crtc != crtc)
9642 continue;
9643
9644 if (!check_single_encoder_cloning(crtc, encoder))
9645 return false;
9646 }
9647
9648 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009649}
9650
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009651static struct intel_crtc_config *
9652intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009653 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009654 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009655{
9656 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009657 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009658 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009659 int plane_bpp, ret = -EINVAL;
9660 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009661
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009662 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009663 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9664 return ERR_PTR(-EINVAL);
9665 }
9666
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009667 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9668 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009669 return ERR_PTR(-ENOMEM);
9670
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009671 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9672 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009673
Daniel Vettere143a212013-07-04 12:01:15 +02009674 pipe_config->cpu_transcoder =
9675 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009676 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009677
Imre Deak2960bc92013-07-30 13:36:32 +03009678 /*
9679 * Sanitize sync polarity flags based on requested ones. If neither
9680 * positive or negative polarity is requested, treat this as meaning
9681 * negative polarity.
9682 */
9683 if (!(pipe_config->adjusted_mode.flags &
9684 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9685 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9686
9687 if (!(pipe_config->adjusted_mode.flags &
9688 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9689 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9690
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009691 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9692 * plane pixel format and any sink constraints into account. Returns the
9693 * source plane bpp so that dithering can be selected on mismatches
9694 * after encoders and crtc also have had their say. */
9695 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9696 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009697 if (plane_bpp < 0)
9698 goto fail;
9699
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009700 /*
9701 * Determine the real pipe dimensions. Note that stereo modes can
9702 * increase the actual pipe size due to the frame doubling and
9703 * insertion of additional space for blanks between the frame. This
9704 * is stored in the crtc timings. We use the requested mode to do this
9705 * computation to clearly distinguish it from the adjusted mode, which
9706 * can be changed by the connectors in the below retry loop.
9707 */
9708 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9709 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9710 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9711
Daniel Vettere29c22c2013-02-21 00:00:16 +01009712encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009713 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009714 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009715 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009716
Daniel Vetter135c81b2013-07-21 21:37:09 +02009717 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009718 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009719
Daniel Vetter7758a112012-07-08 19:40:39 +02009720 /* Pass our mode to the connectors and the CRTC to give them a chance to
9721 * adjust it according to limitations or connector properties, and also
9722 * a chance to reject the mode entirely.
9723 */
9724 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9725 base.head) {
9726
9727 if (&encoder->new_crtc->base != crtc)
9728 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009729
Daniel Vetterefea6e82013-07-21 21:36:59 +02009730 if (!(encoder->compute_config(encoder, pipe_config))) {
9731 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009732 goto fail;
9733 }
9734 }
9735
Daniel Vetterff9a6752013-06-01 17:16:21 +02009736 /* Set default port clock if not overwritten by the encoder. Needs to be
9737 * done afterwards in case the encoder adjusts the mode. */
9738 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009739 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9740 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009741
Daniel Vettera43f6e02013-06-07 23:10:32 +02009742 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009743 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009744 DRM_DEBUG_KMS("CRTC fixup failed\n");
9745 goto fail;
9746 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009747
9748 if (ret == RETRY) {
9749 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9750 ret = -EINVAL;
9751 goto fail;
9752 }
9753
9754 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9755 retry = false;
9756 goto encoder_retry;
9757 }
9758
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009759 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9760 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9761 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9762
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009763 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009764fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009765 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009766 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009767}
9768
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009769/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9770 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9771static void
9772intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9773 unsigned *prepare_pipes, unsigned *disable_pipes)
9774{
9775 struct intel_crtc *intel_crtc;
9776 struct drm_device *dev = crtc->dev;
9777 struct intel_encoder *encoder;
9778 struct intel_connector *connector;
9779 struct drm_crtc *tmp_crtc;
9780
9781 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9782
9783 /* Check which crtcs have changed outputs connected to them, these need
9784 * to be part of the prepare_pipes mask. We don't (yet) support global
9785 * modeset across multiple crtcs, so modeset_pipes will only have one
9786 * bit set at most. */
9787 list_for_each_entry(connector, &dev->mode_config.connector_list,
9788 base.head) {
9789 if (connector->base.encoder == &connector->new_encoder->base)
9790 continue;
9791
9792 if (connector->base.encoder) {
9793 tmp_crtc = connector->base.encoder->crtc;
9794
9795 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9796 }
9797
9798 if (connector->new_encoder)
9799 *prepare_pipes |=
9800 1 << connector->new_encoder->new_crtc->pipe;
9801 }
9802
9803 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9804 base.head) {
9805 if (encoder->base.crtc == &encoder->new_crtc->base)
9806 continue;
9807
9808 if (encoder->base.crtc) {
9809 tmp_crtc = encoder->base.crtc;
9810
9811 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9812 }
9813
9814 if (encoder->new_crtc)
9815 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9816 }
9817
Ville Syrjälä76688512014-01-10 11:28:06 +02009818 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009819 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009820 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009821 continue;
9822
Ville Syrjälä76688512014-01-10 11:28:06 +02009823 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009824 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +02009825 else
9826 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009827 }
9828
9829
9830 /* set_mode is also used to update properties on life display pipes. */
9831 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +02009832 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009833 *prepare_pipes |= 1 << intel_crtc->pipe;
9834
Daniel Vetterb6c51642013-04-12 18:48:43 +02009835 /*
9836 * For simplicity do a full modeset on any pipe where the output routing
9837 * changed. We could be more clever, but that would require us to be
9838 * more careful with calling the relevant encoder->mode_set functions.
9839 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009840 if (*prepare_pipes)
9841 *modeset_pipes = *prepare_pipes;
9842
9843 /* ... and mask these out. */
9844 *modeset_pipes &= ~(*disable_pipes);
9845 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009846
9847 /*
9848 * HACK: We don't (yet) fully support global modesets. intel_set_config
9849 * obies this rule, but the modeset restore mode of
9850 * intel_modeset_setup_hw_state does not.
9851 */
9852 *modeset_pipes &= 1 << intel_crtc->pipe;
9853 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009854
9855 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9856 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009857}
9858
Daniel Vetterea9d7582012-07-10 10:42:52 +02009859static bool intel_crtc_in_use(struct drm_crtc *crtc)
9860{
9861 struct drm_encoder *encoder;
9862 struct drm_device *dev = crtc->dev;
9863
9864 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9865 if (encoder->crtc == crtc)
9866 return true;
9867
9868 return false;
9869}
9870
9871static void
9872intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9873{
9874 struct intel_encoder *intel_encoder;
9875 struct intel_crtc *intel_crtc;
9876 struct drm_connector *connector;
9877
9878 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9879 base.head) {
9880 if (!intel_encoder->base.crtc)
9881 continue;
9882
9883 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9884
9885 if (prepare_pipes & (1 << intel_crtc->pipe))
9886 intel_encoder->connectors_active = false;
9887 }
9888
9889 intel_modeset_commit_output_state(dev);
9890
Ville Syrjälä76688512014-01-10 11:28:06 +02009891 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009892 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009893 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009894 WARN_ON(intel_crtc->new_config &&
9895 intel_crtc->new_config != &intel_crtc->config);
9896 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009897 }
9898
9899 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9900 if (!connector->encoder || !connector->encoder->crtc)
9901 continue;
9902
9903 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9904
9905 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009906 struct drm_property *dpms_property =
9907 dev->mode_config.dpms_property;
9908
Daniel Vetterea9d7582012-07-10 10:42:52 +02009909 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009910 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009911 dpms_property,
9912 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009913
9914 intel_encoder = to_intel_encoder(connector->encoder);
9915 intel_encoder->connectors_active = true;
9916 }
9917 }
9918
9919}
9920
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009921static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009922{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009923 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009924
9925 if (clock1 == clock2)
9926 return true;
9927
9928 if (!clock1 || !clock2)
9929 return false;
9930
9931 diff = abs(clock1 - clock2);
9932
9933 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9934 return true;
9935
9936 return false;
9937}
9938
Daniel Vetter25c5b262012-07-08 22:08:04 +02009939#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9940 list_for_each_entry((intel_crtc), \
9941 &(dev)->mode_config.crtc_list, \
9942 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009943 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009944
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009945static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009946intel_pipe_config_compare(struct drm_device *dev,
9947 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009948 struct intel_crtc_config *pipe_config)
9949{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009950#define PIPE_CONF_CHECK_X(name) \
9951 if (current_config->name != pipe_config->name) { \
9952 DRM_ERROR("mismatch in " #name " " \
9953 "(expected 0x%08x, found 0x%08x)\n", \
9954 current_config->name, \
9955 pipe_config->name); \
9956 return false; \
9957 }
9958
Daniel Vetter08a24032013-04-19 11:25:34 +02009959#define PIPE_CONF_CHECK_I(name) \
9960 if (current_config->name != pipe_config->name) { \
9961 DRM_ERROR("mismatch in " #name " " \
9962 "(expected %i, found %i)\n", \
9963 current_config->name, \
9964 pipe_config->name); \
9965 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009966 }
9967
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009968#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9969 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009970 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009971 "(expected %i, found %i)\n", \
9972 current_config->name & (mask), \
9973 pipe_config->name & (mask)); \
9974 return false; \
9975 }
9976
Ville Syrjälä5e550652013-09-06 23:29:07 +03009977#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9978 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9979 DRM_ERROR("mismatch in " #name " " \
9980 "(expected %i, found %i)\n", \
9981 current_config->name, \
9982 pipe_config->name); \
9983 return false; \
9984 }
9985
Daniel Vetterbb760062013-06-06 14:55:52 +02009986#define PIPE_CONF_QUIRK(quirk) \
9987 ((current_config->quirks | pipe_config->quirks) & (quirk))
9988
Daniel Vettereccb1402013-05-22 00:50:22 +02009989 PIPE_CONF_CHECK_I(cpu_transcoder);
9990
Daniel Vetter08a24032013-04-19 11:25:34 +02009991 PIPE_CONF_CHECK_I(has_pch_encoder);
9992 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009993 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9994 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9995 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9996 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9997 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009998
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009999 PIPE_CONF_CHECK_I(has_dp_encoder);
10000 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10001 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10002 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10003 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10004 PIPE_CONF_CHECK_I(dp_m_n.tu);
10005
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010006 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10007 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10008 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10009 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10010 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10011 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10012
10013 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10014 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10015 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10016 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10017 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10018 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10019
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010020 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020010021 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010022 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10023 IS_VALLEYVIEW(dev))
10024 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010025
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010026 PIPE_CONF_CHECK_I(has_audio);
10027
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010028 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10029 DRM_MODE_FLAG_INTERLACE);
10030
Daniel Vetterbb760062013-06-06 14:55:52 +020010031 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10032 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10033 DRM_MODE_FLAG_PHSYNC);
10034 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10035 DRM_MODE_FLAG_NHSYNC);
10036 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10037 DRM_MODE_FLAG_PVSYNC);
10038 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10039 DRM_MODE_FLAG_NVSYNC);
10040 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010041
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010042 PIPE_CONF_CHECK_I(pipe_src_w);
10043 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010044
Daniel Vetter99535992014-04-13 12:00:33 +020010045 /*
10046 * FIXME: BIOS likes to set up a cloned config with lvds+external
10047 * screen. Since we don't yet re-compute the pipe config when moving
10048 * just the lvds port away to another pipe the sw tracking won't match.
10049 *
10050 * Proper atomic modesets with recomputed global state will fix this.
10051 * Until then just don't check gmch state for inherited modes.
10052 */
10053 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10054 PIPE_CONF_CHECK_I(gmch_pfit.control);
10055 /* pfit ratios are autocomputed by the hw on gen4+ */
10056 if (INTEL_INFO(dev)->gen < 4)
10057 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10058 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10059 }
10060
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010061 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10062 if (current_config->pch_pfit.enabled) {
10063 PIPE_CONF_CHECK_I(pch_pfit.pos);
10064 PIPE_CONF_CHECK_I(pch_pfit.size);
10065 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010066
Jesse Barnese59150d2014-01-07 13:30:45 -080010067 /* BDW+ don't expose a synchronous way to read the state */
10068 if (IS_HASWELL(dev))
10069 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010070
Ville Syrjälä282740f2013-09-04 18:30:03 +030010071 PIPE_CONF_CHECK_I(double_wide);
10072
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010073 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010074 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010075 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010076 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10077 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010078
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010079 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10080 PIPE_CONF_CHECK_I(pipe_bpp);
10081
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010082 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10083 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010084
Daniel Vetter66e985c2013-06-05 13:34:20 +020010085#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010086#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010087#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010088#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010089#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010090
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010091 return true;
10092}
10093
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010094static void
10095check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010096{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010097 struct intel_connector *connector;
10098
10099 list_for_each_entry(connector, &dev->mode_config.connector_list,
10100 base.head) {
10101 /* This also checks the encoder/connector hw state with the
10102 * ->get_hw_state callbacks. */
10103 intel_connector_check_state(connector);
10104
10105 WARN(&connector->new_encoder->base != connector->base.encoder,
10106 "connector's staged encoder doesn't match current encoder\n");
10107 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010108}
10109
10110static void
10111check_encoder_state(struct drm_device *dev)
10112{
10113 struct intel_encoder *encoder;
10114 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010115
10116 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10117 base.head) {
10118 bool enabled = false;
10119 bool active = false;
10120 enum pipe pipe, tracked_pipe;
10121
10122 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10123 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030010124 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010125
10126 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10127 "encoder's stage crtc doesn't match current crtc\n");
10128 WARN(encoder->connectors_active && !encoder->base.crtc,
10129 "encoder's active_connectors set, but no crtc\n");
10130
10131 list_for_each_entry(connector, &dev->mode_config.connector_list,
10132 base.head) {
10133 if (connector->base.encoder != &encoder->base)
10134 continue;
10135 enabled = true;
10136 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10137 active = true;
10138 }
10139 WARN(!!encoder->base.crtc != enabled,
10140 "encoder's enabled state mismatch "
10141 "(expected %i, found %i)\n",
10142 !!encoder->base.crtc, enabled);
10143 WARN(active && !encoder->base.crtc,
10144 "active encoder with no crtc\n");
10145
10146 WARN(encoder->connectors_active != active,
10147 "encoder's computed active state doesn't match tracked active state "
10148 "(expected %i, found %i)\n", active, encoder->connectors_active);
10149
10150 active = encoder->get_hw_state(encoder, &pipe);
10151 WARN(active != encoder->connectors_active,
10152 "encoder's hw state doesn't match sw tracking "
10153 "(expected %i, found %i)\n",
10154 encoder->connectors_active, active);
10155
10156 if (!encoder->base.crtc)
10157 continue;
10158
10159 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10160 WARN(active && pipe != tracked_pipe,
10161 "active encoder's pipe doesn't match"
10162 "(expected %i, found %i)\n",
10163 tracked_pipe, pipe);
10164
10165 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010166}
10167
10168static void
10169check_crtc_state(struct drm_device *dev)
10170{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010171 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010172 struct intel_crtc *crtc;
10173 struct intel_encoder *encoder;
10174 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010175
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010176 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010177 bool enabled = false;
10178 bool active = false;
10179
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010180 memset(&pipe_config, 0, sizeof(pipe_config));
10181
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010182 DRM_DEBUG_KMS("[CRTC:%d]\n",
10183 crtc->base.base.id);
10184
10185 WARN(crtc->active && !crtc->base.enabled,
10186 "active crtc, but not enabled in sw tracking\n");
10187
10188 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10189 base.head) {
10190 if (encoder->base.crtc != &crtc->base)
10191 continue;
10192 enabled = true;
10193 if (encoder->connectors_active)
10194 active = true;
10195 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010196
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010197 WARN(active != crtc->active,
10198 "crtc's computed active state doesn't match tracked active state "
10199 "(expected %i, found %i)\n", active, crtc->active);
10200 WARN(enabled != crtc->base.enabled,
10201 "crtc's computed enabled state doesn't match tracked enabled state "
10202 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10203
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010204 active = dev_priv->display.get_pipe_config(crtc,
10205 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010206
10207 /* hw state is inconsistent with the pipe A quirk */
10208 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10209 active = crtc->active;
10210
Daniel Vetter6c49f242013-06-06 12:45:25 +020010211 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10212 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010213 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010214 if (encoder->base.crtc != &crtc->base)
10215 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010216 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010217 encoder->get_config(encoder, &pipe_config);
10218 }
10219
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010220 WARN(crtc->active != active,
10221 "crtc active state doesn't match with hw state "
10222 "(expected %i, found %i)\n", crtc->active, active);
10223
Daniel Vetterc0b03412013-05-28 12:05:54 +020010224 if (active &&
10225 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10226 WARN(1, "pipe state doesn't match!\n");
10227 intel_dump_pipe_config(crtc, &pipe_config,
10228 "[hw state]");
10229 intel_dump_pipe_config(crtc, &crtc->config,
10230 "[sw state]");
10231 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010232 }
10233}
10234
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010235static void
10236check_shared_dpll_state(struct drm_device *dev)
10237{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010238 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010239 struct intel_crtc *crtc;
10240 struct intel_dpll_hw_state dpll_hw_state;
10241 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010242
10243 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10244 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10245 int enabled_crtcs = 0, active_crtcs = 0;
10246 bool active;
10247
10248 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10249
10250 DRM_DEBUG_KMS("%s\n", pll->name);
10251
10252 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10253
10254 WARN(pll->active > pll->refcount,
10255 "more active pll users than references: %i vs %i\n",
10256 pll->active, pll->refcount);
10257 WARN(pll->active && !pll->on,
10258 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010259 WARN(pll->on && !pll->active,
10260 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010261 WARN(pll->on != active,
10262 "pll on state mismatch (expected %i, found %i)\n",
10263 pll->on, active);
10264
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010265 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010266 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10267 enabled_crtcs++;
10268 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10269 active_crtcs++;
10270 }
10271 WARN(pll->active != active_crtcs,
10272 "pll active crtcs mismatch (expected %i, found %i)\n",
10273 pll->active, active_crtcs);
10274 WARN(pll->refcount != enabled_crtcs,
10275 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10276 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010277
10278 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10279 sizeof(dpll_hw_state)),
10280 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010281 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010282}
10283
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010284void
10285intel_modeset_check_state(struct drm_device *dev)
10286{
10287 check_connector_state(dev);
10288 check_encoder_state(dev);
10289 check_crtc_state(dev);
10290 check_shared_dpll_state(dev);
10291}
10292
Ville Syrjälä18442d02013-09-13 16:00:08 +030010293void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10294 int dotclock)
10295{
10296 /*
10297 * FDI already provided one idea for the dotclock.
10298 * Yell if the encoder disagrees.
10299 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010300 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010301 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010302 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010303}
10304
Ville Syrjälä80715b22014-05-15 20:23:23 +030010305static void update_scanline_offset(struct intel_crtc *crtc)
10306{
10307 struct drm_device *dev = crtc->base.dev;
10308
10309 /*
10310 * The scanline counter increments at the leading edge of hsync.
10311 *
10312 * On most platforms it starts counting from vtotal-1 on the
10313 * first active line. That means the scanline counter value is
10314 * always one less than what we would expect. Ie. just after
10315 * start of vblank, which also occurs at start of hsync (on the
10316 * last active line), the scanline counter will read vblank_start-1.
10317 *
10318 * On gen2 the scanline counter starts counting from 1 instead
10319 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10320 * to keep the value positive), instead of adding one.
10321 *
10322 * On HSW+ the behaviour of the scanline counter depends on the output
10323 * type. For DP ports it behaves like most other platforms, but on HDMI
10324 * there's an extra 1 line difference. So we need to add two instead of
10325 * one to the value.
10326 */
10327 if (IS_GEN2(dev)) {
10328 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10329 int vtotal;
10330
10331 vtotal = mode->crtc_vtotal;
10332 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10333 vtotal /= 2;
10334
10335 crtc->scanline_offset = vtotal - 1;
10336 } else if (HAS_DDI(dev) &&
10337 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10338 crtc->scanline_offset = 2;
10339 } else
10340 crtc->scanline_offset = 1;
10341}
10342
Daniel Vetterf30da182013-04-11 20:22:50 +020010343static int __intel_set_mode(struct drm_crtc *crtc,
10344 struct drm_display_mode *mode,
10345 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010346{
10347 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010348 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010349 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010350 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010351 struct intel_crtc *intel_crtc;
10352 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010353 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010354
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010355 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010356 if (!saved_mode)
10357 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010358
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010359 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010360 &prepare_pipes, &disable_pipes);
10361
Tim Gardner3ac18232012-12-07 07:54:26 -070010362 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010363
Daniel Vetter25c5b262012-07-08 22:08:04 +020010364 /* Hack: Because we don't (yet) support global modeset on multiple
10365 * crtcs, we don't keep track of the new mode for more than one crtc.
10366 * Hence simply check whether any bit is set in modeset_pipes in all the
10367 * pieces of code that are not yet converted to deal with mutliple crtcs
10368 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010369 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010370 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010371 if (IS_ERR(pipe_config)) {
10372 ret = PTR_ERR(pipe_config);
10373 pipe_config = NULL;
10374
Tim Gardner3ac18232012-12-07 07:54:26 -070010375 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010376 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010377 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10378 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010379 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010380 }
10381
Jesse Barnes30a970c2013-11-04 13:48:12 -080010382 /*
10383 * See if the config requires any additional preparation, e.g.
10384 * to adjust global state with pipes off. We need to do this
10385 * here so we can get the modeset_pipe updated config for the new
10386 * mode set on this crtc. For other crtcs we need to use the
10387 * adjusted_mode bits in the crtc directly.
10388 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010389 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010390 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010391
Ville Syrjäläc164f832013-11-05 22:34:12 +020010392 /* may have added more to prepare_pipes than we should */
10393 prepare_pipes &= ~disable_pipes;
10394 }
10395
Daniel Vetter460da9162013-03-27 00:44:51 +010010396 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10397 intel_crtc_disable(&intel_crtc->base);
10398
Daniel Vetterea9d7582012-07-10 10:42:52 +020010399 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10400 if (intel_crtc->base.enabled)
10401 dev_priv->display.crtc_disable(&intel_crtc->base);
10402 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010403
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010404 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10405 * to set it here already despite that we pass it down the callchain.
10406 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010407 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010408 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010409 /* mode_set/enable/disable functions rely on a correct pipe
10410 * config. */
10411 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010412 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010413
10414 /*
10415 * Calculate and store various constants which
10416 * are later needed by vblank and swap-completion
10417 * timestamping. They are derived from true hwmode.
10418 */
10419 drm_calc_timestamping_constants(crtc,
10420 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010421 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010422
Daniel Vetterea9d7582012-07-10 10:42:52 +020010423 /* Only after disabling all output pipelines that will be changed can we
10424 * update the the output configuration. */
10425 intel_modeset_update_state(dev, prepare_pipes);
10426
Daniel Vetter47fab732012-10-26 10:58:18 +020010427 if (dev_priv->display.modeset_global_resources)
10428 dev_priv->display.modeset_global_resources(dev);
10429
Daniel Vettera6778b32012-07-02 09:56:42 +020010430 /* Set up the DPLL and any encoders state that needs to adjust or depend
10431 * on the DPLL.
10432 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010433 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Daniel Vetter4c107942014-04-24 23:55:05 +020010434 struct drm_framebuffer *old_fb;
10435
10436 mutex_lock(&dev->struct_mutex);
10437 ret = intel_pin_and_fence_fb_obj(dev,
10438 to_intel_framebuffer(fb)->obj,
10439 NULL);
10440 if (ret != 0) {
10441 DRM_ERROR("pin & fence failed\n");
10442 mutex_unlock(&dev->struct_mutex);
10443 goto done;
10444 }
10445 old_fb = crtc->primary->fb;
10446 if (old_fb)
10447 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
10448 mutex_unlock(&dev->struct_mutex);
10449
10450 crtc->primary->fb = fb;
10451 crtc->x = x;
10452 crtc->y = y;
10453
Daniel Vetter4271b752014-04-24 23:55:00 +020010454 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10455 x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010456 if (ret)
10457 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010458 }
10459
10460 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030010461 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10462 update_scanline_offset(intel_crtc);
10463
Daniel Vetter25c5b262012-07-08 22:08:04 +020010464 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030010465 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010466
Daniel Vettera6778b32012-07-02 09:56:42 +020010467 /* FIXME: add subpixel order */
10468done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010469 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010470 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010471
Tim Gardner3ac18232012-12-07 07:54:26 -070010472out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010473 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010474 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010475 return ret;
10476}
10477
Damien Lespiaue7457a92013-08-08 22:28:59 +010010478static int intel_set_mode(struct drm_crtc *crtc,
10479 struct drm_display_mode *mode,
10480 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010481{
10482 int ret;
10483
10484 ret = __intel_set_mode(crtc, mode, x, y, fb);
10485
10486 if (ret == 0)
10487 intel_modeset_check_state(crtc->dev);
10488
10489 return ret;
10490}
10491
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010492void intel_crtc_restore_mode(struct drm_crtc *crtc)
10493{
Matt Roperf4510a22014-04-01 15:22:40 -070010494 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010495}
10496
Daniel Vetter25c5b262012-07-08 22:08:04 +020010497#undef for_each_intel_crtc_masked
10498
Daniel Vetterd9e55602012-07-04 22:16:09 +020010499static void intel_set_config_free(struct intel_set_config *config)
10500{
10501 if (!config)
10502 return;
10503
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010504 kfree(config->save_connector_encoders);
10505 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010506 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010507 kfree(config);
10508}
10509
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010510static int intel_set_config_save_state(struct drm_device *dev,
10511 struct intel_set_config *config)
10512{
Ville Syrjälä76688512014-01-10 11:28:06 +020010513 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010514 struct drm_encoder *encoder;
10515 struct drm_connector *connector;
10516 int count;
10517
Ville Syrjälä76688512014-01-10 11:28:06 +020010518 config->save_crtc_enabled =
10519 kcalloc(dev->mode_config.num_crtc,
10520 sizeof(bool), GFP_KERNEL);
10521 if (!config->save_crtc_enabled)
10522 return -ENOMEM;
10523
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010524 config->save_encoder_crtcs =
10525 kcalloc(dev->mode_config.num_encoder,
10526 sizeof(struct drm_crtc *), GFP_KERNEL);
10527 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010528 return -ENOMEM;
10529
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010530 config->save_connector_encoders =
10531 kcalloc(dev->mode_config.num_connector,
10532 sizeof(struct drm_encoder *), GFP_KERNEL);
10533 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010534 return -ENOMEM;
10535
10536 /* Copy data. Note that driver private data is not affected.
10537 * Should anything bad happen only the expected state is
10538 * restored, not the drivers personal bookkeeping.
10539 */
10540 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010541 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010542 config->save_crtc_enabled[count++] = crtc->enabled;
10543 }
10544
10545 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010546 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010547 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010548 }
10549
10550 count = 0;
10551 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010552 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010553 }
10554
10555 return 0;
10556}
10557
10558static void intel_set_config_restore_state(struct drm_device *dev,
10559 struct intel_set_config *config)
10560{
Ville Syrjälä76688512014-01-10 11:28:06 +020010561 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010562 struct intel_encoder *encoder;
10563 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010564 int count;
10565
10566 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010567 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010568 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010569
10570 if (crtc->new_enabled)
10571 crtc->new_config = &crtc->config;
10572 else
10573 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010574 }
10575
10576 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010577 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10578 encoder->new_crtc =
10579 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010580 }
10581
10582 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010583 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10584 connector->new_encoder =
10585 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010586 }
10587}
10588
Imre Deake3de42b2013-05-03 19:44:07 +020010589static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010590is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010591{
10592 int i;
10593
Chris Wilson2e57f472013-07-17 12:14:40 +010010594 if (set->num_connectors == 0)
10595 return false;
10596
10597 if (WARN_ON(set->connectors == NULL))
10598 return false;
10599
10600 for (i = 0; i < set->num_connectors; i++)
10601 if (set->connectors[i]->encoder &&
10602 set->connectors[i]->encoder->crtc == set->crtc &&
10603 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010604 return true;
10605
10606 return false;
10607}
10608
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010609static void
10610intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10611 struct intel_set_config *config)
10612{
10613
10614 /* We should be able to check here if the fb has the same properties
10615 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010616 if (is_crtc_connector_off(set)) {
10617 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010618 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070010619 /*
10620 * If we have no fb, we can only flip as long as the crtc is
10621 * active, otherwise we need a full mode set. The crtc may
10622 * be active if we've only disabled the primary plane, or
10623 * in fastboot situations.
10624 */
Matt Roperf4510a22014-04-01 15:22:40 -070010625 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010626 struct intel_crtc *intel_crtc =
10627 to_intel_crtc(set->crtc);
10628
Matt Roper3b150f02014-05-29 08:06:53 -070010629 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010630 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10631 config->fb_changed = true;
10632 } else {
10633 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10634 config->mode_changed = true;
10635 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010636 } else if (set->fb == NULL) {
10637 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010638 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010639 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010640 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010641 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010642 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010643 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010644 }
10645
Daniel Vetter835c5872012-07-10 18:11:08 +020010646 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010647 config->fb_changed = true;
10648
10649 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10650 DRM_DEBUG_KMS("modes are different, full mode set\n");
10651 drm_mode_debug_printmodeline(&set->crtc->mode);
10652 drm_mode_debug_printmodeline(set->mode);
10653 config->mode_changed = true;
10654 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010655
10656 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10657 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010658}
10659
Daniel Vetter2e431052012-07-04 22:42:15 +020010660static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010661intel_modeset_stage_output_state(struct drm_device *dev,
10662 struct drm_mode_set *set,
10663 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010664{
Daniel Vetter9a935852012-07-05 22:34:27 +020010665 struct intel_connector *connector;
10666 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010667 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010668 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020010669
Damien Lespiau9abdda72013-02-13 13:29:23 +000010670 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020010671 * of connectors. For paranoia, double-check this. */
10672 WARN_ON(!set->fb && (set->num_connectors != 0));
10673 WARN_ON(set->fb && (set->num_connectors == 0));
10674
Daniel Vetter9a935852012-07-05 22:34:27 +020010675 list_for_each_entry(connector, &dev->mode_config.connector_list,
10676 base.head) {
10677 /* Otherwise traverse passed in connector list and get encoders
10678 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010679 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010680 if (set->connectors[ro] == &connector->base) {
10681 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +020010682 break;
10683 }
10684 }
10685
Daniel Vetter9a935852012-07-05 22:34:27 +020010686 /* If we disable the crtc, disable all its connectors. Also, if
10687 * the connector is on the changing crtc but not on the new
10688 * connector list, disable it. */
10689 if ((!set->fb || ro == set->num_connectors) &&
10690 connector->base.encoder &&
10691 connector->base.encoder->crtc == set->crtc) {
10692 connector->new_encoder = NULL;
10693
10694 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10695 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010696 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020010697 }
10698
10699
10700 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010701 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010702 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010703 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010704 }
10705 /* connector->new_encoder is now updated for all connectors. */
10706
10707 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020010708 list_for_each_entry(connector, &dev->mode_config.connector_list,
10709 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010710 struct drm_crtc *new_crtc;
10711
Daniel Vetter9a935852012-07-05 22:34:27 +020010712 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020010713 continue;
10714
Daniel Vetter9a935852012-07-05 22:34:27 +020010715 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020010716
10717 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010718 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020010719 new_crtc = set->crtc;
10720 }
10721
10722 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010010723 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10724 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010725 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020010726 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010727 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10728
10729 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10730 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010731 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020010732 new_crtc->base.id);
10733 }
10734
10735 /* Check for any encoders that needs to be disabled. */
10736 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10737 base.head) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010738 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010739 list_for_each_entry(connector,
10740 &dev->mode_config.connector_list,
10741 base.head) {
10742 if (connector->new_encoder == encoder) {
10743 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010744 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020010745 }
10746 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010747
10748 if (num_connectors == 0)
10749 encoder->new_crtc = NULL;
10750 else if (num_connectors > 1)
10751 return -EINVAL;
10752
Daniel Vetter9a935852012-07-05 22:34:27 +020010753 /* Only now check for crtc changes so we don't miss encoders
10754 * that will be disabled. */
10755 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010756 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010757 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010758 }
10759 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010760 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010761
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010762 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010763 crtc->new_enabled = false;
10764
10765 list_for_each_entry(encoder,
10766 &dev->mode_config.encoder_list,
10767 base.head) {
10768 if (encoder->new_crtc == crtc) {
10769 crtc->new_enabled = true;
10770 break;
10771 }
10772 }
10773
10774 if (crtc->new_enabled != crtc->base.enabled) {
10775 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10776 crtc->new_enabled ? "en" : "dis");
10777 config->mode_changed = true;
10778 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010779
10780 if (crtc->new_enabled)
10781 crtc->new_config = &crtc->config;
10782 else
10783 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010784 }
10785
Daniel Vetter2e431052012-07-04 22:42:15 +020010786 return 0;
10787}
10788
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010789static void disable_crtc_nofb(struct intel_crtc *crtc)
10790{
10791 struct drm_device *dev = crtc->base.dev;
10792 struct intel_encoder *encoder;
10793 struct intel_connector *connector;
10794
10795 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10796 pipe_name(crtc->pipe));
10797
10798 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10799 if (connector->new_encoder &&
10800 connector->new_encoder->new_crtc == crtc)
10801 connector->new_encoder = NULL;
10802 }
10803
10804 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10805 if (encoder->new_crtc == crtc)
10806 encoder->new_crtc = NULL;
10807 }
10808
10809 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010810 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010811}
10812
Daniel Vetter2e431052012-07-04 22:42:15 +020010813static int intel_crtc_set_config(struct drm_mode_set *set)
10814{
10815 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020010816 struct drm_mode_set save_set;
10817 struct intel_set_config *config;
10818 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020010819
Daniel Vetter8d3e3752012-07-05 16:09:09 +020010820 BUG_ON(!set);
10821 BUG_ON(!set->crtc);
10822 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020010823
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010010824 /* Enforce sane interface api - has been abused by the fb helper. */
10825 BUG_ON(!set->mode && set->fb);
10826 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020010827
Daniel Vetter2e431052012-07-04 22:42:15 +020010828 if (set->fb) {
10829 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10830 set->crtc->base.id, set->fb->base.id,
10831 (int)set->num_connectors, set->x, set->y);
10832 } else {
10833 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020010834 }
10835
10836 dev = set->crtc->dev;
10837
10838 ret = -ENOMEM;
10839 config = kzalloc(sizeof(*config), GFP_KERNEL);
10840 if (!config)
10841 goto out_config;
10842
10843 ret = intel_set_config_save_state(dev, config);
10844 if (ret)
10845 goto out_config;
10846
10847 save_set.crtc = set->crtc;
10848 save_set.mode = &set->crtc->mode;
10849 save_set.x = set->crtc->x;
10850 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070010851 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020010852
10853 /* Compute whether we need a full modeset, only an fb base update or no
10854 * change at all. In the future we might also check whether only the
10855 * mode changed, e.g. for LVDS where we only change the panel fitter in
10856 * such cases. */
10857 intel_set_config_compute_mode_changes(set, config);
10858
Daniel Vetter9a935852012-07-05 22:34:27 +020010859 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020010860 if (ret)
10861 goto fail;
10862
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010863 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010864 ret = intel_set_mode(set->crtc, set->mode,
10865 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010866 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070010867 struct drm_i915_private *dev_priv = dev->dev_private;
10868 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
10869
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010870 intel_crtc_wait_for_pending_flips(set->crtc);
10871
Daniel Vetter4f660f42012-07-02 09:47:37 +020010872 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010873 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070010874
10875 /*
10876 * We need to make sure the primary plane is re-enabled if it
10877 * has previously been turned off.
10878 */
10879 if (!intel_crtc->primary_enabled && ret == 0) {
10880 WARN_ON(!intel_crtc->active);
10881 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
10882 intel_crtc->pipe);
10883 }
10884
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010885 /*
10886 * In the fastboot case this may be our only check of the
10887 * state after boot. It would be better to only do it on
10888 * the first update, but we don't have a nice way of doing that
10889 * (and really, set_config isn't used much for high freq page
10890 * flipping, so increasing its cost here shouldn't be a big
10891 * deal).
10892 */
Jani Nikulad330a952014-01-21 11:24:25 +020010893 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010894 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010895 }
10896
Chris Wilson2d05eae2013-05-03 17:36:25 +010010897 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010898 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10899 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010900fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010901 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010902
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010903 /*
10904 * HACK: if the pipe was on, but we didn't have a framebuffer,
10905 * force the pipe off to avoid oopsing in the modeset code
10906 * due to fb==NULL. This should only happen during boot since
10907 * we don't yet reconstruct the FB from the hardware state.
10908 */
10909 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10910 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10911
Chris Wilson2d05eae2013-05-03 17:36:25 +010010912 /* Try to restore the config */
10913 if (config->mode_changed &&
10914 intel_set_mode(save_set.crtc, save_set.mode,
10915 save_set.x, save_set.y, save_set.fb))
10916 DRM_ERROR("failed to restore config after modeset failure\n");
10917 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010918
Daniel Vetterd9e55602012-07-04 22:16:09 +020010919out_config:
10920 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010921 return ret;
10922}
10923
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010924static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010925 .cursor_set = intel_crtc_cursor_set,
10926 .cursor_move = intel_crtc_cursor_move,
10927 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010928 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010929 .destroy = intel_crtc_destroy,
10930 .page_flip = intel_crtc_page_flip,
10931};
10932
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010933static void intel_cpu_pll_init(struct drm_device *dev)
10934{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010935 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010936 intel_ddi_pll_init(dev);
10937}
10938
Daniel Vetter53589012013-06-05 13:34:16 +020010939static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10940 struct intel_shared_dpll *pll,
10941 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010942{
Daniel Vetter53589012013-06-05 13:34:16 +020010943 uint32_t val;
10944
10945 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010946 hw_state->dpll = val;
10947 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10948 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010949
10950 return val & DPLL_VCO_ENABLE;
10951}
10952
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010953static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10954 struct intel_shared_dpll *pll)
10955{
10956 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10957 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10958}
10959
Daniel Vettere7b903d2013-06-05 13:34:14 +020010960static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10961 struct intel_shared_dpll *pll)
10962{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010963 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020010964 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020010965
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010966 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10967
10968 /* Wait for the clocks to stabilize. */
10969 POSTING_READ(PCH_DPLL(pll->id));
10970 udelay(150);
10971
10972 /* The pixel multiplier can only be updated once the
10973 * DPLL is enabled and the clocks are stable.
10974 *
10975 * So write it again.
10976 */
10977 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10978 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010979 udelay(200);
10980}
10981
10982static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10983 struct intel_shared_dpll *pll)
10984{
10985 struct drm_device *dev = dev_priv->dev;
10986 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010987
10988 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010989 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020010990 if (intel_crtc_to_shared_dpll(crtc) == pll)
10991 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10992 }
10993
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010994 I915_WRITE(PCH_DPLL(pll->id), 0);
10995 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010996 udelay(200);
10997}
10998
Daniel Vetter46edb022013-06-05 13:34:12 +020010999static char *ibx_pch_dpll_names[] = {
11000 "PCH DPLL A",
11001 "PCH DPLL B",
11002};
11003
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011004static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011005{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011006 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011007 int i;
11008
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011009 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011010
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011011 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011012 dev_priv->shared_dplls[i].id = i;
11013 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011014 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011015 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11016 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011017 dev_priv->shared_dplls[i].get_hw_state =
11018 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011019 }
11020}
11021
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011022static void intel_shared_dpll_init(struct drm_device *dev)
11023{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011024 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011025
11026 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
11027 ibx_pch_dpll_init(dev);
11028 else
11029 dev_priv->num_shared_dpll = 0;
11030
11031 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011032}
11033
Matt Roper465c1202014-05-29 08:06:54 -070011034static int
11035intel_primary_plane_disable(struct drm_plane *plane)
11036{
11037 struct drm_device *dev = plane->dev;
11038 struct drm_i915_private *dev_priv = dev->dev_private;
11039 struct intel_plane *intel_plane = to_intel_plane(plane);
11040 struct intel_crtc *intel_crtc;
11041
11042 if (!plane->fb)
11043 return 0;
11044
11045 BUG_ON(!plane->crtc);
11046
11047 intel_crtc = to_intel_crtc(plane->crtc);
11048
11049 /*
11050 * Even though we checked plane->fb above, it's still possible that
11051 * the primary plane has been implicitly disabled because the crtc
11052 * coordinates given weren't visible, or because we detected
11053 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11054 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11055 * In either case, we need to unpin the FB and let the fb pointer get
11056 * updated, but otherwise we don't need to touch the hardware.
11057 */
11058 if (!intel_crtc->primary_enabled)
11059 goto disable_unpin;
11060
11061 intel_crtc_wait_for_pending_flips(plane->crtc);
11062 intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
11063 intel_plane->pipe);
11064
11065disable_unpin:
11066 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11067 plane->fb = NULL;
11068
11069 return 0;
11070}
11071
11072static int
11073intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11074 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11075 unsigned int crtc_w, unsigned int crtc_h,
11076 uint32_t src_x, uint32_t src_y,
11077 uint32_t src_w, uint32_t src_h)
11078{
11079 struct drm_device *dev = crtc->dev;
11080 struct drm_i915_private *dev_priv = dev->dev_private;
11081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11082 struct intel_plane *intel_plane = to_intel_plane(plane);
11083 struct drm_rect dest = {
11084 /* integer pixels */
11085 .x1 = crtc_x,
11086 .y1 = crtc_y,
11087 .x2 = crtc_x + crtc_w,
11088 .y2 = crtc_y + crtc_h,
11089 };
11090 struct drm_rect src = {
11091 /* 16.16 fixed point */
11092 .x1 = src_x,
11093 .y1 = src_y,
11094 .x2 = src_x + src_w,
11095 .y2 = src_y + src_h,
11096 };
11097 const struct drm_rect clip = {
11098 /* integer pixels */
11099 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11100 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11101 };
11102 bool visible;
11103 int ret;
11104
11105 ret = drm_plane_helper_check_update(plane, crtc, fb,
11106 &src, &dest, &clip,
11107 DRM_PLANE_HELPER_NO_SCALING,
11108 DRM_PLANE_HELPER_NO_SCALING,
11109 false, true, &visible);
11110
11111 if (ret)
11112 return ret;
11113
11114 /*
11115 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11116 * updating the fb pointer, and returning without touching the
11117 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11118 * turn on the display with all planes setup as desired.
11119 */
11120 if (!crtc->enabled) {
11121 /*
11122 * If we already called setplane while the crtc was disabled,
11123 * we may have an fb pinned; unpin it.
11124 */
11125 if (plane->fb)
11126 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11127
11128 /* Pin and return without programming hardware */
11129 return intel_pin_and_fence_fb_obj(dev,
11130 to_intel_framebuffer(fb)->obj,
11131 NULL);
11132 }
11133
11134 intel_crtc_wait_for_pending_flips(crtc);
11135
11136 /*
11137 * If clipping results in a non-visible primary plane, we'll disable
11138 * the primary plane. Note that this is a bit different than what
11139 * happens if userspace explicitly disables the plane by passing fb=0
11140 * because plane->fb still gets set and pinned.
11141 */
11142 if (!visible) {
11143 /*
11144 * Try to pin the new fb first so that we can bail out if we
11145 * fail.
11146 */
11147 if (plane->fb != fb) {
11148 ret = intel_pin_and_fence_fb_obj(dev,
11149 to_intel_framebuffer(fb)->obj,
11150 NULL);
11151 if (ret)
11152 return ret;
11153 }
11154
11155 if (intel_crtc->primary_enabled)
11156 intel_disable_primary_hw_plane(dev_priv,
11157 intel_plane->plane,
11158 intel_plane->pipe);
11159
11160
11161 if (plane->fb != fb)
11162 if (plane->fb)
11163 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11164
11165 return 0;
11166 }
11167
11168 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11169 if (ret)
11170 return ret;
11171
11172 if (!intel_crtc->primary_enabled)
11173 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11174 intel_crtc->pipe);
11175
11176 return 0;
11177}
11178
11179static void intel_primary_plane_destroy(struct drm_plane *plane)
11180{
11181 struct intel_plane *intel_plane = to_intel_plane(plane);
11182 drm_plane_cleanup(plane);
11183 kfree(intel_plane);
11184}
11185
11186static const struct drm_plane_funcs intel_primary_plane_funcs = {
11187 .update_plane = intel_primary_plane_setplane,
11188 .disable_plane = intel_primary_plane_disable,
11189 .destroy = intel_primary_plane_destroy,
11190};
11191
11192static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11193 int pipe)
11194{
11195 struct intel_plane *primary;
11196 const uint32_t *intel_primary_formats;
11197 int num_formats;
11198
11199 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11200 if (primary == NULL)
11201 return NULL;
11202
11203 primary->can_scale = false;
11204 primary->max_downscale = 1;
11205 primary->pipe = pipe;
11206 primary->plane = pipe;
11207 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11208 primary->plane = !pipe;
11209
11210 if (INTEL_INFO(dev)->gen <= 3) {
11211 intel_primary_formats = intel_primary_formats_gen2;
11212 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11213 } else {
11214 intel_primary_formats = intel_primary_formats_gen4;
11215 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11216 }
11217
11218 drm_universal_plane_init(dev, &primary->base, 0,
11219 &intel_primary_plane_funcs,
11220 intel_primary_formats, num_formats,
11221 DRM_PLANE_TYPE_PRIMARY);
11222 return &primary->base;
11223}
11224
Hannes Ederb358d0a2008-12-18 21:18:47 +010011225static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080011226{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011227 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080011228 struct intel_crtc *intel_crtc;
Matt Roper465c1202014-05-29 08:06:54 -070011229 struct drm_plane *primary;
11230 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011231
Daniel Vetter955382f2013-09-19 14:05:45 +020011232 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080011233 if (intel_crtc == NULL)
11234 return;
11235
Matt Roper465c1202014-05-29 08:06:54 -070011236 primary = intel_primary_plane_create(dev, pipe);
11237 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
11238 NULL, &intel_crtc_funcs);
11239 if (ret) {
11240 drm_plane_cleanup(primary);
11241 kfree(intel_crtc);
11242 return;
11243 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011244
11245 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080011246 for (i = 0; i < 256; i++) {
11247 intel_crtc->lut_r[i] = i;
11248 intel_crtc->lut_g[i] = i;
11249 intel_crtc->lut_b[i] = i;
11250 }
11251
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011252 /*
11253 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
11254 * is hooked to plane B. Hence we want plane A feeding pipe B.
11255 */
Jesse Barnes80824002009-09-10 15:28:06 -070011256 intel_crtc->pipe = pipe;
11257 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010011258 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080011259 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010011260 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070011261 }
11262
Chris Wilson4b0e3332014-05-30 16:35:26 +030011263 intel_crtc->cursor_base = ~0;
11264 intel_crtc->cursor_cntl = ~0;
11265
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030011266 init_waitqueue_head(&intel_crtc->vbl_wait);
11267
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080011268 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11269 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11270 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11271 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11272
Jesse Barnes79e53942008-11-07 14:24:08 -080011273 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020011274
11275 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -080011276}
11277
Jesse Barnes752aa882013-10-31 18:55:49 +020011278enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11279{
11280 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011281 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020011282
Rob Clark51fd3712013-11-19 12:10:12 -050011283 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020011284
11285 if (!encoder)
11286 return INVALID_PIPE;
11287
11288 return to_intel_crtc(encoder->crtc)->pipe;
11289}
11290
Carl Worth08d7b3d2009-04-29 14:43:54 -070011291int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000011292 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070011293{
Carl Worth08d7b3d2009-04-29 14:43:54 -070011294 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020011295 struct drm_mode_object *drmmode_obj;
11296 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011297
Daniel Vetter1cff8f62012-04-24 09:55:08 +020011298 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11299 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011300
Daniel Vetterc05422d2009-08-11 16:05:30 +020011301 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11302 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070011303
Daniel Vetterc05422d2009-08-11 16:05:30 +020011304 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070011305 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030011306 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011307 }
11308
Daniel Vetterc05422d2009-08-11 16:05:30 +020011309 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11310 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011311
Daniel Vetterc05422d2009-08-11 16:05:30 +020011312 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011313}
11314
Daniel Vetter66a92782012-07-12 20:08:18 +020011315static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080011316{
Daniel Vetter66a92782012-07-12 20:08:18 +020011317 struct drm_device *dev = encoder->base.dev;
11318 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011319 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011320 int entry = 0;
11321
Daniel Vetter66a92782012-07-12 20:08:18 +020011322 list_for_each_entry(source_encoder,
11323 &dev->mode_config.encoder_list, base.head) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011324 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020011325 index_mask |= (1 << entry);
11326
Jesse Barnes79e53942008-11-07 14:24:08 -080011327 entry++;
11328 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010011329
Jesse Barnes79e53942008-11-07 14:24:08 -080011330 return index_mask;
11331}
11332
Chris Wilson4d302442010-12-14 19:21:29 +000011333static bool has_edp_a(struct drm_device *dev)
11334{
11335 struct drm_i915_private *dev_priv = dev->dev_private;
11336
11337 if (!IS_MOBILE(dev))
11338 return false;
11339
11340 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11341 return false;
11342
Damien Lespiaue3589902014-02-07 19:12:50 +000011343 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000011344 return false;
11345
11346 return true;
11347}
11348
Damien Lespiauba0fbca2014-01-08 14:18:23 +000011349const char *intel_output_name(int output)
11350{
11351 static const char *names[] = {
11352 [INTEL_OUTPUT_UNUSED] = "Unused",
11353 [INTEL_OUTPUT_ANALOG] = "Analog",
11354 [INTEL_OUTPUT_DVO] = "DVO",
11355 [INTEL_OUTPUT_SDVO] = "SDVO",
11356 [INTEL_OUTPUT_LVDS] = "LVDS",
11357 [INTEL_OUTPUT_TVOUT] = "TV",
11358 [INTEL_OUTPUT_HDMI] = "HDMI",
11359 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11360 [INTEL_OUTPUT_EDP] = "eDP",
11361 [INTEL_OUTPUT_DSI] = "DSI",
11362 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11363 };
11364
11365 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11366 return "Invalid";
11367
11368 return names[output];
11369}
11370
Jesse Barnes79e53942008-11-07 14:24:08 -080011371static void intel_setup_outputs(struct drm_device *dev)
11372{
Eric Anholt725e30a2009-01-22 13:01:02 -080011373 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011374 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011375 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011376
Daniel Vetterc9093352013-06-06 22:22:47 +020011377 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011378
Jesse Barnes27da3bd2014-04-04 16:12:07 -070011379 if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev) && dev_priv->vbt.int_crt_support)
Paulo Zanoni79935fc2012-11-20 13:27:40 -020011380 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011381
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011382 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030011383 int found;
11384
11385 /* Haswell uses DDI functions to detect digital outputs */
11386 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11387 /* DDI A only supports eDP */
11388 if (found)
11389 intel_ddi_init(dev, PORT_A);
11390
11391 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11392 * register */
11393 found = I915_READ(SFUSE_STRAP);
11394
11395 if (found & SFUSE_STRAP_DDIB_DETECTED)
11396 intel_ddi_init(dev, PORT_B);
11397 if (found & SFUSE_STRAP_DDIC_DETECTED)
11398 intel_ddi_init(dev, PORT_C);
11399 if (found & SFUSE_STRAP_DDID_DETECTED)
11400 intel_ddi_init(dev, PORT_D);
11401 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011402 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011403 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020011404
11405 if (has_edp_a(dev))
11406 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011407
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011408 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080011409 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010011410 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011411 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011412 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011413 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011414 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011415 }
11416
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011417 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011418 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011419
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011420 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011421 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011422
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011423 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011424 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011425
Daniel Vetter270b3042012-10-27 15:52:05 +020011426 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011427 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070011428 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030011429 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11430 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11431 PORT_B);
11432 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11433 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11434 }
11435
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011436 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11437 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11438 PORT_C);
11439 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011440 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011441 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053011442
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030011443 if (IS_CHERRYVIEW(dev)) {
11444 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11445 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11446 PORT_D);
11447 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11448 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11449 }
11450 }
11451
Jani Nikula3cfca972013-08-27 15:12:26 +030011452 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080011453 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011454 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080011455
Paulo Zanonie2debe92013-02-18 19:00:27 -030011456 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011457 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011458 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011459 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11460 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011461 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011462 }
Ma Ling27185ae2009-08-24 13:50:23 +080011463
Imre Deake7281ea2013-05-08 13:14:08 +030011464 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011465 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080011466 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011467
11468 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011469
Paulo Zanonie2debe92013-02-18 19:00:27 -030011470 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011471 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011472 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011473 }
Ma Ling27185ae2009-08-24 13:50:23 +080011474
Paulo Zanonie2debe92013-02-18 19:00:27 -030011475 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011476
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011477 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11478 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011479 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011480 }
Imre Deake7281ea2013-05-08 13:14:08 +030011481 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011482 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080011483 }
Ma Ling27185ae2009-08-24 13:50:23 +080011484
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011485 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030011486 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011487 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070011488 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011489 intel_dvo_init(dev);
11490
Zhenyu Wang103a1962009-11-27 11:44:36 +080011491 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011492 intel_tv_init(dev);
11493
Chris Wilson4ef69c72010-09-09 15:14:28 +010011494 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11495 encoder->base.possible_crtcs = encoder->crtc_mask;
11496 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020011497 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080011498 }
Chris Wilson47356eb2011-01-11 17:06:04 +000011499
Paulo Zanonidde86e22012-12-01 12:04:25 -020011500 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020011501
11502 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011503}
11504
11505static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11506{
11507 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080011508
Daniel Vetteref2d6332014-02-10 18:00:38 +010011509 drm_framebuffer_cleanup(fb);
11510 WARN_ON(!intel_fb->obj->framebuffer_references--);
11511 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011512 kfree(intel_fb);
11513}
11514
11515static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000011516 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080011517 unsigned int *handle)
11518{
11519 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011520 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011521
Chris Wilson05394f32010-11-08 19:18:58 +000011522 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080011523}
11524
11525static const struct drm_framebuffer_funcs intel_fb_funcs = {
11526 .destroy = intel_user_framebuffer_destroy,
11527 .create_handle = intel_user_framebuffer_create_handle,
11528};
11529
Daniel Vetterb5ea6422014-03-02 21:18:00 +010011530static int intel_framebuffer_init(struct drm_device *dev,
11531 struct intel_framebuffer *intel_fb,
11532 struct drm_mode_fb_cmd2 *mode_cmd,
11533 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080011534{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011535 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011536 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080011537 int ret;
11538
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011539 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11540
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011541 if (obj->tiling_mode == I915_TILING_Y) {
11542 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010011543 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011544 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011545
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011546 if (mode_cmd->pitches[0] & 63) {
11547 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11548 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010011549 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011550 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011551
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011552 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11553 pitch_limit = 32*1024;
11554 } else if (INTEL_INFO(dev)->gen >= 4) {
11555 if (obj->tiling_mode)
11556 pitch_limit = 16*1024;
11557 else
11558 pitch_limit = 32*1024;
11559 } else if (INTEL_INFO(dev)->gen >= 3) {
11560 if (obj->tiling_mode)
11561 pitch_limit = 8*1024;
11562 else
11563 pitch_limit = 16*1024;
11564 } else
11565 /* XXX DSPC is limited to 4k tiled */
11566 pitch_limit = 8*1024;
11567
11568 if (mode_cmd->pitches[0] > pitch_limit) {
11569 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11570 obj->tiling_mode ? "tiled" : "linear",
11571 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011572 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011573 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011574
11575 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011576 mode_cmd->pitches[0] != obj->stride) {
11577 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11578 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011579 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011580 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011581
Ville Syrjälä57779d02012-10-31 17:50:14 +020011582 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011583 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020011584 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011585 case DRM_FORMAT_RGB565:
11586 case DRM_FORMAT_XRGB8888:
11587 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011588 break;
11589 case DRM_FORMAT_XRGB1555:
11590 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011591 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011592 DRM_DEBUG("unsupported pixel format: %s\n",
11593 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011594 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011595 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020011596 break;
11597 case DRM_FORMAT_XBGR8888:
11598 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011599 case DRM_FORMAT_XRGB2101010:
11600 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011601 case DRM_FORMAT_XBGR2101010:
11602 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011603 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011604 DRM_DEBUG("unsupported pixel format: %s\n",
11605 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011606 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011607 }
Jesse Barnesb5626742011-06-24 12:19:27 -070011608 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020011609 case DRM_FORMAT_YUYV:
11610 case DRM_FORMAT_UYVY:
11611 case DRM_FORMAT_YVYU:
11612 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011613 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011614 DRM_DEBUG("unsupported pixel format: %s\n",
11615 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011616 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011617 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011618 break;
11619 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011620 DRM_DEBUG("unsupported pixel format: %s\n",
11621 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010011622 return -EINVAL;
11623 }
11624
Ville Syrjälä90f9a332012-10-31 17:50:19 +020011625 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11626 if (mode_cmd->offsets[0] != 0)
11627 return -EINVAL;
11628
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011629 aligned_height = intel_align_height(dev, mode_cmd->height,
11630 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020011631 /* FIXME drm helper for size checks (especially planar formats)? */
11632 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11633 return -EINVAL;
11634
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011635 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11636 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020011637 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011638
Jesse Barnes79e53942008-11-07 14:24:08 -080011639 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11640 if (ret) {
11641 DRM_ERROR("framebuffer init failed %d\n", ret);
11642 return ret;
11643 }
11644
Jesse Barnes79e53942008-11-07 14:24:08 -080011645 return 0;
11646}
11647
Jesse Barnes79e53942008-11-07 14:24:08 -080011648static struct drm_framebuffer *
11649intel_user_framebuffer_create(struct drm_device *dev,
11650 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011651 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080011652{
Chris Wilson05394f32010-11-08 19:18:58 +000011653 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011654
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011655 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11656 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000011657 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010011658 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080011659
Chris Wilsond2dff872011-04-19 08:36:26 +010011660 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080011661}
11662
Daniel Vetter4520f532013-10-09 09:18:51 +020011663#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020011664static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020011665{
11666}
11667#endif
11668
Jesse Barnes79e53942008-11-07 14:24:08 -080011669static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080011670 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020011671 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080011672};
11673
Jesse Barnese70236a2009-09-21 10:42:27 -070011674/* Set up chip specific display functions */
11675static void intel_init_display(struct drm_device *dev)
11676{
11677 struct drm_i915_private *dev_priv = dev->dev_private;
11678
Daniel Vetteree9300b2013-06-03 22:40:22 +020011679 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11680 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030011681 else if (IS_CHERRYVIEW(dev))
11682 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020011683 else if (IS_VALLEYVIEW(dev))
11684 dev_priv->display.find_dpll = vlv_find_best_dpll;
11685 else if (IS_PINEVIEW(dev))
11686 dev_priv->display.find_dpll = pnv_find_best_dpll;
11687 else
11688 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11689
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011690 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011691 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011692 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011693 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020011694 dev_priv->display.crtc_enable = haswell_crtc_enable;
11695 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011696 dev_priv->display.off = haswell_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011697 dev_priv->display.update_primary_plane =
11698 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011699 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011700 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011701 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011702 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011703 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11704 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011705 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011706 dev_priv->display.update_primary_plane =
11707 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011708 } else if (IS_VALLEYVIEW(dev)) {
11709 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011710 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011711 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11712 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11713 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11714 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011715 dev_priv->display.update_primary_plane =
11716 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011717 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011718 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011719 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011720 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011721 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11722 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011723 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011724 dev_priv->display.update_primary_plane =
11725 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011726 }
Jesse Barnese70236a2009-09-21 10:42:27 -070011727
Jesse Barnese70236a2009-09-21 10:42:27 -070011728 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070011729 if (IS_VALLEYVIEW(dev))
11730 dev_priv->display.get_display_clock_speed =
11731 valleyview_get_display_clock_speed;
11732 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070011733 dev_priv->display.get_display_clock_speed =
11734 i945_get_display_clock_speed;
11735 else if (IS_I915G(dev))
11736 dev_priv->display.get_display_clock_speed =
11737 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011738 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011739 dev_priv->display.get_display_clock_speed =
11740 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011741 else if (IS_PINEVIEW(dev))
11742 dev_priv->display.get_display_clock_speed =
11743 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070011744 else if (IS_I915GM(dev))
11745 dev_priv->display.get_display_clock_speed =
11746 i915gm_get_display_clock_speed;
11747 else if (IS_I865G(dev))
11748 dev_priv->display.get_display_clock_speed =
11749 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020011750 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011751 dev_priv->display.get_display_clock_speed =
11752 i855_get_display_clock_speed;
11753 else /* 852, 830 */
11754 dev_priv->display.get_display_clock_speed =
11755 i830_get_display_clock_speed;
11756
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080011757 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010011758 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011759 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011760 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080011761 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011762 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011763 dev_priv->display.write_eld = ironlake_write_eld;
Paulo Zanoni9a952a02014-03-07 20:12:34 -030011764 dev_priv->display.modeset_global_resources =
11765 snb_modeset_global_resources;
Jesse Barnes357555c2011-04-28 15:09:55 -070011766 } else if (IS_IVYBRIDGE(dev)) {
11767 /* FIXME: detect B0+ stepping and use auto training */
11768 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011769 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020011770 dev_priv->display.modeset_global_resources =
11771 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011772 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030011773 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080011774 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020011775 dev_priv->display.modeset_global_resources =
11776 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020011777 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070011778 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080011779 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011780 } else if (IS_VALLEYVIEW(dev)) {
11781 dev_priv->display.modeset_global_resources =
11782 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040011783 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070011784 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011785
11786 /* Default just returns -ENODEV to indicate unsupported */
11787 dev_priv->display.queue_flip = intel_default_queue_flip;
11788
11789 switch (INTEL_INFO(dev)->gen) {
11790 case 2:
11791 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11792 break;
11793
11794 case 3:
11795 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11796 break;
11797
11798 case 4:
11799 case 5:
11800 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11801 break;
11802
11803 case 6:
11804 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11805 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011806 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011807 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011808 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11809 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011810 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020011811
11812 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011813}
11814
Jesse Barnesb690e962010-07-19 13:53:12 -070011815/*
11816 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11817 * resume, or other times. This quirk makes sure that's the case for
11818 * affected systems.
11819 */
Akshay Joshi0206e352011-08-16 15:34:10 -040011820static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070011821{
11822 struct drm_i915_private *dev_priv = dev->dev_private;
11823
11824 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011825 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011826}
11827
Keith Packard435793d2011-07-12 14:56:22 -070011828/*
11829 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11830 */
11831static void quirk_ssc_force_disable(struct drm_device *dev)
11832{
11833 struct drm_i915_private *dev_priv = dev->dev_private;
11834 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011835 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070011836}
11837
Carsten Emde4dca20e2012-03-15 15:56:26 +010011838/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010011839 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11840 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010011841 */
11842static void quirk_invert_brightness(struct drm_device *dev)
11843{
11844 struct drm_i915_private *dev_priv = dev->dev_private;
11845 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011846 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011847}
11848
11849struct intel_quirk {
11850 int device;
11851 int subsystem_vendor;
11852 int subsystem_device;
11853 void (*hook)(struct drm_device *dev);
11854};
11855
Egbert Eich5f85f1762012-10-14 15:46:38 +020011856/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11857struct intel_dmi_quirk {
11858 void (*hook)(struct drm_device *dev);
11859 const struct dmi_system_id (*dmi_id_list)[];
11860};
11861
11862static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11863{
11864 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11865 return 1;
11866}
11867
11868static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11869 {
11870 .dmi_id_list = &(const struct dmi_system_id[]) {
11871 {
11872 .callback = intel_dmi_reverse_brightness,
11873 .ident = "NCR Corporation",
11874 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11875 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11876 },
11877 },
11878 { } /* terminating entry */
11879 },
11880 .hook = quirk_invert_brightness,
11881 },
11882};
11883
Ben Widawskyc43b5632012-04-16 14:07:40 -070011884static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070011885 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040011886 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070011887
Jesse Barnesb690e962010-07-19 13:53:12 -070011888 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11889 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11890
Jesse Barnesb690e962010-07-19 13:53:12 -070011891 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11892 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11893
Keith Packard435793d2011-07-12 14:56:22 -070011894 /* Lenovo U160 cannot use SSC on LVDS */
11895 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020011896
11897 /* Sony Vaio Y cannot use SSC on LVDS */
11898 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010011899
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010011900 /* Acer Aspire 5734Z must invert backlight brightness */
11901 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11902
11903 /* Acer/eMachines G725 */
11904 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11905
11906 /* Acer/eMachines e725 */
11907 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11908
11909 /* Acer/Packard Bell NCL20 */
11910 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11911
11912 /* Acer Aspire 4736Z */
11913 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020011914
11915 /* Acer Aspire 5336 */
11916 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070011917};
11918
11919static void intel_init_quirks(struct drm_device *dev)
11920{
11921 struct pci_dev *d = dev->pdev;
11922 int i;
11923
11924 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11925 struct intel_quirk *q = &intel_quirks[i];
11926
11927 if (d->device == q->device &&
11928 (d->subsystem_vendor == q->subsystem_vendor ||
11929 q->subsystem_vendor == PCI_ANY_ID) &&
11930 (d->subsystem_device == q->subsystem_device ||
11931 q->subsystem_device == PCI_ANY_ID))
11932 q->hook(dev);
11933 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020011934 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11935 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11936 intel_dmi_quirks[i].hook(dev);
11937 }
Jesse Barnesb690e962010-07-19 13:53:12 -070011938}
11939
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011940/* Disable the VGA plane that we never use */
11941static void i915_disable_vga(struct drm_device *dev)
11942{
11943 struct drm_i915_private *dev_priv = dev->dev_private;
11944 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011945 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011946
Ville Syrjälä2b37c612014-01-22 21:32:38 +020011947 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011948 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070011949 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011950 sr1 = inb(VGA_SR_DATA);
11951 outb(sr1 | 1<<5, VGA_SR_DATA);
11952 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11953 udelay(300);
11954
11955 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11956 POSTING_READ(vga_reg);
11957}
11958
Daniel Vetterf8175862012-04-10 15:50:11 +020011959void intel_modeset_init_hw(struct drm_device *dev)
11960{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030011961 intel_prepare_ddi(dev);
11962
Daniel Vetterf8175862012-04-10 15:50:11 +020011963 intel_init_clock_gating(dev);
11964
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011965 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070011966
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011967 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020011968}
11969
Imre Deak7d708ee2013-04-17 14:04:50 +030011970void intel_modeset_suspend_hw(struct drm_device *dev)
11971{
11972 intel_suspend_hw(dev);
11973}
11974
Jesse Barnes79e53942008-11-07 14:24:08 -080011975void intel_modeset_init(struct drm_device *dev)
11976{
Jesse Barnes652c3932009-08-17 13:31:43 -070011977 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000011978 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000011979 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080011980 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080011981
11982 drm_mode_config_init(dev);
11983
11984 dev->mode_config.min_width = 0;
11985 dev->mode_config.min_height = 0;
11986
Dave Airlie019d96c2011-09-29 16:20:42 +010011987 dev->mode_config.preferred_depth = 24;
11988 dev->mode_config.prefer_shadow = 1;
11989
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020011990 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080011991
Jesse Barnesb690e962010-07-19 13:53:12 -070011992 intel_init_quirks(dev);
11993
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030011994 intel_init_pm(dev);
11995
Ben Widawskye3c74752013-04-05 13:12:39 -070011996 if (INTEL_INFO(dev)->num_pipes == 0)
11997 return;
11998
Jesse Barnese70236a2009-09-21 10:42:27 -070011999 intel_init_display(dev);
12000
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012001 if (IS_GEN2(dev)) {
12002 dev->mode_config.max_width = 2048;
12003 dev->mode_config.max_height = 2048;
12004 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012005 dev->mode_config.max_width = 4096;
12006 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012007 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012008 dev->mode_config.max_width = 8192;
12009 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012010 }
Damien Lespiau068be562014-03-28 14:17:49 +000012011
12012 if (IS_GEN2(dev)) {
12013 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12014 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12015 } else {
12016 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12017 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12018 }
12019
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012020 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012021
Zhao Yakui28c97732009-10-09 11:39:41 +080012022 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012023 INTEL_INFO(dev)->num_pipes,
12024 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012025
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012026 for_each_pipe(pipe) {
12027 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012028 for_each_sprite(pipe, sprite) {
12029 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012030 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030012031 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000012032 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012033 }
Jesse Barnes79e53942008-11-07 14:24:08 -080012034 }
12035
Jesse Barnesf42bb702013-12-16 16:34:23 -080012036 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080012037 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080012038
Paulo Zanoni79f689a2012-10-05 12:05:52 -030012039 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012040 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012041
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012042 /* Just disable it once at startup */
12043 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012044 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000012045
12046 /* Just in case the BIOS is doing something questionable. */
12047 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012048
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012049 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012050 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012051 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012052
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012053 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080012054 if (!crtc->active)
12055 continue;
12056
Jesse Barnes46f297f2014-03-07 08:57:48 -080012057 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080012058 * Note that reserving the BIOS fb up front prevents us
12059 * from stuffing other stolen allocations like the ring
12060 * on top. This prevents some ugliness at boot time, and
12061 * can even allow for smooth boot transitions if the BIOS
12062 * fb is large enough for the active pipe configuration.
12063 */
12064 if (dev_priv->display.get_plane_config) {
12065 dev_priv->display.get_plane_config(crtc,
12066 &crtc->plane_config);
12067 /*
12068 * If the fb is shared between multiple heads, we'll
12069 * just get the first one.
12070 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080012071 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012072 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080012073 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010012074}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080012075
Daniel Vetter7fad7982012-07-04 17:51:47 +020012076static void intel_enable_pipe_a(struct drm_device *dev)
12077{
12078 struct intel_connector *connector;
12079 struct drm_connector *crt = NULL;
12080 struct intel_load_detect_pipe load_detect_temp;
Rob Clark51fd3712013-11-19 12:10:12 -050012081 struct drm_modeset_acquire_ctx ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020012082
12083 /* We can't just switch on the pipe A, we need to set things up with a
12084 * proper mode and output configuration. As a gross hack, enable pipe A
12085 * by enabling the load detect pipe once. */
12086 list_for_each_entry(connector,
12087 &dev->mode_config.connector_list,
12088 base.head) {
12089 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12090 crt = &connector->base;
12091 break;
12092 }
12093 }
12094
12095 if (!crt)
12096 return;
12097
Rob Clark51fd3712013-11-19 12:10:12 -050012098 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
12099 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020012100
12101
12102}
12103
Daniel Vetterfa555832012-10-10 23:14:00 +020012104static bool
12105intel_check_plane_mapping(struct intel_crtc *crtc)
12106{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012107 struct drm_device *dev = crtc->base.dev;
12108 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012109 u32 reg, val;
12110
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012111 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020012112 return true;
12113
12114 reg = DSPCNTR(!crtc->plane);
12115 val = I915_READ(reg);
12116
12117 if ((val & DISPLAY_PLANE_ENABLE) &&
12118 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12119 return false;
12120
12121 return true;
12122}
12123
Daniel Vetter24929352012-07-02 20:28:59 +020012124static void intel_sanitize_crtc(struct intel_crtc *crtc)
12125{
12126 struct drm_device *dev = crtc->base.dev;
12127 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012128 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020012129
Daniel Vetter24929352012-07-02 20:28:59 +020012130 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020012131 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012132 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12133
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012134 /* restore vblank interrupts to correct state */
12135 if (crtc->active)
12136 drm_vblank_on(dev, crtc->pipe);
12137 else
12138 drm_vblank_off(dev, crtc->pipe);
12139
Daniel Vetter24929352012-07-02 20:28:59 +020012140 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020012141 * disable the crtc (and hence change the state) if it is wrong. Note
12142 * that gen4+ has a fixed plane -> pipe mapping. */
12143 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020012144 struct intel_connector *connector;
12145 bool plane;
12146
Daniel Vetter24929352012-07-02 20:28:59 +020012147 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12148 crtc->base.base.id);
12149
12150 /* Pipe has the wrong plane attached and the plane is active.
12151 * Temporarily change the plane mapping and disable everything
12152 * ... */
12153 plane = crtc->plane;
12154 crtc->plane = !plane;
12155 dev_priv->display.crtc_disable(&crtc->base);
12156 crtc->plane = plane;
12157
12158 /* ... and break all links. */
12159 list_for_each_entry(connector, &dev->mode_config.connector_list,
12160 base.head) {
12161 if (connector->encoder->base.crtc != &crtc->base)
12162 continue;
12163
Egbert Eich7f1950f2014-04-25 10:56:22 +020012164 connector->base.dpms = DRM_MODE_DPMS_OFF;
12165 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012166 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012167 /* multiple connectors may have the same encoder:
12168 * handle them and break crtc link separately */
12169 list_for_each_entry(connector, &dev->mode_config.connector_list,
12170 base.head)
12171 if (connector->encoder->base.crtc == &crtc->base) {
12172 connector->encoder->base.crtc = NULL;
12173 connector->encoder->connectors_active = false;
12174 }
Daniel Vetter24929352012-07-02 20:28:59 +020012175
12176 WARN_ON(crtc->active);
12177 crtc->base.enabled = false;
12178 }
Daniel Vetter24929352012-07-02 20:28:59 +020012179
Daniel Vetter7fad7982012-07-04 17:51:47 +020012180 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12181 crtc->pipe == PIPE_A && !crtc->active) {
12182 /* BIOS forgot to enable pipe A, this mostly happens after
12183 * resume. Force-enable the pipe to fix this, the update_dpms
12184 * call below we restore the pipe to the right state, but leave
12185 * the required bits on. */
12186 intel_enable_pipe_a(dev);
12187 }
12188
Daniel Vetter24929352012-07-02 20:28:59 +020012189 /* Adjust the state of the output pipe according to whether we
12190 * have active connectors/encoders. */
12191 intel_crtc_update_dpms(&crtc->base);
12192
12193 if (crtc->active != crtc->base.enabled) {
12194 struct intel_encoder *encoder;
12195
12196 /* This can happen either due to bugs in the get_hw_state
12197 * functions or because the pipe is force-enabled due to the
12198 * pipe A quirk. */
12199 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12200 crtc->base.base.id,
12201 crtc->base.enabled ? "enabled" : "disabled",
12202 crtc->active ? "enabled" : "disabled");
12203
12204 crtc->base.enabled = crtc->active;
12205
12206 /* Because we only establish the connector -> encoder ->
12207 * crtc links if something is active, this means the
12208 * crtc is now deactivated. Break the links. connector
12209 * -> encoder links are only establish when things are
12210 * actually up, hence no need to break them. */
12211 WARN_ON(crtc->active);
12212
12213 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12214 WARN_ON(encoder->connectors_active);
12215 encoder->base.crtc = NULL;
12216 }
12217 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012218
12219 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010012220 /*
12221 * We start out with underrun reporting disabled to avoid races.
12222 * For correct bookkeeping mark this on active crtcs.
12223 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012224 * Also on gmch platforms we dont have any hardware bits to
12225 * disable the underrun reporting. Which means we need to start
12226 * out with underrun reporting disabled also on inactive pipes,
12227 * since otherwise we'll complain about the garbage we read when
12228 * e.g. coming up after runtime pm.
12229 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010012230 * No protection against concurrent access is required - at
12231 * worst a fifo underrun happens which also sets this to false.
12232 */
12233 crtc->cpu_fifo_underrun_disabled = true;
12234 crtc->pch_fifo_underrun_disabled = true;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012235
12236 update_scanline_offset(crtc);
Daniel Vetter4cc31482014-03-24 00:01:41 +010012237 }
Daniel Vetter24929352012-07-02 20:28:59 +020012238}
12239
12240static void intel_sanitize_encoder(struct intel_encoder *encoder)
12241{
12242 struct intel_connector *connector;
12243 struct drm_device *dev = encoder->base.dev;
12244
12245 /* We need to check both for a crtc link (meaning that the
12246 * encoder is active and trying to read from a pipe) and the
12247 * pipe itself being active. */
12248 bool has_active_crtc = encoder->base.crtc &&
12249 to_intel_crtc(encoder->base.crtc)->active;
12250
12251 if (encoder->connectors_active && !has_active_crtc) {
12252 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12253 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030012254 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012255
12256 /* Connector is active, but has no active pipe. This is
12257 * fallout from our resume register restoring. Disable
12258 * the encoder manually again. */
12259 if (encoder->base.crtc) {
12260 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12261 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030012262 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012263 encoder->disable(encoder);
12264 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012265 encoder->base.crtc = NULL;
12266 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020012267
12268 /* Inconsistent output/port/pipe state happens presumably due to
12269 * a bug in one of the get_hw_state functions. Or someplace else
12270 * in our code, like the register restore mess on resume. Clamp
12271 * things to off as a safer default. */
12272 list_for_each_entry(connector,
12273 &dev->mode_config.connector_list,
12274 base.head) {
12275 if (connector->encoder != encoder)
12276 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020012277 connector->base.dpms = DRM_MODE_DPMS_OFF;
12278 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012279 }
12280 }
12281 /* Enabled encoders without active connectors will be fixed in
12282 * the crtc fixup. */
12283}
12284
Imre Deak04098752014-02-18 00:02:16 +020012285void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012286{
12287 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012288 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012289
Imre Deak04098752014-02-18 00:02:16 +020012290 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12291 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12292 i915_disable_vga(dev);
12293 }
12294}
12295
12296void i915_redisable_vga(struct drm_device *dev)
12297{
12298 struct drm_i915_private *dev_priv = dev->dev_private;
12299
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012300 /* This function can be called both from intel_modeset_setup_hw_state or
12301 * at a very early point in our resume sequence, where the power well
12302 * structures are not yet restored. Since this function is at a very
12303 * paranoid "someone might have enabled VGA while we were not looking"
12304 * level, just check if the power well is enabled instead of trying to
12305 * follow the "don't touch the power well if we don't need it" policy
12306 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020012307 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012308 return;
12309
Imre Deak04098752014-02-18 00:02:16 +020012310 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012311}
12312
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012313static bool primary_get_hw_state(struct intel_crtc *crtc)
12314{
12315 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12316
12317 if (!crtc->active)
12318 return false;
12319
12320 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12321}
12322
Daniel Vetter30e984d2013-06-05 13:34:17 +020012323static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020012324{
12325 struct drm_i915_private *dev_priv = dev->dev_private;
12326 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020012327 struct intel_crtc *crtc;
12328 struct intel_encoder *encoder;
12329 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020012330 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020012331
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012332 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010012333 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020012334
Daniel Vetter99535992014-04-13 12:00:33 +020012335 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12336
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012337 crtc->active = dev_priv->display.get_pipe_config(crtc,
12338 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012339
12340 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012341 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020012342
12343 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12344 crtc->base.base.id,
12345 crtc->active ? "enabled" : "disabled");
12346 }
12347
Daniel Vetter53589012013-06-05 13:34:16 +020012348 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012349 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030012350 intel_ddi_setup_hw_pll_state(dev);
12351
Daniel Vetter53589012013-06-05 13:34:16 +020012352 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12353 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12354
12355 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12356 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012357 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020012358 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12359 pll->active++;
12360 }
12361 pll->refcount = pll->active;
12362
Daniel Vetter35c95372013-07-17 06:55:04 +020012363 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12364 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020012365 }
12366
Daniel Vetter24929352012-07-02 20:28:59 +020012367 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12368 base.head) {
12369 pipe = 0;
12370
12371 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012372 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12373 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012374 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012375 } else {
12376 encoder->base.crtc = NULL;
12377 }
12378
12379 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012380 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020012381 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030012382 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012383 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012384 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020012385 }
12386
12387 list_for_each_entry(connector, &dev->mode_config.connector_list,
12388 base.head) {
12389 if (connector->get_hw_state(connector)) {
12390 connector->base.dpms = DRM_MODE_DPMS_ON;
12391 connector->encoder->connectors_active = true;
12392 connector->base.encoder = &connector->encoder->base;
12393 } else {
12394 connector->base.dpms = DRM_MODE_DPMS_OFF;
12395 connector->base.encoder = NULL;
12396 }
12397 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12398 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012399 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012400 connector->base.encoder ? "enabled" : "disabled");
12401 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020012402}
12403
12404/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12405 * and i915 state tracking structures. */
12406void intel_modeset_setup_hw_state(struct drm_device *dev,
12407 bool force_restore)
12408{
12409 struct drm_i915_private *dev_priv = dev->dev_private;
12410 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012411 struct intel_crtc *crtc;
12412 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020012413 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012414
12415 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020012416
Jesse Barnesbabea612013-06-26 18:57:38 +030012417 /*
12418 * Now that we have the config, copy it to each CRTC struct
12419 * Note that this could go away if we move to using crtc_config
12420 * checking everywhere.
12421 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012422 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020012423 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080012424 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030012425 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12426 crtc->base.base.id);
12427 drm_mode_debug_printmodeline(&crtc->base.mode);
12428 }
12429 }
12430
Daniel Vetter24929352012-07-02 20:28:59 +020012431 /* HW state is read out, now we need to sanitize this mess. */
12432 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12433 base.head) {
12434 intel_sanitize_encoder(encoder);
12435 }
12436
12437 for_each_pipe(pipe) {
12438 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12439 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012440 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020012441 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012442
Daniel Vetter35c95372013-07-17 06:55:04 +020012443 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12444 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12445
12446 if (!pll->on || pll->active)
12447 continue;
12448
12449 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12450
12451 pll->disable(dev_priv, pll);
12452 pll->on = false;
12453 }
12454
Ville Syrjälä96f90c52013-12-05 15:51:38 +020012455 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030012456 ilk_wm_get_hw_state(dev);
12457
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012458 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030012459 i915_redisable_vga(dev);
12460
Daniel Vetterf30da182013-04-11 20:22:50 +020012461 /*
12462 * We need to use raw interfaces for restoring state to avoid
12463 * checking (bogus) intermediate states.
12464 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012465 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070012466 struct drm_crtc *crtc =
12467 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020012468
12469 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070012470 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012471 }
12472 } else {
12473 intel_modeset_update_staged_output_state(dev);
12474 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012475
12476 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010012477}
12478
12479void intel_modeset_gem_init(struct drm_device *dev)
12480{
Jesse Barnes484b41d2014-03-07 08:57:55 -080012481 struct drm_crtc *c;
12482 struct intel_framebuffer *fb;
12483
Imre Deakae484342014-03-31 15:10:44 +030012484 mutex_lock(&dev->struct_mutex);
12485 intel_init_gt_powersave(dev);
12486 mutex_unlock(&dev->struct_mutex);
12487
Chris Wilson1833b132012-05-09 11:56:28 +010012488 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020012489
12490 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012491
12492 /*
12493 * Make sure any fbs we allocated at startup are properly
12494 * pinned & fenced. When we do the allocation it's too early
12495 * for this.
12496 */
12497 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012498 for_each_crtc(dev, c) {
Dave Airlie66e514c2014-04-03 07:51:54 +100012499 if (!c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -080012500 continue;
12501
Dave Airlie66e514c2014-04-03 07:51:54 +100012502 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012503 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12504 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12505 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100012506 drm_framebuffer_unreference(c->primary->fb);
12507 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080012508 }
12509 }
12510 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012511}
12512
Imre Deak4932e2c2014-02-11 17:12:48 +020012513void intel_connector_unregister(struct intel_connector *intel_connector)
12514{
12515 struct drm_connector *connector = &intel_connector->base;
12516
12517 intel_panel_destroy_backlight(connector);
12518 drm_sysfs_connector_remove(connector);
12519}
12520
Jesse Barnes79e53942008-11-07 14:24:08 -080012521void intel_modeset_cleanup(struct drm_device *dev)
12522{
Jesse Barnes652c3932009-08-17 13:31:43 -070012523 struct drm_i915_private *dev_priv = dev->dev_private;
12524 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030012525 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070012526
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012527 /*
12528 * Interrupts and polling as the first thing to avoid creating havoc.
12529 * Too much stuff here (turning of rps, connectors, ...) would
12530 * experience fancy races otherwise.
12531 */
12532 drm_irq_uninstall(dev);
12533 cancel_work_sync(&dev_priv->hotplug_work);
12534 /*
12535 * Due to the hpd irq storm handling the hotplug work can re-arm the
12536 * poll handlers. Hence disable polling after hpd handling is shut down.
12537 */
Keith Packardf87ea762010-10-03 19:36:26 -070012538 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012539
Jesse Barnes652c3932009-08-17 13:31:43 -070012540 mutex_lock(&dev->struct_mutex);
12541
Jesse Barnes723bfd72010-10-07 16:01:13 -070012542 intel_unregister_dsm_handler();
12543
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012544 for_each_crtc(dev, crtc) {
Jesse Barnes652c3932009-08-17 13:31:43 -070012545 /* Skip inactive CRTCs */
Matt Roperf4510a22014-04-01 15:22:40 -070012546 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -070012547 continue;
12548
Daniel Vetter3dec0092010-08-20 21:40:52 +020012549 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070012550 }
12551
Chris Wilson973d04f2011-07-08 12:22:37 +010012552 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012553
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012554 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000012555
Daniel Vetter930ebb42012-06-29 23:32:16 +020012556 ironlake_teardown_rc6(dev);
12557
Kristian Høgsberg69341a52009-11-11 12:19:17 -050012558 mutex_unlock(&dev->struct_mutex);
12559
Chris Wilson1630fe72011-07-08 12:22:42 +010012560 /* flush any delayed tasks or pending work */
12561 flush_scheduled_work();
12562
Jani Nikuladb31af12013-11-08 16:48:53 +020012563 /* destroy the backlight and sysfs files before encoders/connectors */
12564 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020012565 struct intel_connector *intel_connector;
12566
12567 intel_connector = to_intel_connector(connector);
12568 intel_connector->unregister(intel_connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020012569 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030012570
Jesse Barnes79e53942008-11-07 14:24:08 -080012571 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010012572
12573 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030012574
12575 mutex_lock(&dev->struct_mutex);
12576 intel_cleanup_gt_powersave(dev);
12577 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012578}
12579
Dave Airlie28d52042009-09-21 14:33:58 +100012580/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080012581 * Return which encoder is currently attached for connector.
12582 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010012583struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080012584{
Chris Wilsondf0e9242010-09-09 16:20:55 +010012585 return &intel_attached_encoder(connector)->base;
12586}
Jesse Barnes79e53942008-11-07 14:24:08 -080012587
Chris Wilsondf0e9242010-09-09 16:20:55 +010012588void intel_connector_attach_encoder(struct intel_connector *connector,
12589 struct intel_encoder *encoder)
12590{
12591 connector->encoder = encoder;
12592 drm_mode_connector_attach_encoder(&connector->base,
12593 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080012594}
Dave Airlie28d52042009-09-21 14:33:58 +100012595
12596/*
12597 * set vga decode state - true == enable VGA decode
12598 */
12599int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12600{
12601 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000012602 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100012603 u16 gmch_ctrl;
12604
Chris Wilson75fa0412014-02-07 18:37:02 -020012605 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12606 DRM_ERROR("failed to read control word\n");
12607 return -EIO;
12608 }
12609
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020012610 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12611 return 0;
12612
Dave Airlie28d52042009-09-21 14:33:58 +100012613 if (state)
12614 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12615 else
12616 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020012617
12618 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12619 DRM_ERROR("failed to write control word\n");
12620 return -EIO;
12621 }
12622
Dave Airlie28d52042009-09-21 14:33:58 +100012623 return 0;
12624}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012625
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012626struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012627
12628 u32 power_well_driver;
12629
Chris Wilson63b66e52013-08-08 15:12:06 +020012630 int num_transcoders;
12631
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012632 struct intel_cursor_error_state {
12633 u32 control;
12634 u32 position;
12635 u32 base;
12636 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010012637 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012638
12639 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012640 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012641 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030012642 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010012643 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012644
12645 struct intel_plane_error_state {
12646 u32 control;
12647 u32 stride;
12648 u32 size;
12649 u32 pos;
12650 u32 addr;
12651 u32 surface;
12652 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010012653 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020012654
12655 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012656 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020012657 enum transcoder cpu_transcoder;
12658
12659 u32 conf;
12660
12661 u32 htotal;
12662 u32 hblank;
12663 u32 hsync;
12664 u32 vtotal;
12665 u32 vblank;
12666 u32 vsync;
12667 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012668};
12669
12670struct intel_display_error_state *
12671intel_display_capture_error_state(struct drm_device *dev)
12672{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012673 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012674 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020012675 int transcoders[] = {
12676 TRANSCODER_A,
12677 TRANSCODER_B,
12678 TRANSCODER_C,
12679 TRANSCODER_EDP,
12680 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012681 int i;
12682
Chris Wilson63b66e52013-08-08 15:12:06 +020012683 if (INTEL_INFO(dev)->num_pipes == 0)
12684 return NULL;
12685
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012686 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012687 if (error == NULL)
12688 return NULL;
12689
Imre Deak190be112013-11-25 17:15:31 +020012690 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012691 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12692
Damien Lespiau52331302012-08-15 19:23:25 +010012693 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020012694 error->pipe[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012695 intel_display_power_enabled_sw(dev_priv,
12696 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020012697 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012698 continue;
12699
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030012700 error->cursor[i].control = I915_READ(CURCNTR(i));
12701 error->cursor[i].position = I915_READ(CURPOS(i));
12702 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012703
12704 error->plane[i].control = I915_READ(DSPCNTR(i));
12705 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012706 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030012707 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012708 error->plane[i].pos = I915_READ(DSPPOS(i));
12709 }
Paulo Zanonica291362013-03-06 20:03:14 -030012710 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12711 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012712 if (INTEL_INFO(dev)->gen >= 4) {
12713 error->plane[i].surface = I915_READ(DSPSURF(i));
12714 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12715 }
12716
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012717 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030012718
12719 if (!HAS_PCH_SPLIT(dev))
12720 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020012721 }
12722
12723 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12724 if (HAS_DDI(dev_priv->dev))
12725 error->num_transcoders++; /* Account for eDP. */
12726
12727 for (i = 0; i < error->num_transcoders; i++) {
12728 enum transcoder cpu_transcoder = transcoders[i];
12729
Imre Deakddf9c532013-11-27 22:02:02 +020012730 error->transcoder[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012731 intel_display_power_enabled_sw(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020012732 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012733 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012734 continue;
12735
Chris Wilson63b66e52013-08-08 15:12:06 +020012736 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12737
12738 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12739 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12740 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12741 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12742 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12743 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12744 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012745 }
12746
12747 return error;
12748}
12749
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012750#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12751
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012752void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012753intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012754 struct drm_device *dev,
12755 struct intel_display_error_state *error)
12756{
12757 int i;
12758
Chris Wilson63b66e52013-08-08 15:12:06 +020012759 if (!error)
12760 return;
12761
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012762 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020012763 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012764 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012765 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010012766 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012767 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020012768 err_printf(m, " Power: %s\n",
12769 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012770 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030012771 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012772
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012773 err_printf(m, "Plane [%d]:\n", i);
12774 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12775 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012776 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012777 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12778 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012779 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030012780 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012781 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012782 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012783 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12784 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012785 }
12786
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012787 err_printf(m, "Cursor [%d]:\n", i);
12788 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12789 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12790 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012791 }
Chris Wilson63b66e52013-08-08 15:12:06 +020012792
12793 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010012794 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020012795 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012796 err_printf(m, " Power: %s\n",
12797 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020012798 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12799 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12800 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12801 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12802 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12803 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12804 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12805 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012806}